Working with the Pearson's chi-squared tests
In this recipe, we introduced Pearson's chi-squared test, which is used to examine whether the distribution of categorical variables of two groups differ. We will discuss how to conduct Pearson's chi-squared Test in R.
Getting ready
In this recipe, we will use the chisq.test
function that originated from the stat
package.
How to do it…
Perform the following steps to conduct a Pearson's chi-squared test:
First, build a matrix containing the number of male and female smokers and nonsmokers:
>mat<- matrix(c(2047, 2522, 3512, 1919), nrow = 2, dimnames = list(c("smoke","non-smoke"), c("male","female"))) >mat malefemale smoke2047 3512 non-smoke 2522 1919
Then, plot the portion of male and female smokers and nonsmokers in a mosaic plot:
>mosaicplot(mat, main="Portion of male and female smokers/non-smokers", color = TRUE)
Next, perform a Pearson's chi-squared test on the contingency table to test whether the factor...