Error in library(faux): there is no package called 'faux'
Bio300B Lecture 10
Institutt for biovitenskap, UiB
17 August 2025
Error in library(faux): there is no package called 'faux'
Observations not independent
Ignoring it causes pseudoreplication
Error in add_ranef(add_ranef(add_recode(add_between(add_random(subj = subj_n, : could not find function "add_ranef"
Error in `ggplot()`:
! `data` cannot be a function.
ℹ Have you misspelled the `data` argument in `ggplot()`
Error in model.frame.default(formula = Mass_g ~ condition, data = demo, : 'data' must be a data.frame, environment, or list
Error in model.frame.default(formula = Mass_g ~ tank, data = demo, drop.unused.levels = TRUE): 'data' must be a data.frame, environment, or list
Error: object 'mod_bad' not found
Error: object 'mod_bad2' not found
Error in add_ranef(add_ranef(add_recode(add_between(add_random(subj = subj_n, : could not find function "add_ranef"
Error: object 'demo2' not found
Error: object 'outer_plot' not found
\[y_i = \color{red}{\beta_0} + \color{red}{\beta_1}x_i + \color{red}{\beta_2}tank_i +\color{blue}{ \epsilon_i}\]
Not interested in effect of tank
Use a Random effect
Assumes observed tanks from a population of possible tanks
\[y_{ij} = \color{red}{\beta_0} +\color{blue}{b_{0i}}+ \color{red}{\beta_1}x_i +\color{blue}{ \epsilon_{ij}}\]
Residuals from a normal distribution \(\color{blue}{ \epsilon_{ij}} \sim N(0, \sigma_{ind})\)
Random effects from a normal distribution \(\color{blue}{b_{0i}} \sim N(0, \sigma_{clu})\)
Error: object 'demo2' not found
Error: object 'p1' not found
Fixed effects factors:
Random effects factors:
“one modeler’s random effect is another modeler’s fixed effect.”
“Are there enough levels of the factor in the data on which to base an estimate of the variance of the population of effects? No, means fixed effects.”
Error in add_ranef(add_ranef(add_recode(add_within(add_random(subj = subj_n/2, : could not find function "add_ranef"
Error: object 'outer_plot' not found
Error: object 'outer_plot' not found
Linear mixed model fit by REML. t-tests use Satterthwaite's method [
lmerModLmerTest]
Formula: Reaction ~ Days + (1 | Subject)
Data: sleepstudy
Subset: Days >= 2
REML criterion at convergence: 1430
Scaled residuals:
Min 1Q Median 3Q Max
-3.6261 -0.4450 0.0474 0.5199 4.1378
Random effects:
Groups Name Variance Std.Dev.
Subject (Intercept) 1746.9 41.80
Residual 913.1 30.22
Number of obs: 144, groups: Subject, 18
Fixed effects:
Estimate Std. Error df t value Pr(>|t|)
(Intercept) 245.097 11.829 30.617 20.72 <2e-16 ***
Days 11.435 1.099 125.000 10.40 <2e-16 ***
---
Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
Correlation of Fixed Effects:
(Intr)
Days -0.511
Error in library(broom.mixed): there is no package called 'broom.mixed'
Error in `augment()`:
! No `augment()` method for objects of class <lmerModLmerTest>.
Linear mixed model fit by REML. t-tests use Satterthwaite's method [
lmerModLmerTest]
Formula: Reaction ~ Days + (Days | Subject)
Data: sleepstudy
Subset: Days >= 2
REML criterion at convergence: 1404.1
Scaled residuals:
Min 1Q Median 3Q Max
-4.0157 -0.3541 0.0069 0.4681 5.0732
Random effects:
Groups Name Variance Std.Dev. Corr
Subject (Intercept) 992.69 31.507
Days 45.77 6.766 -0.25
Residual 651.59 25.526
Number of obs: 144, groups: Subject, 18
Fixed effects:
Estimate Std. Error df t value Pr(>|t|)
(Intercept) 245.097 9.260 16.999 26.468 2.95e-15 ***
Days 11.435 1.845 17.001 6.197 9.74e-06 ***
---
Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
Correlation of Fixed Effects:
(Intr)
Days -0.454
Error in `augment()`:
! No `augment()` method for objects of class <lmerModLmerTest>.
Data: sleepstudy
Subset: Days >= 2
Models:
fm1: Reaction ~ Days + (1 | Subject)
fm2: Reaction ~ Days + (Days | Subject)
npar AIC BIC logLik -2*log(L) Chisq Df Pr(>Chisq)
fm1 4 1446.5 1458.4 -719.25 1438.5
fm2 6 1425.2 1443.0 -706.58 1413.2 25.332 2 3.156e-06 ***
---
Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
See ?plot.merMod
performance::check_model()
DHARMa package
Two random effects
Eggs from birds nests (first random effect - lay_nest) moved to other nests (second random effect - hatch_nest)
Hierarchical random effects
Sometimes mixed effect models report errors.
Take them seriously.
May need to simplify the model
Having predictors on same scale can help
Generalised linear mixed effect models
Fit with glmer()
Autocorrelated data
nlme::lme()
glmmTMB
packageMixed effect models can be hard to fit
Bayesian model can help
Different statistical philosophy
Use prior information (or uninformative priors)
Bolker B (2021) GLMM FAQ
Harrison et al (2018) A brief introduction to mixed effects modelling and multi-model inference in ecology