Please work out in R by doing a chi-squared test on the treatment (X) and improvement (Y) columns in treatment.csv.
Answer
data <- read.csv("C:/Users/user/Downloads/treatment.csv") # Read Data
head(data, 10) # Shows 10 from 105 data of treatment## id treatment improvement
## 1 1 treated improved
## 2 2 treated improved
## 3 3 not-treated improved
## 4 4 treated improved
## 5 5 treated not-improved
## 6 6 treated not-improved
## 7 7 not-treated not-improved
## 8 8 treated not-improved
## 9 9 not-treated improved
## 10 10 treated improved
##
## improved not-improved
## not-treated 26 29
## treated 35 15
##
## Pearson's Chi-squared test
##
## data: data$treatment and data$improvement
## X-squared = 5.5569, df = 1, p-value = 0.01841
So, the chi-squared value is 5.5569 and p-value is 0.01841 (less than the significance level of 0.05). Then, we reject the null hypothesis and conclude that the two variables are dependent.
Find out if the cyl and carb variables in mtcars dataset are dependent or not.
Answer
##
## 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
So, the chi-squared value is 24.389 and p-value is 0.006632 (less than the significance level of 0.05). Then, we reject the null hypothesis and conclude that the two variables are dependent.
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)
Answer
observed <- c(29, 24, 22, 19, 21, 18, 19, 20, 23, 18, 20, 23)
n <- 256
expected <- c(1/12) * n
alpha <- .05
r <- c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12)
df <- 12 - 1
(chisq <- sum((observed - expected)^2 / expected))## [1] 5.09375
## [1] 0.9265414
##
## Chi-squared test for given probabilities
##
## data: observed
## X-squared = 5.0938, df = 11, p-value = 0.9265
So, the chi-squared value is 5.0938 and p-value is 0.9265 (more than the significance level of 0.05). Then, we do not reject the null hypothesis and that means the Zodiac signs are evenly distributed across visual artists.