1.1. Calculate means & standard deviations for 4 groups
data(CO2)
aggregate(CO2$uptake, by=list(CO2$Type,CO2$Treatment), FUN=mean)
## Group.1 Group.2 x
## 1 Quebec nonchilled 35.33333
## 2 Mississippi nonchilled 25.95238
## 3 Quebec chilled 31.75238
## 4 Mississippi chilled 15.81429
aggregate(CO2$uptake, by=list(CO2$Type,CO2$Treatment), FUN=sd)
## Group.1 Group.2 x
## 1 Quebec nonchilled 9.596371
## 2 Mississippi nonchilled 7.402136
## 3 Quebec chilled 9.644823
## 4 Mississippi chilled 4.058976
1.2. Perform one-way tests twice: once for Type and once for Treatment
fit_type=lm(uptake~Type, data=CO2)
anova(fit_type)
## Analysis of Variance Table
##
## Response: uptake
## Df Sum Sq Mean Sq F value Pr(>F)
## Type 1 3365.5 3365.5 43.519 3.835e-09 ***
## Residuals 82 6341.4 77.3
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
The p-values is smaller than 0.05. Thus we should reject the null hypotheses and conclude that type can affect uptake significantly.
fit_treat=lm(uptake~Treatment, data=CO2)
anova(fit_treat)
## Analysis of Variance Table
##
## Response: uptake
## Df Sum Sq Mean Sq F value Pr(>F)
## Treatment 1 988.1 988.11 9.2931 0.003096 **
## Residuals 82 8718.9 106.33
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
The p-values is smaller than 0.05. Thus we should reject the null hypotheses and conclude that treatment can affect uptake significantly.
1.3. Perform a two-way test for Type and Treatment
fit=lm(uptake~Type*Treatment, data=CO2)
anova(fit)
## Analysis of Variance Table
##
## Response: uptake
## Df Sum Sq Mean Sq F value Pr(>F)
## Type 1 3365.5 3365.5 52.5086 2.378e-10 ***
## Treatment 1 988.1 988.1 15.4164 0.0001817 ***
## Type:Treatment 1 225.7 225.7 3.5218 0.0642128 .
## Residuals 80 5127.6 64.1
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
The p-values for type and treatment are both smaller than 0.05. Thus we should reject the null hypothesis and conclude that both type and treatment can affect uptake significantly. However, the interaction effect between type and treatment on uptake is not significant (p=0.06).
2.1. Use the table() function with the following combinations
attach(mtcars)
table(vs, am)
## am
## vs 0 1
## 0 12 6
## 1 7 7
table(gear, carb)
## carb
## gear 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
table(cyl, gear)
## gear
## cyl 3 4 5
## 4 1 8 2
## 6 2 4 1
## 8 12 0 2
Form the cross tables above, I guess that gear depends on carb, cyl depends on gear, while am won’t influence vs.
2.2. Perform a Chi-Squared analysis for each of the three cases above
chisq.test(table(vs, am))
##
## Pearson's Chi-squared test with Yates' continuity correction
##
## data: table(vs, am)
## X-squared = 0.34754, df = 1, p-value = 0.5555
The p-value (p=0.56) is greater than 0.05. Thus we should accept the null hypothesis and conclude vs doesn’t dependent on am.
chisq.test(table(gear, carb))
## Warning in chisq.test(table(gear, carb)): Chi-squared approximation may be
## incorrect
##
## Pearson's Chi-squared test
##
## data: table(gear, carb)
## X-squared = 16.518, df = 10, p-value = 0.08573
The p-value (p=0.09) is greater than 0.05. Thus we should accept the null hypothesis and conclude gear is independent of carb.
chisq.test(table(cyl, gear))
## Warning in chisq.test(table(cyl, gear)): Chi-squared approximation may be
## incorrect
##
## Pearson's Chi-squared test
##
## data: table(cyl, gear)
## X-squared = 18.036, df = 4, p-value = 0.001214
The p-value is smaller than 0.01. Thus we should reject the null hypothesis and conclude cyl is dependent of gear.