Use as.factor to convert independent groupings to factors!!
Use multiway anova to analyze crop.data.csv
Show all work, comment on what the code is doing
Are all 3 main effects (including blocks) significant?
Are any interaction effects significant?
Which pairs of means show significance under Tukeys HSD after removing non significant effects!
crop.data <- read.csv("crop.data.csv")
# Quick inspection of the raw data
head(crop.data)
## density block fertilizer yield
## 1 1 1 1 177.2287
## 2 2 2 1 177.5500
## 3 1 3 1 176.4085
## 4 2 4 1 177.7036
## 5 1 1 1 177.1255
## 6 2 2 1 176.7783
str(crop.data)
## 'data.frame': 96 obs. of 4 variables:
## $ density : int 1 2 1 2 1 2 1 2 1 2 ...
## $ block : int 1 2 3 4 1 2 3 4 1 2 ...
## $ fertilizer: int 1 1 1 1 1 1 1 1 1 1 ...
## $ yield : num 177 178 176 178 177 ...
# str() reveals density, block, and fertilizer are read as integers by default.
# Convert grouping variables to factors (as.factor)
crop.data$density <- as.factor(crop.data$density) # 2 levels: 1, 2
crop.data$block <- as.factor(crop.data$block) # levels: 1, 2, 3, 4
crop.data$fertilizer <- as.factor(crop.data$fertilizer) # 3 levels: 1, 2, 3
str(crop.data)
## 'data.frame': 96 obs. of 4 variables:
## $ density : Factor w/ 2 levels "1","2": 1 2 1 2 1 2 1 2 1 2 ...
## $ block : Factor w/ 4 levels "1","2","3","4": 1 2 3 4 1 2 3 4 1 2 ...
## $ fertilizer: Factor w/ 3 levels "1","2","3": 1 1 1 1 1 1 1 1 1 1 ...
## $ yield : num 177 178 176 178 177 ...
# Fit the FULL multiway ANOVA model
model.full <- aov(yield ~ density * block * fertilizer, data = crop.data)
# Display the ANOVA table (Type I sequential SS, standard in R's aov)
summary(model.full)
## Df Sum Sq Mean Sq F value Pr(>F)
## density 1 5.122 5.122 14.854 0.000226 ***
## block 2 0.486 0.243 0.705 0.497028
## fertilizer 2 6.068 3.034 8.799 0.000339 ***
## density:fertilizer 2 0.428 0.214 0.620 0.540182
## block:fertilizer 4 0.887 0.222 0.643 0.633043
## Residuals 84 28.963 0.345
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
From the full ANOVA table:
density : F(1,84) = 14.854, p = 0.000226 → SIGNIFICANT ***
block : F(2,84) = 0.705, p = 0.497028 → NOT significant
fertilizer : F(2,84) = 8.799, p = 0.000339 → SIGNIFICANT ***
NO — not all three main effects are significant. density and fertilizer are both highly significant, but block is NOT significant (p = 0.497). The blocking variable did not explain a meaningful portion of yield variance in this dataset.
From the full ANOVA table:
density:fertilizer : F(2,84) = 0.620, p = 0.540 → NOT significant
block:fertilizer : F(4,84) = 0.643, p = 0.633 → NOT significant
(density:block and the 3-way term were aliased/dropped by R due to
the structure of the design — neither contributes additional information)
NONE of the interaction terms are significant. The effects of density, block, and fertilizer on yield are ADDITIVE — the impact of one factor does not depend on the level of another.
# Fit the REDUCED model
model.reduced <- aov(yield ~ density + fertilizer, data = crop.data)
summary(model.reduced)
## Df Sum Sq Mean Sq F value Pr(>F)
## density 1 5.122 5.122 15.316 0.000174 ***
## fertilizer 2 6.068 3.034 9.073 0.000253 ***
## Residuals 92 30.765 0.334
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
# Tukey HSD post-hoc test on the REDUCED model
TukeyHSD(model.reduced)
## Tukey multiple comparisons of means
## 95% family-wise confidence level
##
## Fit: aov(formula = yield ~ density + fertilizer, data = crop.data)
##
## $density
## diff lwr upr p adj
## 2-1 0.461956 0.2275204 0.6963916 0.0001741
##
## $fertilizer
## diff lwr upr p adj
## 2-1 0.1761687 -0.16822506 0.5205625 0.4452958
## 3-1 0.5991256 0.25473179 0.9435194 0.0002219
## 3-2 0.4229569 0.07856306 0.7673506 0.0119381
# Visualize the Tukey confidence intervals
par(mfrow = c(1, 2)) # 2 plots side by side (one per factor)
plot(TukeyHSD(model.reduced))
DENSITY (2 levels → 1 comparison) 2 vs 1 diff = +0.462, p = 0.000184 *** SIGNIFICANT
FERTILIZER (3 levels → 3 comparisons)
2 vs 1 diff = +0.176, p = 0.448 NOT significant
3 vs 1 diff = +0.599, p = 0.000236 *** SIGNIFICANT
3 vs 2 diff = +0.423, p = 0.012 ** SIGNIFICANT
BLOCK
GRAPH:
Fertilizer 3 produces significantly HIGHER yield than both fertilizer 1 and fertilizer 2. Fertilizers 1 and 2 do NOT differ from each other. In summary: fertilizer 3 > fertilizers 1 and 2 (which are equivalent).
SUMMARY OF ALL SIGNIFICANT PAIRS:
density : 2 > 1 ***
fertilizer : 3 > 1 *, 3 > 2