Running 'test_a_utils.R' [3s/5s]
  Running 'test_anova.R' [10s/12s]
  Running 'test_compare_sas.R' [4s/5s]
  Running 'test_contest1D.R' [6s/7s]
  Running 'test_contestMD.R' [6s/7s]
  Running 'test_contrast_utils.R' [4s/4s]
  Running 'test_drop1.R' [6s/7s]
  Running 'test_legacy.R' [7s/8s]
  Running 'test_lmer.R' [7s/8s]
  Running 'test_lmerTest_paper.R' [26s/28s]
  Running 'test_ls_means.R' [6s/7s]
  Running 'test_ranova_step.R' [31s/34s]
  Running 'test_summary.R' [7s/8s]
  Running 'test_zerovar.R' [4s/5s]
  Running 'zlmerTest_zeroDenom.R' [4s/5s]
Running the tests in 'tests/test_anova.R' failed.
Complete output:
  > # test_anova.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()
  > 
  > # 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")
  > TOL <- 1e-4
  > 
  > ####################################
  > ## Basic anova tests
  > ####################################
  > 
  > m <- lmer(Reaction ~ Days + (Days | Subject), sleepstudy)
  > 
  > ####### ddf argument:
  > (an1 <- anova(m)) # Also testing print method.
  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
  > (an2 <- anova(m, ddf="Satterthwaite"))
  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
  > (an2b <- anova(m, ddf="Satterthwaite", type=3))
  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
  > (an2c <- anova(m, ddf="Satterthwaite", type=2))
  Type II 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
  > stopifnot(isTRUE(
  +   all.equal(an1, an2, tolerance=TOL)
  + ))
  > (an3 <- anova(m, ddf="Sat")) ## Abbreviated argument
  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
  > stopifnot(isTRUE(
  +   all.equal(an1, an3, tolerance=TOL)
  + ))
  > if(has_pbkrtest) {
  +   (anova(m, ddf="Kenward-Roger"))
  +   (anova(m, ddf="Kenward-Roger", type=3))
  + }
  Type III Analysis of Variance Table with Kenward-Roger'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
  > (an1 <- anova(m, ddf="lme4"))
  Analysis of Variance Table
       npar Sum Sq Mean Sq F value
  Days    1  30031   30031  45.853
  > (an2 <- anova(m, ddf="lme4", type=3)) # 'type' is ignored with ddf="lme4"
  Analysis of Variance Table
       npar Sum Sq Mean Sq F value
  Days    1  30031   30031  45.853
  > stopifnot(isTRUE(
  +   all.equal(an1, an2, tolerance=TOL)
  + ))
  > res <- assertError(anova(m, ddf="KR")) ## Error on incorrect arg.
  > stopifnot(
  +   grepl("'arg' should be one of ", unlist(res[[1]])$message)
  + )
  > 
  > ## lme4 method:
  > an1 <- anova(m, ddf="lme4")
  > an2 <- anova(as(m, "lmerMod"))
  > stopifnot(isTRUE(
  +   all.equal(an1, an2, tolerance=TOL)
  + ))
  > 
  > ###### type argument:
  > (an1 <- anova(m, type="1")) # valid type arg.
  Type I 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
  > (an2 <- anova(m, type="I")) # same
  Type I 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
  > stopifnot(isTRUE(
  +   all.equal(an1, an2, tolerance=TOL)
  + ))
  > (an3 <- anova(m, type=1)) # Not strictly valid, but accepted
  Type I 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
  > stopifnot(isTRUE(
  +   all.equal(an1, an3, tolerance=TOL)
  + ))
  > 
  > (an1 <- anova(m, type="2")) # valid type arg.
  Type II 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
  > (an2 <- anova(m, type="II")) # same
  Type II 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
  > stopifnot(isTRUE(
  +   all.equal(an1, an2, tolerance=TOL)
  + ))
  > (an3 <- anova(m, type=3)) # Not strictly valid, but accepted
  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
  > stopifnot(isTRUE(
  +   all.equal(an1, an3, check.attributes=FALSE, tolerance=TOL)
  + ))
  > 
  > (an1 <- anova(m, type="3")) # valid type arg.
  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
  > (an2 <- anova(m, type="III")) # same
  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
  > stopifnot(isTRUE(
  +   all.equal(an1, an2, tolerance=TOL)
  + ))
  > (an3 <- anova(m, type=3)) # Not strictly valid, but accepted
  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
  > stopifnot(isTRUE(
  +   all.equal(an1, an3, tolerance=TOL)
  + ))
  > assertError(anova(m, type=0)) # Not valid arg.
  > assertError(anova(m, type="i")) # Not valid arg.
  > 
  > ####### Model comparison:
  > fm <- lm(Reaction ~ Days, sleepstudy)
  > (an <- anova(m, fm))
  refitting model(s) with ML (instead of REML)
  Data: sleepstudy
  Models:
  fm: Reaction ~ Days
  m: Reaction ~ Days + (Days | Subject)
     npar    AIC    BIC  logLik deviance  Chisq Df Pr(>Chisq)    
  fm    3 1906.3 1915.9 -950.15   1900.3                         
  m     6 1763.9 1783.1 -875.97   1751.9 148.35  3  < 2.2e-16 ***
  ---
  Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
  > stopifnot(
  +   nrow(an) == 2L,
  +   rownames(an)[2] == "m"
  + )
  > 
  > m2 <- lmer(Reaction ~ Days + I(Days^2) + (Days | Subject), sleepstudy)
  > (an <- anova(m, m2, refit=FALSE))
  Data: sleepstudy
  Models:
  m: Reaction ~ Days + (Days | Subject)
  m2: Reaction ~ Days + I(Days^2) + (Days | Subject)
     npar    AIC    BIC  logLik deviance  Chisq Df Pr(>Chisq)
  m     6 1755.6 1774.8 -871.81   1743.6                     
  m2    7 1756.8 1779.2 -871.41   1742.8 0.8127  1     0.3673
  > stopifnot(
  +   nrow(an) == 2L,
  +   rownames(an)[1] == "m"
  + )
  > 
  > 
  > ####################################
  > ## Example with factor fixef:
  > ####################################
  > 
  > ## 'temp' is continuous, 'temperature' an ordered factor with 6 levels
  > data("cake", package="lme4")
  > m <- lmer(angle ~ recipe * temp + (1|recipe:replicate), cake)
  > (an <- anova(m))
  Type III Analysis of Variance Table with Satterthwaite's method
               Sum Sq Mean Sq NumDF  DenDF F value Pr(>F)    
  recipe         4.00    2.00     2 254.02  0.0957 0.9088    
  temp        1966.71 1966.71     1 222.00 94.1632 <2e-16 ***
  recipe:temp    1.74    0.87     2 222.00  0.0417 0.9592    
  ---
  Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
  > (an_lme4 <- anova(m, ddf="lme4"))
  Analysis of Variance Table
              npar  Sum Sq Mean Sq F value
  recipe         2   10.39    5.20  0.2488
  temp           1 1966.71 1966.71 94.1632
  recipe:temp    2    1.74    0.87  0.0417
  > 
  > if(has_pbkrtest) {
  +   (an_KR <- anova(m, ddf="Kenward-Roger"))
  +   # res <- all.equal(an[, c("Sum Sq", "Mean Sq", "F value")],
  +   #                  an_lme4[, c("Sum Sq", "Mean Sq", "F value")])
  +   # stopifnot(isTRUE(res))
  +   res <- all.equal(an[, c("Sum Sq", "Mean Sq", "F value")],
  +                    an_KR[, c("Sum Sq", "Mean Sq", "F value")], tolerance=TOL)
  +   stopifnot(isTRUE(res))
  + }
  > stopifnot(all.equal(c(2, 1, 2), an$NumDF, tol=1e-6),
  +           all.equal(c(254.0157612, 222, 222), an$DenDF, tol=TOL))
  > 
  > an3 <- anova(m, type=3)
  > an2 <- anova(m, type=2)
  > an1 <- anova(m, type=1)
  > 
  > ## Data is balanced, so Type II and III should be identical:
  > ## One variable is continuous, so Type I and II/III are different:
  > stopifnot(
  +   isTRUE(all.equal(an3, an2, check.attributes=FALSE, tolerance=TOL)),
  +   !isTRUE(all.equal(an1, an2, check.attributes=FALSE, tolerance=1e-8))
  + )
  > 
  > # Using an ordered factor:
  > m <- lmer(angle ~ recipe * temperature + (1|recipe:replicate), cake)
  > (an1 <- anova(m, type=1))
  Type I Analysis of Variance Table with Satterthwaite's method
                      Sum Sq Mean Sq NumDF DenDF F value Pr(>F)    
  recipe               10.19    5.09     2    42  0.2488 0.7809    
  temperature        2100.30  420.06     5   210 20.5199 <2e-16 ***
  recipe:temperature  205.98   20.60    10   210  1.0062 0.4393    
  ---
  Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
  > (an2 <- anova(m, type=2))
  Type II Analysis of Variance Table with Satterthwaite's method
                      Sum Sq Mean Sq NumDF DenDF F value Pr(>F)    
  recipe               10.19    5.09     2    42  0.2488 0.7809    
  temperature        2100.30  420.06     5   210 20.5199 <2e-16 ***
  recipe:temperature  205.98   20.60    10   210  1.0062 0.4393    
  ---
  Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
  > # Type 3 is also available with ordered factors:
  > (an3 <- anova(m, type=3))
  Type III Analysis of Variance Table with Satterthwaite's method
                      Sum Sq Mean Sq NumDF DenDF F value Pr(>F)    
  recipe               10.19    5.09     2    42  0.2488 0.7809    
  temperature        2100.30  420.06     5   210 20.5199 <2e-16 ***
  recipe:temperature  205.98   20.60    10   210  1.0062 0.4393    
  ---
  Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
  > ## Balanced data and only factors: Type I, II and III should be the same:
  > stopifnot(
  +   isTRUE(all.equal(an1, an2, check.attributes=FALSE, tolerance=TOL)),
  +   isTRUE(all.equal(an1, an3, check.attributes=FALSE, tolerance=TOL))
  + )
  > 
  > (an <- anova(m, type=1))
  Type I Analysis of Variance Table with Satterthwaite's method
                      Sum Sq Mean Sq NumDF DenDF F value Pr(>F)    
  recipe               10.19    5.09     2    42  0.2488 0.7809    
  temperature        2100.30  420.06     5   210 20.5199 <2e-16 ***
  recipe:temperature  205.98   20.60    10   210  1.0062 0.4393    
  ---
  Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
  > (an_lme4 <- anova(m, type=1, ddf="lme4"))
  Analysis of Variance Table
                     npar  Sum Sq Mean Sq F value
  recipe                2   10.19    5.09  0.2488
  temperature           5 2100.30  420.06 20.5199
  recipe:temperature   10  205.98   20.60  1.0062
  > res <- all.equal(an[, c("Sum Sq", "Mean Sq", "F value")],
  +                  an_lme4[, c("Sum Sq", "Mean Sq", "F value")], tolerance=TOL)
  > stopifnot(isTRUE(res))
  > if(has_pbkrtest) {
  +   (an_KR <- anova(m, type=1, ddf="Kenward-Roger"))
  +   res <- all.equal(an[, c("Sum Sq", "Mean Sq", "F value")],
  +                    an_KR[, c("Sum Sq", "Mean Sq", "F value")], tolerance=TOL)
  +   stopifnot(isTRUE(res))
  + }
  > stopifnot(all.equal(c(2, 5, 10), an$NumDF, tolerance=TOL),
  +           all.equal(c(42, 210, 210), an$DenDF, tolerance=TOL))
  > 
  > ########
  > ## Make case with balanced unordered factors:
  > cake2 <- cake
  > cake2$temperature <- factor(cake2$temperature, ordered = FALSE)
  > # str(cake2)
  > stopifnot(
  +   !is.ordered(cake2$temperature)
  + )
  > m <- lmer(angle ~ recipe * temperature + (1|recipe:replicate), cake2)
  > (an1 <- anova(m, type=1))
  Type I Analysis of Variance Table with Satterthwaite's method
                      Sum Sq Mean Sq NumDF DenDF F value Pr(>F)    
  recipe               10.19    5.09     2    42  0.2488 0.7809    
  temperature        2100.30  420.06     5   210 20.5199 <2e-16 ***
  recipe:temperature  205.98   20.60    10   210  1.0062 0.4393    
  ---
  Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
  > (an2 <- anova(m, type=2))
  Type II Analysis of Variance Table with Satterthwaite's method
                      Sum Sq Mean Sq NumDF DenDF F value Pr(>F)    
  recipe               10.19    5.09     2    42  0.2488 0.7809    
  temperature        2100.30  420.06     5   210 20.5199 <2e-16 ***
  recipe:temperature  205.98   20.60    10   210  1.0062 0.4393    
  ---
  Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
  > (an3 <- anova(m, type=3))
  Type III Analysis of Variance Table with Satterthwaite's method
                      Sum Sq Mean Sq NumDF DenDF F value Pr(>F)    
  recipe               10.19    5.09     2    42  0.2488 0.7809    
  temperature        2100.30  420.06     5   210 20.5199 <2e-16 ***
  recipe:temperature  205.98   20.60    10   210  1.0062 0.4393    
  ---
  Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
  > ## Balanced data and only factors: Type I, II, and III should be the same:
  > stopifnot(
  +   isTRUE(all.equal(an1, an2, check.attributes=FALSE, tolerance=TOL)),
  +   isTRUE(all.equal(an3, an2, check.attributes=FALSE, tolerance=TOL))
  + )
  > ########
  > 
  > # No intercept:
  > m <- lmer(angle ~ 0 + recipe * temp + (1|recipe:replicate), cake)
  > (an <- anova(m, type=1))
  Type I Analysis of Variance Table with Satterthwaite's method
               Sum Sq Mean Sq NumDF DenDF  F value Pr(>F)    
  recipe      21442.9  7147.6     3    42 342.2200 <2e-16 ***
  temp         1966.7  1966.7     1   222  94.1632 <2e-16 ***
  recipe:temp     1.7     0.9     2   222   0.0417 0.9592    
  ---
  Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
  > (an2 <- anova(m, type=2))
  Type II Analysis of Variance Table with Satterthwaite's method
               Sum Sq Mean Sq NumDF  DenDF F value Pr(>F)    
  recipe         4.48    1.49     3 254.02  0.0714 0.9752    
  temp        1966.71 1966.71     1 222.00 94.1632 <2e-16 ***
  recipe:temp    1.74    0.87     2 222.00  0.0417 0.9592    
  ---
  Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
  > (an2 <- anova(m, type=3))
  Type III Analysis of Variance Table with Satterthwaite's method
               Sum Sq Mean Sq NumDF  DenDF F value Pr(>F)    
  recipe         4.48    1.49     3 254.02  0.0714 0.9752    
  temp        1966.71 1966.71     1 222.00 94.1632 <2e-16 ***
  recipe:temp    1.74    0.87     2 222.00  0.0417 0.9592    
  ---
  Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
  > if(has_pbkrtest)
  +   (an_KR <- anova(m, ddf="Kenward-Roger"))
  Type III Analysis of Variance Table with Kenward-Roger's method
               Sum Sq Mean Sq NumDF  DenDF F value Pr(>F)    
  recipe         4.48    1.49     3 254.02  0.0714 0.9752    
  temp        1966.71 1966.71     1 222.00 94.1632 <2e-16 ***
  recipe:temp    1.74    0.87     2 222.00  0.0417 0.9592    
  ---
  Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
  > (an_lme4 <- anova(m, ddf="lme4"))
  Analysis of Variance Table
              npar  Sum Sq Mean Sq  F value
  recipe         3 21442.9  7147.6 342.2200
  temp           1  1966.7  1966.7  94.1632
  recipe:temp    2     1.7     0.9   0.0417
  > res <- all.equal(an[, c("Sum Sq", "Mean Sq", "F value")],
  +                  an_lme4[, c("Sum Sq", "Mean Sq", "F value")], tolerance=TOL)
  > stopifnot(isTRUE(res))
  > 
  > # ML-fit:
  > m <- lmer(angle ~ recipe * temp + (1|recipe:replicate), cake, REML=FALSE)
  > (an <- anova(m, type=1))
  Type I Analysis of Variance Table with Satterthwaite's method
               Sum Sq Mean Sq NumDF DenDF F value Pr(>F)    
  recipe        10.99    5.49     2    45  0.2666 0.7672    
  temp        1966.71 1966.71     1   225 95.4357 <2e-16 ***
  recipe:temp    1.74    0.87     2   225  0.0423 0.9586    
  ---
  Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
  > if(has_pbkrtest)
  +   assertError(an <- anova(m, ddf="Kenward-Roger")) # KR fits should be REML
  > (an_lme4 <- anova(m, ddf="lme4"))
  Analysis of Variance Table
              npar  Sum Sq Mean Sq F value
  recipe         2   10.99    5.49  0.2666
  temp           1 1966.71 1966.71 95.4357
  recipe:temp    2    1.74    0.87  0.0423
  > res <- all.equal(an[, c("Sum Sq", "Mean Sq", "F value")],
  +                  an_lme4[, c("Sum Sq", "Mean Sq", "F value")], tolerance=TOL)
  > stopifnot(isTRUE(res))
  > 
  > ####################################
  > ## Using contr.sum:
  > ####################################
  > 
  > m <- lmer(angle ~ recipe * temp + (1|recipe:replicate), cake,
  +           contrasts = list('recipe' = "contr.sum"))
  > (an <- anova(m, type=1))
  Type I Analysis of Variance Table with Satterthwaite's method
               Sum Sq Mean Sq NumDF DenDF F value Pr(>F)    
  recipe        10.39    5.20     2    42  0.2488 0.7809    
  temp        1966.71 1966.71     1   222 94.1632 <2e-16 ***
  recipe:temp    1.74    0.87     2   222  0.0417 0.9592    
  ---
  Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
  > (an2 <- anova(m, type=2))
  Type II Analysis of Variance Table with Satterthwaite's method
               Sum Sq Mean Sq NumDF  DenDF F value Pr(>F)    
  recipe         4.00    2.00     2 254.02  0.0957 0.9088    
  temp        1966.71 1966.71     1 222.00 94.1632 <2e-16 ***
  recipe:temp    1.74    0.87     2 222.00  0.0417 0.9592    
  ---
  Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
  > (an3 <- anova(m, type=3))
  Type III Analysis of Variance Table with Satterthwaite's method
               Sum Sq Mean Sq NumDF  DenDF F value Pr(>F)    
  recipe         4.00    2.00     2 254.02  0.0957 0.9088    
  temp        1966.71 1966.71     1 222.00 94.1632 <2e-16 ***
  recipe:temp    1.74    0.87     2 222.00  0.0417 0.9592    
  ---
  Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
  > stopifnot(
  +   isTRUE(all.equal(an2, an3, check.attributes=FALSE, tolerance=TOL))
  + )
  > if(has_pbkrtest)
  +   (an_KR <- anova(m, type=1, ddf="Kenward-Roger"))
  Type I Analysis of Variance Table with Kenward-Roger's method
               Sum Sq Mean Sq NumDF DenDF F value Pr(>F)    
  recipe        10.39    5.20     2    42  0.2488 0.7809    
  temp        1966.71 1966.71     1   222 94.1632 <2e-16 ***
  recipe:temp    1.74    0.87     2   222  0.0417 0.9592    
  ---
  Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
  > (an_lme4 <- anova(m, ddf="lme4"))
  Analysis of Variance Table
              npar  Sum Sq Mean Sq F value
  recipe         2   10.39    5.20  0.2488
  temp           1 1966.71 1966.71 94.1632
  recipe:temp    2    1.74    0.87  0.0417
  > res <- all.equal(an[, c("Sum Sq", "Mean Sq", "F value")],
  +                  an_lme4[, c("Sum Sq", "Mean Sq", "F value")], tolerance=TOL)
  > stopifnot(isTRUE(res))
  > 
  > 
  > ####################################
  > ## Example with continuous fixef:
  > ####################################
  > 
  > # Example with no fixef:
  > m <- lmer(Reaction ~ -1 + (Days | Subject), sleepstudy)
  > # m <- lmer(Reaction ~ 0 + (Days | Subject), sleepstudy) # alternative
  > stopifnot(length(fixef(m)) == 0L)
  > (an <- anova(m, type=1))
  Type I Analysis of Variance Table with Satterthwaite's method
       Sum Sq Mean Sq NumDF DenDF F value Pr(>F)
  > (an_2 <- anova(m, type=2))
  Type II Analysis of Variance Table with Satterthwaite's method
       Sum Sq Mean Sq NumDF DenDF F value Pr(>F)
  > (an_3 <- anova(m, type=3))
  Type III Analysis of Variance Table with Satterthwaite's method
       Sum Sq Mean Sq NumDF DenDF F value Pr(>F)
  > stopifnot(nrow(an) == 0L,
  +           nrow(an_2) == 0L,
  +           nrow(an_3) == 0L)
  > # anova(m, ddf="lme4") # Bug in lme4 it seems
  > if(has_pbkrtest) {
  +   (an_KR <- anova(m, ddf="Kenward-Roger"))
  +   stopifnot(
  +     nrow(an_KR) == 0L
  +   )
  + }
  > 
  > # Example with intercept only:
  > m <- lmer(Reaction ~ (Days | Subject), sleepstudy)
  > # m <- lmer(Reaction ~ 1 + (Days | Subject), sleepstudy) # alternative
  > stopifnot(length(fixef(m)) == 1L,
  +           names(fixef(m)) == "(Intercept)")
  > (an <- anova(m))
  Type III Analysis of Variance Table with Satterthwaite's method
       Sum Sq Mean Sq NumDF DenDF F value Pr(>F)
  > (an_2 <- anova(m, type=2))
  Type II Analysis of Variance Table with Satterthwaite's method
       Sum Sq Mean Sq NumDF DenDF F value Pr(>F)
  > (an_3 <- anova(m, type=3))
  Type III Analysis of Variance Table with Satterthwaite's method
       Sum Sq Mean Sq NumDF DenDF F value Pr(>F)
  > (an_lme4 <- anova(m, ddf="lme4"))
  Analysis of Variance Table
       npar Sum Sq Mean Sq F value
  > stopifnot(nrow(an) == 0L,
  +           nrow(an_2) == 0L,
  +           nrow(an_3) == 0L,
  +           nrow(an_lme4) == 0L)
  > if(has_pbkrtest) {
  +   (an_KR <- anova(m, ddf="Kenward-Roger"))
  +   stopifnot(
  +     nrow(an_KR) == 0L
  +   )
  + }
  > 
  > # Example with 1 fixef without intercept:
  > # for packageVersion("lme4") < 1.1.20
  > # mOld <- lmer(Reaction ~ Days - 1 + (Days | Subject), sleepstudy)
  > # for packageVersion("lme4") >= 1.1.20 we need to specify the old default
  > # optimizer to get the model to converge well enough.
  > m <- lmer(Reaction ~ Days - 1 + (Days | Subject), sleepstudy,
  +           control=lmerControl(optimizer="bobyqa"))
  > # m <- lmer(Reaction ~ 0 + Days + (Days | Subject), sleepstudy) # alternative
  > stopifnot(length(fixef(m)) == 1L,
  +           names(fixef(m)) == "Days")
  > (an <- anova(m))
  Type III Analysis of Variance Table with Satterthwaite's method
       Sum Sq Mean Sq NumDF DenDF F value    Pr(>F)    
  Days  93564   93564     1    17  142.86 1.069e-09 ***
  ---
  Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
  > (an_2 <- anova(m, type=2))
  Type II Analysis of Variance Table with Satterthwaite's method
       Sum Sq Mean Sq NumDF DenDF F value    Pr(>F)    
  Days  93564   93564     1    17  142.86 1.069e-09 ***
  ---
  Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
  > (an_3 <- anova(m, type=3))
  Type III Analysis of Variance Table with Satterthwaite's method
       Sum Sq Mean Sq NumDF DenDF F value    Pr(>F)    
  Days  93564   93564     1    17  142.86 1.069e-09 ***
  ---
  Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
  > (an_lme4 <- anova(m, ddf="lme4"))
  Analysis of Variance Table
       npar Sum Sq Mean Sq F value
  Days    1  93564   93564  142.86
  > stopifnot(nrow(an) == 1L,
  +           nrow(an_2) == 1L,
  +           nrow(an_3) == 1L,
  +           nrow(an_lme4) == 1L)
  > if(has_pbkrtest) {
  +   (an_KR <- anova(m, ddf="Kenward-Roger"))
  +   stopifnot(
  +     nrow(an_KR) == 1L
  +   )
  + }
  > 
  > res <- all.equal(an[, c("Sum Sq", "Mean Sq", "F value")],
  +                  an_lme4[, c("Sum Sq", "Mean Sq", "F value")], tolerance=TOL)
  > stopifnot(isTRUE(res))
  > stopifnot(isTRUE(all.equal(
  +   c(1, 17), unname(unlist(an[, c("NumDF", "DenDF")])), tolerance=TOL
  + )))
  > 
  > # Example with >1 fixef without intercept:
  > m <- lmer(Reaction ~ Days - 1 + I(Days^2) + (Days | Subject), sleepstudy)
  > stopifnot(length(fixef(m)) == 2L,
  +           names(fixef(m)) == c("Days", "I(Days^2)"))
  > (an <- anova(m))
  Type III Analysis of Variance Table with Satterthwaite's method
             Sum Sq Mean Sq NumDF   DenDF F value   Pr(>F)    
  Days      22127.2 22127.2     1  69.048 33.9280 1.65e-07 ***
  I(Days^2)   744.7   744.7     1 143.679  1.1419    0.287    
  ---
  Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
  > (an_2 <- anova(m, type=2))
  Type II Analysis of Variance Table with Satterthwaite's method
             Sum Sq Mean Sq NumDF   DenDF F value   Pr(>F)    
  Days      22127.2 22127.2     1  69.048 33.9280 1.65e-07 ***
  I(Days^2)   744.7   744.7     1 143.679  1.1419    0.287    
  ---
  Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
  > (an_3 <- anova(m, type=3))
  Type III Analysis of Variance Table with Satterthwaite's method
             Sum Sq Mean Sq NumDF   DenDF F value   Pr(>F)    
  Days      22127.2 22127.2     1  69.048 33.9280 1.65e-07 ***
  I(Days^2)   744.7   744.7     1 143.679  1.1419    0.287    
  ---
  Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
  > (an_lme4 <- anova(m, ddf="lme4"))
  Analysis of Variance Table
            npar Sum Sq Mean Sq  F value
  Days         1  93129   93129 142.7959
  I(Days^2)    1    745     745   1.1419
  > stopifnot(nrow(an) == 2L,
  +           nrow(an_3) == 2L,
  +           nrow(an_lme4) == 2L)
  > if(has_pbkrtest) {
  +   (an_KR <- anova(m, ddf="Kenward-Roger"))
  +   stopifnot(
  +     nrow(an_KR) == 2L
  +   )
  + }
  > # Here is a diff in SSQ which doesn't seem well-defined anyway...
  > # SSQ for I(Days^2) agree though.
  > # t-statistics also agree:
  > coef(summary(m))
             Estimate Std. Error        df  t value     Pr(>|t|)
  Days      15.884431  2.7270460  69.04781 5.824776 1.649534e-07
  I(Days^2)  0.279552  0.2616034 143.67926 1.068610 2.870375e-01
  > Lmat <- diag(length(fixef(m)))
  > lmerTest:::rbindall(lapply(1:nrow(Lmat), function(i) contest1D(m, Lmat[i, ])))
     Estimate Std. Error        df  t value     Pr(>|t|)
  1 15.884431  2.7270460  69.04781 5.824776 1.649534e-07
  2  0.279552  0.2616034 143.67926 1.068610 2.870375e-01
  > 
  > # Example with >1 fixef and intercept:
  > m <- lmer(Reaction ~ Days + I(Days^2) + (Days | Subject), sleepstudy)
  > stopifnot(length(fixef(m)) == 3L)
  > (an <- anova(m, type=1))
  Type I Analysis of Variance Table with Satterthwaite's method
             Sum Sq Mean Sq NumDF DenDF F value    Pr(>F)    
  Days      29894.8 29894.8     1    17 45.8530 3.264e-06 ***
  I(Days^2)  1079.5  1079.5     1   143  1.6558    0.2003    
  ---
  Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
  > (an_2 <- anova(m, type=2))
  Type II Analysis of Variance Table with Satterthwaite's method
            Sum Sq Mean Sq NumDF  DenDF F value  Pr(>F)   
  Days      4534.5  4534.5     1 114.43  6.9551 0.00952 **
  I(Days^2) 1079.5  1079.5     1 143.00  1.6558 0.20026   
  ---
  Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
  > (an_3 <- anova(m, type=3))
  Type III Analysis of Variance Table with Satterthwaite's method
            Sum Sq Mean Sq NumDF  DenDF F value  Pr(>F)   
  Days      4534.5  4534.5     1 114.43  6.9551 0.00952 **
  I(Days^2) 1079.5  1079.5     1 143.00  1.6558 0.20026   
  ---
  Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
  > (an_lme4 <- anova(m, ddf="lme4"))
  Analysis of Variance Table
            npar  Sum Sq Mean Sq F value
  Days         1 29894.8 29894.8 45.8530
  I(Days^2)    1  1079.5  1079.5  1.6558
  > res <- all.equal(an[, c("Sum Sq", "Mean Sq", "F value")],
  +                  an_lme4[, c("Sum Sq", "Mean Sq", "F value")], tolerance=TOL)
  > stopifnot(isTRUE(res))
  > 
  > if(has_pbkrtest) {
  +   (an_KR <- anova(m, ddf="Kenward-Roger"))
  +   res <- all.equal(an_3[, c("Sum Sq", "Mean Sq", "DenDF", "F value")],
  +                    an_KR[, c("Sum Sq", "Mean Sq", "DenDF", "F value")], tolerance=TOL)
  +   stopifnot(isTRUE(res))
  + }
  > 
  > ## FIXME: Test the use of refit arg to lme4:::anova.merMod
  > 
  > ##############################
  > # Test that type III anova is the same regardless of contrast coding:
  > # 3 x 3 factorial with missing diagonal
  > data("cake", package="lme4")
  > 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)
  'data.frame':	90 obs. of  5 variables:
   $ replicate  : Factor w/ 15 levels "1","2","3","4",..: 1 1 1 1 1 1 2 2 2 2 ...
   $ recipe     : Factor w/ 3 levels "A","B","C": 1 1 2 2 3 3 1 1 2 2 ...
   $ temperature: Factor w/ 3 levels "175","185","195": 2 3 1 3 1 2 2 3 1 3 ...
   $ angle      : int  46 47 39 51 46 44 29 35 35 47 ...
   $ temp       : num  185 195 175 195 175 185 185 195 175 195 ...
  > with(cake4, table(recipe, temperature))
        temperature
  recipe 175 185 195
       A   0  15  15
       B  15   0  15
       C  15  15   0
  > # load_all(r2path)
  > 
  > fm1 <- lmer(angle ~ recipe * temperature + (1|recipe:replicate), cake4)
  fixed-effect model matrix is rank deficient so dropping 3 columns / coefficients
  Error: Dropping columns failed to produce full column rank design matrix
  In addition: Warning message:
  In qr2rankMatrix(q.r, tol = tol, isBqr = x.dense, do.warn = warn.qr) :
    qr2rankMatrix(.): QR has negative diag(R) entries
  Execution halted
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
  Error: Dropping columns failed to produce full column rank design matrix
  In addition: Warning message:
  In qr2rankMatrix(q.r, tol = tol, isBqr = x.dense, do.warn = warn.qr) :
    qr2rankMatrix(.): QR has negative diag(R) entries
  Execution halted
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
  Error: Dropping columns failed to produce full column rank design matrix
  In addition: Warning message:
  In qr2rankMatrix(q.r, tol = tol, isBqr = x.dense, do.warn = warn.qr) :
    qr2rankMatrix(.): QR has negative diag(R) entries
  Execution halted
