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:

  1. 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
    
  2. Then, plot the portion of male and female smokers and nonsmokers ...

Get R for Data Science Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.