Table of Contents

[ Collection: Introduction to R ]

The Chi-Square test

Prerequisites

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