Running 'test_a_utils.R' [168s]
  Running 'test_anova.R' [6s]
  Running 'test_compare_sas.R' [178s]
  Running 'test_contest1D.R' [162s]
  Running 'test_contestMD.R' [4s]
  Running 'test_contrast_utils.R' [158s]
  Running 'test_drop1.R' [4s]
  Running 'test_legacy.R' [4s]
  Running 'test_lmer.R' [5s]
  Running 'test_lmerTest_paper.R' [182s]
  Running 'test_ls_means.R' [172s]
  Running 'test_ranova_step.R' [166s]
  Running 'test_summary.R' [4s]
  Running 'test_zerovar.R' [171s]
  Running 'zlmerTest_zeroDenom.R' [3s]
Running the tests in 'tests/test_a_utils.R' failed.
Complete output:
  > # test_a_utils.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
  
  > 
  > # test safeDeparse() - equivalence and differences to deparse():
  > deparse_args <- formals(deparse)
  > safeDeparse_args <- formals(lmerTest:::safeDeparse)
  > stopifnot(
  +   all.equal(names(deparse_args), names(safeDeparse_args)),
  +   all.equal(deparse_args[!names(deparse_args) %in% c("control", "width.cutoff")],
  +             safeDeparse_args[!names(safeDeparse_args) %in% c("control", "width.cutoff")]),
  +   all.equal(deparse_args[["width.cutoff"]], 60L),
  +   all(eval(safeDeparse_args[["control"]]) %in% eval(deparse_args[["control"]])),
  +   all.equal(safeDeparse_args[["width.cutoff"]], 500L)
  + )
  > 
  > 
  > proc.time()
     user  system elapsed 
     2.23    0.29    2.51 
Running the tests in 'tests/test_compare_sas.R' failed.
Complete output:
  > # test_compare_sas.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()
  > 
  > #####################################################################
  > 
  > 
  > # Use contrasts to get particular estimates for the summary table:
  > l <- list(Frequency="contr.SAS", Income="contr.SAS")
  > m.carrots <- lmer(Preference ~ sens2*Frequency*Income
  +                   +(1+sens2|Consumer), data=carrots, contrasts=l)
  fixed-effect model matrix is rank deficient so dropping 12 columns / coefficients
  > an.m <- anova(m.carrots)
  Missing cells for: Frequency3:Income1, Frequency5:Income1, Frequency4:Income2, Frequency3:Income4, Frequency4:Income4, Frequency5:Income4, sens2:Frequency3:Income1, sens2:Frequency5:Income1, sens2:Frequency4:Income2, sens2:Frequency3:Income4, sens2:Frequency4:Income4, sens2:Frequency5:Income4.  
  Interpret type III hypotheses with care.
  > 
  > TOL <- 1e-4
  > TOL2 <- 1e-5
  > # with 4 decimals should agree with SAS output
  > # numbers before decimals should agree with SAS output
  > stopifnot(
  +   all.equal(an.m[,"Pr(>F)"],
  +             c(2e-5, 0.15512,  0.06939, 0.08223, 0.52459, 0.03119, 0.48344),
  +             tolerance = TOL),
  +   all.equal(round(an.m$DenDF), c(83, 83, 83, 83, 83, 83, 83))
  + )
  > 
  > sm <- summary(m.carrots)
  > stopifnot(
  +   isTRUE(all.equal(sm$coefficients[,"Pr(>|t|)"],
  +                    c(1e-10, 0.005061, 0.6865554, 0.342613, 0.129157,
  +                      0.088231, 0.846000, 0.354472, 0.526318, 0.020646, 0.010188,
  +                      0.031242, 0.055356, 0.694689, 0.099382, 0.28547,
  +                      0.977774, 0.855653, 0.427737, 0.321086, 0.417465 , 0.204385, 0.784437,
  +                      0.681434, 0.106180, 0.149122, 0.390870, 0.273686), tolerance=TOL,
  +                    check.attributes = FALSE))
  + )
  > 
  > # Takes too long to run:
  > # if(requireNamespace("pbkrtest", quietly = TRUE)) {
  > #   sm.kr <- summary(m.carrots, ddf = "Kenward-Roger")
  > #
  > #   ## coefficients for Sat and KR agree in this example
  > #   #   cbind(sm$coefficients[,"Pr(>|t|)"], sm.kr$coefficients[,"Pr(>|t|)"])
  > #   all.equal(sm$coefficients[,"Pr(>|t|)"], sm.kr$coefficients[,"Pr(>|t|)"],
  > #             tol=TOL)
  > # }
  > 
  > ################################################################################
  > ## checking lsmeans and difflsmeans
  > ## compare with SAS output
  > m <- lmer(Informed.liking ~ Product*Information*Gender
  +           + (1|Product:Consumer) + (1|Consumer) , data=ham)
  > 
  > 
  > lsm <- lsmeansLT(m, which = "Product")
  > # head(lsm)
  > 
  > stopifnot(
  +   isTRUE(all.equal(lsm[, "Estimate"], c(5.8084, 5.1012, 6.0909, 5.9256),
  +                    tol=TOL, check.attributes = FALSE)),
  +   isTRUE(all.equal(round(lsm[, "t value"], 2), c(24.93, 21.89, 26.14, 25.43), tolerance=TOL,
  +                    check.attributes = FALSE)),
  +   isTRUE(all.equal(lsm[, "lower"], c(5.3499, 4.6428, 5.6324, 5.4672), tolerance=TOL,
  +                    check.attributes = FALSE)),
  +   isTRUE(all.equal(lsm[, "upper"], c(6.2668, 5.5597, 6.5493, 6.3840), tolerance=TOL,
  +                    check.attributes = FALSE))
  + )
  > 
  > ################################################################################
  > # Not actually 'hard-coded' tests versus SAS results...
  > 
  > m.carrots <- lmer(Preference ~ 0 + sens2 + Homesize +
  +                     (1+sens2 | Consumer), data=carrots,
  +                   control=lmerControl(optimizer="bobyqa"))
  > summary(m.carrots)
  Linear mixed model fit by REML. t-tests use Satterthwaite's method [
  lmerModLmerTest]
  Formula: Preference ~ 0 + sens2 + Homesize + (1 + sens2 | Consumer)
     Data: carrots
  Control: lmerControl(optimizer = "bobyqa")
  
  REML criterion at convergence: 3748.9
  
  Scaled residuals: 
      Min      1Q  Median      3Q     Max 
  -3.5322 -0.5571  0.0308  0.6297  2.8552 
  
  Random effects:
   Groups   Name        Variance Std.Dev. Corr
   Consumer (Intercept) 0.195168 0.44178      
            sens2       0.002779 0.05271  0.18
   Residual             1.070441 1.03462      
  Number of obs: 1233, groups:  Consumer, 103
  
  Fixed effects:
             Estimate Std. Error        df t value Pr(>|t|)    
  sens2     7.068e-02  9.545e-03 1.020e+02   7.404 3.89e-11 ***
  Homesize1 4.910e+00  7.056e-02 1.013e+02  69.586  < 2e-16 ***
  Homesize3 4.661e+00  7.850e-02 1.013e+02  59.374  < 2e-16 ***
  ---
  Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
  
  Correlation of Fixed Effects:
            sens2 Homsz1
  Homesize1 0.061       
  Homesize3 0.055 0.003 
  > 
  > (an.1 <- anova(m.carrots, type=1))
  Type I Analysis of Variance Table with Satterthwaite's method
           Sum Sq Mean Sq NumDF  DenDF  F value    Pr(>F)    
  sens2      60.5    60.5     1 102.01   56.539 2.211e-11 ***
  Homesize 8927.2  4463.6     2 101.31 4169.863 < 2.2e-16 ***
  ---
  Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
  > (an.3 <- anova(m.carrots))
  Type III Analysis of Variance Table with Satterthwaite's method
           Sum Sq Mean Sq NumDF  DenDF  F value    Pr(>F)    
  sens2      58.7    58.7     1 102.01   54.821 3.892e-11 ***
  Homesize 8927.2  4463.6     2 101.31 4169.863 < 2.2e-16 ***
  ---
  Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
  > (an.lme4 <- anova(m.carrots, ddf = "lme4")) # difference in SSQ MS and F-values
  Analysis of Variance Table
           npar Sum Sq Mean Sq   F value
  sens2       1    0.0     0.0    0.0122
  Homesize    2 8927.2  4463.6 4169.8633
  > # Is this a problem with lme4?
  > # fm <- lm(Preference ~ 0 + sens2 + Homesize, data=carrots)
  > # anova(fm)
  > # coef(summary(fm))
  > # Here the F value is a little greater than the squared t-value (as expected)
  > 
  > stopifnot(all.equal(an.1[, "F value"], c(56.5394, 4169.87), tolerance = TOL2),
  +           all.equal(an.3[, "F value"], c(54.8206, 4169.87), tolerance = TOL2))
  > 
  > 
  > ################################################################################
  > # Check exmaple from GLM SAS report
  > 
  > ### example from the paper GLM SAS 101 report
  > a <- factor(c(1,1,1,2,2,2,2,2,1,2))
  > b <- factor(c(1,1,2,1,2,2,2,2,2,1))
  > f=factor(c(1,2,1,2,1,2,1,2,1,2))
  > y <- c(12,14,11,20,17,23,35,46,15,16)
  > dd <- data.frame(a=a, b=b, y=y, f=f)
  > 
  > ## check type 2 is order independent
  > model <- lmer(y ~ a*b + (1|f), data=dd)
  boundary (singular) fit: see help('isSingular')
  > model2 <- lmer(y ~ b*a + (1|f), data=dd)
  boundary (singular) fit: see help('isSingular')
  > (an <- anova(model, type=2))
  Type II Analysis of Variance Table with Satterthwaite's method
      Sum Sq Mean Sq NumDF DenDF F value  Pr(>F)  
  a   336.00  336.00     1     6  3.9013 0.09566 .
  b   114.33  114.33     1     6  1.3275 0.29308  
  a:b  85.75   85.75     1     6  0.9956 0.35689  
  ---
  Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
  > (an2 <- anova(model2, type=2))
  Type II Analysis of Variance Table with Satterthwaite's method
      Sum Sq Mean Sq NumDF DenDF F value  Pr(>F)  
  b   114.33  114.33     1     6  1.3275 0.29308  
  a   336.00  336.00     1     6  3.9013 0.09566 .
  b:a  85.75   85.75     1     6  0.9956 0.35689  
  ---
  Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
  > stopifnot(
  +   isTRUE(all.equal(an,an2[c(2,1,3),], check.attributes = FALSE, tolerance=TOL2))
  + )
  > 
  > ## check the results are the same as from SAS proc mixed
  > stopifnot(
  +   isTRUE(all.equal(an[,"F value"], c(3.90131, 1.32753, 0.99565), tolerance=TOL2))
  + )
  > ################################################################################
  > ## Check type II and III anova tables versus SAS
  > 
  > m.carrots <- lmer(Preference ~ sens2*Homesize
  +                   +(1+sens2|Consumer), data=carrots)
  > (ancar <- anova(m.carrots, type=2))
  Type II Analysis of Variance Table with Satterthwaite's method
                 Sum Sq Mean Sq NumDF  DenDF F value    Pr(>F)    
  sens2          58.697  58.697     1 101.02 54.8339 4.042e-11 ***
  Homesize        5.526   5.526     1 100.99  5.1621   0.02521 *  
  sens2:Homesize  1.103   1.103     1 101.02  1.0303   0.31251    
  ---
  Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
  > 
  > stopifnot(
  +   isTRUE(all.equal(ancar[,"F value"], c(54.8361, 5.16138, 1.03035), tolerance = TOL))
  + )
  > 
  > m <- lmer(Informed.liking ~ Product*Age
  +           + (1|Consumer) , data=ham)
  > (an <- anova(m, type=2))
  Type II Analysis of Variance Table with Satterthwaite's method
              Sum Sq Mean Sq NumDF DenDF F value  Pr(>F)  
  Product     32.498 10.8327     3   561  2.4814 0.06014 .
  Age          0.024  0.0235     1    79  0.0054 0.94168  
  Product:Age 19.442  6.4808     3   561  1.4845 0.21772  
  ---
  Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
  > 
  > stopifnot(
  +   isTRUE(all.equal(an[,"F value"], c(2.48135, .005387, 1.48451), tolerance = TOL2))
  + )
  > 
  > 
  > fm <- lmer(Preference ~ sens2*Homesize*sens1 + (1|Product),
  +            data=carrots)
  > (ant2 <- anova(fm, type=2))
  Type II Analysis of Variance Table with Satterthwaite's method
                        Sum Sq Mean Sq NumDF   DenDF F value    Pr(>F)    
  sens2                21.0228 21.0228     1    7.97 16.4832 0.0036583 ** 
  Homesize             17.8570 17.8570     1 1217.01 14.0010 0.0001912 ***
  sens1                 0.6709  0.6709     1    7.98  0.5260 0.4889911    
  sens2:Homesize        1.5068  1.5068     1 1216.99  1.1814 0.2772780    
  sens2:sens1           0.1372  0.1372     1    7.98  0.1076 0.7513796    
  Homesize:sens1        0.4275  0.4275     1 1217.00  0.3352 0.5627339    
  sens2:Homesize:sens1  1.3512  1.3512     1 1216.99  1.0595 0.3035426    
  ---
  Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
  > (ant3 <- anova(fm, type=3))
  Type III Analysis of Variance Table with Satterthwaite's method
                        Sum Sq Mean Sq NumDF   DenDF F value    Pr(>F)    
  sens2                21.5710 21.5710     1    8.02 16.9130 0.0033602 ** 
  Homesize             17.8570 17.8570     1 1217.01 14.0010 0.0001912 ***
  sens1                 0.6136  0.6136     1    8.03  0.4811 0.5074890    
  sens2:Homesize        1.5068  1.5068     1 1216.99  1.1814 0.2772780    
  sens2:sens1           0.0946  0.0946     1    8.02  0.0742 0.7922017    
  Homesize:sens1        0.4275  0.4275     1 1217.00  0.3352 0.5627339    
  sens2:Homesize:sens1  1.3512  1.3512     1 1216.99  1.0595 0.3035426    
  ---
  Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
  > 
  > stopifnot(
  +   isTRUE(all.equal(ant2[,"F value"],
  +                    c(16.4842, 14.0010, .526076, 1.18144,
  +                      .107570, .335177, 1.05946), tolerance = TOL)),
  +   isTRUE(all.equal(ant3[,"F value"],
  +                    c(16.9140, 14.0010,.481148, 1.18144,
  +                      .074201, .335177, 1.05946), tolerance = TOL))
  + )
  > 
  > ################################################################################
  > 
  > 
  > 
  > proc.time()
     user  system elapsed 
     3.37    0.35    3.73 
