Skip to content

Commit

Permalink
change anova() header names (#528)
Browse files Browse the repository at this point in the history
  • Loading branch information
bbolker committed Jun 28, 2019
1 parent ab85cad commit 98d8c8a
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 15 deletions.
18 changes: 10 additions & 8 deletions R/lmer.R
Original file line number Diff line number Diff line change
Expand Up @@ -538,10 +538,10 @@ anovaLmer <- function(object, ..., refit = TRUE, model.names=NULL) {
## devs <- sapply(mods, deviance)
llks <- lapply(mods, logLik)
## Order models by increasing degrees of freedom:
ii <- order(Df <- vapply(llks, attr, FUN.VALUE=numeric(1), "df"))
ii <- order(npar <- vapply(llks, attr, FUN.VALUE=numeric(1), "df"))
mods <- mods[ii]
llks <- llks[ii]
Df <- Df [ii]
npar <- npar [ii]
calls <- lapply(mods, getCall)
data <- lapply(calls, `[[`, "data")
if(!all(vapply(data, identical, NA, data[[1]])))
Expand All @@ -554,14 +554,16 @@ anovaLmer <- function(object, ..., refit = TRUE, model.names=NULL) {
header <- c(header, paste("Subset:", abbrDeparse(subset[[1]])))
llk <- unlist(llks)
chisq <- 2 * pmax(0, c(NA, diff(llk)))
dfChisq <- c(NA, diff(Df))
val <- data.frame(Df = Df,
dfChisq <- c(NA, diff(npar))
val <- data.frame(npar = npar,
## afraid to swap in vapply here; wondering
## why .sapply was needed in the first place ...
AIC = .sapply(llks, AIC), # FIXME? vapply()
BIC = .sapply(llks, BIC), # " "
logLik = llk,
deviance = -2*llk,
Chisq = chisq,
"Chi Df" = dfChisq,
Df = dfChisq,
"Pr(>Chisq)" = pchisq(chisq, dfChisq, lower.tail = FALSE),
row.names = names(mods), check.names = FALSE)
class(val) <- c("anova", class(val))
Expand Down Expand Up @@ -594,8 +596,8 @@ anovaLmer <- function(object, ..., refit = TRUE, model.names=NULL) {
table <- data.frame(df, ss, ms, f)
dimnames(table) <-
list(nmeffects,
## c("Df", "Sum Sq", "Mean Sq", "Denom", "F value", "Pr(>F)"))
c("Df", "Sum Sq", "Mean Sq", "F value"))
## c("npar", "Sum Sq", "Mean Sq", "Denom", "F value", "Pr(>F)"))
c("npar", "Sum Sq", "Mean Sq", "F value"))
if ("(Intercept)" %in% nmeffects)
table <- table[-match("(Intercept)", nmeffects), ]
structure(table, heading = "Analysis of Variance Table",
Expand Down Expand Up @@ -832,7 +834,7 @@ drop1.merMod <- function(object, scope, scale = 0, test = c("none", "Chisq", "us
} else {
dfs <- ans[1L, 1L] - ans[, 1L]
dfs[1L] <- NA
aod <- data.frame(Df = dfs, AIC = ans[,2])
aod <- data.frame(npar = dfs, AIC = ans[,2])
if(test == "Chisq") {
## reconstruct deviance from AIC (ugh)
dev <- ans[, 2L] - k*ans[, 1L]
Expand Down
5 changes: 5 additions & 0 deletions inst/NEWS.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@
\item In \code{isSingular(x, tol)}, the default tolerance (\code{tol}) has
been increased from 1e-5 to \code{1e-4}, the default of
\code{check.conv.singular} in \code{g?lmerControl()}.

\item for clarity and consistency with base R methods,
changes in some column names of \code{anova()} output:
"Df" becomes "npar", "Chi Df" becomes "Df" (GH #528)

}
}
\subsection{BUG FIXES}{
Expand Down
8 changes: 4 additions & 4 deletions tests/glmer-1.R
Original file line number Diff line number Diff line change
Expand Up @@ -219,8 +219,8 @@ stopifnot(isTRUE(chkFixed(m0, true.coef = c(1,2))),

stopifnot(all.equal(a01$Chisq[2], 554.334056, tolerance=1e-5),
all.equal(a01$logLik, c(-1073.77193, -796.604902), tolerance=1e-6),
a01$ Df == 3:4,
a01$`Chi Df`[2] == 1)
a01$ npar == 3:4,
na.omit(a01$ Df) == 1)

if(lme4:::testLevel() > 1) {
nsim <- 10
Expand All @@ -233,8 +233,8 @@ if(lme4:::testLevel() > 1) {
m0 <- glmer(y~x + (1|f), family="poisson", data=dd)
m1 <- glmer(y~x + (1|f) + (1|obs), family="poisson", data=dd)
a01 <- anova(m0, m1)
stopifnot(a01$ Df == 3:4,
a01$`Chi Df`[2] == 1)
stopifnot(a01$ npar == 3:4,
a01$ Df == 1)
list(chk0 = chkFixed(m0, true.coef = c(1,2)),
chk1 = chkFixed(m1, true.coef = c(1,2)),
chisq= a01$Chisq[2],
Expand Down
2 changes: 1 addition & 1 deletion tests/lmer-1.Rout.save
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ Data: sleepstudy
Models:
fm2: Reaction ~ Days + (1 | Subject) + (0 + Days | Subject)
fm1: Reaction ~ Days + (Days | Subject)
Df AIC BIC logLik deviance Chisq Chi Df Pr(>Chisq)
npar AIC BIC logLik deviance Chisq Df Pr(>Chisq)
fm2 5 1762 1778 -876 1752
fm1 6 1764 1783 -876 1752 0.06 1 0.8
>
Expand Down
6 changes: 5 additions & 1 deletion tests/testthat/test-methods.R
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,11 @@ test_that("summary", {

context("anova")
test_that("lmer", {
expect_that(suppressMessages(anova(fm0,fm1)), is_a("anova"))
aa <- suppressMessages(anova(fm0,fm1))
expect_that(aa, is_a("anova"))
expect_equal(names(aa),
c("npar", "AIC", "BIC", "logLik", "deviance", "Chisq", "Df",
"Pr(>Chisq)"))
expect_warning(do.call(anova,list(fm0,fm1)), "assigning generic names")
##
dat <- data.frame(y = 1:5,
Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/test-rank.R
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ test_that("lmerRank", {
## should work:
expect_is(fm2 <- lmer( z ~ ab + (1|r), data=d2), "lmerMod")
expect_equal(logLik(fm), logLik(fm2))
expect_equal(sum(anova(fm)[, "Df"]), anova(fm2)[, "Df"])
expect_equal(sum(anova(fm)[, "npar"]), anova(fm2)[, "npar"])
expect_equal(sum(anova(fm)[, "Sum Sq"]), anova(fm2)[, "Sum Sq"])
})

Expand Down

0 comments on commit 98d8c8a

Please sign in to comment.