# Compute two proportion tests (with three variables?)

I want to make sure I'm running these proportion tests correctly to show that there are differences in outcomes for three groups. I am working off of this guide, except I have an additional group. Does the proportional test from this code work?:

crosstab(hcad_acs$$type_flag, (hcad_acs$$distance < 50), plot=FALSE, prop.r=TRUE)
Cell Contents
|-------------------------|
|                   Count |
|             Row Percent |
|-------------------------|

===========================================
(hcad_acs$$distance < 50) hcad_acs$$type_flag    FALSE    TRUE   Total
-------------------------------------------
25.9%   74.1%   11.6%
-------------------------------------------
prox                   557    1161    1718
32.4%   67.6%   68.6%
-------------------------------------------
safe                   204     291     495
41.2%   58.8%   19.8%
-------------------------------------------
Total                  836    1667    2503
===========================================

prop.test(x=c(215, 1161, 291), n = c(290, 1718, 495), p = NULL, alternative = "two.sided",
+           correct = TRUE)

3-sample test for equality of proportions without continuity correction

data:  c(215, 1161, 291) out of c(290, 1718, 495)
X-squared = 22, df = 2, p-value = 0.00002
alternative hypothesis: two.sided
sample estimates:
prop 1 prop 2 prop 3
0.741  0.676  0.588

• The manual page for prop.test states "If p is NULL and there is more than one group, the null tested is that the proportions in each group are the same. ... The alternative is always "two.sided", the returned confidence interval is NULL, and continuity correction is never used." Are you looking for anything more than that?
– whuber
May 26, 2022 at 20:30

Comment. One more thing: In R, the procedure prop.test is essentially the same as a chi-squared test of homogeneity, using your $$3 \times 2$$ table. Except for rounding, the chi-squared statistic, DF, and P-value are the same, so that the null hypothesis of equal proportions is rejected.

B = c(75, 215);  P = c(557, 1161);  S = c(204,291)
TBL = rbind(B,P,S);  TBL
[,1] [,2]
B   75  215
P  557 1161
S  204  291
chisq.test(TBL)

Pearson's Chi-squared test

data:  TBL
X-squared = 21.728, df = 2, p-value = 1.913e-05


Depending on objectives, either format can be more convenient.

In the chi-squared test, one can look at Pearson residuals. The sum of their squares is the chi-squared statistic. So, looking for residuals largest in absolute value provides a guide as to the cells of the table in which departure from homogeneity is most extreme.

chisq.test(TBL)$resi [,1] [,2] B -2.2211293 1.5729292 P -0.7017778 0.4969755 S 3.0074838 -2.1297991  Here, it might be worthwhile to compare the first and last row: TBL[c(1,3),] [,1] [,2] B 75 215 S 204 291  Using cor=F to decline the Yates continuity correction for moderately large counts. chisq.test(TBL[c(1,3),], cor=F) Pearson's Chi-squared test data: TBL[c(1, 3), ] X-squared = 18.808, df = 1, p-value = 1.446e-05  Notes: (a) If several such ad hoc tests are done in a larger table, you should use some method (such as Bonferroni) to avoid false discovery from repeated analyses on the same data. (b) In case some of the cells of the table have very low counts, you may get a warning message that the P-value of the chi-squared test may not be correct. Then you can use parameter sim=T to simulate a more useful P-value. Illustration for a table with sparse counts: TBL.s = rbind(c(2,3, 30), c(5,5,10)) TBL.s [,1] [,2] [,3] [1,] 2 3 30 [2,] 5 5 10  Warning message: chisq.test(TBL.s) Pearson's Chi-squared test data: TBL.s X-squared = 8.3131, df = 2, p-value = 0.01566 Warning message: In chisq.test(TBL.s) : Chi-squared approximation may be incorrect  Warning triggered because some expected counts are below $$5.$$ chisq.test(TBL.s)$exp
[,1]     [,2]     [,3]
[1,] 4.454545 5.090909 25.45455
[2,] 2.545455 2.909091 14.54545
Warning message:
In chisq.test(TBL.s) :
Chi-squared approximation may be incorrect


Simulation shows significance at 2% level.

chisq.test(TBL.s, sim=T)

Pearson's Chi-squared test
with simulated p-value
(based on 2000 replicates)

data:  TBL.s
X-squared = 8.3131, df = NA, p-value = 0.01949