Running the tests in 'tests/test_contest1D.R' failed.
Complete output:
  > # test_contest1D.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")
  > 
  > ####################################
  > ## Tests of contest1D
  > ####################################
  > 
  > fm <- lmer(Reaction ~ Days + I(Days^2) + (1|Subject) + (0+Days|Subject),
  +            sleepstudy)
  > # Basic tests:
  > L <- c(0, 1, 0)
  > contest1D(fm, L)
    Estimate Std. Error       df  t value   Pr(>|t|)
  1 7.434085   2.824283 114.9943 2.632203 0.00964808
  > contest1D(fm, L, confint = TRUE)
    Estimate Std. Error       df  t value    lower    upper   Pr(>|t|)
  1 7.434085   2.824283 114.9943 2.632203 1.839722 13.02845 0.00964808
  > contest1D(fm, L, confint = TRUE, level=0.99)
    Estimate Std. Error       df  t value      lower    upper   Pr(>|t|)
  1 7.434085   2.824283 114.9943 2.632203 0.03655396 14.83162 0.00964808
  > if(has_pbkrtest)
  +   contest1D(fm, L, ddf="Kenward-Roger")
    Estimate Std. Error       df  t value    Pr(>|t|)
  1 7.434085   2.824283 115.5148 2.632203 0.009642692
  > 
  > # Test too long L
  > assertError(contest1D(fm, c(0, 1, 1, 1)))
  > 
  > # Test too short L
  > assertError(contest1D(fm, c(0, 1)))
  > 
  > # Test matrix L
  > contest1D(fm, matrix(L, nrow=1))
    Estimate Std. Error       df  t value   Pr(>|t|)
  1 7.434085   2.824283 114.9943 2.632203 0.00964808
  > contest1D(fm, matrix(L, ncol=1))
    Estimate Std. Error       df  t value   Pr(>|t|)
  1 7.434085   2.824283 114.9943 2.632203 0.00964808
  > assertError(contest1D(fm, matrix(c(0, 1), ncol=1)))
  > assertError(contest1D(fm, matrix(c(0, 1, 0, 0), nrow=1)))
  > L <- matrix(numeric(0L), ncol=3)
  > assertError(contest1D(fm, L)) # "empty" matrix
  > assertError(contest1D(fm, matrix(1, ncol=3, nrow=2)))
  > 
  > # Test list L
  > assertError(contest1D(fm, list(c(0, 1, 0))))
  > 
  > # Test equivalence to coef(summary(fm)):
  > Lmat <- diag(length(fixef(fm)))
  > (coef_mat <- lmerTest:::rbindall(lapply(1:ncol(Lmat), function(i)
  +   contest1D(fm, Lmat[i, ]))))
       Estimate Std. Error        df   t value     Pr(>|t|)
  1 255.4493728  7.5663677  26.22939 33.761163 3.810057e-23
  2   7.4340850  2.8242827 114.99433  2.632203 9.648080e-03
  3   0.3370223  0.2616475 144.63110  1.288078 1.997759e-01
  > (coef_mat_lme4 <- coef(summary(fm, ddf="lme4")))
                 Estimate Std. Error   t value
  (Intercept) 255.4493728  7.5663677 33.761163
  Days          7.4340850  2.8242827  2.632203
  I(Days^2)     0.3370223  0.2616475  1.288078
  > rownames(coef_mat) <- rownames(coef_mat_lme4)
  > stopifnot(isTRUE(
  +   all.equal(as.data.frame(coef_mat_lme4),
  +             coef_mat[, c("Estimate", "Std. Error", "t value")], tolerance=TOL)
  + ))
  > 
  > if(has_pbkrtest) {
  +   (coef_mat_KR <- lmerTest:::rbindall(lapply(1:ncol(Lmat), function(i)
  +     contest1D(fm, Lmat[i, ], ddf="Kenward-Roger"))))
  +   rownames(coef_mat_KR) <- rownames(coef_mat_lme4)
  +   stopifnot(isTRUE(
  +     all.equal(as.data.frame(coef_mat_lme4),
  +               coef_mat_KR[, c("Estimate", "Std. Error", "t value")], tolerance=TOL)
  +   ))
  + }
  > # Test of 0-length beta
  > fm1 <- lmer(Reaction ~ 0 + (1|Subject) + (0+Days|Subject),
  +             sleepstudy)
  > stopifnot(length(fixef(fm1)) == 0L)
  > if(has_pbkrtest) {
  +   (ans <- contest1D(fm1, numeric(0L), ddf="Kenward-Roger"))
  +   stopifnot(nrow(ans) == 0L)
  + }
  > 
  > ## Test rhs argument:
  > fm <- lmer(Reaction ~ Days + (Days|Subject), sleepstudy)
  > contest1D(fm, L=cbind(0, 1))
    Estimate Std. Error       df  t value     Pr(>|t|)
  1 10.46729    1.54579 16.99998 6.771481 3.263824e-06
  > contest1D(fm, L=cbind(0, 1), rhs=10)
    Estimate Std. Error       df  t value  Pr(>|t|)
  1 10.46729    1.54579 16.99998 0.302296 0.7660937
  > if(has_pbkrtest) {
  +   contest1D(fm, L=cbind(0, 1), ddf="Kenward-Roger")
  +   contest1D(fm, L=cbind(0, 1), ddf="Kenward-Roger", rhs=10)
  + }
    Estimate Std. Error df  t value  Pr(>|t|)
  1 10.46729    1.54579 17 0.302296 0.7660937
  > 
  > contest1D(fm, L=c(0, 1), rhs = 10.467)
    Estimate Std. Error       df      t value  Pr(>|t|)
  1 10.46729    1.54579 16.99998 0.0001849926 0.9998546
  > 
  > (ct1 <- contest1D(fm, L=cbind(c(0, 1)), rhs = 10))
    Estimate Std. Error       df  t value  Pr(>|t|)
  1 10.46729    1.54579 16.99998 0.302296 0.7660937
  > (ct2 <- contestMD(fm, L=rbind(c(0, 1)), rhs = 10))
      Sum Sq  Mean Sq NumDF    DenDF    F value    Pr(>F)
  1 59.85028 59.85028     1 16.99998 0.09138285 0.7660937
  > stopifnot(
  +   isTRUE(all.equal(ct1[, "t value"]^2, ct2[, "F value"], tolerance=1e-6))
  + )
  > 
  > ## Test 'lmerMod' method:
  > fm <- lme4::lmer(Reaction ~ Days + I(Days^2) + (1|Subject) + (0+Days|Subject),
  +                  sleepstudy)
  > # Basic tests:
  > L <- c(0, 1, 0)
  > contest1D(fm, L)
    Estimate Std. Error       df  t value   Pr(>|t|)
  1 7.434085   2.824283 114.9943 2.632203 0.00964808
  > contest1D(fm, L, confint = TRUE)
    Estimate Std. Error       df  t value    lower    upper   Pr(>|t|)
  1 7.434085   2.824283 114.9943 2.632203 1.839722 13.02845 0.00964808
  > contest1D(fm, L, confint = TRUE, level=0.99)
    Estimate Std. Error       df  t value      lower    upper   Pr(>|t|)
  1 7.434085   2.824283 114.9943 2.632203 0.03655396 14.83162 0.00964808
  > if(has_pbkrtest)
  +   contest1D(fm, L, ddf="Kenward-Roger")
    Estimate Std. Error       df  t value    Pr(>|t|)
  1 7.434085   2.824283 115.5148 2.632203 0.009642692
  > 
  > 
  > proc.time()
     user  system elapsed 
     3.42    0.25    3.68 