Running the tests in 'tests/test_drop1.R' failed.
Complete output:
  > # test_drop1.R
  > 
  > library(lmerTest)
  Loading required package: lme4
  Loading required package: Matrix
  
  Attaching package: 'lmerTest'
  
  The following object is masked from 'package:lme4':
  
      lmer
  
  The following object is masked from 'package:stats':
  
      step
  
  > 
  > # WRE says "using if(requireNamespace("pkgname")) is preferred, if possible."
  > # even in tests:
  > assertError <- function(expr, ...)
  +   if(requireNamespace("tools")) tools::assertError(expr, ...) else invisible()
  > assertWarning <- function(expr, ...)
  +   if(requireNamespace("tools")) tools::assertWarning(expr, ...) else invisible()
  > 
  > TOL <- 1e-4
  > # Kenward-Roger only available with pbkrtest and only then validated in R >= 3.3.3
  > # (faulty results for R < 3.3.3 may be due to unstated dependencies in pbkrtest)
  > has_pbkrtest <- requireNamespace("pbkrtest", quietly = TRUE) && getRversion() >= "3.3.3"
  > 
  > data("sleepstudy", package="lme4")
  > 
  > ######### Basic usage
  > 
  > data("cake", package="lme4")
  > cake2 <- cake
  > cake2$temperature <- factor(cake2$temperature, ordered = FALSE)
  > fm <- lmer(angle ~ recipe + temperature + (1|recipe:replicate), cake2)
  > (an1 <- drop1(fm))
  Single term deletions using Satterthwaite's method:
  
  Model:
  angle ~ recipe + temperature + (1 | recipe:replicate)
               Sum Sq Mean Sq NumDF DenDF F value Pr(>F)    
  recipe        10.19    5.09     2    42  0.2488 0.7809    
  temperature 2100.30  420.06     5   220 20.5141 <2e-16 ***
  ---
  Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
  > (an2 <- drop1(fm, force_get_contrasts = TRUE))
  Single term deletions using Satterthwaite's method:
  
  Model:
  angle ~ recipe + temperature + (1 | recipe:replicate)
               Sum Sq Mean Sq NumDF DenDF F value Pr(>F)    
  recipe        10.19    5.09     2    42  0.2488 0.7809    
  temperature 2100.30  420.06     5   220 20.5141 <2e-16 ***
  ---
  Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
  > drop1(fm, ddf="lme4", test="Chi")
  Single term deletions
  
  Model:
  angle ~ recipe + temperature + (1 | recipe:replicate)
              npar    AIC    LRT Pr(Chi)    
  <none>           1709.6                   
  recipe         2 1706.1  0.530  0.7672    
  temperature    5 1785.7 86.106  <2e-16 ***
  ---
  Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
  > if(has_pbkrtest)
  +   drop1(fm, ddf="Kenward-Roger")
  Single term deletions using Kenward-Roger's method:
  
  Model:
  angle ~ recipe + temperature + (1 | recipe:replicate)
               Sum Sq Mean Sq NumDF DenDF F value Pr(>F)    
  recipe        10.19    5.09     2    42  0.2488 0.7809    
  temperature 2100.30  420.06     5   220 20.5141 <2e-16 ***
  ---
  Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
  > 
  > tests1 <- show_tests(an1)
  > tests2 <- show_tests(an2)
  > 
  > stopifnot(
  +   # Tests are the same:
  +   isTRUE(all.equal(an1, an2, check.attributes = FALSE, tolerance=TOL)),
  +   # But contrast matrices are not:
  +   all(!mapply(function(x, y) isTRUE(all.equal(x, y)), tests1, tests2))
  + )
  > 
  > fm <- lmer(angle ~ recipe * temperature + (1|recipe:replicate), cake2)
  > drop1(fm)
  Single term deletions using Satterthwaite's method:
  
  Model:
  angle ~ recipe * temperature + (1 | recipe:replicate)
                     Sum Sq Mean Sq NumDF DenDF F value Pr(>F)
  recipe:temperature 205.98  20.598    10   210  1.0062 0.4393
  > drop1(fm, ddf="lme4")
  Single term deletions
  
  Model:
  angle ~ recipe * temperature + (1 | recipe:replicate)
                     npar    AIC
  <none>                  1719.0
  recipe:temperature   10 1709.6
  > if(has_pbkrtest)
  +   drop1(fm, ddf="Kenward-Roger")
  Single term deletions using Kenward-Roger's method:
  
  Model:
  angle ~ recipe * temperature + (1 | recipe:replicate)
                     Sum Sq Mean Sq NumDF DenDF F value Pr(>F)
  recipe:temperature 205.98  20.598    10   210  1.0062 0.4393
  > 
  > # Incorrect arguments:
  > assertError(drop1(fm, scope="recipe")) # Correct Error
  > assertError(drop1(fm, scope=3)) # Correct Error
  > assertError(drop1(fm, scope=list("recipe"))) # Correct Error
  > 
  > # Polynomial terms:
  > 
  > fm <- lmer(Reaction ~ 0 + (Days|Subject), sleepstudy)
  > (an0 <- drop1(fm)) # No fixef!
  Single term deletions using Satterthwaite's method:
  
  Model:
  Reaction ~ 0 + (Days | Subject)
       Sum Sq Mean Sq NumDF DenDF F value Pr(>F)
  > fm <- lmer(Reaction ~ Days + (Days|Subject), sleepstudy)
  > (an1 <- drop1(fm))
  Single term deletions using Satterthwaite's method:
  
  Model:
  Reaction ~ Days + (Days | Subject)
       Sum Sq Mean Sq NumDF DenDF F value    Pr(>F)    
  Days  30031   30031     1    17  45.853 3.264e-06 ***
  ---
  Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
  > fm <- lmer(Reaction ~ Days + I(Days^2) + (Days|Subject), sleepstudy)
  > (an2 <- (drop1(fm)))
  Single term deletions using Satterthwaite's method:
  
  Model:
  Reaction ~ Days + I(Days^2) + (Days | Subject)
            Sum Sq Mean Sq NumDF  DenDF F value  Pr(>F)   
  Days      4534.5  4534.5     1 114.43  6.9551 0.00952 **
  I(Days^2) 1079.5  1079.5     1 143.00  1.6558 0.20026   
  ---
  Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
  > fm <- lmer(Reaction ~ poly(Days, 2) + (Days|Subject), sleepstudy)
  > (an3 <- drop1(fm))
  Single term deletions using Satterthwaite's method:
  
  Model:
  Reaction ~ poly(Days, 2) + (Days | Subject)
                Sum Sq Mean Sq NumDF  DenDF F value    Pr(>F)    
  poly(Days, 2)  30974   15487     2 29.115  23.754 7.625e-07 ***
  ---
  Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
  > stopifnot(
  +   nrow(an0) == 0L,
  +   nrow(an1) == 1L,
  +   nrow(an2) == 2L,
  +   nrow(an3) == 1L
  + )
  > 
  > # Consider a rank-deficient design matrix:
  > fm <- lmer(angle ~ recipe + temp + temperature + (1|recipe:replicate), cake)
  fixed-effect model matrix is rank deficient so dropping 1 column / coefficient
  Error: Dropping columns failed to produce full column rank design matrix
  In addition: Warning message:
  In qr2rankMatrix(q.r, tol = tol, isBqr = x.dense, do.warn = warn.qr) :
    qr2rankMatrix(.): QR has negative diag(R) entries
  Execution halted
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
  Error: Dropping columns failed to produce full column rank design matrix
  In addition: Warning message:
  In qr2rankMatrix(q.r, tol = tol, isBqr = x.dense, do.warn = warn.qr) :
    qr2rankMatrix(.): QR has negative diag(R) entries
  Execution halted