Please work out in R by doing a chi-squared test on the treatment (X) and improvement (Y) columns in treatment.csv.
## 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
## 11 11 not-treated improved
## 12 12 not-treated not-improved
## 13 13 not-treated not-improved
## 14 14 not-treated not-improved
## 15 15 not-treated improved
## 16 16 not-treated improved
## 17 17 treated improved
## 18 18 treated improved
## 19 19 not-treated not-improved
## 20 20 not-treated not-improved
## 21 21 treated not-improved
## 22 22 not-treated not-improved
## 23 23 treated not-improved
## 24 24 not-treated improved
## 25 25 treated improved
## 26 26 treated improved
## 27 27 not-treated not-improved
## 28 28 not-treated improved
## 29 29 treated not-improved
## 30 30 treated improved
## 31 31 not-treated not-improved
## 32 32 not-treated not-improved
## 33 33 treated improved
## 34 34 not-treated improved
## 35 35 treated not-improved
## 36 36 not-treated improved
## 37 37 treated improved
## 38 38 not-treated not-improved
## 39 39 not-treated improved
## 40 40 treated improved
## 41 41 not-treated improved
## 42 42 not-treated improved
## 43 43 not-treated not-improved
## 44 44 not-treated improved
## 45 45 not-treated improved
## 46 46 treated improved
## 47 47 treated not-improved
## 48 48 not-treated not-improved
## 49 49 treated improved
## 50 50 treated improved
## 51 51 not-treated not-improved
## 52 52 treated improved
## 53 53 not-treated improved
## 54 54 treated improved
## 55 55 treated improved
## 56 56 not-treated improved
## 57 57 treated improved
## 58 58 not-treated not-improved
## 59 59 treated improved
## 60 60 treated improved
## 61 61 treated improved
## 62 62 not-treated improved
## 63 63 treated not-improved
## 64 64 treated not-improved
## 65 65 not-treated improved
## 66 66 not-treated improved
## 67 67 not-treated improved
## 68 68 not-treated not-improved
## 69 69 not-treated not-improved
## 70 70 treated improved
## 71 71 treated not-improved
## 72 72 not-treated not-improved
## 73 73 treated not-improved
## 74 74 not-treated improved
## 75 75 not-treated not-improved
## 76 76 not-treated not-improved
## 77 77 treated not-improved
## 78 78 not-treated improved
## 79 79 treated improved
## 80 80 treated improved
## 81 81 treated improved
## 82 82 not-treated not-improved
## 83 83 treated improved
## 84 84 not-treated not-improved
## 85 85 treated improved
## 86 86 not-treated improved
## 87 87 not-treated not-improved
## 88 88 treated improved
## 89 89 not-treated not-improved
## 90 90 treated improved
## 91 91 not-treated not-improved
## 92 92 not-treated improved
## 93 93 treated not-improved
## 94 94 treated not-improved
## 95 95 not-treated not-improved
## 96 96 treated improved
## 97 97 not-treated improved
## 98 98 treated improved
## 99 99 not-treated not-improved
## 100 100 not-treated improved
## 101 101 treated improved
## 102 102 treated improved
## 103 103 not-treated not-improved
## 104 104 treated improved
## 105 105 not-treated not-improved
##
## improved not-improved
## not-treated 26 29
## treated 35 15
##
## Pearson's Chi-squared test
##
## data: data.imelda$treatment and data.imelda$improvement
## X-squared = 5.5569, df = 1, p-value = 0.01841
After we calculate the data, We have a chi-squared value of 5.55. Since we get a p-Value less than the significance level of 0.05.
Find out if the cyl and carb variables in mtcars dataset are dependent or not.
## mpg cyl disp hp drat wt qsec vs am gear carb
## Mazda RX4 21.0 6 160.0 110 3.90 2.620 16.46 0 1 4 4
## Mazda RX4 Wag 21.0 6 160.0 110 3.90 2.875 17.02 0 1 4 4
## Datsun 710 22.8 4 108.0 93 3.85 2.320 18.61 1 1 4 1
## Hornet 4 Drive 21.4 6 258.0 110 3.08 3.215 19.44 1 0 3 1
## Hornet Sportabout 18.7 8 360.0 175 3.15 3.440 17.02 0 0 3 2
## Valiant 18.1 6 225.0 105 2.76 3.460 20.22 1 0 3 1
## Duster 360 14.3 8 360.0 245 3.21 3.570 15.84 0 0 3 4
## Merc 240D 24.4 4 146.7 62 3.69 3.190 20.00 1 0 4 2
## Merc 230 22.8 4 140.8 95 3.92 3.150 22.90 1 0 4 2
## Merc 280 19.2 6 167.6 123 3.92 3.440 18.30 1 0 4 4
## Merc 280C 17.8 6 167.6 123 3.92 3.440 18.90 1 0 4 4
## Merc 450SE 16.4 8 275.8 180 3.07 4.070 17.40 0 0 3 3
## Merc 450SL 17.3 8 275.8 180 3.07 3.730 17.60 0 0 3 3
## Merc 450SLC 15.2 8 275.8 180 3.07 3.780 18.00 0 0 3 3
## Cadillac Fleetwood 10.4 8 472.0 205 2.93 5.250 17.98 0 0 3 4
## Lincoln Continental 10.4 8 460.0 215 3.00 5.424 17.82 0 0 3 4
## Chrysler Imperial 14.7 8 440.0 230 3.23 5.345 17.42 0 0 3 4
## Fiat 128 32.4 4 78.7 66 4.08 2.200 19.47 1 1 4 1
## Honda Civic 30.4 4 75.7 52 4.93 1.615 18.52 1 1 4 2
## Toyota Corolla 33.9 4 71.1 65 4.22 1.835 19.90 1 1 4 1
## Toyota Corona 21.5 4 120.1 97 3.70 2.465 20.01 1 0 3 1
## Dodge Challenger 15.5 8 318.0 150 2.76 3.520 16.87 0 0 3 2
## AMC Javelin 15.2 8 304.0 150 3.15 3.435 17.30 0 0 3 2
## Camaro Z28 13.3 8 350.0 245 3.73 3.840 15.41 0 0 3 4
## Pontiac Firebird 19.2 8 400.0 175 3.08 3.845 17.05 0 0 3 2
## Fiat X1-9 27.3 4 79.0 66 4.08 1.935 18.90 1 1 4 1
## Porsche 914-2 26.0 4 120.3 91 4.43 2.140 16.70 0 1 5 2
## Lotus Europa 30.4 4 95.1 113 3.77 1.513 16.90 1 1 5 2
## Ford Pantera L 15.8 8 351.0 264 4.22 3.170 14.50 0 1 5 4
## Ferrari Dino 19.7 6 145.0 175 3.62 2.770 15.50 0 1 5 6
## Maserati Bora 15.0 8 301.0 335 3.54 3.570 14.60 0 1 5 8
## Volvo 142E 21.4 4 121.0 109 4.11 2.780 18.60 1 1 4 2
##
## 1 2 3 4 6 8
## 4 5 6 0 0 0 0
## 6 2 0 0 4 1 0
## 8 0 4 3 6 0 1
## Warning in chisq.test(mtcars$cyl, mtcars$carb, correct = FALSE): Chi-squared
## approximation may be incorrect
##
## Pearson's Chi-squared test
##
## data: mtcars$cyl and mtcars$carb
## X-squared = 24.389, df = 10, p-value = 0.006632
Of the calculation that have been made, we get high chi-squared value and a p-value of less that 0.05 significance level. When \(a > p-value\) we reject the null hypothesis and conclude that \(carb\) and \(cyl\) have a significant relationship (independent).
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)
category <- c("Aries",
"Taurus",
"Gemini",
"Cancer",
"Leo",
"Virgo",
"Libra",
"Scorpio",
"Sagittarius",
"Capricorn",
"Aquarius",
"Pisces")
observed <- c(29,24,22,19,21,18,19,20,23,18,20,23)
expected <- c(256/12)
residual <- c(observed - expected)
obs_exp <- c((observed-expected)^2)
component <- c(obs_exp/expected)
zodiac_sign <- (data.frame(category,
observed,
expected,
residual,
obs_exp,
component))
print(zodiac_sign)## category observed expected residual obs_exp component
## 1 Aries 29 21.33333 7.6666667 58.7777778 2.755208333
## 2 Taurus 24 21.33333 2.6666667 7.1111111 0.333333333
## 3 Gemini 22 21.33333 0.6666667 0.4444444 0.020833333
## 4 Cancer 19 21.33333 -2.3333333 5.4444444 0.255208333
## 5 Leo 21 21.33333 -0.3333333 0.1111111 0.005208333
## 6 Virgo 18 21.33333 -3.3333333 11.1111111 0.520833333
## 7 Libra 19 21.33333 -2.3333333 5.4444444 0.255208333
## 8 Scorpio 20 21.33333 -1.3333333 1.7777778 0.083333333
## 9 Sagittarius 23 21.33333 1.6666667 2.7777778 0.130208333
## 10 Capricorn 18 21.33333 -3.3333333 11.1111111 0.520833333
## 11 Aquarius 20 21.33333 -1.3333333 1.7777778 0.083333333
## 12 Pisces 23 21.33333 1.6666667 2.7777778 0.130208333
## [1] 5.09375
If we calculate the data manual, we’re going through a lot of steps. The above steps are manual steps by calculating one by one the existing data. Then, add the total of the component variabel as the value of its Chi-squared.
A faster way is to use the \(chisq.test\) function to find out the value of its Chi-squared without having to go through a long step, because we only give 1 variable to be researched.
##
## Chi-squared test for given probabilities
##
## data: zodiac_sign$observed
## X-squared = 5.0938, df = 11, p-value = 0.9265
Of the calculation that have been made, we get chi-squared value of 5.09 and a p-value is greater than 0.05 significance level. When \(a < p-value\) we accepted the null hypothesis and conclude that zodiac signs are evenly distributed across visual artists.