A chi-square test of independence using R

Here is how to compute a chisquare test of independence on R. In this example, the vector matrix y is a 3 by 2 matrix representing the counts in the contingency table. The columns refer to male and female students at the College; the rows are the political participation categories "Eligible only", "Registered; not voted", and "Has voted".


y <- matrix(c(11,3,14,3,5,19),ncol=2)

chisq.test(y)

	

One can assign the output of the chisquare test to a variable, which creates a list of various components of the chisquare test. In what follows, the variable chi.test contains the results. So chi.test is a list of length 8. We then print the entire list.

chi.test <- chisq.test(y)

chi.test[] # prints out all parts of the output.
> chi.test[]
$statistic
X-squared
 5.812744

$parameter
df
 2

$p.value
[1] 0.05467372

$method
[1] "Pearson's Chi-squared test"

$data.name
[1] "y"

$observed
     [,1] [,2]
[1,]   11    3
[2,]    3    5
[3,]   14   19

$expected
          [,1]      [,2]
[1,]  7.127273  6.872727
[2,]  4.072727  3.927273
[3,] 16.800000 16.200000

$residuals
           [,1]       [,2]
[1,]  1.4506252 -1.4772444
[2,] -0.5315531  0.5413072
[3,] -0.6831301  0.6956656