7.1 Exercice Please work out in R by doing a chi-squared test on the treatment (X) and improvement (Y) columns in treatment.csv.
##
## improved not-improved
## not-treated 26 29
## treated 35 15
##
## Pearson's Chi-squared test
##
## data: treatment$treatment and treatment$improvement
## X-squared = 5.5569, df = 1, p-value = 0.01841
The chi-squared value is 5.55. Since we get a p-Value less than the significance level of 0.05, we could reject the null hypothesis and conclude that the two variables are in fact dependent
7.2 Exercice 2 Find out if the cyl and carb variables in mtcars dataset are dependent or not.
##
## 4 6 8
## 1 5 2 0
## 2 6 0 4
## 3 0 0 3
## 4 0 4 6
## 6 0 1 0
## 8 0 0 1
## Warning in chisq.test(mtcars$carb, mtcars$cyl): Chi-squared approximation may be
## incorrect
##
## Pearson's Chi-squared test
##
## data: mtcars$carb and mtcars$cyl
## X-squared = 24.389, df = 10, p-value = 0.006632
The chi-squared value is 24.389 and we got a p-value is less than 0.05 significance level. So we reject the null hypothesis and we know that carb and cyl have a significant relationship.
7.3 Exercise 3 256 visual artists were surveyed to find out their zodiac sign. The results were: Aries (29), Taurus (24), Gemini (22), Cancer (19), Leo (21), Virgo (18), Libra (19), Scorpio (20), Sagittarius (23), Capricorn (18), Aquarius (20), Pisces (23). Test the hypothesis that zodiac signs are evenly distributed across visual artists. (Reference)
## # A tibble: 12 x 3
## Category Observed Expected
## <chr> <dbl> <dbl>
## 1 Aries 29 21.3
## 2 Taurus 24 21.3
## 3 Gemini 22 21.3
## 4 Cancer 19 21.3
## 5 Leo 21 21.3
## 6 Virgo 18 21.3
## 7 Libra 19 21.3
## 8 Scorpio 20 21.3
## 9 Sagittarius 23 21.3
## 10 Capricorn 18 21.3
## 11 Aquarius 20 21.3
## 12 Pisces 23 21.3
#chisq-test
Residual<-(Zodiac$Observed - Zodiac$Expected)
x<-(Residual)^2
Component<-(x/Zodiac$Expected)
print(Component)## [1] 2.755490976 0.333421882 0.020854498 0.255139408 0.005198003 0.520737308
## [7] 0.255139408 0.083292973 0.130262457 0.520737308 0.083292973 0.130262457
## [1] 5.09383