| f_lmer | R Documentation |
lme4::lmer() including assumption checks, diagnostics, R-squared and post hoc tests.Fits a linear mixed-effects model using lme4::lmer() (with p-values
from lmerTest) and produces a fully-formatted report containing
the fixed-effects table, random-effects variance components, model-fit
indices (AIC, BIC, logLik, marginal & conditional R^2), residual and
BLUP diagnostics, convergence / singular-fit warnings, and post hoc
comparisons (emmeans) on factor fixed effects. Results can be
returned to the console or written to 'pdf', 'Word' or 'Excel'.
f_lmer(
formula,
data = NULL,
REML = TRUE,
ddf = "Satterthwaite",
alpha = 0.05,
adjust = "sidak",
diagnostic_plots = TRUE,
effect_plot = TRUE,
contrast_plots = FALSE,
post_hoc = TRUE,
intro_text = TRUE,
output_type = "default",
save_as = NULL,
save_in_wdir = FALSE,
close_generated_files = FALSE,
open_generated_files = interactive(),
norm_plots = lifecycle::deprecated(),
...
)
formula |
A two-sided formula passed to More than one response variable can be supplied on the left-hand side
using |
data |
A data frame containing the variables in the model. |
REML |
Logical. If |
ddf |
Character. Method for computing denominator degrees of freedom for fixed-effects p-values. One of:
|
alpha |
Numeric. Significance level for the fixed-effects table and
the post hoc tests. Default is |
adjust |
Character. Method used to adjust p-values for multiple
pairwise comparisons in the post hoc step (passed to
|
diagnostic_plots |
Logical. If |
effect_plot |
Logical. If |
contrast_plots |
Logical. If |
post_hoc |
Logical. If |
intro_text |
Logical. If |
output_type |
Character. Output format. One of:
|
save_as |
Character. Output file path. See |
save_in_wdir |
Logical. If |
close_generated_files |
Logical. Closes any open Word or Excel
files before writing. Cross-platform (Windows taskkill, macOS / Linux
pkill). Default |
open_generated_files |
Logical. Whether to open the generated output
files after creation. Defaults to |
norm_plots |
'r lifecycle::badge("deprecated")' Deprecated in
version 4.0.0. Use |
... |
Additional arguments forwarded to |
What is a linear mixed model?
A linear mixed model (LMM) extends ordinary regression / ANOVA by
allowing two kinds of effects:
Fixed effects - factors you actively manipulated or whose specific levels you care about (treatment, dose, time, genotype). Reported as estimates with confidence intervals.
Random effects - grouping structure that creates non-independence in your data but whose levels are a random sample from a larger population (subjects measured repeatedly, plots within fields, observers, batches). Reported as variance components.
Use an LMM whenever observations share something that makes them more
alike than two random observations from the dataset. Ignoring such
grouping (running a plain aov or lm) is
pseudoreplication, i.e. treating non-independent observations
as if they were independent: standard errors shrink, p-values shrink,
false positives explode.
Vocabulary.
Before going further, a few terms used throughout the report:
Subject - the experimental unit that is measured
repeatedly (a person, animal, pot and plot, cell line); in
lme4 syntax it is the grouping factor on the right of
the |, e.g. (1 | subject).
Within-subject factor - a predictor whose levels vary within the same subject (time in a longitudinal study, treatment in a cross-over study).
Between-subject factor - a predictor whose levels vary across subjects but are constant within a subject (sex, genotype, treatment arm in a parallel-groups trial). Both within- and between-subject factors are fixed effects.
BLUP - Best Linear Unbiased Predictor. The model's estimate of the random-effect value for each subject (e.g. how much a particular subject deviates from the population intercept). BLUPs are checked for normality just like residuals.
ICC - intraclass correlation coefficient. The share of total variance attributable to between-group differences. ICC = 0 means the grouping factor is irrelevant; ICC = 1 means observations within a group are identical.
REML - restricted maximum likelihood. The default fitting method for variance components; gives less biased estimates than ordinary maximum likelihood.
Satterthwaite / Kenward-Roger - methods to approximate the denominator degrees of freedom for fixed-effect p-values, since there is no exact df in an LMM.
Reading the (1 | group) syntax.
Every random-effects term has the form ( <varying> | <group> ).
The bar reads as "varies by". The grouping factor on the right is what
creates the non-independence. The left side is what is allowed to differ
between groups. Common patterns:
(1 | subject) - random intercept per subject (each
subject has its own baseline). Repeated measures, longitudinal data.
(1 | field) - randomised block design or multi-site
trial; one intercept per block.
(1 | field/plot) - plot nested in field;
equivalent to (1|field) + (1|field:plot). Split-plot or
hierarchical sampling.
(1 + time | subject) - random intercept and random slope
of time per subject. Subjects differ both in baseline and
in how fast they change. Growth curves.
(1 | subject) + (1 | observer) - crossed random
effects: every observer can rate every subject. Inter-rater designs.
Rule of thumb: if you can answer "if I duplicated this
experiment, would I draw new levels of this factor?" with yes,
it belongs on the right of a |. If you would re-use the exact
same levels (e.g. control vs treated) it is a fixed effect.
When to use a linear mixed model.
The most common reason is a repeated-measures design, in
which the same experimental units are measured on more than one
occasion or under more than one treatment. Compared with a
between-groups design analysed by plain ANOVA this gives two real
advantages: fewer experimental units are needed (each subject acts
as its own control, removing between-subject variation from the
comparison) and individual differences cannot bias the treatment
groups (in a cross-over design every subject receives every
treatment). Two canonical examples:
Longitudinal study - same subjects measured at
several time points: y ~ time + (1 | subject). If
subjects also differ in how fast they change, add a random
slope: y ~ time + (1 + time | subject).
Cross-over design - every subject receives every
treatment in sequence: y ~ treatment + (1 | subject). If
carry-over between periods is a concern, add period as
a fixed effect.
LMMs also apply to non-repeated structures that still create non-independence: randomised block designs, split-plot trials, multi-site studies, inter-rater designs.
Assumptions of a linear mixed model:
Linearity in the parameters of the fixed-effects part.
Independence of observations conditional on the random effects. If structure remains (e.g. temporal autocorrelation), more random effects or a correlation structure are needed.
Normality of level-1 residuals (Q-Q plot of residuals(m)).
Normality of the random-effect BLUPs
(Q-Q plot of ranef(m)). This is the assumption most
users forget.
Homoscedasticity: residual variance roughly constant across fitted values and across grouping levels.
At least ~5 levels of each grouping factor; with
3-4 levels it is usually better to treat the factor as
fixed.
After the diagnostics the report adds a Recommendations for Heteroscedasticity and/or non-normal residuals section, but only when a flagged violation survives a follow-up check rather than on a raw significant p-value alone. A significant Levene's test triggers it only when the Scale-Location panel shows a corroborating variance trend across the fitted scale (a by-group Levene test on its own over-fires); a significant Shapiro-Wilk test on the level-1 residuals triggers it only when the rejection reflects genuine skew rather than a few outliers or heavy but symmetric tails (judged by the tail-trimmed Q-Q correlation and the residual skewness); a significant Shapiro-Wilk test on the random-effect BLUPs triggers it directly. The section gives concrete next steps (generalised mixed model, transformation).
Convergence and singular fits.
f_lmer surfaces lme4 convergence warnings and the
"boundary (singular) fit" message prominently in the output. A singular
fit usually means the random-effects structure is too complex for the
data (often a random slope with too few levels) - simplify the model
before interpreting results.
This function requires Pandoc (>= 1.12.3) for pdf, word
and rmd output. See f_aov for installation notes.
An object of class f_lmer: a named list containing the
fitted lmerModLmerTest model, the ANOVA-style fixed-effects
table (fixed_effects; the displayed table reports NumDF, DenDF,
F and p only, while the full lmerTest table including the
non-additive Sum Sq / Mean Sq columns is kept in
fixed_effects_full), the variance components and ICC (plus a
per-grouping-factor ICC breakdown in icc_by_group when the
model has two or more intercept grouping factors), the R^2
values, the
observed descriptives table (raw-data n, mean, sd, se, min, Q1,
median, Q3, max grouped by the categorical fixed-effect predictors),
post hoc results (if any; per main-effect terms plus, for a
significant categorical interaction, a cell-means entry keyed by the
interaction term name), diagnostic plots, and convergence
diagnostics. When more than one response variable is supplied on the
left-hand side, these elements are nested one level deep under each
response name, e.g. out$y1$fixed_effects,
out$y2$fixed_effects. When output_type = "rmd" the
markdown string is stored in $rmd.
When several response variables are analysed in a single call
(e.g. y1 + y2 + y3 ~ treatment + (1 | subject)), each linear
mixed model is an independent null-hypothesis test at level
alpha. The post hoc adjustments (adjust = "sidak",
"tukey", etc.) only control the family-wise error rate
within one model (across pairwise contrasts for that
response). They do not protect against the inflation of
Type I error across the set of responses.
Practical implication: With k independent response
variables all tested at \alpha = 0.05, the probability of
obtaining at least one false positive is
1 - (1 - 0.05)^k, which reaches ~40% for k = 10.
When this matters: The risk is highest in exploratory studies where many responses are screened simultaneously without a clear a priori hypothesis for each one. It is less of a concern when each response is a pre-specified primary outcome with its own biological rationale.
Possible remedies:
Bonferroni correction across responses: use
alpha = 0.05 / k where k is the number of response
variables. Conservative but simple.
False Discovery Rate (FDR): apply
p.adjust(p_values, method = "fdr") to the vector of
per-response fixed-effect p-values after the fact.
Multivariate model: if the responses are correlated
and you want a single omnibus test, fit a joint multivariate
mixed model (e.g. MCMCglmm, brms) before
interpreting individual responses.
Pre-registration: declare primary vs. exploratory responses before data collection to justify differential correction thresholds.
Sander H. van Delden plantmind@proton.me
# sleepstudy: reaction time vs days of sleep deprivation,
# repeated measures within Subject (ships with lme4).
data(sleepstudy, package = "lme4")
# 1) Random intercept per subject - the simplest mixed model.
# Each subject has its own baseline reaction time; the fixed
# effect of Days is the average slope across subjects.
# With output_type = "default" (the default), the result auto-
# prints if not assigned, so no print() call is needed.
f_lmer_out <- f_lmer(Reaction ~ Days + (1 | Subject),
data = sleepstudy)
# Re-print the stored result and show the diagnostic plots.
print(f_lmer_out)
plot(f_lmer_out)
# 2) Random intercept AND random slope of Days per subject,
# fitted with Kenward-Roger denominator df, saved to MS Word.
f_lmer(Reaction ~ Days + (1 + Days | Subject),
data = sleepstudy,
ddf = "Kenward-Roger",
output_type = "word"
)
# 3) A factor fixed effect triggers a post hoc test.
# Bin Days into three sleep-deprivation phases so that the
# fixed effect is categorical and emmeans pairwise comparisons
# with a compact letter display are produced automatically.
sleepstudy$Phase <- cut(sleepstudy$Days,
breaks = c(-Inf, 2, 6, Inf),
labels = c("early", "mid", "late"))
f_lmer(Reaction ~ Phase + (1 | Subject),
data = sleepstudy,
adjust = "tukey")
# 4) A minimal report: suppress the intro text and the diagnostic
# plots, and save it directly to MS Word. Useful when embedding
# many models in one document or when you only need the tables.
f_lmer(Reaction ~ Days + (1 | Subject),
data = sleepstudy,
intro_text = FALSE,
diagnostic_plots = FALSE,
output_type = "word"
)
# 5) Get the raw markdown back for embedding in an R Markdown
# document. Use it inside a chunk with results = 'asis'.
f_lmer_rmd_out <- f_lmer(Reaction ~ Days + (1 | Subject),
data = sleepstudy,
output_type = "rmd")
cat(f_lmer_rmd_out$rmd)
# 6) Two response variables analysed in one call. A separate model
# is fit for each, sharing the same right-hand side. The results
# are nested under each response name.
sleepstudy$Reaction2 <- sleepstudy$Reaction + rnorm(nrow(sleepstudy), 0, 5)
multi_out <- f_lmer(Reaction + Reaction2 ~ Days + (1 | Subject),
data = sleepstudy,
intro_text = FALSE,
diagnostic_plots = FALSE)
multi_out$Reaction$fixed_effects
multi_out$Reaction2$fixed_effects
# 7) Blocks, treatment and time together: a randomized complete
# block design with repeated measures. The bundled plant_trial
# dataset has five field blocks (block), one plant per treatment
# per block measured at three time points (plant_id), four
# treatments and a time factor.
data(plant_trial)
# 'treatment' and 'time_weeks' are crossed fixed effects (we care about their
# main effects and their interaction). 'block' and 'plant' are sources
# of unwanted variation we want to account for, not estimate, so
# they are random. 'plant_id' is nested in block - written compactly
# as (1 | block/plant_id), which expands to
# (1 | block) + (1 | block:plant_id). The random plant intercept is
# what makes this a repeated-measures model: the three time points
# on one plant share that plant's level.
f_lmer(height_cm ~ treatment * time_weeks + (1 | block/plant_id),
data = plant_trial)
# The same design fitted with Kenward-Roger denominator df (the
# gold standard for small, balanced designs like this one), with
# the contrast forest plots turned on and the report saved to Word.
f_lmer(height_cm ~ treatment * time_weeks + (1 | block/plant_id),
data = plant_trial,
ddf = "Kenward-Roger",
contrast_plots = TRUE,
output_type = "word"
)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.