# [R] Variance and Covariance Matrix D and R in nlme or lme4 part II [Please]

Alexandre Galvão Patriota alexandrepatrot at yahoo.com.br
Sat Nov 13 18:15:50 CET 2004

```Hi Douglas, I need to find the matrix V=ZDZ'+R, for
example:

require(nlme)
data(Orthodont)
attach(Orthodont)
fm1 <- lme(distance ~ age + Sex, data =
Orthodont,random=~age)
X<-model.matrix(distance ~ age + Sex)
Z<-model.matrix(distance ~ age + Subject -1)
D<-diag(ncol(Z))
cova<-VarCorr(fm1,rdig=7)
D[,1]<-D[1,]<-as.numeric(cova[2,3])*as.numeric(cova[1,2])*as.numeric(cova[2,2])
diag(D)<-c(as.numeric(cova[1,1]),rep(as.numeric(cova[2,1]),
27))
se2<-summary(fm1)\$sigma^2
V<-Z%*%D%*%t(Z) + se2*diag(108)

Is It right?? I compared a function vcov(fm1) with
solve(t(X)%*%solve(V)%*%X). Could you give me a hint
on why the two functions are giving incompatible
results?

--- Douglas Bates <bates at stat.wisc.edu> escreveu:
> Alexandre GalvÃ£o Patriota wrote:
> > The model is Y = XB + Zg + e
> >
> > where
> >
> > g~N(0, D)
> >
> > e~N(0, R)
> >
> > How to extract the VAR(g)= D, VAR(e)=R and
> V=ZDZ'+R?
> >
> > thanks
>
> The VarCorr function can provide D.
>
>

```