Chapter 10.

Chi-squared tests


Splus has the following commands to do different chi-squared tests: prop.test, fisher.test, chisq.test, chisq.gof, mcnemar.test.

To do a test of homogeneity for several populations, we use the command prop.test. If we use the data in Table 10.2, page 462, we get:

****** program 10a **********
x_c(4,1)
n_c(12,21)
prop.test(x,n,correct=F)
prop.test(x,n,alternative="greater",correct=F)
y_n-x
mat_cbind(x,y)
fisher.test(mat)
chisq.test(mat, correct = F)
****** outcome **********
> prop.test(x, n, correct = F)
Warning messages:
Warning in prop.test(x, n, correct = F): 
Expected counts < 5. 
Chi-square/normal approximation may not be appropriate.

2-sample test for equality of proportions without continuity correction

data:  x out of n 
X-square = 4.849, df = 1, p-value = 0.0277 
alternative hypothesis: two.sided 
95 percent confidence interval:
 0.003873697 0.567554875 
sample estimates:
 prop'n in Group 1 prop'n in Group 2 
         0.3333333        0.04761905

> prop.test(x, n, alternative = "greater", correct = F)
Warning messages:
Warning in prop.test(x, n, alternative = "greater", correct = F): 
Expected counts < 5. 
Chi-square/normal approximation may not be appropriate.

2-sample test for equality of proportions without continuity correction

data:  x out of n 
X-square = 4.849, df = 1, p-value = 0.0138 
alternative hypothesis: greater 
95 percent confidence interval:
 0.04918621 1.00000000 
sample estimates:
 prop'n in Group 1 prop'n in Group 2 
         0.3333333        0.04761905

> fisher.test(mat)

Fisher's exact test

data:  mat 
p-value = 0.0471 
alternative hypothesis: two.sided 

> chisq.test(mat, correct = F)
Warning messages:
Warning in chisq.test(mat, correct = F):
Expected counts < 5.
Chi-square approximation may not be appropriate.
Pearson's chi-square test without Yates' continuity correction

data:  mat 
X-square = 4.849, df = 1, p-value = 0.0277 

We can do one and two sided tests. The conclusion of the one sided test is: the chances of getting a liver scan is higher for the black patients than for the white patients.

The command chisq.test() can be used for tests of homogeneity and tests of independence.

To do goodness fit tests, we can use the command chisq.gof():

# program 10b
# We do a chi squared test of normality
# We use the data in  page 530 in the textbook. 
x_c(12.8,12.9,13.3,13.4,13.7,13.8,14.5)
xbar_mean(x)
s_stdev(x)
rm(a)
a_min(x)-1
a[2]_xbar-s
a[3]_xbar+s
a[4]_max(x)+1
cut(x,a)
chisq.gof(x,cut.points=a, distribution="normal",mean=xbar,sd=s) 
***** outcome ******************
> cut(x, a)
[1] 1 1 2 2 2 2 3
attr(, "levels"):
[1] "11.80000+ thru 12.90425" "12.90425+ thru 14.06717"
[3] "14.06717+ thru 15.50000"
> chisq.gof(x, cut.points = a, distribution = "normal", 
	mean = xbar, sd = s)
Warning messages:
Warning in chisq.gof(x, cut.points = a, distribution = "normal", : 
Expected counts < 5. 
Chi-squared approximation may not be appropriate.
Chi-square Goodness of Fit Test
data:  x 
Chi-square = 0.8798, df = 2, p-value = 0.6441 
alternative hypothesis: 
True cdf does not equal the normal Distn. for at least one sample point. 
We conclude that the data could come from a normal distribution. The number of points is so small than we do not have enough points to have 5 points in each cell.

Next, we do the McNemar test for the data in Table 10.5 page 469.

****** program 10c **********
> mat <- cbind(c(26, 15), c(7, 37))
> mcnemar.test(mat, correct = F)

	McNemar's chi-square test without continuity correction

data:  mat 
McNemar's chi-square = 2.9091, df = 1, p-value = 0.0881 
Splus only does two sided McNemar's tests. We conclude that there is no difference between patients and siblings.

Comments to: Miguel A. Arcones