Running the tests in 'tests/test_contrast_utils.R' failed.
Complete output:
  > # test_contrast_utils.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
  
  > 
  > ##########
  > # Test that a message is printed if some cells have zero data:
  > # Missing a single cell:
  > data("cake", package="lme4")
  > cake4 <- cake
  > cake4$temperature <- factor(cake4$temperature, ordered=FALSE)
  > cake4 <- droplevels(subset(cake4, !(recipe == "A" & temperature == "175") ))
  > with(cake4, table(recipe, temperature))
        temperature
  recipe 175 185 195 205 215 225
       A   0  15  15  15  15  15
       B  15  15  15  15  15  15
       C  15  15  15  15  15  15
  > 
  > fm1 <- lmer(angle ~ recipe * temperature + (1|recipe:replicate), cake4)
  fixed-effect model matrix is rank deficient so dropping 1 column / coefficient
  > an <- anova(fm1)
  Missing cells for: recipeA:temperature175.  
  Interpret type III hypotheses with care.
  > txt <- capture.output(an <- anova(fm1), type = "message")
  > stopifnot(length(grep("Missing cells for:", txt)) > 0,
  +           length(grep("Interpret type III hypotheses with care.", txt)) > 0)
  > 
  > ##########
  > # Test that a message is printed if some cells have zero data:
  > # Missing diagonal:
  > cake4 <- cake
  > cake4$temperature <- factor(cake4$temperature, ordered=FALSE)
  > cake4 <- droplevels(subset(cake4, temperature %in% levels(cake4$temperature)[1:3]))
  > cake4 <- droplevels(subset(cake4, !((recipe == "A" & temperature == "175") |
  +                                       (recipe == "B" & temperature == "185") |
  +                                       (recipe == "C" & temperature == "195") )))
  > cake4$temp0 <- cake4$temp - mean(cake4$temp)
  > with(cake4, table(recipe, temperature))
        temperature
  recipe 175 185 195
       A   0  15  15
       B  15   0  15
       C  15  15   0
  > 
  > fm1 <- lmer(angle ~ recipe * temperature + (1|recipe:replicate), cake4)
  fixed-effect model matrix is rank deficient so dropping 3 columns / coefficients
  > an <- anova(fm1)
  Missing cells for: recipeA:temperature175, recipeB:temperature185, recipeC:temperature195.  
  Interpret type III hypotheses with care.
  > txt <- capture.output(an <- anova(fm1), type = "message")
  > stopifnot(length(grep("Missing cells for:", txt)) > 0,
  +           length(grep("Interpret type III hypotheses with care.", txt)) > 0)
  > 
  > ##########
  > # Test that a message is NOT printed with centered covariates:
  > fm1 <- lmer(angle ~ recipe * temp0 + (1|recipe:replicate), cake4)
  > an <- anova(fm1)
  > txt <- capture.output(an <- anova(fm1), type = "message")
  > stopifnot(length(grep("Missing cells for:", txt)) == 0,
  +           length(grep("Interpret type III hypotheses with care.", txt)) == 0)
  > # Note: in many cases a message would not be printed anyway because the
  > # columns sums in the rdX design matrix would not be exactly zero but just a
  > # small number very close to zero.
  > 
  > 
  > 
  > 
  > proc.time()
     user  system elapsed 
     2.35    0.35    2.70 
