library(readxl)
library(dplyr)
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
library(ggplot2)
library(broom)
data <- read_excel("~/Downloads/sunny2024_traits.xlsx")
data <- data |>
mutate(root_mass_fraction = as.numeric(root_mass_fraction))
## Warning: There was 1 warning in `mutate()`.
## ℹ In argument: `root_mass_fraction = as.numeric(root_mass_fraction)`.
## Caused by warning:
## ! NAs introduced by coercion
Initial Visualization
ggplot(data) +
aes(x = water, y = root_mass_fraction, fill = light) +
geom_boxplot() +
facet_wrap(~species) +
theme_bw()
## Warning: Removed 1 row containing non-finite outside the scale range
## (`stat_boxplot()`).
data |>
group_by(water, light) |>
summarise(
mean_root = mean(root_mass_fraction, na.rm = TRUE),
sd_root = sd(root_mass_fraction, na.rm = TRUE),
.groups = "drop"
)
## # A tibble: 4 × 4
## water light mean_root sd_root
## <chr> <chr> <dbl> <dbl>
## 1 High Shade 0.270 0.0715
## 2 High Sun 0.253 0.0558
## 3 Low Shade 0.329 0.103
## 4 Low Sun 0.332 0.0865
#main analysis
RootResult <- lm(root_mass_fraction ~ water * light + species,
data = data)
tidy(RootResult, conf.int = TRUE)
## # A tibble: 7 × 7
## term estimate std.error statistic p.value conf.low conf.high
## <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 (Intercept) 0.328 0.0170 19.2 2.81e-33 0.294 0.361
## 2 waterLow 0.0614 0.0183 3.36 1.16e- 3 0.0251 0.0977
## 3 lightSun -0.0177 0.0181 -0.978 3.31e- 1 -0.0536 0.0183
## 4 speciesMemycelon umb… -0.109 0.0183 -5.96 5.03e- 8 -0.145 -0.0727
## 5 speciesSyzygium cumi… -0.108 0.0183 -5.90 6.52e- 8 -0.144 -0.0716
## 6 speciesTerminalia ch… -0.0121 0.0183 -0.661 5.10e- 1 -0.0484 0.0242
## 7 waterLow:lightSun 0.0176 0.0257 0.684 4.96e- 1 -0.0335 0.0687
# Diagnostic plots
plot(RootResult)
## Interpreting the water-light interaction
# b: Effect of limited water under shade (baseline group)
b <- coef(RootResult)[["waterLow"]]
# d: Difference in water effect between sun and shade
d <- coef(RootResult)[["waterLow:lightSun"]]
waterEffects <- c(
shade = b, # Effect in baseline group
sun = b + d, # Effect in other group
interaction = d # Difference between effects
)
print(waterEffects)
## shade sun interaction
## 0.06139317 0.07898249 0.01758932
#interaction plot
ggplot(data) +
aes(x = water, y = root_mass_fraction,
colour = light, group = light) +
stat_summary(fun = mean, geom = "point") +
stat_summary(fun = mean, geom = "line") +
theme_bw()
## Warning: Removed 1 row containing non-finite outside the scale range (`stat_summary()`).
## Removed 1 row containing non-finite outside the scale range (`stat_summary()`).