Chi-Square Test

To analyze the frequency table Chi-Square Test is used. This table is formed by two categorical variables. This test is used to check whether there is a significant relationship between these two variables. The variable may be from the same population or maybe categories like On/Off, Male/Female, etc. The chisq.test() function is used to perform this test in R.

This function takes data in the table form as input, containing the count value of the variables in the observation.

Chi-Square Test

In R, there is the following syntax of chisq.test() function:

Chi-Square Test Example

Let’s take an example to understand this test:

library("MASS")

# Create a data frame from survey datset in MASS library
df <- data.frame(survey$Smoke, survey$Exer) 

# Create a table 
df= table(survey$Smoke, survey$Exer) 
print(df)

# Perform the Chi-Square test
print(chisq.test(df))

Output:

cs1