Running the tests in 'tests/test_lmerTest_paper.R' failed.
Complete output:
  > # test_lmerTest_paper.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
  
  > 
  > # 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"
  > 
  > # Read in data set
  > load(system.file("testdata","test_paper_objects.RData", package="lmerTest"))
  > 
  > # Evaluate code from paper:
  > ## Section 8.2:
  > tv <- lmer(Sharpnessofmovement ~ TVset * Picture + (1 | Assessor) +
  +              (1 | Assessor:TVset) + (1 | Assessor:Picture), data = TVbo,
  +            control=lmerControl(optimizer="bobyqa"))
  > 
  > (an8.2 <- anova(tv))
  Type III Analysis of Variance Table with Satterthwaite's method
                Sum Sq Mean Sq NumDF DenDF F value    Pr(>F)    
  TVset          1.765  0.8825     2    14  0.2437 0.7869818    
  Picture       51.857 17.2857     3    21  4.7735 0.0108785 *  
  TVset:Picture 90.767 15.1279     6   138  4.1777 0.0006845 ***
  ---
  Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
  > 
  > if(has_pbkrtest)
  +   (ankr8.2 <- anova(tv, type=2, ddf="Kenward-Roger"))
  Type II Analysis of Variance Table with Kenward-Roger's method
                Sum Sq Mean Sq NumDF DenDF F value    Pr(>F)    
  TVset          1.765  0.8825     2    14  0.2437 0.7869818    
  Picture       51.857 17.2857     3    21  4.7735 0.0108785 *  
  TVset:Picture 90.767 15.1279     6   138  4.1777 0.0006845 ***
  ---
  Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
  > 
  > ## Section 8.3:
  > m.carrots <- lmer(Preference ~ sens1 + sens2 + (1 + sens1 + sens2 | Consumer) +
  +                     (1 | Product), data=carrots,
  +                   control=lmerControl(optimizer="bobyqa"))
  boundary (singular) fit: see help('isSingular')
  > (sum8.3 <- coef(summary(m.carrots)))
                Estimate Std. Error        df    t value     Pr(>|t|)
  (Intercept) 4.79911155 0.07529236 20.721527 63.7396848 2.921549e-25
  sens1       0.01082919 0.01502847  9.167909  0.7205783 4.891337e-01
  sens2       0.07064553 0.01727806 10.944315  4.0887415 1.812128e-03
  > 
  > ## Section 8.4:
  > tv <- lmer(Sharpnessofmovement ~ TVset * Picture +
  +              (1 | Assessor:TVset) + (1 | Assessor:Picture) +
  +              (1 | Assessor:Picture:TVset) + (1 | Repeat) + (1 | Repeat:Picture) +
  +              (1 | Repeat:TVset) + (1 | Repeat:TVset:Picture) + (1 | Assessor),
  +            data = TVbo,
  +            control=lmerControl(optimizer="bobyqa"))
  boundary (singular) fit: see help('isSingular')
  > st <- step(tv)
  boundary (singular) fit: see help('isSingular')
  boundary (singular) fit: see help('isSingular')
  boundary (singular) fit: see help('isSingular')
  boundary (singular) fit: see help('isSingular')
  boundary (singular) fit: see help('isSingular')
  boundary (singular) fit: see help('isSingular')
  boundary (singular) fit: see help('isSingular')
  boundary (singular) fit: see help('isSingular')
  boundary (singular) fit: see help('isSingular')
  boundary (singular) fit: see help('isSingular')
  boundary (singular) fit: see help('isSingular')
  boundary (singular) fit: see help('isSingular')
  boundary (singular) fit: see help('isSingular')
  boundary (singular) fit: see help('isSingular')
  boundary (singular) fit: see help('isSingular')
  boundary (singular) fit: see help('isSingular')
  boundary (singular) fit: see help('isSingular')
  boundary (singular) fit: see help('isSingular')
  boundary (singular) fit: see help('isSingular')
  boundary (singular) fit: see help('isSingular')
  boundary (singular) fit: see help('isSingular')
  boundary (singular) fit: see help('isSingular')
  boundary (singular) fit: see help('isSingular')
  boundary (singular) fit: see help('isSingular')
  boundary (singular) fit: see help('isSingular')
  boundary (singular) fit: see help('isSingular')
  boundary (singular) fit: see help('isSingular')
  boundary (singular) fit: see help('isSingular')
  boundary (singular) fit: see help('isSingular')
  boundary (singular) fit: see help('isSingular')
  boundary (singular) fit: see help('isSingular')
  boundary (singular) fit: see help('isSingular')
  boundary (singular) fit: see help('isSingular')
  > (elim_tab_random8.4 <- st$random)
  Backward reduced random-effect table:
  
                               Eliminated npar  logLik    AIC     LRT Df
  <none>                                    21 -412.70 867.41           
  (1 | Assessor:Picture:TVset)          1   20 -412.70 865.41  0.0000  1
  (1 | Repeat)                          2   19 -412.70 863.41  0.0000  1
  (1 | Repeat:Picture)                  3   18 -412.70 861.41  0.0000  1
  (1 | Repeat:TVset)                    4   17 -412.70 859.41  0.0000  1
  (1 | Repeat:TVset:Picture)            5   16 -412.70 857.41  0.0000  1
  (1 | Assessor:TVset)                  0   15 -414.10 858.20  2.7891  1
  (1 | Assessor:Picture)                0   15 -418.88 867.76 12.3473  1
  (1 | Assessor)                        0   15 -416.44 862.88  7.4698  1
                               Pr(>Chisq)    
  <none>                                     
  (1 | Assessor:Picture:TVset)  1.0000000    
  (1 | Repeat)                  1.0000000    
  (1 | Repeat:Picture)          0.9999996    
  (1 | Repeat:TVset)            1.0000000    
  (1 | Repeat:TVset:Picture)    0.9999997    
  (1 | Assessor:TVset)          0.0949061 .  
  (1 | Assessor:Picture)        0.0004416 ***
  (1 | Assessor)                0.0062742 ** 
  ---
  Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
  > (elim_tab_fixed8.4 <- st$fixed)
  Backward reduced fixed-effect table:
  Degrees of freedom method: Satterthwaite 
  
                Eliminated Sum Sq Mean Sq NumDF DenDF F value    Pr(>F)    
  TVset:Picture          0 90.767  15.128     6   138  4.1777 0.0006845 ***
  ---
  Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
  > (an8.4 <- anova(get_model(st)))
  Type III Analysis of Variance Table with Satterthwaite's method
                Sum Sq Mean Sq NumDF DenDF F value    Pr(>F)    
  TVset          1.765  0.8825     2    14  0.2437 0.7869818    
  Picture       51.857 17.2857     3    21  4.7735 0.0108785 *  
  TVset:Picture 90.767 15.1279     6   138  4.1777 0.0006845 ***
  ---
  Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
  > 
  > ## Section 8.5:
  > # L <- matrix(0, ncol = 12, nrow = 6)
  > # L[1, 7] <- L[2, 8] <- L[3, 9] <- L[4, 10] <- L[5, 11] <- L[6, 12] <- 1
  > L <- cbind(array(0, dim=c(6, 6)), diag(6))
  > (con1_8.5 <- calcSatterth(tv, L))
  $denom
  [1] 138
  
  $Fstat
           [,1]
  [1,] 4.177656
  
  $pvalue
               [,1]
  [1,] 0.0006844787
  
  $ndf
  [1] 6
  
  > (con2_8.5 <- contest(tv, L))
      Sum Sq  Mean Sq NumDF DenDF  F value       Pr(>F)
  1 90.76719 15.12786     6   138 4.177656 0.0006844787
  > 
  > ## Section C:
  > # m.carrots <- lmer(Preference ~ sens1 + sens2 + (1 + sens1 + sens2 | Consumer) +
  > #                     (1 | product), data = carrots)
  > # step(m.carrots, reduce.fixed = FALSE)
  > (ran_C <- ranova(m.carrots))
  boundary (singular) fit: see help('isSingular')
  boundary (singular) fit: see help('isSingular')
  ANOVA-like table for random-effects: Single term deletions
  
  Model:
  Preference ~ sens1 + sens2 + (1 + sens1 + sens2 | Consumer) + (1 | Product)
                                          npar  logLik    AIC     LRT Df
  <none>                                    11 -1869.7 3761.5           
  sens1 in (1 + sens1 + sens2 | Consumer)    8 -1870.7 3757.3  1.8274  3
  sens2 in (1 + sens1 + sens2 | Consumer)    8 -1874.5 3765.0  9.5464  3
  (1 | Product)                             10 -1878.7 3777.4 17.9080  1
                                          Pr(>Chisq)    
  <none>                                                
  sens1 in (1 + sens1 + sens2 | Consumer)    0.60899    
  sens2 in (1 + sens1 + sens2 | Consumer)    0.02284 *  
  (1 | Product)                            2.318e-05 ***
  ---
  Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
  Warning message:
  Model failed to converge with 1 negative eigenvalue: -1.5e+03 
  > 
  > # Compare to validated outputs:
  > TOL <- 1e-4
  > stopifnot(
  +   isTRUE(all.equal(an8.2_save, an8.2, check.attributes = FALSE, tolerance=TOL)),
  +   isTRUE(all.equal(sum8.3_save, sum8.3, check.attributes = FALSE, tolerance=TOL)),
  +   isTRUE(all.equal(elim_tab_random8.4_save, elim_tab_random8.4,
  +                    check.attributes = FALSE, tolerance=TOL)),
  +   isTRUE(all.equal(elim_tab_fixed8.4_save, elim_tab_fixed8.4,
  +                    check.attributes = FALSE, tolerance=TOL)),
  +   isTRUE(all.equal(an8.4_save, an8.4, check.attributes = FALSE, tolerance=TOL)),
  +   isTRUE(all.equal(con1_8.5_save, con1_8.5, check.attributes = FALSE, tolerance=TOL)),
  +   isTRUE(all.equal(con2_8.5_save, con2_8.5, check.attributes = FALSE, tolerance=TOL))
  + )
  > if(has_pbkrtest) {
  +   stopifnot(
  +     isTRUE(all.equal(ankr8.2_save, ankr8.2, check.attributes = FALSE, tolerance=TOL))
  +   )
  + }
  > 
  > 
  > proc.time()
     user  system elapsed 
    15.93    0.37   16.29 
