在nlme的lme中访问随机效应方差估计

时间:2013-05-20 12:52:10

标签: r statistics mixed-models

有没有办法在nlme包lme模型中获得随机项的方差?

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

换句话说,我希望得到3.468834。

3 个答案:

答案 0 :(得分:4)

那么困难; VarCorr访问器方法的设计正是为了恢复这些信息。由于VarCorr方法将方差 - 协方差作为字符矩阵而不是数字(我使用storage.mode转换为数字而不丢失结构,而suppressWarnings返回数值,因此它应该比应该更难一点,并且{{ 1}}忽略关于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 

在您的情况下,您将使用vc["t","StdDev"]

答案 1 :(得分:1)

这是用其中一种打印方法计算的(我怀疑是print.summary.pdMat)。最简单的方法是捕获输出。

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

这是计算它的方法:

fit$sigma * attr(corMatrix(fit$modelStruct[[1]])[[1]],"stdDev")
#(Intercept)         age 
#  7.3913363   0.6942889 

答案 2 :(得分:1)

> 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"