# Cell chi square test

I need to do a cell chi square test, but googling availeth me not (I suspect because of all the pages about the cells in chi square discussions generally). How does one calculate this? Any good sources on it, or on it in R specifically?

• What is the "cell" chi-squared test? – gung - Reinstate Monica Jun 3 '13 at 15:59
• It tests whether each cell is significantly different from its expected value in the overall table; I know SPSS does it, but I don't currently have access to SPSS, and I'm having trouble finding enough information that would let me recreate it. – Krysta Jun 3 '13 at 16:01
• To get an idea what the OP means, see here. – COOLSerdash Jun 3 '13 at 16:24
• @gung It collapses a larger table to a bunch of 2x2 chi-square tests (the cells [i,j]; [(i),j]; [i,(j)] and [(i),(j)]) - nothing more. (If you already have a 2x2 table, it's simply the ordinary 2x2 chisquare test repeated four times - about as pointless as things get) – Glen_b -Reinstate Monica Jun 3 '13 at 16:45
• Regarding google, you might want to look for “partitioning chi-squared” for a similar idea and other material that might be relevant to your purpose. “Cell chi-square test” appears to be SPSS-specific terminology. Also relevant would be standardized residuals (cf. Agresti). – Gala Jun 3 '13 at 19:18

As @Glen noted in the comments, the cell $\chi^{2}$-test simply calculates a bunch of $\chi^{2}$-tests on the collapsed 2x2 tables. It is fairly easy to implement in R:

# Example table
M <- as.table(rbind(c(762, 327, 468), c(484, 239, 477)))
dimnames(M) <- list(gender = c("M","F"),
party = c("Democrat","Independent", "Republican"))

M
party
gender Democrat Independent Republican
M      762         327        468
F      484         239        477

res.table <- matrix(NA, nrow=dim(M)[1], ncol=dim(M)[2])
dimnames(res.table) <- dimnames(M)

# Loop over all cells of the table

for ( i in 1:dim(M)[1] ) {
for ( j in 1:dim(M)[2] ) {

temp.table <- matrix(NA, 2, 2) # the collapsed 2x2 table

temp.table[1,1] <- M[i,j]
temp.table[1,2] <- sum(M[-i, j], na.rm=TRUE)
temp.table[2,1] <- sum(M[i, -j], na.rm=TRUE)
temp.table[2,2] <- sum(M[-i, -j], na.rm=TRUE)

chi2 <- chisq.test(temp.table, correct=TRUE) # chi2-test with continuity correction

# Automatically choose significance level (see SPSS documentation)

sig.level <- ifelse(M[i,j] <= 300, 0.1,
ifelse(M[i,j] > 300 & M[i,j] <= 1000, 0.05,
ifelse(M[i,j] > 1000 & M[i,j] <= 4000, 0.025,
ifelse(M[i,j] > 4000 & M[i,j] <= 20000,0.005,0.001)
)
)
)

if ( chi2$p.value < sig.level ) { res.table[i, j] <- paste( chi2$observed[1],
ifelse(chi2$observed[1] < chi2$expected[1], "<", ">"),
round(chi2\$expected[1], 2))

} else {

res.table[i, j] <- "n.s."

}
}
}

res.table

party
gender Democrat       Independent Republican
M "762 > 703.67" "n.s."      "468 < 533.68"
F "484 < 542.33" "n.s."      "477 > 411.32"