Reputation: 943
Is there any way of obtaining the variance of a random term in a nlme package lme model?
Random effects:
Formula: ~t | UID
Structure: General positive-definite, Log-Cholesky parametrization
StdDev Corr
(Intercept) 520.310397 (Intr)
t 3.468834 0.273
Residual 31.071987
In other words in the above, I would like to get at the 3.468834.
Upvotes: 2
Views: 4674
Reputation: 1306
> fit <- lme(distance ~ Sex, data = Orthodont, random = ~ age|Subject)
> getVarCov(fit)
Random effects variance covariance matrix
(Intercept) age
(Intercept) 54.6320 -4.97540
age -4.9754 0.48204
Standard Deviations: 7.3913 0.69429
> # In contrast to VarCorr(), this returns a numeric matrix:
> str(getVarCov(fit))
random.effects [1:2, 1:2] 54.632 -4.975 -4.975 0.482
- attr(*, "dimnames")=List of 2
..$ : chr [1:2] "(Intercept)" "age"
..$ : chr [1:2] "(Intercept)" "age"
- attr(*, "class")= chr [1:2] "random.effects" "VarCov"
- attr(*, "group.levels")= chr "Subject"
> unclass(getVarCov(fit))
(Intercept) age
(Intercept) 54.631852 -4.975417
age -4.975417 0.482037
attr(,"group.levels")
[1] "Subject"
Upvotes: 1
Reputation: 132576
This is calculated in one of the print methods (I suspect print.summary.pdMat
). The easiest way is to capture the output.
library(nlme)
fit <- lme(distance ~ Sex, data = Orthodont, random = ~ age|Subject)
summary(fit)
# Linear mixed-effects model fit by REML
# Data: Orthodont
# AIC BIC logLik
# 483.1635 499.1442 -235.5818
#
# Random effects:
# Formula: ~age | Subject
# Structure: General positive-definite, Log-Cholesky parametrization
# StdDev Corr
# (Intercept) 7.3913363 (Intr)
# age 0.6942889 -0.97
# Residual 1.3100396
# <snip/>
ttt <- capture.output(print(summary(fit$modelStruct), sigma = fit$sigma))
as.numeric(unlist(strsplit(ttt[[6]],"\\s+"))[[2]])
#[1] 0.6942889
And here is the way to calculate it:
fit$sigma * attr(corMatrix(fit$modelStruct[[1]])[[1]],"stdDev")
#(Intercept) age
# 7.3913363 0.6942889
Upvotes: 1
Reputation: 226027
This is not that difficult; the VarCorr
accessor method is designed precisely to recover this information. It's a little bit harder than it should be since the VarCorr
method returns the variance-covariance as a character matrix rather than as numeric (I use storage.mode
to convert to numeric without losing the structure, and suppressWarnings
to ignore the warnings about NAs)
library(nlme)
fit <- lme(distance ~ Sex, data = Orthodont, random = ~ age|Subject)
vc <- VarCorr(fit)
suppressWarnings(storage.mode(vc) <- "numeric")
vc[1:2,"StdDev"]
## (Intercept) age
## 7.3913363 0.6942889
In your case, you would use vc["t","StdDev"]
.
Upvotes: 4