At the end of this lesson, students will …
soilN_biomass.csv file to a data frame called soilN_biomassfield: character column to identify which field the measurement comes fromsoilN: numeric column for the soil N valuebiomass: numeric column for the plant biomass valueggplot() (Don’t worry about the code used to make this plot for now.)~
LHS ~ RHS+:* indicates all possible combinations of interactions for a set of variables (I don’t recommend)weight ~ height + sex + height:sexmodel weight = height sex height*sex1 to the RHS
weight ~ 1 + height1 with 0
weight ~ 0 + heightlm() (linear model)y ~ xdata argument is the data frame containing columns biomass and soilNsummary()soilNIf you just want the coefficients you can use the coef() function:
separate_lm_fits <- soilN_biomass %>%
group_by(field) %>%
group_map(~ lm(biomass ~ soilN, data = .))
separate_lm_coefs <- data.frame(field = c('a', 'b', 'c', 'd', 'e'),
intercept = map_dbl(separate_lm_fits, ~ coef(.)[1]),
slope = map_dbl(separate_lm_fits, ~ coef(.)[2]))
ggplot(soilN_biomass, aes(x = soilN, y = biomass, color = field)) +
geom_point(size = 1.5) +
theme_bw() +
geom_abline(aes(intercept = intercept, slope = slope, color = field), size = 1, data = separate_lm_coefs)Mixed models meme courtesy of Chelsea Parlett-Pelleriti on Twitter
lmer() instead of lm()+ (1 | field) – what’s that new thing?(1 | field)
()| in the middle of it.| is the design component (1 indicates an intercept)| are the grouping factors (here it is only field)Plot model fit with ggplot2 (again don’t worry about code)
pred_grid <- expand.grid(soilN = c(0, 10), field = letters[1:5])
mm_pred_group <- cbind(pred_grid, biomass = predict(mm_fit, newdata = pred_grid))
mm_pred_population <- data.frame(soilN = c(0, 10), biomass = predict(mm_fit, newdata = data.frame(soilN = c(0, 10)), re.form = ~ 0))
ggplot(soilN_biomass, aes(x = soilN, y = biomass, color = field)) +
geom_point(size = 1.5) +
theme_bw() +
geom_line(data = mm_pred_group) +
geom_line(data = mm_pred_population, color = 'black', size = 1)(soilN | field)soilN (and intercept) will be different for each fieldmm_pred_group_randomslopes <- cbind(pred_grid, biomass = predict(mm_fit_randomslopes, newdata = pred_grid))
mm_pred_population_randomslopes <- data.frame(soilN = c(0, 10), biomass = predict(mm_fit_randomslopes, newdata = data.frame(soilN = c(0, 10)), re.form = ~ 0))
ggplot(soilN_biomass, aes(x = soilN, y = biomass, color = field)) +
geom_point(size = 1.5) +
theme_bw() +
geom_line(data = mm_pred_group_randomslopes) +
geom_line(data = mm_pred_population_randomslopes, color = 'black', size = 1)check_model() from easystats package produces all the diagnostic plots you needsummary() of the fitted model object gives us a lot of output
ranef(): random effectsfixef(): fixed effectscoef(): coefficients
anova()treatment factor levels to a logical ordertreatmentlow, and treatmenthighVarCorr()Impressive!