Skip to content

lmer / variance-covariance matrix random effects

2 messages · Roel de Jong, Spencer Graves

#
Hello,

has someone written by chance a function to extract the 
variance-covariance matrix from a lmer-object? I've noticed the VarCorr 
function, but it gives unhandy output.

Regards,
	Roel de Jong
20 days later
#
Have you received a reply to this post?  I haven't seen one.  I agree 
that VarCorr(lmer(...)) is "unhandy" if I want to do further 
computations with those numbers, which I often do.  The following solves 
that problem, at least for the example in the lmer VarCorr documentation:

fm2 <- lmer(Reaction ~ Days + (1|Subject) + (0+Days|Subject), sleepstudy))
vc.lmer <- VarCorr(fm2)
str(vc.lmer) # to understand the structure

vc.lmer <- function(obj, sep=":"){
   vc <- show(VarCorr(obj))
   nR <- dim(vc)[1]
   nC <- dim(vc)[2]
   vc. <- as.numeric(vc[, nC-(1:0)])
   colNms <- dimnames(vc)[[2]][nC-(1:0)]
   vcNames <- vc[,1]
   if(nC>3)for(i in 2:(nC-2))
     vcNames <- paste(vcNames, vc[,i], sep=sep)
   VC <- array(vc., dim=c(nR, 2),
     dimnames=list(vcNames, colNms))
   VC
}

(tst <- vc.lmer(fm2))
                     Variance Std.Dev.
Subject:(Intercept)  627.507  25.0501
Subject:Days          35.858   5.9882
Residual:            653.589  25.5654

	  Hope this helps.
	  spencer graves
Roel de Jong wrote: