Table of Contents
[ Collection: Introduction to R ]
The Chi-Square test
Prerequisites
- Your data must have the form of a contingency table (see Contingency Tables).
How to calculate the chi-square test
Assuming that you have a contingency table that is stored in a variable called mytable
, you calculate a chi-square test using the chisq.test
command as follows:
chisq.test(mytable, correct = FALSE)
The option correct = FALSE
ensures that the standard chi-square test is calculated, i.e., that no corrections are applied. If more than a quarter of the cells in your table have an expected value smaller than 5, you should use Yates' continuity correction; you can do this by not adding the correct
option at all (i.e., R applies the correction by default), or by using the option correct = TRUE
.
By default, R does not show the expected frequencies or the residuals, but they are created as internal variables by the chi.square()
function internally, and you can force R to output them by attaching the name of the internal variable to the function, separated by a $
sign:
To show the expected frequencies, attach the option expected to the end of the command, separated by a $ sign:
chisq.test(mytable, correct = FALSE)$expected
or
chisq.test(mytable, correct = FALSE)$residuals
Additional information
- When you report the result of a chi-square test, you should include a) the chi-square value, b) the degrees of freedom, and c) the p-value
- If you want to present your data visually, a bar plot is usually the right way of doing so (see Bar Plots).