Working with Pearson's Chi-squared test

In this recipe, we introduce Pearson's Chi-squared test, which is used to examine whether the distributions of categorical variables of two groups differ. We will discuss how to conduct Pearson's Chi-squared Test in R.

Getting ready

Ensure that mtcars has already been loaded into a data frame within an R session. Since the chisq.test function is originated from the stats? package, make sure the library, stats, is loaded.

How to do it

Perform the following steps:

  1. To make the counting table, we first use the contingency table built with the inputs of the transmission type and number of forward gears:
    > ftable = table(mtcars$am, mtcars$gear)
    > ftable
       
         3  4  5
      0 15  4  0
      1  0  8  5
    
  2. We then plot the mosaic plot of the ...

Get R: Recipes for Analysis, Visualization and Machine Learning 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.