The documentation for nlme's functions anova.lme()
and anova.gls()
clearly states:
"When only one fitted model object is present, a data frame with the sums of squares,..." is being returned.
When running the example below, no sums of squares are printed:
library(nlme)
fm1 <- lme(distance ~ age, Orthodont, random = ~ 1 | Subject)
anova(fm1)
# numDF denDF F-value p-value
# (Intercept) 1 80 3126.1443 <.0001
# age 1 80 114.8383 <.0001
This looks like a bug to me. I believe it should be reported on the R bugzilla page but I don't seem to have access there.
Running the same model using aov()
results n the following:
summary(aov(distance ~ age + Error(Subject), Orthodont))
# Error: Subject
# Df Sum Sq Mean Sq F value Pr(>F)
# Residuals 26 518.4 19.94
# Error: Within
# Df Sum Sq Mean Sq F value Pr(>F)
# age 1 235.4 235.36 114.8 <2e-16 ***
# Residuals 80 164.0 2.05
This is a workaround for calculating the sums of squares for fixed effects and the residuals manually.
Based on @Drey 's comment, I tried to retrieve the sums of squares directly from the anova.lme()
function. The source code can be seen by executing anova.lme
. The relevant lines appear to be the one belows (for sequential and marginal sums of squares)
for (i in 1:nTerms) {
nDF[i] <- length(assign[[i]])
if (type == "sequential") {
c0i <- c0[assign[[i]]]
}
else {
c0i <- c(qr.qty(qr(vFix[, assign[[i]], drop = FALSE]),
c0))[1:nDF[i]]
}
Fval[i] <- sum(c0i^2)/nDF[i]
Pval[i] <- 1 - pf(Fval[i], nDF[i], dDF[i])
For the above model, c0i
= 55.9 for the first iteration and 10.7 for the second.
sum(c0i^2)
= 3126.1 for the first iteration (F-value of Intercept) and 114.8 for the second iteration (F-value for age).
The F-value for age can be calculated by dividing the Mean Sq of age (235.36) by the Mean Sq of the residuals (2.05), as shown in the table from aov()
. anova.lme
is doing something different here but I don't know what. Does someone know what's going on here and how to make it more consistent with the aov()
implementation?