# Chi-Squared test using Motor Trend Car Road Tests data set.
# Store the data in the variable mydata
mydata2 <- mtcars
# Research Question:Is there an association between vs Vs am, gear Vs carb, gear Vs cylinder?
#H0 = two variables are independent
#H1 = two variables are related
# Generate frequency table. If values in all cells are same, then have a balanced design.
case1 <- table(mydata2$vs, mydata2$am)
case1
##
## 0 1
## 0 12 6
## 1 7 7
case2 <- table(mydata2$gear, mydata2$carb)
case2
##
## 1 2 3 4 6 8
## 3 3 4 3 5 0 0
## 4 4 4 0 4 0 0
## 5 0 2 0 1 1 1
case3 <- table(mydata2$cyl, mydata2$gear)
case3
##
## 3 4 5
## 4 1 8 2
## 6 2 4 1
## 8 12 0 2
#chisquare case1 test
#the p-value > 0.05, then there is a no tilt to the
#stacks (the counts are not likely to be from chance).
chisq.test(case1)
##
## Pearson's Chi-squared test with Yates' continuity correction
##
## data: case1
## X-squared = 0.34754, df = 1, p-value = 0.5555
#chisquare case2 test
#the p-value > 0.05, then there is a no tilt to the
#stacks (the counts are not likely to be from chance).
chisq.test(case2)
## Warning in chisq.test(case2): Chi-squared approximation may be incorrect
##
## Pearson's Chi-squared test
##
## data: case2
## X-squared = 16.518, df = 10, p-value = 0.08573
#chisquare case3 test
#the p-value < 0.05, then there is a tilt to the
#stacks (the counts are not likely to be from chance).
chisq.test(case3)
## Warning in chisq.test(case3): Chi-squared approximation may be incorrect
##
## Pearson's Chi-squared test
##
## data: case3
## X-squared = 18.036, df = 4, p-value = 0.001214
#critical value case1
#Chi Square value < Critical Value, fail to reject the null hypothesis that means
#two variables are independent- Engine(vs) (0 = V-shaped, 1 = straight) is not related to
#Transmission(am) (0 = automatic, 1 = manual)
qchisq(0.95,1)
## [1] 3.841459
#critical value case2
#Chi Square value < Critical Value, fail to reject the null hypothesis that means
#two variables are independent, Number of forward gears is not related to Number of carburetors
qchisq(0.95,10)
## [1] 18.30704
#critical value case3
#Chi Square value > Critical Value, reject the null hypothesis that means accepting two variables
#are related, Number of cylinders are related to Number of forward gears
qchisq(0.95,4)
## [1] 9.487729