From bates@stat.wisc.edu  Wed Nov 14 18:17:28 2001
From: bates@stat.wisc.edu (Douglas Bates)
Date: 14 Nov 2001 12:17:28 -0600
Subject: [Rd] lme: how to extract the variance components?
In-Reply-To: <DAV528MwHmLJRrtgMVN00013474@hotmail.com>
References: <DAV528MwHmLJRrtgMVN00013474@hotmail.com>
Message-ID: <6ry9l9ushz.fsf@franz.stat.wisc.edu>

"Jean Wu" <darmy16@hotmail.com> writes:

>       Dear all, 
>       Here is the question:
>       For example, using the "petrol" data offered with R.

I think you mean the "petrol" data from the MASS library for R.

>       pet3.lme<-lme(Y~SG+VP+V10+EP,random=~1|No,data=petrol)
>       pet3.lme$sigma gives the residual StdDev.
>       But I can't figure out how to extract the "(intercept) StdDev", 
>       although it is in the print out if I do "summary(pet3.lme)".

You can get a character representation of the number from VarCorr
> VarCorr(pet3.lme)
No = pdLogChol(1) 
            Variance StdDev  
(Intercept) 2.088107 1.445028
Residual    3.504931 1.872146
> VarCorr(pet3.lme)["(Intercept)", "Variance"]
[1] "2.088107"
> as.numeric(VarCorr(pet3.lme)["(Intercept)", "Variance"])
[1] 2.088107

A better but more complicated approach is to extract the reStruct
component of the modelStruct component of the fitted model and convert
convert to a matrix.

> str(as.matrix(pet3.lme$modelStruct$reStruct))
List of 1
 $ No: num 0.596
  ..- attr(*, "dimnames")=List of 2
  .. ..$ : chr "(Intercept)"
  .. ..$ : chr "(Intercept)"

The result is actually a list of matrices because we must allow for
cases of nested grouping factors.  The first (and only, in this case)
element of the list is the relative variance-covariance matrix of the
random effects for the first grouping factor.  To convert it to the
variance-covariance matrix you multiply by sigma^2.

> as.matrix(pet3.lme$modelStruct$reStruct)[[1]] * pet3.lme$sigma^2
            (Intercept)
(Intercept)    2.088107

That number prints the same as the previous value derived from VarCorr
but this extraction retains the full numerical precision because it
does not convert to character and back to numeric.

>       In S-plus3.4 , $var.ran is the estimate of the variance of the random 
>       effects between clusters. what is the equivalent thing to do in R?

That's from a very old version of the NLME package that uses different
computational methods from those used in NLME 3.0 and higher.
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-devel mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-devel-request@stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._


[Rd] lme: how to extract the variance components?
Douglas Bates