################################################################################ #### Inference for Contingency Tables #### ################################################################################ #### By Jimin Ding, 04//07/2017 #### 1. Pearson Chi-square Test for Goodness-of-fit ## Create One-way Table carpool=rbind(0:4, c(136,45,12,5,2)) rownames(carpool)=c("N of PAX", "Freq" ) carpool ## find Poisson Prob. (lambdahat=mean(rep(carpool[1,],times=carpool[2,]))) p0=dpois(carpool[1,],lambda=lambdahat) p0[5]=1-sum(p0[1:4]) p0 ## Pearson Chi-square test statistics and p-value for goodness-of-fit test n=sum(carpool[2,]) (e=n*p0) # expected counts (Q=sum((carpool[2,]-e)^2/e)) # test stat 1-pchisq(Q,df=5-1-1) # p-value # p-value is small and we reject the null hypothesis and # conclude that there is a signficant evidence that data do not follow Poisson distribution. ## Use chisq.test in R chisq.test(carpool[2,],p=p0) ## Note that chisq.test give the correct test statistics, but used incorrect df. ## Remark: How far away data is from Poisson distribution? ## If we check the relative square distance between observed counts and expected counts, ## we found that the last cell has a large difference. This is actually caused by combining some cells. (carpool[2,]-e)^2/e #### 2. Pearson Chi-square Test for two-way tables ## Example 1. belief=matrix(c(435,147,375,134),ncol=2,dimnames=list(c("F","M"),c("Yes","No"))) belief chisq.test(belief,correct=FALSE) ## Fail to reject the null hypothesis and ## there is no sigificant difference between females and males in terms of belief in afterlife. ## Example 2. kidney=matrix(c(273,289,77,61),ncol=2) colnames(kidney)=c("success","failure") rownames(kidney)=c("New","Standard") kidney chisq.test(kidney) ## 3-way table mytable=array(c(81,192,6,71,234,55,36,25),dim=c(2,2,2), dimnames=list(c("Small","Large"),c("success","failure"),c("New","Standard"))) mytable ## It is better to put the stone size as the third dimension. (mytable2=aperm(mytable,c(3,2,1))) ## Pearson Chisquare Test for small stone cases chisq.test(mytable2[,,1]) prop.test(x=c(81,234),n=c(87,270)) ## Pearson Chisquare Test for large stone cases chisq.test(mytable2[,,2]) prop.test(x=c(192,55),n=c(263,80)) #CMH test mantelhaen.test(mytable2)