'Residual variance extracted from glm and lmer in R

I am trying to take what I have read about multilevel modelling and merge it with what I know about glm in R. I am now using the height growth data from here.

I have done some coding shown below:

library(lme4)
library(ggplot2)

setwd("~/Documents/r_code/multilevel_modelling/")

rm(list=ls())

oxford.df <- read.fwf("oxboys/OXBOYS.DAT",widths=c(2,7,6,1))
names(oxford.df) <- c("stu_code","age_central","height","occasion_id")
oxford.df <- oxford.df[!is.na(oxford.df[,"age_central"]),]
oxford.df[,"stu_code"] <- factor(as.character(oxford.df[,"stu_code"]))
oxford.df[,"dummy"] <- 1

chart <- ggplot(data=oxford.df,aes(x=occasion_id,y=height))
chart <- chart + geom_point(aes(colour=stu_code))

# see if lm and glm give the same estimate
glm.01 <- lm(height~age_central+occasion_id,data=oxford.df)
glm.02 <- glm(height~age_central+occasion_id,data=oxford.df,family="gaussian")
summary(glm.02)
vcov(glm.02)
var(glm.02$residual)
(logLik(glm.01)*-2)-(logLik(glm.02)*-2)
1-pchisq(-2.273737e-13,1)
# lm and glm give the same estimation
# so glm.02 will be used from now on

# see if lmer without level2 variable give same result as glm.02
mlm.03 <- lmer(height~age_central+occasion_id+(1|dummy),data=oxford.df,REML=FALSE)
(logLik(glm.02)*-2)-(logLik(mlm.03)*-2)
# 1-pchisq(-3.408097e-07,1)
# glm.02 and mlm.03 give the same estimation, only if REML=FALSE

mlm.03 gives me the following output:

> mlm.03
Linear mixed model fit by maximum likelihood 
Formula: height ~ age_central + occasion_id + (1 | dummy) 
   Data: oxford.df 
  AIC  BIC logLik deviance REMLdev
 1650 1667 -819.9     1640    1633
Random effects:
 Groups   Name        Variance Std.Dev.
 dummy    (Intercept)  0.000   0.0000  
 Residual             64.712   8.0444  
Number of obs: 234, groups: dummy, 1

Fixed effects:
            Estimate Std. Error t value
(Intercept)  142.994     21.132   6.767
age_central    1.340     17.183   0.078
occasion_id    1.299      4.303   0.302

Correlation of Fixed Effects:
            (Intr) ag_cnt
age_central  0.999       
occasion_id -1.000 -0.999

You can see that there is a variance for the residual in the random effect section, which I have read from Applied Multilevel Analysis - A Practical Guide by Jos W.R. Twisk, that this represents the amount of "unexplained variance" from the model.

I wondered if I could arrive at the same residual variance from glm.02, so I tried the following:

> var(resid(glm.01))
[1] 64.98952
> sd(resid(glm.01))
[1] 8.061608

The results are slightly different from the mlm.03 output. Does this refer to the same "residual variance" stated in mlm.03?



Solution 1:[1]

Your glm.02 and glm.01 estimate a simple linear regression model using least squares. On the other hand, mlm.03 is a linear mixed model estimated through maximum likelihood. I don't know your dataset, but it looks like you use the dummy variable to create a cluster structure at level-2 with zero variance.

So your question has basically two answers, but only the second answer is important in your case. The models glm.02 and mlm.03 do not contain the same residual variance estimate, because...

  1. The models are usually different (mixed effects vs. classical regression). In your case, however, the dummy variable seems to supress the additional variance component in the mixed model. So for me the models seem to be equal.

  2. The method used to estimate the residual variance is different. glm uses LS, lmer uses ML in your code. ML estimates for the residual variance are slightly biased (resulting in smaller variance estimates). This can be solved by using REML instead of ML to estimate variance components.

Using classic ML (instead of REML), however, is still necessary and correct for the likelihood-ratio test. Using REML the comparison of the two likelihoods would not be correct.

Cheers!

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 SimonG