8 Comparing proportions in 2 X 2 tables
8.0.1 Large sample test procedures
\[H_o: \pi_1 = \pi_2\]
- \(p = \frac{n_{11} + n_{21}}{{n_1} + n_{2}}\) : an estimator of the hypothesized common success probability
n1=60; n2=40; n=n1+n2; n11=20; n21=30
p1=n11/n1; p2=n21/n2; p=(n11+n21)/n
stat=(p1-p2)/sqrt(p*(1-p)/n1+p*(1-p)/n2)
c(stat,2*pnorm(stat))## [1] -4.082483e+00 4.455709e-05
c(stat^2,pchisq(stat^2,1,lower.tail=F))## [1] 1.666667e+01 4.455709e-05
s=sqrt(p1*(1-p1)/n1+p2*(1-p2)/n2); z=qnorm(0.975)
p1-p2+c(-1,1)*z*s## [1] -0.5962063 -0.2371271
prop.test(c(n11,n21),c(n1,n2),correct=F)##
## 2-sample test for equality of proportions without continuity
## correction
##
## data: c(n11, n21) out of c(n1, n2)
## X-squared = 16.667, df = 1, p-value = 4.456e-05
## alternative hypothesis: two.sided
## 95 percent confidence interval:
## -0.5962063 -0.2371271
## sample estimates:
## prop 1 prop 2
## 0.3333333 0.7500000
yates=min(0.5,abs(p1-p2)/(1/n1+1/n2))
p1-p2+c(-1,1)*(z*s+yates*(1/n1+1/n2))## [1] -0.6170396 -0.2162937
prop.test(c(n11,n21),c(n1,n2))##
## 2-sample test for equality of proportions with continuity correction
##
## data: c(n11, n21) out of c(n1, n2)
## X-squared = 15.042, df = 1, p-value = 0.0001052
## alternative hypothesis: two.sided
## 95 percent confidence interval:
## -0.6170396 -0.2162937
## sample estimates:
## prop 1 prop 2
## 0.3333333 0.7500000
yates=min(0.5,abs(p1-p2)/(1/n1+1/n2))
p1-p2+c(-1,1)*(z*s+yates*(1/n1+1/n2))## [1] -0.6170396 -0.2162937
## [1] -0.6170396 -0.2162937
prop.test(c(n11,n21),c(n1,n2))##
## 2-sample test for equality of proportions with continuity correction
##
## data: c(n11, n21) out of c(n1, n2)
## X-squared = 15.042, df = 1, p-value = 0.0001052
## alternative hypothesis: two.sided
## 95 percent confidence interval:
## -0.6170396 -0.2162937
## sample estimates:
## prop 1 prop 2
## 0.3333333 0.7500000
8.0.2 2X2 Chi-Squared test of homogeneity
n.1=n11+n21; p=n.1/n
nv=c(n11,n1-n11,n21,n2-n21)
ev=c(n1*p,n1*(1-p),n2*p,n2*(1-p))
cstat=sum((abs(nv -ev) - yates)^2/ev)
pchisq(cstat, 1, lower.tail = F)## [1] 0.0001051636
## [1] 0.0001051636
prop.test(c(n11,n21),c(n1,n2))##
## 2-sample test for equality of proportions with continuity correction
##
## data: c(n11, n21) out of c(n1, n2)
## X-squared = 15.042, df = 1, p-value = 0.0001052
## alternative hypothesis: two.sided
## 95 percent confidence interval:
## -0.6170396 -0.2162937
## sample estimates:
## prop 1 prop 2
## 0.3333333 0.7500000
xm=matrix(c(n11,n1-n11,n21,n2-n21),2,2,byrow=T)
xm## [,1] [,2]
## [1,] 20 40
## [2,] 30 10
chisq.test(xm)##
## Pearson's Chi-squared test with Yates' continuity correction
##
## data: xm
## X-squared = 15.042, df = 1, p-value = 0.0001052
8.0.3 2X2 Chi-Squared test of independence
n11=50; n12=40; n21=60; n22=50
xm=matrix(c(n11,n12,n21,n22),2,2,byrow=T);
xm## [,1] [,2]
## [1,] 50 40
## [2,] 60 50
chisq.test(xm,correct=F)##
## Pearson's Chi-squared test
##
## data: xm
## X-squared = 0.020406, df = 1, p-value = 0.8864
n1=n11+n12; n2=n21+n22
prop.test(c(n11,n21),c(n1,n2),correct=F)##
## 2-sample test for equality of proportions without continuity
## correction
##
## data: c(n11, n21) out of c(n1, n2)
## X-squared = 0.020406, df = 1, p-value = 0.8864
## alternative hypothesis: two.sided
## 95 percent confidence interval:
## -0.1284537 0.1486558
## sample estimates:
## prop 1 prop 2
## 0.5555556 0.5454545
chitest=chisq.test(xm,correct=F)
names(chitest)## [1] "statistic" "parameter" "p.value" "method" "data.name" "observed"
## [7] "expected" "residuals" "stdres"