Solution to the hypothesis testing exercise with the maples dataset

1 Solution with output

## load packages
library(dplyr)
library(readr)
library(tidyr)
library(ggplot2)
library(ggfortify)

## theme for ggplot
theme_set(theme_classic())
theme_update(text = element_text(size = 14))

## Load dataset
df_maples_prep <- read_csv("data/06_maples.csv")

## Apply transformation
df_maples <- df_maples_prep %>%
  mutate(calcium_addition = factor(watershed, 
                                   levels = c("Reference", "W1"),
                                   labels = c("Control", "Ca addition"))) %>%
  select(stem_length, elevation, calcium_addition)


## Fit linear model
lm_maples <- lm(stem_length ~ elevation + calcium_addition, 
                data = df_maples)

## Check assumption
autoplot(lm_maples, which = 1:2)

## Hypothesis testing (LRT = F-test)
drop1(lm_maples, test = "F")
Single term deletions

Model:
stem_length ~ elevation + calcium_addition
                 Df Sum of Sq   RSS    AIC F value   Pr(>F)    
<none>                        47411 1274.6                     
elevation         1    189.22 47601 1273.6  0.9458 0.331770    
calcium_addition  1   2857.29 50269 1286.7 14.2830 0.000199 ***
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## Check effect size and standard error
summary(lm_maples)

Call:
lm(formula = stem_length ~ elevation + calcium_addition, data = df_maples)

Residuals:
    Min      1Q  Median      3Q     Max 
-27.397 -10.504  -1.485   8.083  45.302 

Coefficients:
                            Estimate Std. Error t value Pr(>|t|)    
(Intercept)                   80.097      1.581  50.652  < 2e-16 ***
elevationMid                   1.776      1.826   0.973 0.331770    
calcium_additionCa addition    6.901      1.826   3.779 0.000199 ***
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Residual standard error: 14.14 on 237 degrees of freedom
Multiple R-squared:  0.06038,   Adjusted R-squared:  0.05245 
F-statistic: 7.614 on 2 and 237 DF,  p-value: 0.0006237
## Bonus: add line in plot
df_maples_pred <- expand_grid(
  elevation = c("Low", "Mid"),
  calcium_addition = c("Control", "Ca addition")) %>%
  bind_cols(predict(lm_maples, newdata = ., 
                    interval = "confidence")) %>%
  rename(conf_low = lwr, conf_up = upr) %>%
  select(-fit) %>%
  bind_cols(predict(lm_maples, newdata = ., 
                  interval = "prediction")) %>%
  rename(pred_low = lwr, pred_up = upr) 

ggplot(df_maples, aes(calcium_addition, stem_length, color = elevation)) +
    geom_point(size = 3, alpha = 0.3,
               position = position_jitterdodge(
                   dodge.width = 1, jitter.width = 0.2, 
                   jitter.height = 0.0)) +
    geom_pointrange(aes(y = fit, ymin = pred_low, ymax = pred_up),
                    data = df_maples_pred,
                    position = position_dodge(width = 1),
                    linewidth = 1, size = 0) +
    geom_pointrange(aes(y = fit, ymin = conf_low, ymax = conf_up),
                    data = df_maples_pred,
                    position = position_dodge(width = 1),
                    linewidth = 2, size = 0.8) +
    geom_line(aes(y = fit, group = elevation), 
              data = df_maples_pred, position = position_dodge(width = 1),
              linetype = "longdash") +
    labs(x = "", y = "Stem length of seedlings (mm)") 

2 Explanation

The effect of calcium addition on stem length of sugar maple seedlings was tested in a factorial experiment with two levels of calcium addition (control and calcium addition) and two levels of elevation (low and mid). Calcium addition has a significant effect on stem length (F = 14.28, df = 1, p = 0.0002). The effect size is 6.9 with a standard error of 1.83. This means that the mean stem length of the seedlings in the experimental conditions increased by 6.9 mm when additional calcium was applied.

3 Solution for copy-pasting

## load packages
library(dplyr)
library(readr)
library(tidyr)
library(ggplot2)
library(ggfortify)

## theme for ggplot
theme_set(theme_classic())
theme_update(text = element_text(size = 14))

## Load dataset
df_maples_prep <- read_csv("data/06_maples.csv")

## Apply transformation
df_maples <- df_maples_prep %>%
  mutate(calcium_addition = factor(watershed, 
                                   levels = c("Reference", "W1"),
                                   labels = c("Control", "Ca addition"))) %>%
  select(stem_length, elevation, calcium_addition)


## Fit linear model
lm_maples <- lm(stem_length ~ elevation + calcium_addition, 
                data = df_maples)

## Check assumption
autoplot(lm_maples, which = 1:2)

## Hypothesis testing (LRT = F-test)
drop1(lm_maples, test = "F")

## Check effect size and standard error
summary(lm_maples)

## Bonus: add line in plot
df_maples_pred <- expand_grid(
  elevation = c("Low", "Mid"),
  calcium_addition = c("Control", "Ca addition")) %>%
  bind_cols(predict(lm_maples, newdata = ., 
                    interval = "confidence")) %>%
  rename(conf_low = lwr, conf_up = upr) %>%
  select(-fit) %>%
  bind_cols(predict(lm_maples, newdata = ., 
                  interval = "prediction")) %>%
  rename(pred_low = lwr, pred_up = upr) 

ggplot(df_maples, aes(calcium_addition, stem_length, color = elevation)) +
    geom_point(size = 3, alpha = 0.3,
               position = position_jitterdodge(
                   dodge.width = 1, jitter.width = 0.2, 
                   jitter.height = 0.0)) +
    geom_pointrange(aes(y = fit, ymin = pred_low, ymax = pred_up),
                    data = df_maples_pred,
                    position = position_dodge(width = 1),
                    linewidth = 1, size = 0) +
    geom_pointrange(aes(y = fit, ymin = conf_low, ymax = conf_up),
                    data = df_maples_pred,
                    position = position_dodge(width = 1),
                    linewidth = 2, size = 0.8) +
    geom_line(aes(y = fit, group = elevation), 
              data = df_maples_pred, position = position_dodge(width = 1),
              linetype = "longdash") +
    labs(x = "", y = "Stem length of seedlings (mm)")