Statistical Analysis in R of 3 groups with categorical variables New to using R, and I am trying to assess a group of patients over three time points. There are 21 patients who have a categorical variable (a score of 1 through 6). The three time points are arranged in columns.
So effectively there is a 21x3 table (21 observations of a categorical variable (score of 1 through 6) at each timepoint.
I want to compare to see if there is significant improvement in score over the three time points. I was using Chi-square analysis but want to see if there are more appropriate testing options in R to assess this problem.
 A: The Friedman test is a nonparametric test for block data.
Here is a matrix x in which the three time periods (main effect) are in the three columns. The 21 rows are for patients (block effect). Data are ordinal categorical data potentially with values 1 through 6, although I don't
think there happen to be any 6's in x.
The test compares the levels of the main effect, across
the blocks, but does not compare blocks (patients) with each other. (In most applications, it is assumed
blocks differ at random.)
x
      t1 t2 t3
 [1,]  3  2  3
 [2,]  3  4  4
 [3,]  3  4  3
 [4,]  4  4  4
 [5,]  4  4  5
 [6,]  4  5  4
 [7,]  4  4  4
 [8,]  4  5  4
 [9,]  2  3  2
[10,]  1  2  1
[11,]  2  3  3
[12,]  4  3  5
[13,]  3  3  4
[14,]  3  3  4
[15,]  4  5  4
[16,]  3  2  3
[17,]  4  5  5
[18,]  4  5  5
[19,]  2  3  3
[20,]  3  4  4
[21,]  1  1  2

friedman.test(x)

        Friedman rank sum test
data:  x
Friedman chi-squared = 10.483, df = 2, 
  p-value = 0.005293

The test finds significant differences among the columns
with P-value 0.005. [Although the output refers to
a chi-squared statistic, this is not the same as a
chi-squared test for independence. Because data are
ordinal categories, it would be inappropriate to
treat them as counts.]
You might use paired Wilcoxon (signed rank tests) ad hoc
to see if there are significant differences between
t1 & t2, and so on. There are many ties in the data,
so these tests may not be exact.
You should use some
method to protect against false discovery, such as the
Bonferroni method; it would suggest using the 1% or 2%
level, depending on the number of ad hoc comparisons
you make. For my fake data, it seems t1 differs
significantly from t2, but t2 does differ significantly from t3.
wilcox.test(x[,1], x[,2], pair=T)

        Wilcoxon signed rank test 
        with continuity correction

data:  x[, 1] and x[, 2]
V = 24, p-value = 0.02193
alternative hypothesis: 
  true location shift is not equal to 0

Warning messages:
1: In wilcox.test.default(x[, 1], x[, 2], pair = T) :
   cannot compute exact p-value with ties
2: In wilcox.test.default(x[, 1], x[, 2], pair = T) :
   cannot compute exact p-value with zeroes

wilcox.test(x[,2], x[,3], pair=T)$p.val
[1] 0.6444123
Warning messages:

Note: Fake data for illustration were simulated in R as follows:
set.seed(705)
t1 = sample(1:4, 21, rep=T)
t2 = t1 + sample(-1:1, 21, rep=T, p=c(1:3))
t3 = t1 + sample(0:1, 21, rep=T)
x = cbind(t1,t2,t3)