Running the tests in 'tests/test_ls_means.R' failed.
Complete output:
  > # test_lsmeans.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
  
  > 
  > 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"
  > 
  > ########### Basic model structures:
  > 
  > # Factor * covariate:
  > data("cake", package="lme4")
  > model <- lmer(angle ~ recipe * temp + (1|recipe:replicate), cake)
  > (lsm <- ls_means(model))
  Least Squares Means table:
  
          Estimate Std. Error df t value   lower   upper  Pr(>|t|)    
  recipeA  33.1222     1.7368 42  19.070 29.6172 36.6273 < 2.2e-16 ***
  recipeB  31.6444     1.7368 42  18.220 28.1394 35.1495 < 2.2e-16 ***
  recipeC  31.6000     1.7368 42  18.194 28.0949 35.1051 < 2.2e-16 ***
  ---
  Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
  
    Confidence level: 95%
    Degrees of freedom method: Satterthwaite 
  > stopifnot(
  +   nrow(lsm) == 3L,
  +   ncol(lsm) == 7L,
  +   # Balanced, so LS-means equal raw means:
  +   isTRUE(all.equal(c(with(cake, tapply(angle, recipe, mean))), lsm[, "Estimate"],
  +                    check.attributes=FALSE, tolerance=TOL))
  + )
  > 
  > # Pairwise differences of LS-means:
  > plsm <- ls_means(model, pairwise = TRUE)
  > plsm2 <- difflsmeans(model)
  > C <- as.matrix(lmerTest:::get_pairs(rownames(lsm)))
  > stopifnot(
  +   isTRUE(all.equal(plsm, plsm2, tolerance=TOL)),
  +   isTRUE(all.equal(plsm[, "Estimate"], c(lsm[, "Estimate"] %*% C),
  +                    check.attributes=FALSE, tolerance=TOL))
  + )
  > 
  > # Contrasts vectors:
  > show_tests(lsm)
  $recipe
          (Intercept) recipeB recipeC temp recipeB:temp recipeC:temp
  recipeA           1       0       0  200            0            0
  recipeB           1       1       0  200          200            0
  recipeC           1       0       1  200            0          200
  
  > show_tests(plsm)
  $recipe
                    (Intercept) recipeB recipeC temp recipeB:temp recipeC:temp
  recipeA - recipeB           0      -1       0    0         -200            0
  recipeA - recipeC           0       0      -1    0            0         -200
  recipeB - recipeC           0       1      -1    0          200         -200
  
  > 
  > # Factor * Ordered:
  > model <- lmer(angle ~ recipe * temperature + (1|recipe:replicate), cake)
  > (lsm2 <- ls_means(model))
  Least Squares Means table:
  
                         Estimate Std. Error   df t value   lower   upper
  recipeA                 33.1222     1.7368 42.0  19.070 29.6172 36.6273
  recipeB                 31.6444     1.7368 42.0  18.220 28.1394 35.1495
  recipeC                 31.6000     1.7368 42.0  18.194 28.0949 35.1051
  temperature175          27.9778     1.1767 77.4  23.776 25.6349 30.3207
  temperature185          29.9556     1.1767 77.4  25.457 27.6127 32.2985
  temperature195          31.4222     1.1767 77.4  26.704 29.0793 33.7651
  temperature205          32.1778     1.1767 77.4  27.346 29.8349 34.5207
  temperature215          35.8444     1.1767 77.4  30.462 33.5015 38.1873
  temperature225          35.3556     1.1767 77.4  30.046 33.0127 37.6985
  recipeA:temperature175  29.1333     2.0381 77.4  14.294 25.0753 33.1913
  recipeB:temperature175  26.8667     2.0381 77.4  13.182 22.8087 30.9247
  recipeC:temperature175  27.9333     2.0381 77.4  13.706 23.8753 31.9913
  recipeA:temperature185  31.5333     2.0381 77.4  15.472 27.4753 35.5913
  recipeB:temperature185  29.4000     2.0381 77.4  14.425 25.3420 33.4580
  recipeC:temperature185  28.9333     2.0381 77.4  14.196 24.8753 32.9913
  recipeA:temperature195  30.8000     2.0381 77.4  15.112 26.7420 34.8580
  recipeB:temperature195  31.7333     2.0381 77.4  15.570 27.6753 35.7913
  recipeC:temperature195  31.7333     2.0381 77.4  15.570 27.6753 35.7913
  recipeA:temperature205  33.5333     2.0381 77.4  16.453 29.4753 37.5913
  recipeB:temperature205  32.1333     2.0381 77.4  15.766 28.0753 36.1913
  recipeC:temperature205  30.8667     2.0381 77.4  15.145 26.8087 34.9247
  recipeA:temperature215  38.6667     2.0381 77.4  18.972 34.6087 42.7247
  recipeB:temperature215  34.4667     2.0381 77.4  16.911 30.4087 38.5247
  recipeC:temperature215  34.4000     2.0381 77.4  16.878 30.3420 38.4580
  recipeA:temperature225  35.0667     2.0381 77.4  17.206 31.0087 39.1247
  recipeB:temperature225  35.2667     2.0381 77.4  17.304 31.2087 39.3247
  recipeC:temperature225  35.7333     2.0381 77.4  17.533 31.6753 39.7913
                          Pr(>|t|)    
  recipeA                < 2.2e-16 ***
  recipeB                < 2.2e-16 ***
  recipeC                < 2.2e-16 ***
  temperature175         < 2.2e-16 ***
  temperature185         < 2.2e-16 ***
  temperature195         < 2.2e-16 ***
  temperature205         < 2.2e-16 ***
  temperature215         < 2.2e-16 ***
  temperature225         < 2.2e-16 ***
  recipeA:temperature175 < 2.2e-16 ***
  recipeB:temperature175 < 2.2e-16 ***
  recipeC:temperature175 < 2.2e-16 ***
  recipeA:temperature185 < 2.2e-16 ***
  recipeB:temperature185 < 2.2e-16 ***
  recipeC:temperature185 < 2.2e-16 ***
  recipeA:temperature195 < 2.2e-16 ***
  recipeB:temperature195 < 2.2e-16 ***
  recipeC:temperature195 < 2.2e-16 ***
  recipeA:temperature205 < 2.2e-16 ***
  recipeB:temperature205 < 2.2e-16 ***
  recipeC:temperature205 < 2.2e-16 ***
  recipeA:temperature215 < 2.2e-16 ***
  recipeB:temperature215 < 2.2e-16 ***
  recipeC:temperature215 < 2.2e-16 ***
  recipeA:temperature225 < 2.2e-16 ***
  recipeB:temperature225 < 2.2e-16 ***
  recipeC:temperature225 < 2.2e-16 ***
  ---
  Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
  
    Confidence level: 95%
    Degrees of freedom method: Satterthwaite 
  > stopifnot(
  +   nrow(lsm2) == 3 + 6 + 3*6,
  +   ncol(lsm) == 7L,
  +   # Balanced, so LS-means equal raw means:
  +   isTRUE(all.equal(lsm[1:3, ], lsm2[1:3, ],
  +                    check.attributes=FALSE, tolerance=TOL))
  + )
  > 
  > 
  > # Factor * Factor:
  > cake2 <- cake
  > cake2$temperature <- factor(cake2$temperature, ordered = FALSE)
  > model <- lmer(angle ~ recipe * temperature + (1|recipe:replicate), cake2)
  > (lsm3 <- ls_means(model))
  Least Squares Means table:
  
                         Estimate Std. Error   df t value   lower   upper
  recipeA                 33.1222     1.7368 42.0  19.070 29.6172 36.6273
  recipeB                 31.6444     1.7368 42.0  18.220 28.1394 35.1495
  recipeC                 31.6000     1.7368 42.0  18.194 28.0949 35.1051
  temperature175          27.9778     1.1767 77.4  23.776 25.6349 30.3207
  temperature185          29.9556     1.1767 77.4  25.457 27.6127 32.2985
  temperature195          31.4222     1.1767 77.4  26.704 29.0793 33.7651
  temperature205          32.1778     1.1767 77.4  27.346 29.8349 34.5207
  temperature215          35.8444     1.1767 77.4  30.462 33.5015 38.1873
  temperature225          35.3556     1.1767 77.4  30.046 33.0127 37.6985
  recipeA:temperature175  29.1333     2.0381 77.4  14.294 25.0753 33.1913
  recipeB:temperature175  26.8667     2.0381 77.4  13.182 22.8087 30.9247
  recipeC:temperature175  27.9333     2.0381 77.4  13.706 23.8753 31.9913
  recipeA:temperature185  31.5333     2.0381 77.4  15.472 27.4753 35.5913
  recipeB:temperature185  29.4000     2.0381 77.4  14.425 25.3420 33.4580
  recipeC:temperature185  28.9333     2.0381 77.4  14.196 24.8753 32.9913
  recipeA:temperature195  30.8000     2.0381 77.4  15.112 26.7420 34.8580
  recipeB:temperature195  31.7333     2.0381 77.4  15.570 27.6753 35.7913
  recipeC:temperature195  31.7333     2.0381 77.4  15.570 27.6753 35.7913
  recipeA:temperature205  33.5333     2.0381 77.4  16.453 29.4753 37.5913
  recipeB:temperature205  32.1333     2.0381 77.4  15.766 28.0753 36.1913
  recipeC:temperature205  30.8667     2.0381 77.4  15.145 26.8087 34.9247
  recipeA:temperature215  38.6667     2.0381 77.4  18.972 34.6087 42.7247
  recipeB:temperature215  34.4667     2.0381 77.4  16.911 30.4087 38.5247
  recipeC:temperature215  34.4000     2.0381 77.4  16.878 30.3420 38.4580
  recipeA:temperature225  35.0667     2.0381 77.4  17.206 31.0087 39.1247
  recipeB:temperature225  35.2667     2.0381 77.4  17.304 31.2087 39.3247
  recipeC:temperature225  35.7333     2.0381 77.4  17.533 31.6753 39.7913
                          Pr(>|t|)    
  recipeA                < 2.2e-16 ***
  recipeB                < 2.2e-16 ***
  recipeC                < 2.2e-16 ***
  temperature175         < 2.2e-16 ***
  temperature185         < 2.2e-16 ***
  temperature195         < 2.2e-16 ***
  temperature205         < 2.2e-16 ***
  temperature215         < 2.2e-16 ***
  temperature225         < 2.2e-16 ***
  recipeA:temperature175 < 2.2e-16 ***
  recipeB:temperature175 < 2.2e-16 ***
  recipeC:temperature175 < 2.2e-16 ***
  recipeA:temperature185 < 2.2e-16 ***
  recipeB:temperature185 < 2.2e-16 ***
  recipeC:temperature185 < 2.2e-16 ***
  recipeA:temperature195 < 2.2e-16 ***
  recipeB:temperature195 < 2.2e-16 ***
  recipeC:temperature195 < 2.2e-16 ***
  recipeA:temperature205 < 2.2e-16 ***
  recipeB:temperature205 < 2.2e-16 ***
  recipeC:temperature205 < 2.2e-16 ***
  recipeA:temperature215 < 2.2e-16 ***
  recipeB:temperature215 < 2.2e-16 ***
  recipeC:temperature215 < 2.2e-16 ***
  recipeA:temperature225 < 2.2e-16 ***
  recipeB:temperature225 < 2.2e-16 ***
  recipeC:temperature225 < 2.2e-16 ***
  ---
  Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
  
    Confidence level: 95%
    Degrees of freedom method: Satterthwaite 
  > stopifnot(
  +   isTRUE(all.equal(lsm2, lsm3, check.attributes=FALSE, tolerance=TOL))
  + )
  > 
  > # Covariate (only):
  > data("sleepstudy", package="lme4")
  > m <- lmer(Reaction ~ Days + (Days | Subject), sleepstudy)
  > (lsm <- ls_means(m))
  Least Squares Means table:
  
       Estimate Std. Error df t value lower upper Pr(>|t|)
  
    Confidence level: 95%
    Degrees of freedom method: Satterthwaite 
  > stopifnot(
  +   nrow(lsm) == 0L,
  +   ncol(lsm) == 7L
  + )
  > 
  > # No fixef:
  > m <- lmer(Reaction ~ 0 + (Days | Subject), sleepstudy)
  > (lsm <- ls_means(m))
  Least Squares Means table:
  
       Estimate Std. Error df t value lower upper Pr(>|t|)
  
    Confidence level: 95%
    Degrees of freedom method: Satterthwaite 
  > stopifnot(
  +   nrow(lsm) == 0L,
  +   ncol(lsm) == 7L
  + )
  > 
  > ########### Arguments and options:
  > 
  > # which
  > model <- lmer(angle ~ recipe * temperature + (1|recipe:replicate), cake2)
  > (lsm4 <- ls_means(model, which = "recipe"))
  Least Squares Means table:
  
          Estimate Std. Error df t value   lower   upper  Pr(>|t|)    
  recipeA  33.1222     1.7368 42  19.070 29.6172 36.6273 < 2.2e-16 ***
  recipeB  31.6444     1.7368 42  18.220 28.1394 35.1495 < 2.2e-16 ***
  recipeC  31.6000     1.7368 42  18.194 28.0949 35.1051 < 2.2e-16 ***
  ---
  Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
  
    Confidence level: 95%
    Degrees of freedom method: Satterthwaite 
  > stopifnot(
  +   nrow(lsm4) == 3L,
  +   ncol(lsm4) == 7L,
  +   isTRUE(all.equal(lsm3[1:3, ], lsm4, check.attributes=FALSE, tolerance=TOL))
  + )
  > 
  > # KR:
  > if(has_pbkrtest)
  +   (lsm5 <- ls_means(model, which = "recipe", ddf = "Kenward-Roger"))
  Least Squares Means table:
  
          Estimate Std. Error df t value   lower   upper  Pr(>|t|)    
  recipeA  33.1222     1.7368 42  19.070 29.6172 36.6273 < 2.2e-16 ***
  recipeB  31.6444     1.7368 42  18.220 28.1394 35.1495 < 2.2e-16 ***
  recipeC  31.6000     1.7368 42  18.194 28.0949 35.1051 < 2.2e-16 ***
  ---
  Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
  
    Confidence level: 95%
    Degrees of freedom method: Kenward-Roger 
  > 
  > # level:
  > (lsm6 <- ls_means(model, which = "recipe", level=0.99))
  Least Squares Means table:
  
          Estimate Std. Error df t value   lower   upper  Pr(>|t|)    
  recipeA  33.1222     1.7368 42  19.070 28.4361 37.8083 < 2.2e-16 ***
  recipeB  31.6444     1.7368 42  18.220 26.9584 36.3305 < 2.2e-16 ***
  recipeC  31.6000     1.7368 42  18.194 26.9139 36.2861 < 2.2e-16 ***
  ---
  Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
  
    Confidence level: 99%
    Degrees of freedom method: Satterthwaite 
  > 
  > stopifnot(
  +   all(lsm6[, "lower"] < lsm4[, "lower"]),
  +   all(lsm6[, "upper"] > lsm4[, "upper"])
  + )
  > 
  > 
  > 
  > ########### Missing cels -> unestimable contrasts:
  > 
  > # Missing cell:
  > cake3 <- cake
  > cake3$temperature <- factor(cake3$temperature, ordered=FALSE)
  > cake3 <- droplevels(subset(cake3, temperature %in% levels(cake3$temperature)[1:3]))
  > cake3 <- droplevels(subset(cake3, !(recipe == "C" & temperature == "195") ))
  > str(cake3)
  'data.frame':	120 obs. of  5 variables:
   $ replicate  : Factor w/ 15 levels "1","2","3","4",..: 1 1 1 1 1 1 1 1 2 2 ...
   $ recipe     : Factor w/ 3 levels "A","B","C": 1 1 1 2 2 2 3 3 1 1 ...
   $ temperature: Factor w/ 3 levels "175","185","195": 1 2 3 1 2 3 1 2 1 2 ...
   $ angle      : int  42 46 47 39 46 51 46 44 47 29 ...
   $ temp       : num  175 185 195 175 185 195 175 185 175 185 ...
  > with(cake3, table(recipe, temperature))
        temperature
  recipe 175 185 195
       A  15  15  15
       B  15  15  15
       C  15  15   0
  > model <- lmer(angle ~ recipe * temperature + (1|recipe:replicate), cake3)
  fixed-effect model matrix is rank deficient so dropping 1 column / coefficient
  > (lsm7 <- ls_means(model))
  Least Squares Means table:
  
                         Estimate Std. Error   df t value   lower   upper
  recipeA                 30.4889     1.7407 40.3  17.515 26.9716 34.0062
  recipeB                 29.3333     1.7407 40.3  16.851 25.8160 32.8506
  recipeC                      NA         NA   NA      NA      NA      NA
  temperature175          27.9778     1.1016 57.2  25.398 25.7720 30.1835
  temperature185          29.9556     1.1016 57.2  27.193 27.7498 32.1613
  temperature195               NA         NA   NA      NA      NA      NA
  recipeA:temperature175  29.1333     1.9080 57.2  15.269 25.3129 32.9538
  recipeB:temperature175  26.8667     1.9080 57.2  14.081 23.0462 30.6871
  recipeC:temperature175  27.9333     1.9080 57.2  14.640 24.1129 31.7538
  recipeA:temperature185  31.5333     1.9080 57.2  16.527 27.7129 35.3538
  recipeB:temperature185  29.4000     1.9080 57.2  15.409 25.5795 33.2205
  recipeC:temperature185  28.9333     1.9080 57.2  15.164 25.1129 32.7538
  recipeA:temperature195  30.8000     1.9080 57.2  16.143 26.9795 34.6205
  recipeB:temperature195  31.7333     1.9080 57.2  16.632 27.9129 35.5538
  recipeC:temperature195       NA         NA   NA      NA      NA      NA
                          Pr(>|t|)    
  recipeA                < 2.2e-16 ***
  recipeB                < 2.2e-16 ***
  recipeC                       NA    
  temperature175         < 2.2e-16 ***
  temperature185         < 2.2e-16 ***
  temperature195                NA    
  recipeA:temperature175 < 2.2e-16 ***
  recipeB:temperature175 < 2.2e-16 ***
  recipeC:temperature175 < 2.2e-16 ***
  recipeA:temperature185 < 2.2e-16 ***
  recipeB:temperature185 < 2.2e-16 ***
  recipeC:temperature185 < 2.2e-16 ***
  recipeA:temperature195 < 2.2e-16 ***
  recipeB:temperature195 < 2.2e-16 ***
  recipeC:temperature195        NA    
  ---
  Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
  
    Confidence level: 95%
    Degrees of freedom method: Satterthwaite 
  > 
  > # Using show_tests with options:
  > show_tests(lsm7, fractions = TRUE)
  $recipe
          (Intercept) recipeB recipeC temperature185 temperature195
  recipeA   1           0       0     1/3            1/3           
  recipeB   1           1       0     1/3            1/3           
  recipeC   1           0       1     1/3            1/3           
          recipeB:temperature185 recipeC:temperature185 recipeB:temperature195
  recipeA   0                      0                      0                   
  recipeB 1/3                      0                    1/3                   
  recipeC   0                    1/3                      0                   
          recipeC:temperature195
  recipeA   0                   
  recipeB   0                   
  recipeC 1/3                   
  
  $temperature
                 (Intercept) recipeB recipeC temperature185 temperature195
  temperature175   1         1/3     1/3       0              0           
  temperature185   1         1/3     1/3       1              0           
  temperature195   1         1/3     1/3       0              1           
                 recipeB:temperature185 recipeC:temperature185
  temperature175   0                      0                   
  temperature185 1/3                    1/3                   
  temperature195   0                      0                   
                 recipeB:temperature195 recipeC:temperature195
  temperature175   0                      0                   
  temperature185   0                      0                   
  temperature195 1/3                    1/3                   
  
  $`recipe:temperature`
                         (Intercept) recipeB recipeC temperature185
  recipeA:temperature175 1           0       0       0             
  recipeB:temperature175 1           1       0       0             
  recipeC:temperature175 1           0       1       0             
  recipeA:temperature185 1           0       0       1             
  recipeB:temperature185 1           1       0       1             
  recipeC:temperature185 1           0       1       1             
  recipeA:temperature195 1           0       0       0             
  recipeB:temperature195 1           1       0       0             
  recipeC:temperature195 1           0       1       0             
                         temperature195 recipeB:temperature185
  recipeA:temperature175 0              0                     
  recipeB:temperature175 0              0                     
  recipeC:temperature175 0              0                     
  recipeA:temperature185 0              0                     
  recipeB:temperature185 0              1                     
  recipeC:temperature185 0              0                     
  recipeA:temperature195 1              0                     
  recipeB:temperature195 1              0                     
  recipeC:temperature195 1              0                     
                         recipeC:temperature185 recipeB:temperature195
  recipeA:temperature175 0                      0                     
  recipeB:temperature175 0                      0                     
  recipeC:temperature175 0                      0                     
  recipeA:temperature185 0                      0                     
  recipeB:temperature185 0                      0                     
  recipeC:temperature185 1                      0                     
  recipeA:temperature195 0                      0                     
  recipeB:temperature195 0                      1                     
  recipeC:temperature195 0                      0                     
                         recipeC:temperature195
  recipeA:temperature175 0                     
  recipeB:temperature175 0                     
  recipeC:temperature175 0                     
  recipeA:temperature185 0                     
  recipeB:temperature185 0                     
  recipeC:temperature185 0                     
  recipeA:temperature195 0                     
  recipeB:temperature195 0                     
  recipeC:temperature195 1                     
  
  > show_tests(lsm7, fractions = TRUE, names = FALSE)
  $recipe
       [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9]
  [1,]   1    0    0  1/3  1/3    0    0    0    0 
  [2,]   1    1    0  1/3  1/3  1/3    0  1/3    0 
  [3,]   1    0    1  1/3  1/3    0  1/3    0  1/3 
  
  $temperature
       [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9]
  [1,]   1  1/3  1/3    0    0    0    0    0    0 
  [2,]   1  1/3  1/3    1    0  1/3  1/3    0    0 
  [3,]   1  1/3  1/3    0    1    0    0  1/3  1/3 
  
  $`recipe:temperature`
        [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9]
   [1,] 1    0    0    0    0    0    0    0    0   
   [2,] 1    1    0    0    0    0    0    0    0   
   [3,] 1    0    1    0    0    0    0    0    0   
   [4,] 1    0    0    1    0    0    0    0    0   
   [5,] 1    1    0    1    0    1    0    0    0   
   [6,] 1    0    1    1    0    0    1    0    0   
   [7,] 1    0    0    0    1    0    0    0    0   
   [8,] 1    1    0    0    1    0    0    1    0   
   [9,] 1    0    1    0    1    0    0    0    1   
  
  > 
  > # Missing diagonal:
  > cake4 <- cake
  > cake4$temperature <- factor(cake4$temperature, ordered=FALSE)
  > cake4 <- droplevels(subset(cake4, temperature %in% levels(cake4$temperature)[1:3]))
  > cake4 <- droplevels(subset(cake4, !((recipe == "A" & temperature == "175") |
  +                                       (recipe == "B" & temperature == "185") |
  +                                       (recipe == "C" & temperature == "195") )))
  > # str(cake4)
  > with(cake4, table(recipe, temperature))
        temperature
  recipe 175 185 195
       A   0  15  15
       B  15   0  15
       C  15  15   0
  > model <- lmer(angle ~ recipe * temperature + (1|recipe:replicate), cake4)
  fixed-effect model matrix is rank deficient so dropping 3 columns / coefficients
  > ls_means(model)
  Least Squares Means table:
  
                         Estimate Std. Error   df t value   lower   upper
  recipeA                      NA         NA   NA      NA      NA      NA
  recipeB                      NA         NA   NA      NA      NA      NA
  recipeC                      NA         NA   NA      NA      NA      NA
  temperature175               NA         NA   NA      NA      NA      NA
  temperature185               NA         NA   NA      NA      NA      NA
  temperature195               NA         NA   NA      NA      NA      NA
  recipeA:temperature175       NA         NA   NA      NA      NA      NA
  recipeB:temperature175  26.8667     1.8908 53.5  14.209 23.0750 30.6584
  recipeC:temperature175  27.9333     1.8908 53.5  14.773 24.1416 31.7250
  recipeA:temperature185  31.5333     1.8908 53.5  16.677 27.7416 35.3250
  recipeB:temperature185       NA         NA   NA      NA      NA      NA
  recipeC:temperature185  28.9333     1.8908 53.5  15.302 25.1416 32.7250
  recipeA:temperature195  30.8000     1.8908 53.5  16.289 27.0083 34.5917
  recipeB:temperature195  31.7333     1.8908 53.5  16.783 27.9416 35.5250
  recipeC:temperature195       NA         NA   NA      NA      NA      NA
                          Pr(>|t|)    
  recipeA                       NA    
  recipeB                       NA    
  recipeC                       NA    
  temperature175                NA    
  temperature185                NA    
  temperature195                NA    
  recipeA:temperature175        NA    
  recipeB:temperature175 < 2.2e-16 ***
  recipeC:temperature175 < 2.2e-16 ***
  recipeA:temperature185 < 2.2e-16 ***
  recipeB:temperature185        NA    
  recipeC:temperature185 < 2.2e-16 ***
  recipeA:temperature195 < 2.2e-16 ***
  recipeB:temperature195 < 2.2e-16 ***
  recipeC:temperature195        NA    
  ---
  Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
  
    Confidence level: 95%
    Degrees of freedom method: Satterthwaite 
  > 
  > 
  > ########### Various contrasts codings:
  > 
  > model <- lmer(angle ~ recipe * temperature + (1|recipe:replicate), cake3,
  +               contrasts = list(recipe="contr.sum", temperature="contr.helmert"))
  fixed-effect model matrix is rank deficient so dropping 1 column / coefficient
  > (lsm8 <- ls_means(model))
  Least Squares Means table:
  
                         Estimate Std. Error   df t value   lower   upper
  recipeA                 30.4889     1.7407 40.3  17.515 26.9716 34.0062
  recipeB                 29.3333     1.7407 40.3  16.851 25.8160 32.8506
  recipeC                      NA         NA   NA      NA      NA      NA
  temperature175          27.9778     1.1016 57.2  25.398 25.7720 30.1835
  temperature185          29.9556     1.1016 57.2  27.193 27.7498 32.1613
  temperature195               NA         NA   NA      NA      NA      NA
  recipeA:temperature175  29.1333     1.9080 57.2  15.269 25.3129 32.9538
  recipeB:temperature175  26.8667     1.9080 57.2  14.081 23.0462 30.6871
  recipeC:temperature175  27.9333     1.9080 57.2  14.640 24.1129 31.7538
  recipeA:temperature185  31.5333     1.9080 57.2  16.527 27.7129 35.3538
  recipeB:temperature185  29.4000     1.9080 57.2  15.409 25.5795 33.2205
  recipeC:temperature185  28.9333     1.9080 57.2  15.164 25.1129 32.7538
  recipeA:temperature195  30.8000     1.9080 57.2  16.143 26.9795 34.6205
  recipeB:temperature195  31.7333     1.9080 57.2  16.632 27.9129 35.5538
  recipeC:temperature195       NA         NA   NA      NA      NA      NA
                          Pr(>|t|)    
  recipeA                < 2.2e-16 ***
  recipeB                < 2.2e-16 ***
  recipeC                       NA    
  temperature175         < 2.2e-16 ***
  temperature185         < 2.2e-16 ***
  temperature195                NA    
  recipeA:temperature175 < 2.2e-16 ***
  recipeB:temperature175 < 2.2e-16 ***
  recipeC:temperature175 < 2.2e-16 ***
  recipeA:temperature185 < 2.2e-16 ***
  recipeB:temperature185 < 2.2e-16 ***
  recipeC:temperature185 < 2.2e-16 ***
  recipeA:temperature195 < 2.2e-16 ***
  recipeB:temperature195 < 2.2e-16 ***
  recipeC:temperature195        NA    
  ---
  Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
  
    Confidence level: 95%
    Degrees of freedom method: Satterthwaite 
  > # show_tests(lsm7)
  > # show_tests(lsm8)
  > stopifnot(
  +   isTRUE(all.equal(lsm7, lsm8, check.attributes=FALSE, tolerance=TOL))
  + )
  > 
  > # ambient contrasts not contr.treatment:
  > options("contrasts")
  $contrasts
          unordered           ordered 
  "contr.treatment"      "contr.poly" 
  
  > options(contrasts = c("contr.sum", "contr.poly"))
  > model <- lmer(angle ~ recipe * temperature + (1|recipe:replicate), cake3)
  fixed-effect model matrix is rank deficient so dropping 1 column / coefficient
  > (lsm9 <- ls_means(model))
  Least Squares Means table:
  
                         Estimate Std. Error   df t value   lower   upper
  recipeA                 30.4889     1.7407 40.3  17.515 26.9716 34.0062
  recipeB                 29.3333     1.7407 40.3  16.851 25.8160 32.8506
  recipeC                      NA         NA   NA      NA      NA      NA
  temperature175          27.9778     1.1016 57.2  25.398 25.7720 30.1835
  temperature185          29.9556     1.1016 57.2  27.193 27.7498 32.1613
  temperature195               NA         NA   NA      NA      NA      NA
  recipeA:temperature175  29.1333     1.9080 57.2  15.269 25.3129 32.9538
  recipeB:temperature175  26.8667     1.9080 57.2  14.081 23.0462 30.6871
  recipeC:temperature175  27.9333     1.9080 57.2  14.640 24.1129 31.7538
  recipeA:temperature185  31.5333     1.9080 57.2  16.527 27.7129 35.3538
  recipeB:temperature185  29.4000     1.9080 57.2  15.409 25.5795 33.2205
  recipeC:temperature185  28.9333     1.9080 57.2  15.164 25.1129 32.7538
  recipeA:temperature195  30.8000     1.9080 57.2  16.143 26.9795 34.6205
  recipeB:temperature195  31.7333     1.9080 57.2  16.632 27.9129 35.5538
  recipeC:temperature195       NA         NA   NA      NA      NA      NA
                          Pr(>|t|)    
  recipeA                < 2.2e-16 ***
  recipeB                < 2.2e-16 ***
  recipeC                       NA    
  temperature175         < 2.2e-16 ***
  temperature185         < 2.2e-16 ***
  temperature195                NA    
  recipeA:temperature175 < 2.2e-16 ***
  recipeB:temperature175 < 2.2e-16 ***
  recipeC:temperature175 < 2.2e-16 ***
  recipeA:temperature185 < 2.2e-16 ***
  recipeB:temperature185 < 2.2e-16 ***
  recipeC:temperature185 < 2.2e-16 ***
  recipeA:temperature195 < 2.2e-16 ***
  recipeB:temperature195 < 2.2e-16 ***
  recipeC:temperature195        NA    
  ---
  Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
  
    Confidence level: 95%
    Degrees of freedom method: Satterthwaite 
  > options(contrasts = c("contr.treatment", "contr.poly"))
  > options("contrasts")
  $contrasts
  [1] "contr.treatment" "contr.poly"     
  
  > stopifnot(
  +   isTRUE(all.equal(lsm7, lsm9, check.attributes=FALSE, tolerance=TOL))
  + )
  > 
  > 
  > 
  > proc.time()
     user  system elapsed 
     3.43    0.43    3.85 
