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_summary.R > > # 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() > > # 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" > > 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 > > data("sleepstudy", package="lme4") > data("cake", package="lme4") > > # Fit basic model and compute summary: > fm <- lmer(Reaction ~ Days + (1|Subject) + (0+Days|Subject), sleepstudy) > (sfm <- summary(fm)) Linear mixed model fit by REML. t-tests use Satterthwaite's method [ lmerModLmerTest] Formula: Reaction ~ Days + (1 | Subject) + (0 + Days | Subject) Data: sleepstudy REML criterion at convergence: 1743.7 Scaled residuals: Min 1Q Median 3Q Max -3.9626 -0.4625 0.0204 0.4653 5.1860 Random effects: Groups Name Variance Std.Dev. Subject (Intercept) 627.57 25.051 Subject.1 Days 35.86 5.988 Residual 653.58 25.565 Number of obs: 180, groups: Subject, 18 Fixed effects: Estimate Std. Error df t value Pr(>|t|) (Intercept) 251.405 6.885 18.156 36.513 < 2e-16 *** Days 10.467 1.560 18.156 6.712 2.59e-06 *** --- Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1 Correlation of Fixed Effects: (Intr) Days -0.184 > > ## Test class: > stopifnot(all( + class(sfm) == c("summary.lmerModLmerTest", "summary.merMod"), + all(c("df", "Pr(>|t|)") %in% colnames(coef(sfm))) + )) > stopifnot(class(summary(fm, ddf="lme4")) == "summary.merMod") > > ## Test coefficient table names: > mat <- coef(summary(fm)) > stopifnot(all( # colnames + colnames(mat) == c("Estimate", "Std. Error", "df", "t value", "Pr(>|t|)") + )) > stopifnot(all( # rownames + names(fixef(fm)) == rownames(mat) + )) > > ## Test pass of 'correlation' argument to lme4:::summary.merMod: > x <- capture.output(summary(fm)) > x_nocor <- capture.output(summary(fm, correlation=FALSE)) > txt <- "Correlation of Fixed Effects:" > stopifnot( + any(grep(txt, x)), + !any(grepl(txt, x_nocor)) + ) > > # Test warning with unrecognized arguments (caught by lme4:::summary.merMod): > assertWarning(summary(fm, false_arg=FALSE)) > > ## Test pass of extra arguments to lme4:::print.summary.merMod: > x <- capture.output(print(summary(fm), signif.stars=TRUE)) > x_nocor <- capture.output(print(summary(fm), signif.stars=FALSE)) > txt <- "Signif. codes:" > stopifnot( + any(grep(txt, x)), + !any(grepl(txt, x_nocor)) + ) > > ####### ddf argument: > (an1 <- summary(fm)) # Also testing print method. Linear mixed model fit by REML. t-tests use Satterthwaite's method [ lmerModLmerTest] Formula: Reaction ~ Days + (1 | Subject) + (0 + Days | Subject) Data: sleepstudy REML criterion at convergence: 1743.7 Scaled residuals: Min 1Q Median 3Q Max -3.9626 -0.4625 0.0204 0.4653 5.1860 Random effects: Groups Name Variance Std.Dev. Subject (Intercept) 627.57 25.051 Subject.1 Days 35.86 5.988 Residual 653.58 25.565 Number of obs: 180, groups: Subject, 18 Fixed effects: Estimate Std. Error df t value Pr(>|t|) (Intercept) 251.405 6.885 18.156 36.513 < 2e-16 *** Days 10.467 1.560 18.156 6.712 2.59e-06 *** --- Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1 Correlation of Fixed Effects: (Intr) Days -0.184 > (an2 <- summary(fm, ddf="Satterthwaite")) Linear mixed model fit by REML. t-tests use Satterthwaite's method [ lmerModLmerTest] Formula: Reaction ~ Days + (1 | Subject) + (0 + Days | Subject) Data: sleepstudy REML criterion at convergence: 1743.7 Scaled residuals: Min 1Q Median 3Q Max -3.9626 -0.4625 0.0204 0.4653 5.1860 Random effects: Groups Name Variance Std.Dev. Subject (Intercept) 627.57 25.051 Subject.1 Days 35.86 5.988 Residual 653.58 25.565 Number of obs: 180, groups: Subject, 18 Fixed effects: Estimate Std. Error df t value Pr(>|t|) (Intercept) 251.405 6.885 18.156 36.513 < 2e-16 *** Days 10.467 1.560 18.156 6.712 2.59e-06 *** --- Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1 Correlation of Fixed Effects: (Intr) Days -0.184 > stopifnot(isTRUE( + all.equal(an1, an2) + )) > (an3 <- summary(fm, ddf="Sat")) ## Abbreviated argument Linear mixed model fit by REML. t-tests use Satterthwaite's method [ lmerModLmerTest] Formula: Reaction ~ Days + (1 | Subject) + (0 + Days | Subject) Data: sleepstudy REML criterion at convergence: 1743.7 Scaled residuals: Min 1Q Median 3Q Max -3.9626 -0.4625 0.0204 0.4653 5.1860 Random effects: Groups Name Variance Std.Dev. Subject (Intercept) 627.57 25.051 Subject.1 Days 35.86 5.988 Residual 653.58 25.565 Number of obs: 180, groups: Subject, 18 Fixed effects: Estimate Std. Error df t value Pr(>|t|) (Intercept) 251.405 6.885 18.156 36.513 < 2e-16 *** Days 10.467 1.560 18.156 6.712 2.59e-06 *** --- Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1 Correlation of Fixed Effects: (Intr) Days -0.184 > stopifnot(isTRUE( + all.equal(an1, an3) + )) > (summary(fm, ddf="lme4")) Linear mixed model fit by REML ['lmerMod'] Formula: Reaction ~ Days + (1 | Subject) + (0 + Days | Subject) Data: sleepstudy REML criterion at convergence: 1743.7 Scaled residuals: Min 1Q Median 3Q Max -3.9626 -0.4625 0.0204 0.4653 5.1860 Random effects: Groups Name Variance Std.Dev. Subject (Intercept) 627.57 25.051 Subject.1 Days 35.86 5.988 Residual 653.58 25.565 Number of obs: 180, groups: Subject, 18 Fixed effects: Estimate Std. Error t value (Intercept) 251.405 6.885 36.513 Days 10.467 1.560 6.712 Correlation of Fixed Effects: (Intr) Days -0.184 > if(has_pbkrtest) { + (summary(fm, ddf="Kenward-Roger")) + assertError(summary(fm, ddf="KR")) ## Error on incorrect arg. + } > > ## lme4 method: > an1 <- summary(fm, ddf="lme4") > an2 <- summary(as(fm, "lmerMod")) > stopifnot(isTRUE( + all.equal(an1, an2) + )) > > > # Test printed output > # - Satterthwaite > x <- capture.output(sfm) # equal to output of 'print(sfm)' > txt <- c("lmerModLmerTest", "t-tests use Satterthwaite's method", + "df", "t value", "Pr(>|t|)") > stopifnot(all( + sapply(txt, function(text) any(grepl(text, x))) + )) > > # Test printed output > # - KR > if(has_pbkrtest) { + (sfm <- summary(fm, ddf="Kenward-Roger")) + x <- capture.output(sfm) + txt <- c("lmerModLmerTest", "t-tests use Kenward-Roger's method", + "df", "t value", "Pr(>|t|)") + stopifnot(all( + sapply(txt, function(text) any(grepl(text, x))) + )) + } > > #################################### > ## Test 'boundary' fixef structures: > #################################### > > # Example with no fixef: > m <- lmer(Reaction ~ -1 + (Days | Subject), sleepstudy) > # m <- lmer(Reaction ~ 0 + (Days | Subject), sleepstudy) # alternative > stopifnot(length(fixef(m)) == 0L) > stopifnot( + nrow(coef(summary(m))) == 0L, + nrow(coef(summary(m, ddf="lme4"))) == 0L + ) > if(has_pbkrtest){ + stopifnot(nrow(coef(summary(m, ddf="Kenward-Roger"))) == 0L) + } > > # Example with intercept only: > m <- lmer(Reaction ~ (Days | Subject), sleepstudy) > # m <- lmer(Reaction ~ 1 + (Days | Subject), sleepstudy) # alternative > stopifnot(length(fixef(m)) == 1L, + names(fixef(m)) == "(Intercept)") > stopifnot( + nrow(coef(summary(m))) == 1L, + nrow(coef(summary(m, ddf="lme4"))) == 1L + ) > if(has_pbkrtest){ + stopifnot(nrow(coef(summary(m, ddf="Kenward-Roger"))) == 1L) + } > > # Example with >1 fixef without intercept: > m <- lmer(Reaction ~ Days - 1 + I(Days^2) + (Days | Subject), sleepstudy) > stopifnot(length(fixef(m)) == 2L, + names(fixef(m)) == c("Days", "I(Days^2)")) > stopifnot( + nrow(coef(summary(m))) == 2L, + nrow(coef(summary(m, ddf="lme4"))) == 2L + ) > if(has_pbkrtest){ + stopifnot(nrow(coef(summary(m, ddf="Kenward-Roger"))) == 2L) + } > > > proc.time() user system elapsed 3.53 0.21 3.73