2. Conduct Test of Independence/Homogeneity
A survey was conducted among adults in a large metropolitan area. Adults were randomly chosen and asked about their exercise and coffee consumption habits. Using R and the survey data shown below, perform a chi-squared hypothesis test to determine if there is a relationship between exercise and coffee consumption habits.
|Frequently Exerise||Moderate Exercise||Never Exercise|
|Heavy Coffee Drinker||23||31||35|
|Moderate Coffee Drinker||37||36||24|
|Never Drink Coffee||48||24||29|
Lets begin by entering the data into R row by row as shown below.
# ENTER THE DATA BY ROW row1 = c(23, 31, 35) row2 = c(37, 36, 24) row3 = c(48, 24, 29)
Next, let's use the rbind() function to combine the rows to create a single matrix.
> # USE THE rbind FUNCTION TO BIND THE ROWS > data.table = rbind(row1, row2, row3) > > data.table [,1] [,2] [,3] row1 23 31 35 row2 37 36 24 row3 48 24 29
Now that we have the data in 1 matrix, lets go ahead and use the chisq.test() function to perform a chi-square hypothesis test.
> chisq.test(data.table) Pearson's Chi-squared test data: data.table X-squared = 12.5119, df = 4, p-value = 0.01392
This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.