Running the tests in 'tests/test_ranova_step.R' failed.
Complete output:
  > # 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)    
  <none>                      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)    
  <none>                      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)
  <none>                                 6 -871.81 1755.6                     
  Days in (Days | Subject)          0    4 -893.23 1794.5 42.837  2   4.99e-10
                              
  <none>                      
  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)    
  <none>                      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)    
  <none>                      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)
  <none>                                 6 -871.81 1755.6                     
  Days in (Days | Subject)          0    4 -893.23 1794.5 42.837  2   4.99e-10
                              
  <none>                      
  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)    
  <none>                      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 deviance  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)    
  <none>              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)    
  <none>                      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 | Subject) - 1
                npar   logLik    AIC    LRT Df Pr(>Chisq)    
  <none>           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)    
  <none>                      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)    
  <none>           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 deviance  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
  <none>                                     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)    
  <none>                                     
  (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)    
  <none>                          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)    
  <none>                  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)
  <none>                                     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)
  <none>                          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)    
  <none>                      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 deviance 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)    
  <none>                  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
  <none>                                      4 -883.26 1774.5         
  Days in (-1 + Days | Subject)          0    4 -893.23 1794.5 19.94  0
                                Pr(>Chisq)
  <none>                                  
  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)
  <none>                           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
  <none>                                                       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)    
  <none>                                                                  
  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)
  <none>                           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
  <none>                                                      10 -856.40 1732.8
  poly(Days, 2) in (1 + poly(Days, 2) | Subject)          0    5 -884.67 1779.3
                                                    LRT Df Pr(>Chisq)    
  <none>                                                                 
  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
  <none>                                           10 -856.40 1732.8          
  poly(Days, 2) in (1 + poly(Days, 2) | Subject)    5 -884.67 1779.3 56.524  5
                                                 Pr(>Chisq)    
  <none>                                                       
  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)    
  <none>                      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 deviance  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)    
  <none>                          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
  <none>                                     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)    
  <none>                                     
  (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)    
  <none>                          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)    
  <none>                            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) + TVset:Picture
                       npar  logLik    AIC    LRT Df Pr(>Chisq)    
  <none>                 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)    
  <none>                            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) + TVset:Picture
                       npar  logLik    AIC    LRT Df Pr(>Chisq)    
  <none>                 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)
  <none>                                 6 -871.81 1755.6                     
  Days in (Days | Subject)          0    4 -893.23 1794.5 42.837  2   4.99e-10
                              
  <none>                      
  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)    
  <none>                      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)    
  <none>                        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))
  + )
  > 
  > # 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
  <none>                                           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)  
  <none>                                          
  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
  <none>                                                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)  
  <none>                                                  
  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
  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
  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
  > 
  > # 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
  <none>                                          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)    
  <none>                                          
  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
  <none>                                          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)    
  <none>                                          
  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
  <none>                                          10 -1874.1 3768.2          
  poly(sens2, 2) in (poly(sens2, 2) | Consumer)    5 -1879.9 3769.7 11.552  5
                                                Pr(>Chisq)  
  <none>                                                    
  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
  <none>                                                     10 -1874.1 3768.2
  poly(sens2, 2) in (poly(sens2, 2) | Consumer)          0    5 -1879.9 3769.7
                                                   LRT Df Pr(>Chisq)  
  <none>                                                              
  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)
  > (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
  <none>                                          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)   
  <none>                                                     
  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
  <none>                                                     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)  
  <none>                                                              
  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
  <none>                                             7 -1875.3 3764.6          
  log(10 + sens2) in (log(10 + sens2) | Consumer)    5 -1879.9 3769.7 9.0854  2
                                                  Pr(>Chisq)  
  <none>                                                      
  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
  <none>                                                        7 -1875.3 3764.6
  log(10 + sens2) in (log(10 + sens2) | Consumer)          0    5 -1879.9 3769.7
                                                     LRT Df Pr(>Chisq)  
  <none>                                                                
  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)    
  <none>                   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)    
  <none>                              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)    
  <none>                   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)    
  <none>                              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)    
  <none>            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)
  <none>                                  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
                                
  <none>                        
  (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)
  <none>                                  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
                                
  <none>                        
  (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)
  <none>                                  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
                                
  <none>                        
  (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)
  <none>                                  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
                                
  <none>                        
  (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)
  <none>                                  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
                                
  <none>                        
  (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)
  <none>                                  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
                                
  <none>                        
  (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 
    17.53    0.64   18.18 
Running the tests in 'tests/test_zerovar.R' failed.
Complete output:
  > # test_zerovar.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
  
  > data("sleepstudy", package="lme4")
  > 
  > # Baseline fit:
  > m0 <- lmer(Reaction ~ Days +  (Days | Subject), sleepstudy,
  +            control=lmerControl(optimizer="bobyqa"))
  > ## default optimizer does not converge proporly
  > m0
  Linear mixed model fit by REML ['lmerModLmerTest']
  Formula: Reaction ~ Days + (Days | Subject)
     Data: sleepstudy
  REML criterion at convergence: 1743.628
  Random effects:
   Groups   Name        Std.Dev. Corr
   Subject  (Intercept) 24.740       
            Days         5.922   0.07
   Residual             25.592       
  Number of obs: 180, groups:  Subject, 18
  Fixed Effects:
  (Intercept)         Days  
       251.41        10.47  
  > (an0 <- anova(m0))
  Type III Analysis of Variance Table with Satterthwaite's method
       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
  > 
  > # Make a fit with a zero variance estimate:
  > n <- nrow(sleepstudy)
  > g <- factor(rep(1:2, c(n - 10, 10)))
  > m <- lmer(Reaction ~ Days +  (Days | Subject) + (1|g), sleepstudy,
  +           control=lmerControl(optimizer="bobyqa"))
  boundary (singular) fit: see help('isSingular')
  > m
  Linear mixed model fit by REML ['lmerModLmerTest']
  Formula: Reaction ~ Days + (Days | Subject) + (1 | g)
     Data: sleepstudy
  REML criterion at convergence: 1743.628
  Random effects:
   Groups   Name        Std.Dev. Corr
   Subject  (Intercept) 24.740       
            Days         5.922   0.07
   g        (Intercept)  0.000       
   Residual             25.592       
  Number of obs: 180, groups:  Subject, 18; g, 2
  Fixed Effects:
  (Intercept)         Days  
       251.41        10.47  
  optimizer (bobyqa) convergence code: 0 (OK) ; 0 optimizer warnings; 1 lme4 warnings 
  > (an <- anova(m))
  Type III Analysis of Variance Table with Satterthwaite's method
       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
  > 
  > # check that fit has a zero variance
  > vc <- as.data.frame(VarCorr(m))
  > stopifnot(isTRUE(
  +   all.equal(0, vc[vc$grp == "g", "sdcor"], tolerance=1e-4)
  + ))
  > # The hessian/vcov is actually positive definite:
  > stopifnot(isTRUE(
  +   all(eigen(m@vcov_varpar, only.values = TRUE)$values > 0)
  + ))
  > 
  > # Check that ANOVA tables are the same:
  > stopifnot(isTRUE(
  +   all.equal(an0[, 1:5], an[, 1:5], tolerance=1e-4)
  + ))
  > 
  > stopifnot(isTRUE( # Equality of summary tables
  +   all.equal(coef(summary(m0)), coef(summary(m)), tolerance=1e-4)
  + ))
  > stopifnot(isTRUE( # Equality of lme4-anova tables
  +   all.equal(anova(m0, ddf="lme4"), anova(m, ddf="lme4"), tolerance=1e-4)
  + ))
  > 
  > 
  > proc.time()
     user  system elapsed 
     2.59    0.37    2.96