Null hypothesis:
\(\mu_{diet1}=\mu_{diet2}=\mu_{diet3}=\mu_{diet4}\)
Test and why:
IV: Diet: 1 Factor with 4 levels
DV: Consumption (kg/wk) by deer
I will use a 1-factor ANOVA because our IV is 1 factor with 4 levels.
deer <- data.frame(Diet = as.factor(rep(1:4, times = c(5, 6, 6, 5))),Consumption = c(4.7, 4.9, 5.0, 4.8, 4.7,4.6, 4.4, 4.3, 4.4, 4.1, 4.2, 4.8, 4.7, 4.6, 4.4, 4.7, 4.8,4.9, 5.2, 5.4, 5.1, 5.6))
#Shapiro test
shapiro.test(deer$Consumption)#normal!
##
## Shapiro-Wilk normality test
##
## data: deer$Consumption
## W = 0.97138, p-value = 0.7427
#Bartlett test
bartlett.test(Consumption~Diet, data=deer) #looks equal var enough
##
## Bartlett test of homogeneity of variances
##
## data: Consumption by Diet
## Bartlett's K-squared = 2.4553, df = 3, p-value = 0.4834
What do the assumption checks tell me?
The shapiro.test
tells me that the data are normal enough, if the p-value returns greater
than 0.05 for the response variable.
The bartlet.test
tells me whether the variances between the IV and DV are equal enough
(they should be similar) and if the p-value returns > 0.05 then we
can proceed with an ANOVA.
deermodel <- lm(Consumption ~ Diet, data=deer) #fit model
summary(deermodel)
##
## Call:
## lm(formula = Consumption ~ Diet, data = deer)
##
## Residuals:
## Min 1Q Median 3Q Max
## -0.34000 -0.12000 0.00667 0.12000 0.36000
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 4.82000 0.08344 57.763 < 2e-16 ***
## Diet2 -0.48667 0.11298 -4.307 0.000424 ***
## Diet3 -0.15333 0.11298 -1.357 0.191518
## Diet4 0.42000 0.11801 3.559 0.002242 **
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 0.1866 on 18 degrees of freedom
## Multiple R-squared: 0.7864, Adjusted R-squared: 0.7507
## F-statistic: 22.08 on 3 and 18 DF, p-value: 2.937e-06
#Anova test whether diet significantly effects consumption
Anova(deermodel) #significcant! but who?
## Anova Table (Type II tests)
##
## Response: Consumption
## Sum Sq Df F value Pr(>F)
## Diet 2.30652 3 22.084 2.937e-06 ***
## Residuals 0.62667 18
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
#which group is different?
m1<-aov(Consumption~Diet, data=deer)
TukeyHSD(m1)
## Tukey multiple comparisons of means
## 95% family-wise confidence level
##
## Fit: aov(formula = Consumption ~ Diet, data = deer)
##
## $Diet
## diff lwr upr p adj
## 2-1 -0.4866667 -0.80599282 -0.1673405 0.0021859
## 3-1 -0.1533333 -0.47265948 0.1659928 0.5405724
## 4-1 0.4200000 0.08647471 0.7535253 0.0109631
## 3-2 0.3333333 0.02886780 0.6377989 0.0290758
## 4-2 0.9066667 0.58734052 1.2259928 0.0000013
## 4-3 0.5733333 0.25400718 0.8926595 0.0004209
Results and conclusion:
Diet has an effect on consumption in deer. A post-hoc Tukey analysis shows that the differences are significant between the following groups: 2-1, 4-1, 3-2, 4-2, 4-3, with an alpha of 0.05.
Diet explained 75% of the variation in weekly food consumption.\(R^2_{adj.}=0.751\)
#add significance letter
letters <- data.frame(Diet = factor(1:4),label = c("A", "B", "A", "C"),height = c(5.2, 4.8, 5.0, 5.8))
#boxplot it
ggplot(deer, aes(Diet, Consumption, color=Diet))+
geom_boxplot()+
theme(legend.position = "none")+
geom_text(data = letters, aes(Diet, height, label=label))
Figure 1. Diet significantly affected weekly food consumption (one-way ANOVA: \(F_{3,18}\) = 22.08, p < 0.001), explaining 78.6% of the variation. Therefore, I rejected the null hypothesis that all four diets had equal mean consumption. Tukey’s HSD showed that deer on Diet 4 consumed significantly more food than deer on all other diets, while deer on Diet 2 consumed significantly less than those on all other diets (adjusted p < 0.05). Diets 1 and 3 did not differ significantly (adjusted p = 0.541).
Independent variable:
Soil Types as a factor with 4 levels
Dependent variable:
Inorganic Carbon (%)
Null hypothesis:
\(\mu_{clay}=\mu_{sand}=\mu_{brunisol}=\mu_{podzol}\)
Alternative hypothesis:
At least one mean above is not equivalent to the others.
soil <- data.frame(Soil = factor(rep(c("Clay", "Sand", "Brunisol", "Podzol"), each = 6)),Carbon = c(4, 4, 7, 5, 3, 6, 7, 8, 6, 7, 8, 9,7, 9, 10, 10, 10, 11, 2, 1, 7, 3, 2, 3))
#Shapiro test
shapiro.test(soil$Carbon) #normal enough!
##
## Shapiro-Wilk normality test
##
## data: soil$Carbon
## W = 0.95022, p-value = 0.274
#Bartlett test
bartlett.test(Carbon~Soil, data=soil) #variances are equal enough
##
## Bartlett test of homogeneity of variances
##
## data: Carbon by Soil
## Bartlett's K-squared = 2.2892, df = 3, p-value = 0.5146
What do the assumption checks tell me?
The shapiro.test
tells me that the data are normal enough, if the p-value returns greater
than 0.05 for the response variable.
The bartlet.test
tells me whether the variances between the IV and DV are equal enough
(they should be similar) and if the p-value returns > 0.05 then we
can proceed with an ANOVA.
#linear model
soilmodel <- lm(Carbon~Soil, data=soil)
summary(soilmodel) #someone in there is different! Maybe more than 1 level.
##
## Call:
## lm(formula = Carbon ~ Soil, data = soil)
##
## Residuals:
## Min 1Q Median 3Q Max
## -2.500 -0.875 0.000 0.500 4.000
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 9.5000 0.6314 15.047 2.27e-12 ***
## SoilClay -4.6667 0.8929 -5.227 4.09e-05 ***
## SoilPodzol -6.5000 0.8929 -7.280 4.85e-07 ***
## SoilSand -2.0000 0.8929 -2.240 0.0366 *
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 1.547 on 20 degrees of freedom
## Multiple R-squared: 0.7559, Adjusted R-squared: 0.7193
## F-statistic: 20.64 on 3 and 20 DF, p-value: 2.451e-06
#also, note that the lm summary only tells you who is different with respect to the alphabetic reference level. not between the other groups.
Results and conclusion:
The Soil type significantly affected carbon stored (one-way ANOVA: \(F_{3,20}\) = 20.64, p < 0.001), explaining 75.6% of the variation. Therefore, I rejected the null hypothesis that all four soil types had equal mean carbon. Tukey’s HSD showed that Brunisol and Sand each had significantly more carbon than Clay and Podzol (adjusted p < 0.05). Brunisol and Sand did not differ significantly, nor did Clay and Podzol.
m2 <- aov(Carbon ~ Soil, data = soil)
tukey <- TukeyHSD(m2)
tukey
## Tukey multiple comparisons of means
## 95% family-wise confidence level
##
## Fit: aov(formula = Carbon ~ Soil, data = soil)
##
## $Soil
## diff lwr upr p adj
## Clay-Brunisol -4.666667 -7.1657612 -2.1675721 0.0002222
## Podzol-Brunisol -6.500000 -8.9990946 -4.0009054 0.0000027
## Sand-Brunisol -2.000000 -4.4990946 0.4990946 0.1466221
## Podzol-Clay -1.833333 -4.3324279 0.6657612 0.2025056
## Sand-Clay 2.666667 0.1675721 5.1657612 0.0338736
## Sand-Podzol 4.500000 2.0009054 6.9990946 0.0003386
plot(tukey) #SWEEEEET!!!!! <3 it
Which means are different?
Clay and Podzol had statistically significant differences in their mean carbon concentration.
#add significance letter
letters2 <- data.frame(Soil = factor(c("Brunisol", "Clay", "Podzol", "Sand")),label = c("A", "B", "C", "A"),height = c(12, 7.8, 7.8, 9.3))
#boxplot it
ggplot(soil, aes(Soil, Carbon, color=Soil))+
geom_boxplot()+
theme(legend.position = "none")+
geom_text(data = letters2, aes(Soil, height, label=label))
Figure 2. Soil type significantly affected carbon stored (one-way ANOVA: F₃,₂₀ = 20.64, p < 0.001), explaining 75.6% of the variation. Therefore, I rejected the null hypothesis that all four soil types had equal mean carbon. Tukey’s HSD showed that Brunisol and Sand each had significantly more carbon than Clay and Podzol (adjusted p < 0.05). Brunisol and Sand did not differ significantly, nor did Clay and Podzol.