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.
Ensure that mtcars
has already been loaded into a DataFrame within an R session.
Since the chisq.test
function is originated from the stats?
package, and make sure the library, stats
, is loaded.
Perform the following steps:
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 Output: 3 4 5 0 15 4 0 1 0 8 5
- We then plot the mosaic plot of the contingency table:
> mosaicplot(ftable, main="Number of Forward Gears Within Automatic
and Manual Cars", color = TRUE)
Number of forward gears in automatic and manual...