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_ranova.R > > # Test functionality _before_ attaching lmerTest > stopifnot(!"lmerTest" %in% .packages()) # ensure that lmerTest is NOT attached > data("sleepstudy", package="lme4") > f <- function(form, data) lmerTest::lmer(form, data=data) > form <- "Reaction ~ Days + (Days|Subject)" > fm <- f(form, data=sleepstudy) > lmerTest::ranova(fm) ANOVA-like table for random-effects: Single term deletions Model: Reaction ~ Days + (Days | Subject) npar logLik AIC LRT Df Pr(>Chisq) 6 -871.81 1755.6 Days in (Days | Subject) 4 -893.23 1794.5 42.837 2 4.99e-10 *** --- Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1 > lmerTest::rand(fm) ANOVA-like table for random-effects: Single term deletions Model: Reaction ~ Days + (Days | Subject) npar logLik AIC LRT Df Pr(>Chisq) 6 -871.81 1755.6 Days in (Days | Subject) 4 -893.23 1794.5 42.837 2 4.99e-10 *** --- Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1 > lmerTest::step(fm) Backward reduced random-effect table: Eliminated npar logLik AIC LRT Df Pr(>Chisq) 6 -871.81 1755.6 Days in (Days | Subject) 0 4 -893.23 1794.5 42.837 2 4.99e-10 Days in (Days | Subject) *** --- Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1 Backward reduced fixed-effect table: Degrees of freedom method: Satterthwaite Eliminated Sum Sq Mean Sq NumDF DenDF F value Pr(>F) Days 0 30031 30031 1 17 45.853 3.264e-06 *** --- Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1 Model found: Reaction ~ Days + (Days | Subject) > > 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 > ##################################################################### > data("sleepstudy", package="lme4") > > # Test reduction of (Days | Subject) to (1 | Subject): > fm1 <- lmer(Reaction ~ Days + (Days|Subject), sleepstudy) > (an <- rand(fm1)) # 2 df test ANOVA-like table for random-effects: Single term deletions Model: Reaction ~ Days + (Days | Subject) npar logLik AIC LRT Df Pr(>Chisq) 6 -871.81 1755.6 Days in (Days | Subject) 4 -893.23 1794.5 42.837 2 4.99e-10 *** --- Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1 > (an <- ranova(fm1)) # 2 df test ANOVA-like table for random-effects: Single term deletions Model: Reaction ~ Days + (Days | Subject) npar logLik AIC LRT Df Pr(>Chisq) 6 -871.81 1755.6 Days in (Days | Subject) 4 -893.23 1794.5 42.837 2 4.99e-10 *** --- Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1 > step(fm1) Backward reduced random-effect table: Eliminated npar logLik AIC LRT Df Pr(>Chisq) 6 -871.81 1755.6 Days in (Days | Subject) 0 4 -893.23 1794.5 42.837 2 4.99e-10 Days in (Days | Subject) *** --- Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1 Backward reduced fixed-effect table: Degrees of freedom method: Satterthwaite Eliminated Sum Sq Mean Sq NumDF DenDF F value Pr(>F) Days 0 30031 30031 1 17 45.853 3.264e-06 *** --- Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1 Model found: Reaction ~ Days + (Days | Subject) > stopifnot( + nrow(an) == 2L, + an[2L, "Df"] == 2L + ) > > # This test can also be achieved with anova(): > fm2 <- lmer(Reaction ~ Days + (1|Subject), sleepstudy) > (stp <- step(fm2)) Backward reduced random-effect table: Eliminated npar logLik AIC LRT Df Pr(>Chisq) 4 -893.23 1794.5 (1 | Subject) 0 3 -946.83 1899.7 107.2 1 < 2.2e-16 *** --- Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1 Backward reduced fixed-effect table: Degrees of freedom method: Satterthwaite Eliminated Sum Sq Mean Sq NumDF DenDF F value Pr(>F) Days 0 162703 162703 1 161 169.4 < 2.2e-16 *** --- Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1 Model found: Reaction ~ Days + (1 | Subject) > get_model(stp) Linear mixed model fit by REML ['lmerModLmerTest'] Formula: Reaction ~ Days + (1 | Subject) Data: sleepstudy REML criterion at convergence: 1786.465 Random effects: Groups Name Std.Dev. Subject (Intercept) 37.12 Residual 30.99 Number of obs: 180, groups: Subject, 18 Fixed Effects: (Intercept) Days 251.41 10.47 > (ana <- anova(fm1, fm2, refit=FALSE)) Data: sleepstudy Models: fm2: Reaction ~ Days + (1 | Subject) fm1: Reaction ~ Days + (Days | Subject) npar AIC BIC logLik -2*log(L) Chisq Df Pr(>Chisq) fm2 4 1794.5 1807.2 -893.23 1786.5 fm1 6 1755.6 1774.8 -871.81 1743.6 42.837 2 4.99e-10 *** --- Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1 > > stopifnot( + all.equal(an[2L, "LRT"], ana[2L, "Chisq"], tolerance=TOL) + ) > > # Illustrate complete.test argument: > # Test removal of (Days | Subject): > (an <- ranova(fm1, reduce.terms = FALSE)) # 3 df test ANOVA-like table for random-effects: Single term deletions Model: Reaction ~ Days + (Days | Subject) npar logLik AIC LRT Df Pr(>Chisq) 6 -871.81 1755.6 (Days | Subject) 3 -946.83 1899.7 150.03 3 < 2.2e-16 *** --- Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1 > > # The likelihood ratio test statistic is in this case: > fm3 <- lm(Reaction ~ Days, sleepstudy) > LRT <- 2*c(logLik(fm1, REML=TRUE) - logLik(fm3, REML=TRUE)) # LRT > stopifnot( + nrow(an) == 2L, + an[2L, "Df"] == 3L, + all.equal(an[2L, "LRT"], LRT, tolerance=TOL) + ) > > ## _NULL_ model: > fm <- lmer(Reaction ~ -1 + (1|Subject), sleepstudy) > step(fm) Backward reduced random-effect table: Eliminated npar logLik AIC LRT Df Pr(>Chisq) 2 -992.84 1989.7 (1 | Subject) 0 1 -1284.32 2570.7 582.97 1 < 2.2e-16 *** --- Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1 Backward reduced fixed-effect table: Degrees of freedom method: Satterthwaite Eliminated Sum Sq Mean Sq NumDF DenDF F value Pr(>F) Model found: Reaction ~ -1 + (1 | Subject) > ranova(fm) ANOVA-like table for random-effects: Single term deletions Model: Reaction ~ -1 + (1 | Subject) npar logLik AIC LRT Df Pr(>Chisq) 2 -992.84 1989.7 (1 | Subject) 1 -1284.32 2570.7 582.97 1 < 2.2e-16 *** --- Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1 > lm1 <- lm(Reaction ~ 0, data=sleepstudy) > LRT <- 2*c(logLik(fm, REML=FALSE) - logLik(lm1, REML=FALSE)) > > ## Tests of ML-fits agree with anova(): > fm1 <- lmer(Reaction ~ Days + (1|Subject), sleepstudy, REML=FALSE) > step(fm1) Backward reduced random-effect table: Eliminated npar logLik AIC LRT Df Pr(>Chisq) 4 -897.04 1802.1 (1 | Subject) 0 3 -950.15 1906.3 106.21 1 < 2.2e-16 *** --- Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1 Backward reduced fixed-effect table: Degrees of freedom method: Satterthwaite Eliminated Sum Sq Mean Sq NumDF DenDF F value Pr(>F) Days 0 162703 162703 1 162 170.45 < 2.2e-16 *** --- Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1 Model found: Reaction ~ Days + (1 | Subject) > lm2 <- lm(Reaction ~ Days, sleepstudy) > (an1 <- ranova(fm1)) ANOVA-like table for random-effects: Single term deletions Model: Reaction ~ Days + (1 | Subject) npar logLik AIC LRT Df Pr(>Chisq) 4 -897.04 1802.1 (1 | Subject) 3 -950.15 1906.3 106.21 1 < 2.2e-16 *** --- Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1 > (an2 <- anova(fm1, lm2)) Data: sleepstudy Models: lm2: Reaction ~ Days fm1: Reaction ~ Days + (1 | Subject) npar AIC BIC logLik -2*log(L) Chisq Df Pr(>Chisq) lm2 3 1906.3 1915.9 -950.15 1900.3 fm1 4 1802.1 1814.8 -897.04 1794.1 106.21 1 < 2.2e-16 *** --- Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1 > j <- grep("Chi Df|Df", colnames(an2)) > stopifnot( + all.equal(an1[2, "LRT"], an2[2, "Chisq"], tolerance=TOL), + all.equal(an1[2, "Df"], an2[2, j[length(j)]], tolerance=TOL), + all.equal(an1[1:2, "logLik"], an2[2:1, "logLik"], tolerance=TOL) + ) > ## Note that lme4 version <1.1-22 use "Chi Df" while >=1.1-22 use "Df" > > # Expect warnings when old (version < 3.0-0) arguments are used: > assertWarning(step(fm, reduce.fixed = FALSE, reduce.random = FALSE, + type=3, fixed.calc = FALSE, lsmeans.calc = FALSE, + difflsmeans.calc = TRUE, test.effs = 42, keep.e="save")) > assertWarning(step(fm, reduce.fixed = FALSE, reduce.random = FALSE, + lsmeans=3)) > > > check_nrow <- function(obj, expect_nrow) { + stopifnot( + is.numeric(expect_nrow), + nrow(obj) == expect_nrow + ) + } > > # Statistical nonsense, but it works: > fm1 <- lmer(Reaction ~ Days + (1 | Subject) + (0 + Days|Subject), sleepstudy) > step(fm1) Backward reduced random-effect table: Eliminated npar logLik AIC LRT Df 5 -871.83 1753.7 (1 | Subject) 0 4 -883.26 1774.5 22.856 1 Days in (0 + Days | Subject) 0 4 -893.23 1794.5 42.796 1 Pr(>Chisq) (1 | Subject) 1.746e-06 *** Days in (0 + Days | Subject) 6.076e-11 *** --- Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1 Backward reduced fixed-effect table: Degrees of freedom method: Satterthwaite Eliminated Sum Sq Mean Sq NumDF DenDF F value Pr(>F) Days 0 29442 29442 1 18.156 45.046 2.594e-06 *** --- Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1 Model found: Reaction ~ Days + (1 | Subject) + (0 + Days | Subject) > (an <- ranova(fm1)) ANOVA-like table for random-effects: Single term deletions Model: Reaction ~ Days + (1 | Subject) + (0 + Days | Subject) npar logLik AIC LRT Df Pr(>Chisq) 5 -871.83 1753.7 (1 | Subject) 4 -883.26 1774.5 22.856 1 1.746e-06 *** Days in (0 + Days | Subject) 4 -893.23 1794.5 42.796 1 6.076e-11 *** --- Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1 > check_nrow(an, 3) > ranova(fm1, reduce.terms = FALSE) ANOVA-like table for random-effects: Single term deletions Model: Reaction ~ Days + (1 | Subject) + (0 + Days | Subject) npar logLik AIC LRT Df Pr(>Chisq) 5 -871.83 1753.7 (1 | Subject) 4 -883.26 1774.5 22.856 1 1.746e-06 *** (0 + Days | Subject) 4 -893.23 1794.5 42.796 1 6.076e-11 *** --- Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1 > > # Statistical nonsense, but it works: > fm1 <- lmer(Reaction ~ Days + (0 + Days|Subject), sleepstudy) > step(fm1) Backward reduced random-effect table: Eliminated npar logLik AIC LRT Df Pr(>Chisq) 4 -883.26 1774.5 Days in (0 + Days | Subject) 0 4 -893.23 1794.5 19.94 0 Backward reduced fixed-effect table: Degrees of freedom method: Satterthwaite Eliminated Sum Sq Mean Sq NumDF DenDF F value Pr(>F) Days 0 26395 26395 1 21.679 31.347 1.32e-05 *** --- Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1 Model found: Reaction ~ Days + (0 + Days | Subject) > (an <- ranova(fm1)) # no test of non-nested models ANOVA-like table for random-effects: Single term deletions Model: Reaction ~ Days + (0 + Days | Subject) npar logLik AIC LRT Df Pr(>Chisq) 4 -883.26 1774.5 Days in (0 + Days | Subject) 4 -893.23 1794.5 19.94 0 > stopifnot( + nrow(an) == 2L, + an[2L, "Df"] == 0, + all(is.na(an[2L, "Pr(>Chisq)"])) + ) > fm0 <- lmer(Reaction ~ Days + (1|Subject), sleepstudy) > step(fm0) Backward reduced random-effect table: Eliminated npar logLik AIC LRT Df Pr(>Chisq) 4 -893.23 1794.5 (1 | Subject) 0 3 -946.83 1899.7 107.2 1 < 2.2e-16 *** --- Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1 Backward reduced fixed-effect table: Degrees of freedom method: Satterthwaite Eliminated Sum Sq Mean Sq NumDF DenDF F value Pr(>F) Days 0 162703 162703 1 161 169.4 < 2.2e-16 *** --- Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1 Model found: Reaction ~ Days + (1 | Subject) > (an2 <- anova(fm1, fm0, refit=FALSE)) Data: sleepstudy Models: fm1: Reaction ~ Days + (0 + Days | Subject) fm0: Reaction ~ Days + (1 | Subject) npar AIC BIC logLik -2*log(L) Chisq Df Pr(>Chisq) fm1 4 1774.5 1787.3 -883.26 1766.5 fm0 4 1794.5 1807.2 -893.23 1786.5 0 0 > stopifnot( + (packageVersion("lme4")<="1.1.23" && an2[2L, "Pr(>Chisq)"] == 1) || + is.na(an2[2L, "Pr(>Chisq)"]) + ) > ranova(fm1, reduce.terms = FALSE) ANOVA-like table for random-effects: Single term deletions Model: Reaction ~ Days + (0 + Days | Subject) npar logLik AIC LRT Df Pr(>Chisq) 4 -883.26 1774.5 (0 + Days | Subject) 3 -946.83 1899.7 127.14 1 < 2.2e-16 *** --- Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1 > > fm1 <- lmer(Reaction ~ Days + (-1 + Days|Subject), sleepstudy) > step(fm1) Backward reduced random-effect table: Eliminated npar logLik AIC LRT Df 4 -883.26 1774.5 Days in (-1 + Days | Subject) 0 4 -893.23 1794.5 19.94 0 Pr(>Chisq) Days in (-1 + Days | Subject) Backward reduced fixed-effect table: Degrees of freedom method: Satterthwaite Eliminated Sum Sq Mean Sq NumDF DenDF F value Pr(>F) Days 0 26395 26395 1 21.679 31.347 1.32e-05 *** --- Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1 Model found: Reaction ~ Days + (-1 + Days | Subject) > (an3 <- ranova(fm1)) # no test of non-nested models ANOVA-like table for random-effects: Single term deletions Model: Reaction ~ Days + (-1 + Days | Subject) npar logLik AIC LRT Df Pr(>Chisq) 4 -883.26 1774.5 Days in (-1 + Days | Subject) 4 -893.23 1794.5 19.94 0 > stopifnot( + all.equal(an, an3, check.attributes=FALSE, tolerance=TOL) + ) > > # Example where comparison of non-nested models is generated > fm <- lmer(Reaction ~ poly(Days, 2) + (0 + poly(Days, 2) | Subject), sleepstudy) boundary (singular) fit: see help('isSingular') > step(fm) Backward reduced random-effect table: Eliminated npar logLik AIC 7 -937.05 1888.1 poly(Days, 2) in (0 + poly(Days, 2) | Subject) 1 5 -884.67 1779.3 (1 | Subject) 0 4 -938.16 1884.3 LRT Df Pr(>Chisq) poly(Days, 2) in (0 + poly(Days, 2) | Subject) -104.77 2 1 (1 | Subject) 106.98 1 <2e-16 *** --- Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1 Backward reduced fixed-effect table: Degrees of freedom method: Satterthwaite Eliminated Sum Sq Mean Sq NumDF DenDF F value Pr(>F) poly(Days, 2) 0 163782 81891 2 160 85.329 < 2.2e-16 *** --- Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1 Model found: Reaction ~ poly(Days, 2) + (1 | Subject) > an <- ranova(fm) > stopifnot( + nrow(an) == 2L, + an[2, "Pr(>Chisq)"] == 1 + ) > ranova(fm, reduce.terms = FALSE) # test of nested models ANOVA-like table for random-effects: Single term deletions Model: Reaction ~ poly(Days, 2) + (0 + poly(Days, 2) | Subject) npar logLik AIC LRT Df Pr(>Chisq) 7 -937.05 1888.1 (0 + poly(Days, 2) | Subject) 4 -938.16 1884.3 2.2153 3 0.5289 > > # These models are nested, though: > fm <- lmer(Reaction ~ poly(Days, 2) + (1 + poly(Days, 2) | Subject), sleepstudy) > step(fm) Backward reduced random-effect table: Eliminated npar logLik AIC 10 -856.40 1732.8 poly(Days, 2) in (1 + poly(Days, 2) | Subject) 0 5 -884.67 1779.3 LRT Df Pr(>Chisq) poly(Days, 2) in (1 + poly(Days, 2) | Subject) 56.524 5 6.338e-11 *** --- Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1 Backward reduced fixed-effect table: Degrees of freedom method: Satterthwaite Eliminated Sum Sq Mean Sq NumDF DenDF F value Pr(>F) poly(Days, 2) 0 24216 12108 2 17 23.367 1.323e-05 *** --- Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1 Model found: Reaction ~ poly(Days, 2) + (1 + poly(Days, 2) | Subject) > ranova(fm) ANOVA-like table for random-effects: Single term deletions Model: Reaction ~ poly(Days, 2) + (1 + poly(Days, 2) | Subject) npar logLik AIC LRT Df 10 -856.40 1732.8 poly(Days, 2) in (1 + poly(Days, 2) | Subject) 5 -884.67 1779.3 56.524 5 Pr(>Chisq) poly(Days, 2) in (1 + poly(Days, 2) | Subject) 6.338e-11 *** --- Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1 > fm0 <- lmer(Reaction ~ poly(Days, 2) + (1 | Subject), sleepstudy) > step(fm0) Backward reduced random-effect table: Eliminated npar logLik AIC LRT Df Pr(>Chisq) 5 -884.67 1779.3 (1 | Subject) 0 4 -938.16 1884.3 106.98 1 < 2.2e-16 *** --- Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1 Backward reduced fixed-effect table: Degrees of freedom method: Satterthwaite Eliminated Sum Sq Mean Sq NumDF DenDF F value Pr(>F) poly(Days, 2) 0 163782 81891 2 160 85.329 < 2.2e-16 *** --- Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1 Model found: Reaction ~ poly(Days, 2) + (1 | Subject) > anova(fm0, fm, refit=FALSE) Data: sleepstudy Models: fm0: Reaction ~ poly(Days, 2) + (1 | Subject) fm: Reaction ~ poly(Days, 2) + (1 + poly(Days, 2) | Subject) npar AIC BIC logLik -2*log(L) Chisq Df Pr(>Chisq) fm0 5 1779.3 1795.3 -884.67 1769.3 fm 10 1732.8 1764.7 -856.40 1712.8 56.524 5 6.338e-11 *** --- Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1 > ranova(fm, reduce.terms = FALSE) ANOVA-like table for random-effects: Single term deletions Model: Reaction ~ poly(Days, 2) + (1 + poly(Days, 2) | Subject) npar logLik AIC LRT Df Pr(>Chisq) 10 -856.40 1732.8 (1 + poly(Days, 2) | Subject) 4 -938.16 1884.3 163.51 6 < 2.2e-16 *** --- Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1 > > # A model with ||-notation: > fm1 <- lmer(Reaction ~ Days + (Days||Subject), sleepstudy) > step(fm1) Backward reduced random-effect table: Eliminated npar logLik AIC LRT Df 5 -871.83 1753.7 (1 | Subject) 0 4 -883.26 1774.5 22.856 1 Days in (0 + Days | Subject) 0 4 -893.23 1794.5 42.796 1 Pr(>Chisq) (1 | Subject) 1.746e-06 *** Days in (0 + Days | Subject) 6.076e-11 *** --- Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1 Backward reduced fixed-effect table: Degrees of freedom method: Satterthwaite Eliminated Sum Sq Mean Sq NumDF DenDF F value Pr(>F) Days 0 29442 29442 1 18.156 45.046 2.594e-06 *** --- Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1 Model found: Reaction ~ Days + ((1 | Subject) + (0 + Days | Subject)) > ranova(fm1) ANOVA-like table for random-effects: Single term deletions Model: Reaction ~ Days + (1 | Subject) + (0 + Days | Subject) npar logLik AIC LRT Df Pr(>Chisq) 5 -871.83 1753.7 (1 | Subject) 4 -883.26 1774.5 22.856 1 1.746e-06 *** Days in (0 + Days | Subject) 4 -893.23 1794.5 42.796 1 6.076e-11 *** --- Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1 > > # What about models with nested factors? > fm <- lmer(Coloursaturation ~ TVset*Picture + (1|Assessor:TVset) + (1|Assessor), + data=TVbo) > step(fm) Backward reduced random-effect table: Eliminated npar logLik AIC LRT Df Pr(>Chisq) 15 -280.22 590.44 (1 | Assessor) 1 14 -280.27 588.53 0.090 1 0.7643 (1 | Assessor:TVset) 0 13 -312.24 650.47 63.941 1 1.282e-15 *** --- Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1 Backward reduced fixed-effect table: Degrees of freedom method: Satterthwaite Eliminated Sum Sq Mean Sq NumDF DenDF F value Pr(>F) TVset:Picture 0 13.8 2.2999 6 159 2.6761 0.01679 * --- Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1 Model found: Coloursaturation ~ TVset + Picture + (1 | Assessor:TVset) + TVset:Picture > (an1 <- ranova(fm)) ANOVA-like table for random-effects: Single term deletions Model: Coloursaturation ~ TVset * Picture + (1 | Assessor:TVset) + (1 | Assessor) npar logLik AIC LRT Df Pr(>Chisq) 15 -280.22 590.44 (1 | Assessor:TVset) 14 -302.61 633.22 44.777 1 2.208e-11 *** (1 | Assessor) 14 -280.27 588.53 0.090 1 0.7643 --- Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1 > > fm <- lmer(Coloursaturation ~ TVset * Picture + + (1|Assessor/TVset), data=TVbo) > step(fm) Backward reduced random-effect table: Eliminated npar logLik AIC LRT Df Pr(>Chisq) 15 -280.22 590.44 (1 | Assessor) 1 14 -280.27 588.53 0.090 1 0.7643 (1 | TVset:Assessor) 0 13 -312.24 650.47 63.941 1 1.282e-15 *** --- Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1 Backward reduced fixed-effect table: Degrees of freedom method: Satterthwaite Eliminated Sum Sq Mean Sq NumDF DenDF F value Pr(>F) TVset:Picture 0 13.8 2.2999 6 159 2.6761 0.01679 * --- Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1 Model found: Coloursaturation ~ TVset + Picture + (1 | TVset:Assessor) + TVset:Picture > (an2 <- ranova(fm)) ANOVA-like table for random-effects: Single term deletions Model: Coloursaturation ~ TVset * Picture + (1 | TVset:Assessor) + (1 | Assessor) npar logLik AIC LRT Df Pr(>Chisq) 15 -280.22 590.44 (1 | TVset:Assessor) 14 -302.61 633.22 44.777 1 2.208e-11 *** (1 | Assessor) 14 -280.27 588.53 0.090 1 0.7643 --- Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1 > stopifnot( + all.equal(an1, an2, check.attributes=FALSE, tolerance=TOL) + ) > > ##################################################################### > # Test evaluation within functions, i.e. in other environments etc. > attach(sleepstudy) > fm <- lmer(Reaction ~ Days + (Days|Subject)) > step(fm) Backward reduced random-effect table: Eliminated npar logLik AIC LRT Df Pr(>Chisq) 6 -871.81 1755.6 Days in (Days | Subject) 0 4 -893.23 1794.5 42.837 2 4.99e-10 Days in (Days | Subject) *** --- Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1 Backward reduced fixed-effect table: Degrees of freedom method: Satterthwaite Eliminated Sum Sq Mean Sq NumDF DenDF F value Pr(>F) Days 0 30031 30031 1 17 45.853 3.264e-06 *** --- Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1 Model found: Reaction ~ Days + (Days | Subject) > ranova(fm) # OK ANOVA-like table for random-effects: Single term deletions Model: Reaction ~ Days + (Days | Subject) npar logLik AIC LRT Df Pr(>Chisq) 6 -871.81 1755.6 Days in (Days | Subject) 4 -893.23 1794.5 42.837 2 4.99e-10 *** --- Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1 > detach(sleepstudy) > > # Evaluating in a function works: > f <- function(form, data) lmer(form, data=data) > form <- "Informed.liking ~ Product+Information+ + (1|Consumer) + (1|Product:Consumer) + (1|Information:Consumer)" > fm <- f(form, data=ham) > ranova(fm) boundary (singular) fit: see help('isSingular') ANOVA-like table for random-effects: Single term deletions Model: Informed.liking ~ Product + Information + (1 | Consumer) + (1 | Product:Consumer) + (1 | Information:Consumer) npar logLik AIC LRT Df Pr(>Chisq) 9 -1353.9 2725.8 (1 | Consumer) 8 -1355.1 2726.1 2.295 1 0.1298 (1 | Product:Consumer) 8 -1436.3 2888.6 164.753 1 <2e-16 *** (1 | Information:Consumer) 8 -1354.5 2725.1 1.255 1 0.2626 --- Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1 > step_res <- step(fm) boundary (singular) fit: see help('isSingular') > stopifnot( + all(c("Sum Sq", "Mean Sq", "NumDF", "DenDF", "F value", "Pr(>F)") %in% + colnames(step_res$fixed)) + ) > > ########################### > # Evaluation in function without the formula: > # Reported by Uwe Ligges 2025-01-16. > f <- function(data) { + lmer(Petal.Length ~ Sepal.Length + (1|Species), data=data) + } > > res <- f(iris) > ranova(res) # used to fail - now it works ANOVA-like table for random-effects: Single term deletions Model: Petal.Length ~ Sepal.Length + (1 | Species) npar logLik AIC LRT Df Pr(>Chisq) 4 -34.66 77.32 (1 | Species) 3 -193.84 393.68 318.36 1 < 2.2e-16 *** --- Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1 > > data <- iris > ranova(res) # now it works ANOVA-like table for random-effects: Single term deletions Model: Petal.Length ~ Sepal.Length + (1 | Species) npar logLik AIC LRT Df Pr(>Chisq) 4 -34.66 77.32 (1 | Species) 3 -193.84 393.68 318.36 1 < 2.2e-16 *** --- Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1 > ########################### > # Evaluation in function without the formula(2): > # A model with ||-notation: > f2 <- function(data) { + lmer(Reaction ~ Days + (Days||Subject), data) + } > res <- f2(sleepstudy) > ranova(res) ANOVA-like table for random-effects: Single term deletions Model: Reaction ~ Days + (1 | Subject) + (0 + Days | Subject) npar logLik AIC LRT Df Pr(>Chisq) 5 -871.83 1753.7 (1 | Subject) 4 -883.26 1774.5 22.856 1 1.746e-06 *** Days in (0 + Days | Subject) 4 -893.23 1794.5 42.796 1 6.076e-11 *** --- Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1 > > # A model with multiple RE terms: > f3 <- function(data) { + lmer(Coloursaturation ~ TVset*Picture + (1|Assessor:TVset) + (1|Assessor), data) + } > res <- f3(TVbo) > ranova(res) ANOVA-like table for random-effects: Single term deletions Model: Coloursaturation ~ TVset * Picture + (1 | Assessor:TVset) + (1 | Assessor) npar logLik AIC LRT Df Pr(>Chisq) 15 -280.22 590.44 (1 | Assessor:TVset) 14 -302.61 633.22 44.777 1 2.208e-11 *** (1 | Assessor) 14 -280.27 588.53 0.090 1 0.7643 --- Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1 > ########################### > > # Check that step works when form is a character vector > m <- lmer(form, data=ham) > step_res <- step(m) boundary (singular) fit: see help('isSingular') > (drop1_table <- attr(step_res, "drop1")) Single term deletions using Satterthwaite's method: Model: Informed.liking ~ Product + Information + (1 | Consumer) + (1 | Product:Consumer) Sum Sq Mean Sq NumDF DenDF F value Pr(>F) Product 19.3466 6.4489 3 240 3.8291 0.01048 * Information 6.5201 6.5201 1 323 3.8714 0.04997 * --- Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1 > stopifnot( + all(c("Sum Sq", "Mean Sq", "NumDF", "DenDF", "F value", "Pr(>F)") %in% + colnames(drop1_table)) + ) > # In version < 3.0-1.9002 attr(step_res, "drop1") picked up lme4::drop1.merMod > # and returned an AIC table after the model had been update'd. > > ##################################################################### > # Model with 2 ranef covarites: > > # Model of the form (x1 + x2 | gr): > model <- lmer(Preference ~ sens2 + Homesize + (sens1 + sens2 | Consumer) + , data=carrots) boundary (singular) fit: see help('isSingular') Warning message: Model failed to converge with 1 negative eigenvalue: -3.1e+02 > step(model) boundary (singular) fit: see help('isSingular') Backward reduced random-effect table: Eliminated npar logLik AIC LRT Df 10 -1874.4 3768.8 sens1 in (sens1 + sens2 | Consumer) 1 7 -1874.5 3762.9 0.1333 3 sens2 in (sens2 | Consumer) 0 5 -1878.0 3765.9 6.9944 2 Pr(>Chisq) sens1 in (sens1 + sens2 | Consumer) 0.98757 sens2 in (sens2 | Consumer) 0.03028 * --- Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1 Backward reduced fixed-effect table: Degrees of freedom method: Satterthwaite Eliminated Sum Sq Mean Sq NumDF DenDF F value Pr(>F) sens2 0 58.685 58.685 1 102.02 54.8236 3.888e-11 *** Homesize 0 5.979 5.979 1 100.95 5.5852 0.02003 * --- Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1 Model found: Preference ~ sens2 + Homesize + (sens2 | Consumer) > stopifnot( + nrow(ranova(model)) == 3L, + nrow(ranova(model, reduce.terms = FALSE)) == 2L + ) boundary (singular) fit: see help('isSingular') > > # Model of the form (f1 + f2 | gr): > model <- lmer(Preference ~ sens2 + Homesize + Gender + + (Gender+Homesize|Consumer), data=carrots) boundary (singular) fit: see help('isSingular') > step(model) Backward reduced random-effect table: Eliminated npar logLik AIC LRT 11 -1872.3 3766.5 Gender in (Gender + Homesize | Consumer) 1 8 -1875.1 3766.3 5.7408 Homesize in (Homesize | Consumer) 0 6 -1878.4 3768.7 6.4705 Df Pr(>Chisq) Gender in (Gender + Homesize | Consumer) 3 0.12493 Homesize in (Homesize | Consumer) 2 0.03935 * --- Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1 Backward reduced fixed-effect table: Degrees of freedom method: Satterthwaite Eliminated Sum Sq Mean Sq NumDF DenDF F value Pr(>F) Gender 1 0.632 0.632 1 91.80 0.5686 0.45275 sens2 0 83.385 83.385 1 1129.09 75.0490 < 2e-16 *** Homesize 0 6.212 6.212 1 97.82 5.5910 0.02003 * --- Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1 Model found: Preference ~ sens2 + Homesize + (Homesize | Consumer) Warning messages: 1: In checkConv(attr(opt, "derivs"), opt$par, ctrl = control$checkConv, : unable to evaluate scaled gradient 2: In checkConv(attr(opt, "derivs"), opt$par, ctrl = control$checkConv, : Model failed to converge: degenerate Hessian with 1 negative eigenvalues See ?lme4::convergence and ?lme4::troubleshooting. 3: In checkConv(attr(opt, "derivs"), opt$par, ctrl = control$checkConv, : unable to evaluate scaled gradient 4: In checkConv(attr(opt, "derivs"), opt$par, ctrl = control$checkConv, : Model failed to converge: degenerate Hessian with 1 negative eigenvalues See ?lme4::convergence and ?lme4::troubleshooting. 5: Model failed to converge with 1 negative eigenvalue: -2.7e-04 > stopifnot( + nrow(ranova(model)) == 3L, + nrow(ranova(model, reduce.terms = FALSE)) == 2L + ) Warning messages: 1: In checkConv(attr(opt, "derivs"), opt$par, ctrl = control$checkConv, : unable to evaluate scaled gradient 2: In checkConv(attr(opt, "derivs"), opt$par, ctrl = control$checkConv, : Model failed to converge: degenerate Hessian with 1 negative eigenvalues See ?lme4::convergence and ?lme4::troubleshooting. > > # Model of the form (-1 + f2 | gr): > model <- lmer(Preference ~ sens2 + Homesize + Gender + + (Gender -1 |Consumer), data=carrots) Warning message: In checkConv(attr(opt, "derivs"), opt$par, ctrl = control$checkConv, : Model is nearly unidentifiable: large eigenvalue ratio - Rescale variables? > step(model) Backward reduced random-effect table: Eliminated npar logLik AIC LRT Df 8 -1876.4 3768.9 Gender in (Gender - 1 | Consumer) 1 6 -1878.4 3768.7 3.863 2 (1 | Consumer) 0 5 -1917.2 3844.5 77.714 1 Pr(>Chisq) Gender in (Gender - 1 | Consumer) 0.145 (1 | Consumer) <2e-16 *** --- Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1 Backward reduced fixed-effect table: Degrees of freedom method: Satterthwaite Eliminated Sum Sq Mean Sq NumDF DenDF F value Pr(>F) Gender 1 1.879 1.879 1 99.98 1.6909 0.19647 sens2 0 83.363 83.363 1 1129.00 75.0256 < 2e-16 *** Homesize 0 5.729 5.729 1 100.97 5.1557 0.02529 * --- Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1 Model found: Preference ~ sens2 + Homesize + (1 | Consumer) > an1 <- ranova(model) > an1b <- ranova(model, reduce.terms = FALSE) > > model <- lmer(Preference ~ sens2 + Homesize + Gender + + (0 + Gender|Consumer), data=carrots) Warning message: In checkConv(attr(opt, "derivs"), opt$par, ctrl = control$checkConv, : Model is nearly unidentifiable: large eigenvalue ratio - Rescale variables? > step(model) Backward reduced random-effect table: Eliminated npar logLik AIC LRT Df 8 -1876.4 3768.9 Gender in (0 + Gender | Consumer) 1 6 -1878.4 3768.7 3.863 2 (1 | Consumer) 0 5 -1917.2 3844.5 77.714 1 Pr(>Chisq) Gender in (0 + Gender | Consumer) 0.145 (1 | Consumer) <2e-16 *** --- Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1 Backward reduced fixed-effect table: Degrees of freedom method: Satterthwaite Eliminated Sum Sq Mean Sq NumDF DenDF F value Pr(>F) Gender 1 1.879 1.879 1 99.98 1.6909 0.19647 sens2 0 83.363 83.363 1 1129.00 75.0256 < 2e-16 *** Homesize 0 5.729 5.729 1 100.97 5.1557 0.02529 * --- Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1 Model found: Preference ~ sens2 + Homesize + (1 | Consumer) > an2 <- ranova(model) > an2b <- ranova(model, reduce.terms = FALSE) > > stopifnot( + all.equal(an1, an2, check.attributes=FALSE, tolerance=TOL), + all.equal(an1b, an2b, check.attributes=FALSE, tolerance=TOL) + ) > > ####### Polynomial terms: > model <- lmer(Preference ~ sens2 + Gender + (poly(sens2, 2) | Consumer), + data=carrots) > (an <- ranova(model)) ANOVA-like table for random-effects: Single term deletions Model: Preference ~ sens2 + Gender + (poly(sens2, 2) | Consumer) npar logLik AIC LRT Df 10 -1874.1 3768.2 poly(sens2, 2) in (poly(sens2, 2) | Consumer) 5 -1879.9 3769.7 11.552 5 Pr(>Chisq) poly(sens2, 2) in (poly(sens2, 2) | Consumer) 0.04147 * --- Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1 > step(model) Backward reduced random-effect table: Eliminated npar logLik AIC 10 -1874.1 3768.2 poly(sens2, 2) in (poly(sens2, 2) | Consumer) 0 5 -1879.9 3769.7 LRT Df Pr(>Chisq) poly(sens2, 2) in (poly(sens2, 2) | Consumer) 11.552 5 0.04147 * --- Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1 Backward reduced fixed-effect table: Degrees of freedom method: Satterthwaite Eliminated Sum Sq Mean Sq NumDF DenDF F value Pr(>F) Gender 1 1.188 1.188 1 101.03 1.1262 0.2911 sens2 0 53.447 53.447 1 114.59 50.6761 1.019e-10 *** --- Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1 Model found: Preference ~ sens2 + (poly(sens2, 2) | Consumer) Warning message: Model failed to converge with 1 negative eigenvalue: -2.7e-02 > > model <- lmer(Preference ~ sens2 + Gender + (sens2 + I(sens2^2) | Consumer), + data=carrots) Warning message: In checkConv(attr(opt, "derivs"), opt$par, ctrl = control$checkConv, : Model failed to converge with max|grad| = 0.099278 (tol = 0.002, component 1) See ?lme4::convergence and ?lme4::troubleshooting. > (an2 <- ranova(model)) boundary (singular) fit: see help('isSingular') ANOVA-like table for random-effects: Single term deletions Model: Preference ~ sens2 + Gender + (sens2 + I(sens2^2) | Consumer) npar logLik AIC LRT Df 10 -1874.1 3768.2 sens2 in (sens2 + I(sens2^2) | Consumer) 7 -1879.8 3773.5 11.3637 3 I(sens2^2) in (sens2 + I(sens2^2) | Consumer) 7 -1876.5 3767.0 4.8755 3 Pr(>Chisq) sens2 in (sens2 + I(sens2^2) | Consumer) 0.009913 ** I(sens2^2) in (sens2 + I(sens2^2) | Consumer) 0.181146 --- Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1 > step(model) boundary (singular) fit: see help('isSingular') Backward reduced random-effect table: Eliminated npar logLik AIC 10 -1874.1 3768.2 I(sens2^2) in (sens2 + I(sens2^2) | Consumer) 1 7 -1876.5 3767.0 sens2 in (sens2 | Consumer) 0 5 -1879.9 3769.7 LRT Df Pr(>Chisq) I(sens2^2) in (sens2 + I(sens2^2) | Consumer) 4.8755 3 0.18115 sens2 in (sens2 | Consumer) 6.6735 2 0.03555 * --- Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1 Backward reduced fixed-effect table: Degrees of freedom method: Satterthwaite Eliminated Sum Sq Mean Sq NumDF DenDF F value Pr(>F) Gender 1 1.211 1.211 1 101.02 1.1309 0.2901 sens2 0 58.688 58.688 1 102.01 54.8276 3.883e-11 *** --- Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1 Model found: Preference ~ sens2 + (sens2 | Consumer) > stopifnot( + nrow(an) == 2L, + an[2L, "Df"] == 5L, + nrow(an2) == 3L, + all(an2[2:3, "Df"] == 3L) + ) > > ######## Functions of terms in random effects: > model <- lmer(Preference ~ sens2 + Gender + (log(10+sens2) | Consumer), + data=carrots) > ranova(model) # Works ANOVA-like table for random-effects: Single term deletions Model: Preference ~ sens2 + Gender + (log(10 + sens2) | Consumer) npar logLik AIC LRT Df 7 -1875.3 3764.6 log(10 + sens2) in (log(10 + sens2) | Consumer) 5 -1879.9 3769.7 9.0854 2 Pr(>Chisq) log(10 + sens2) in (log(10 + sens2) | Consumer) 0.01064 * --- Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1 > step(model) Backward reduced random-effect table: Eliminated npar logLik AIC 7 -1875.3 3764.6 log(10 + sens2) in (log(10 + sens2) | Consumer) 0 5 -1879.9 3769.7 LRT Df Pr(>Chisq) log(10 + sens2) in (log(10 + sens2) | Consumer) 9.0854 2 0.01064 * --- Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1 Backward reduced fixed-effect table: Degrees of freedom method: Satterthwaite Eliminated Sum Sq Mean Sq NumDF DenDF F value Pr(>F) Gender 1 1.200 1.200 1 101.03 1.1296 0.2904 sens2 0 54.185 54.185 1 109.20 51.0020 1.083e-10 *** --- Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1 Model found: Preference ~ sens2 + (log(10 + sens2) | Consumer) > > ##################################################################### > > # Missing values changes the number of observations in use: > m <- lmer(Preference ~ sens2 + Homesize + + (1 |Consumer:Income), data=carrots) > assertError(step(m)) > ans <- try(ranova(m), silent = TRUE) > stopifnot( + inherits(ans, "try-error"), + grepl("number of rows in use has changed", ans) + ) > > ## Removing missing values solves the problem: > m2 <- lmer(Preference ~ sens2 + Homesize + + (1 |Consumer:Income), data=carrots[complete.cases(carrots), ]) > ranova(m2) # Works ANOVA-like table for random-effects: Single term deletions Model: Preference ~ sens2 + Homesize + (1 | Consumer:Income) npar logLik AIC LRT Df Pr(>Chisq) 5 -1752.0 3513.9 (1 | Consumer:Income) 4 -1778.6 3565.2 53.272 1 2.904e-13 *** --- Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1 > step(m2) Backward reduced random-effect table: Eliminated npar logLik AIC LRT Df Pr(>Chisq) 5 -1752.0 3513.9 (1 | Consumer:Income) 0 4 -1778.6 3565.2 53.272 1 2.904e-13 *** --- Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1 Backward reduced fixed-effect table: Degrees of freedom method: Satterthwaite Eliminated Sum Sq Mean Sq NumDF DenDF F value Pr(>F) sens2 0 77.308 77.308 1 1058.49 69.7915 < 2.2e-16 *** Homesize 0 8.569 8.569 1 95.23 7.7355 0.006527 ** --- Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1 Model found: Preference ~ sens2 + Homesize + (1 | Consumer:Income) > > ## Including the variable with missing values (Income) among the fixed effects > ## also solves the problem: > m <- lmer(Preference ~ sens2 + Homesize + Income + #(1 + sens2 | Consumer) + + (1 |Consumer:Income), data=carrots) > ranova(m) ANOVA-like table for random-effects: Single term deletions Model: Preference ~ sens2 + Homesize + Income + (1 | Consumer:Income) npar logLik AIC LRT Df Pr(>Chisq) 8 -1766.0 3547.9 (1 | Consumer:Income) 7 -1791.1 3596.3 50.395 1 1.257e-12 *** --- Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1 > step(m) Backward reduced random-effect table: Eliminated npar logLik AIC LRT Df Pr(>Chisq) 8 -1766.0 3547.9 (1 | Consumer:Income) 0 7 -1791.1 3596.3 50.395 1 1.257e-12 *** --- Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1 Backward reduced fixed-effect table: Degrees of freedom method: Satterthwaite Eliminated Sum Sq Mean Sq NumDF DenDF F value Pr(>F) Income 1 3.567 1.189 3 91.92 1.0627 0.368918 sens2 0 78.191 78.191 1 1062.96 69.8919 < 2.2e-16 *** Homesize 0 7.983 7.983 1 94.93 7.1355 0.008894 ** --- Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1 Model found: Preference ~ sens2 + Homesize + (1 | Consumer:Income) > > # Missing values in a an insignificant fixed effect causes the an error in step: > m0 <- lmer(Preference ~ sens2 + Homesize + Income + #(1 + sens2 | Consumer) + + (1 |Consumer), data=carrots) > ranova(m0) ANOVA-like table for random-effects: Single term deletions Model: Preference ~ sens2 + Homesize + Income + (1 | Consumer) npar logLik AIC LRT Df Pr(>Chisq) 8 -1766.0 3547.9 (1 | Consumer) 7 -1791.1 3596.3 50.395 1 1.257e-12 *** --- Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1 > ans <- try(step(m0), silent = TRUE) > stopifnot( + inherits(ans, "try-error"), + grepl("number of rows in use has changed", ans) + ) > > # Check that step still works for linear models (etc.) > flm <- lm(Coloursaturation ~ TVset * Picture, data=TVbo) > res <- step(flm, trace=0) > stopifnot( + inherits(res, "lm") + ) > > ##################### Using reduce and keep args: > # Fit a model to the ham dataset: > m <- lmer(Informed.liking ~ Product*Information+ + (1|Consumer) + (1|Product:Consumer) + + (1|Information:Consumer), data=ham) > > # Backward elimination using terms with default alpha-levels: > (step_res <- step(m)) boundary (singular) fit: see help('isSingular') Backward reduced random-effect table: Eliminated npar logLik AIC LRT Df Pr(>Chisq) 12 -1352.0 2728.1 (1 | Information:Consumer) 1 11 -1352.8 2727.5 1.444 1 0.22952 (1 | Consumer) 0 10 -1354.4 2728.7 3.184 1 0.07437 (1 | Product:Consumer) 0 10 -1435.0 2890.0 164.443 1 < 2e-16 (1 | Information:Consumer) (1 | Consumer) . (1 | Product:Consumer) *** --- Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1 Backward reduced fixed-effect table: Degrees of freedom method: Satterthwaite Eliminated Sum Sq Mean Sq NumDF DenDF F value Pr(>F) Product:Information 1 10.3873 3.4624 3 320 2.0765 0.10321 Product 0 19.3466 6.4489 3 240 3.8291 0.01048 * Information 0 6.5201 6.5201 1 323 3.8714 0.04997 * --- Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1 Model found: Informed.liking ~ Product + Information + (1 | Consumer) + (1 | Product:Consumer) > > (step_res <- step(m, reduce.random = FALSE)) boundary (singular) fit: see help('isSingular') Backward reduced random-effect table: Eliminated npar logLik AIC LRT Df Pr(>Chisq) 12 -1352.0 2728.1 (1 | Consumer) 0 11 -1353.2 2728.3 2.252 1 0.1335 (1 | Product:Consumer) 0 11 -1435.0 2892.0 165.887 1 <2e-16 (1 | Information:Consumer) 0 11 -1352.8 2727.5 1.444 1 0.2295 (1 | Consumer) (1 | Product:Consumer) *** (1 | Information:Consumer) --- Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1 Backward reduced fixed-effect table: Degrees of freedom method: Satterthwaite Eliminated Sum Sq Mean Sq NumDF DenDF F value Pr(>F) Product:Information 1 10.3873 3.4624 3 240 2.2014 0.08855 . Information 2 5.3335 5.3335 1 80 3.3414 0.07129 . Product 0 18.3361 6.1120 3 240 3.8291 0.01048 * --- Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1 Model found: Informed.liking ~ Product + (1 | Consumer) + (1 | Product:Consumer) + (1 | Information:Consumer) > (step_res <- step(m, reduce.fixed = FALSE)) boundary (singular) fit: see help('isSingular') Backward reduced random-effect table: Eliminated npar logLik AIC LRT Df Pr(>Chisq) 12 -1352.0 2728.1 (1 | Information:Consumer) 1 11 -1352.8 2727.5 1.444 1 0.22952 (1 | Consumer) 0 10 -1354.4 2728.7 3.184 1 0.07437 (1 | Product:Consumer) 0 10 -1435.0 2890.0 164.443 1 < 2e-16 (1 | Information:Consumer) (1 | Consumer) . (1 | Product:Consumer) *** --- Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1 Backward reduced fixed-effect table: Degrees of freedom method: Satterthwaite Eliminated Sum Sq Mean Sq NumDF DenDF F value Pr(>F) Product:Information 0 10.387 3.4624 3 320 2.0765 0.1032 Model found: Informed.liking ~ Product + Information + (1 | Consumer) + (1 | Product:Consumer) + Product:Information > (step_res <- step(m, reduce.fixed = FALSE, reduce.random = FALSE)) boundary (singular) fit: see help('isSingular') Backward reduced random-effect table: Eliminated npar logLik AIC LRT Df Pr(>Chisq) 12 -1352.0 2728.1 (1 | Consumer) 0 11 -1353.2 2728.3 2.252 1 0.1335 (1 | Product:Consumer) 0 11 -1435.0 2892.0 165.887 1 <2e-16 (1 | Information:Consumer) 0 11 -1352.8 2727.5 1.444 1 0.2295 (1 | Consumer) (1 | Product:Consumer) *** (1 | Information:Consumer) --- Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1 Backward reduced fixed-effect table: Degrees of freedom method: Satterthwaite Eliminated Sum Sq Mean Sq NumDF DenDF F value Pr(>F) Product:Information 0 10.387 3.4624 3 240 2.2014 0.08855 . --- Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1 Model found: Informed.liking ~ Product * Information + (1 | Consumer) + (1 | Product:Consumer) + (1 | Information:Consumer) > > (step_res <- step(m, reduce.random = FALSE, keep="Information")) boundary (singular) fit: see help('isSingular') Backward reduced random-effect table: Eliminated npar logLik AIC LRT Df Pr(>Chisq) 12 -1352.0 2728.1 (1 | Consumer) 0 11 -1353.2 2728.3 2.252 1 0.1335 (1 | Product:Consumer) 0 11 -1435.0 2892.0 165.887 1 <2e-16 (1 | Information:Consumer) 0 11 -1352.8 2727.5 1.444 1 0.2295 (1 | Consumer) (1 | Product:Consumer) *** (1 | Information:Consumer) --- Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1 Backward reduced fixed-effect table: Degrees of freedom method: Satterthwaite Eliminated Sum Sq Mean Sq NumDF DenDF F value Pr(>F) Product:Information 1 10.3873 3.4624 3 240 2.2014 0.08855 . Product 0 18.3363 6.1121 3 240 3.8292 0.01048 * Information 0 5.3335 5.3335 1 80 3.3414 0.07129 . --- Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1 Model found: Informed.liking ~ Product + Information + (1 | Consumer) + (1 | Product:Consumer) + (1 | Information:Consumer) > (step_res <- step(m, reduce.random = FALSE, keep="Product:Information")) boundary (singular) fit: see help('isSingular') Backward reduced random-effect table: Eliminated npar logLik AIC LRT Df Pr(>Chisq) 12 -1352.0 2728.1 (1 | Consumer) 0 11 -1353.2 2728.3 2.252 1 0.1335 (1 | Product:Consumer) 0 11 -1435.0 2892.0 165.887 1 <2e-16 (1 | Information:Consumer) 0 11 -1352.8 2727.5 1.444 1 0.2295 (1 | Consumer) (1 | Product:Consumer) *** (1 | Information:Consumer) --- Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1 Backward reduced fixed-effect table: Degrees of freedom method: Satterthwaite Eliminated Sum Sq Mean Sq NumDF DenDF F value Pr(>F) Product:Information 0 10.387 3.4624 3 240 2.2014 0.08855 . --- Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1 Model found: Informed.liking ~ Product * Information + (1 | Consumer) + (1 | Product:Consumer) + (1 | Information:Consumer) > > > ########################### > ## Test that `step` works even if all random terms are reduced away: > set.seed(101) > test <- data.frame(TM = factor(rep(rep(c("org","min"),each=3),3)), + dep = runif(18,0,20), + ind = runif(18,0,7), + dorp = factor(rep(1:3,each=6))) > full.model <- lmer(dep ~ TM + ind + (1 | dorp), data=test) > res <- step(full.model) > # res$random > # res$fixed > # attr(res, "model") > # attr(res, "drop1") > > > > proc.time() user system elapsed 13.12 0.40 13.51