# SPSS's CROSSTABS statistics calculated by R with libraries # (C) Minato Nakazawa, 17 May 2014. # Example from http://www.med.nagoya-u.ac.jp/spss/basic/kiso/task3_7.html x <- matrix(c(12, 6, 304, 90, 122, 14), 2, 3) rownames(x) <- c("males", "females") colnames(x) <- c("underweight", "normal", "overweight") print(x) chisq.test(x) # Pearson's chi-square test of independence chisq.test(x, simulate.p.value=TRUE) # Monte Carlo method to calculate p-value library(vcd) # table2d_summary(as.table(x)) # make summary table with Pearson's test assocstats(x) # It gives Likelihood Ratio chi-square. library(coin) lbl_test(as.table(x)) # Calculate "linear-by-linear association test" see Agresti (2002) sex <- c(rep(1:2,x[,1]),rep(1:2,x[,2]),rep(1:2,x[,3])) bodyphysique <- rep(1:3,colSums(x)) print(Xsq <- (sum(x)-1)*cor(sex, bodyphysique)^2) print(1-pchisq(Xsq, 1)) # http://home.comcast.net/~lthompson221/Splusdiscrete2.pdf pp.39-40.