R Under development (unstable) (2026-01-12 r89300 ucrt) -- "Unsuffered Consequences" Copyright (C) 2026 The R Foundation for Statistical Computing Platform: x86_64-w64-mingw32/x64 R is free software and comes with ABSOLUTELY NO WARRANTY. You are welcome to redistribute it under certain conditions. Type 'license()' or 'licence()' for distribution details. R is a collaborative project with many contributors. Type 'contributors()' for more information and 'citation()' on how to cite R or R packages in publications. Type 'demo()' for some demos, 'help()' for on-line help, or 'help.start()' for an HTML browser interface to help. Type 'q()' to quit R. > # test_drop1.R > > library(lmerTest) Loading required package: lme4 Loading required package: Matrix Attaching package: 'lmerTest' The following object is masked from 'package:lme4': lmer The following object is masked from 'package:stats': step > > # WRE says "using if(requireNamespace("pkgname")) is preferred, if possible." > # even in tests: > assertError <- function(expr, ...) + if(requireNamespace("tools")) tools::assertError(expr, ...) else invisible() > assertWarning <- function(expr, ...) + if(requireNamespace("tools")) tools::assertWarning(expr, ...) else invisible() > > TOL <- 1e-4 > # Kenward-Roger only available with pbkrtest and only then validated in R >= 3.3.3 > # (faulty results for R < 3.3.3 may be due to unstated dependencies in pbkrtest) > has_pbkrtest <- requireNamespace("pbkrtest", quietly = TRUE) && getRversion() >= "3.3.3" > > data("sleepstudy", package="lme4") > > ######### Basic usage > > data("cake", package="lme4") > cake2 <- cake > cake2$temperature <- factor(cake2$temperature, ordered = FALSE) > fm <- lmer(angle ~ recipe + temperature + (1|recipe:replicate), cake2) > (an1 <- drop1(fm)) Single term deletions using Satterthwaite's method: Model: angle ~ recipe + temperature + (1 | recipe:replicate) Sum Sq Mean Sq NumDF DenDF F value Pr(>F) recipe 10.19 5.09 2 42 0.2488 0.7809 temperature 2100.30 420.06 5 220 20.5141 <2e-16 *** --- Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1 > (an2 <- drop1(fm, force_get_contrasts = TRUE)) Single term deletions using Satterthwaite's method: Model: angle ~ recipe + temperature + (1 | recipe:replicate) Sum Sq Mean Sq NumDF DenDF F value Pr(>F) recipe 10.19 5.09 2 42 0.2488 0.7809 temperature 2100.30 420.06 5 220 20.5141 <2e-16 *** --- Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1 > drop1(fm, ddf="lme4", test="Chi") Single term deletions Model: angle ~ recipe + temperature + (1 | recipe:replicate) npar AIC LRT Pr(Chi) 1709.6 recipe 2 1706.1 0.530 0.7672 temperature 5 1785.7 86.106 <2e-16 *** --- Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1 > if(has_pbkrtest) + drop1(fm, ddf="Kenward-Roger") Single term deletions using Kenward-Roger's method: Model: angle ~ recipe + temperature + (1 | recipe:replicate) Sum Sq Mean Sq NumDF DenDF F value Pr(>F) recipe 10.19 5.09 2 42 0.2488 0.7809 temperature 2100.30 420.06 5 220 20.5141 <2e-16 *** --- Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1 > > tests1 <- show_tests(an1) > tests2 <- show_tests(an2) > > stopifnot( + # Tests are the same: + isTRUE(all.equal(an1, an2, check.attributes = FALSE, tolerance=TOL)), + # But contrast matrices are not: + all(!mapply(function(x, y) isTRUE(all.equal(x, y)), tests1, tests2)) + ) > > fm <- lmer(angle ~ recipe * temperature + (1|recipe:replicate), cake2) > drop1(fm) Single term deletions using Satterthwaite's method: Model: angle ~ recipe * temperature + (1 | recipe:replicate) Sum Sq Mean Sq NumDF DenDF F value Pr(>F) recipe:temperature 205.98 20.598 10 210 1.0062 0.4393 > drop1(fm, ddf="lme4") Single term deletions Model: angle ~ recipe * temperature + (1 | recipe:replicate) npar AIC 1719.0 recipe:temperature 10 1709.6 > if(has_pbkrtest) + drop1(fm, ddf="Kenward-Roger") Single term deletions using Kenward-Roger's method: Model: angle ~ recipe * temperature + (1 | recipe:replicate) Sum Sq Mean Sq NumDF DenDF F value Pr(>F) recipe:temperature 205.98 20.598 10 210 1.0062 0.4393 > > # Incorrect arguments: > assertError(drop1(fm, scope="recipe")) # Correct Error > assertError(drop1(fm, scope=3)) # Correct Error > assertError(drop1(fm, scope=list("recipe"))) # Correct Error > > # Polynomial terms: > > fm <- lmer(Reaction ~ 0 + (Days|Subject), sleepstudy) > (an0 <- drop1(fm)) # No fixef! Single term deletions using Satterthwaite's method: Model: Reaction ~ 0 + (Days | Subject) Sum Sq Mean Sq NumDF DenDF F value Pr(>F) > fm <- lmer(Reaction ~ Days + (Days|Subject), sleepstudy) > (an1 <- drop1(fm)) Single term deletions using Satterthwaite's method: Model: Reaction ~ Days + (Days | Subject) Sum Sq Mean Sq NumDF DenDF F value Pr(>F) Days 30031 30031 1 17 45.853 3.264e-06 *** --- Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1 > fm <- lmer(Reaction ~ Days + I(Days^2) + (Days|Subject), sleepstudy) > (an2 <- (drop1(fm))) Single term deletions using Satterthwaite's method: Model: Reaction ~ Days + I(Days^2) + (Days | Subject) Sum Sq Mean Sq NumDF DenDF F value Pr(>F) Days 4534.5 4534.5 1 114.43 6.9551 0.00952 ** I(Days^2) 1079.5 1079.5 1 143.00 1.6558 0.20026 --- Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1 > fm <- lmer(Reaction ~ poly(Days, 2) + (Days|Subject), sleepstudy) > (an3 <- drop1(fm)) Single term deletions using Satterthwaite's method: Model: Reaction ~ poly(Days, 2) + (Days | Subject) Sum Sq Mean Sq NumDF DenDF F value Pr(>F) poly(Days, 2) 30974 15487 2 29.115 23.754 7.625e-07 *** --- Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1 > stopifnot( + nrow(an0) == 0L, + nrow(an1) == 1L, + nrow(an2) == 2L, + nrow(an3) == 1L + ) > > # Consider a rank-deficient design matrix: > fm <- lmer(angle ~ recipe + temp + temperature + (1|recipe:replicate), cake) fixed-effect model matrix is rank deficient so dropping 1 column / coefficient > # Here temp accounts for the linear effect of temperature, and > # temperature is an (ordered) factor that accounts for the remaining > # variation between temperatures (4 df). > (an4 <- drop1(fm)) Single term deletions using Satterthwaite's method: Model: angle ~ recipe + temp + temperature + (1 | recipe:replicate) Sum Sq Mean Sq NumDF DenDF F value Pr(>F) recipe 10.189 5.094 2 42 0.2488 0.7809 temp temperature 133.595 33.399 4 220 1.6311 0.1674 > # While temperature is in the model, we cannot test the effect of dropping > # temp. After removing temperature we can test the effect of dropping temp: > (an5 <- drop1(update(fm, ~.-temperature))) Single term deletions using Satterthwaite's method: Model: angle ~ recipe + temp + (1 | recipe:replicate) Sum Sq Mean Sq NumDF DenDF F value Pr(>F) recipe 10.3 5.15 2 42 0.2488 0.7809 temp 1966.7 1966.71 1 224 94.9759 <2e-16 *** --- Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1 > > stopifnot( + nrow(an4) == 3, + rownames(an4)[2] == "temp", + all(is.na(an4[2, ])), + all(!is.na(an4[-2, ])), + all(rownames(an5) == c("recipe", "temp")) + ) > > > > > proc.time() user system elapsed 3.07 0.37 3.45