Solved – Post hoc $\chi^2$ test with R

chi-squared-testpost-hocr

This is a quite simple question but I don't find any good, clear, precise answers:
I'm looking for a way to perform post hoc test on a chi$^2$ test.

I have 2 variables : var1 : good/fair/poor and var2: a/b/c

This is the contingency table :

      a   b  c
good 120  70 13
fair 230 130 26
poor  84  83 18

with R :

mat <- matrix(c(120,230,84,70,130,83,13,26,18),3)
dimnames(mat) <- list(c("good","fair","poor"),c("a","b","c"))
mat

I perform $\chi^2$

chisq.test(mat)

I obtain p =0.022, so there is a relation between var1 and var2… but which one? How can I handle this? (contribution analysis?, pairwise prop test?)

Best Answer

I like this question because too often, people do omnibus tests and then don't ask more specific questions about what is happening.

If the goal is to compare "treatments" a, b, and c, I would suggest summarizing the data showing the percentages within each column, so you can see more clearly how they differ. Then to test these comparisons, one simple idea is to do the $\chi^2$ test on each pair of columns:

> for (j in 1:3) print(chisq.test(mat[, -j]))

    Pearson's Chi-squared test

data:  mat[, -j]
X-squared = 0.1542, df = 2, p-value = 0.9258


    Pearson's Chi-squared test

data:  mat[, -j]
X-squared = 4.5868, df = 2, p-value = 0.1009


    Pearson's Chi-squared test

data:  mat[, -j]
X-squared = 9.5653, df = 2, p-value = 0.008374

Since 3 tests are done, a Bonferroni correction is advised (multiply each $P$ value by 3). The last test, where column 3 is omitted, has a very low $P$ value, so you can conclude that the distributions of (good, fair, poor) are different for conditions a and b. Note, however, that condition c does not have much data, and that's largely why the other two results are nonsignificant.

You could use a similar strategy to do pairwise comparisons of the rows.