Email             :
RPubs            : https://rpubs.com/ferdnw/
Address         : ARA Center, Matana University Tower
                         Jl. CBD Barat Kav, RT.1, Curug Sangereng, Kelapa Dua,



Exercise 6.1

Please work out in R by doing a chi-squared test on the treatment (X) and improvement (Y) columns in treatment.csv.

Ho: The variables are independent. HA: The variables are not independent (meaning they are related).

treat = read.csv('treatment.csv')
tbl = table(treat$treatment, treat$improvement);tbl        # the contingency table 
##              
##               improved not-improved
##   not-treated       26           29
##   treated           35           15
chisq.test(tbl,simulate.p.value = TRUE)           # perform the Chi-Squared test
## 
##  Pearson's Chi-squared test with simulated p-value (based on 2000
##  replicates)
## 
## data:  tbl
## X-squared = 5.5569, df = NA, p-value = 0.02099

As p-value 0.03 < taraf signifikansi 0.05 maka H0 di tolak, yang berarti bahwa Pasien yang mengalami Improvement dipengaruhi oleh Treatment atau tidak

Exercise 6.2

Find out if the cyl and carb variables in mtcars dataset are dependent or not.

Ho: The variables are independent. HA: The variables are not independent (meaning they are related).

chisq.test(mtcars$cyl, mtcars$carb)
## 
##  Pearson's Chi-squared test
## 
## data:  mtcars$cyl and mtcars$carb
## X-squared = 24.389, df = 10, p-value = 0.006632

We have a high chi-squared value and a p-value of less than 0.05 significance level. So we reject the null hypothesis and conclude that carb and cyl have a significant (dependent) relationship.

Exercise 6.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

Ho: The variables are not evenly distributed HA: The zodiacs are evenly distributed

zodiac = c("Aries","Taurus",'Gemini', 'Cancer', 'Leo', 'Virgo', 'Libra', 'Scorpio', 'Sagitarius', 'Capricorn', 'Aquarius', 'Pisces')
Total = c(29,24,22,19,21,18,19,20,23,18,20,23)

zchi = data.frame(zodiac, Total)
n = 256
zchi$Expected = 1/12 * n

zchi
##        zodiac Total Expected
## 1       Aries    29 21.33333
## 2      Taurus    24 21.33333
## 3      Gemini    22 21.33333
## 4      Cancer    19 21.33333
## 5         Leo    21 21.33333
## 6       Virgo    18 21.33333
## 7       Libra    19 21.33333
## 8     Scorpio    20 21.33333
## 9  Sagitarius    23 21.33333
## 10  Capricorn    18 21.33333
## 11   Aquarius    20 21.33333
## 12     Pisces    23 21.33333
df <- 12 - 1
(chisq <- sum((zchi$Total - zchi$Expected)^2 / zchi$Expected))
## [1] 5.09375
(p_value <- pchisq(q = chisq, df = df, lower.tail = F))
## [1] 0.9265414
(chisq.test.result <- chisq.test(x = zchi$Total, p = zchi$Expected / n)) 
## 
##  Chi-squared test for given probabilities
## 
## data:  zchi$Total
## X-squared = 5.0938, df = 11, p-value = 0.9265

We have a p-value of more than 0.05 significance level. So we accept the null hypothesis and conclude that The Zodiacs are evenly distributed across 12 of them