2

有没有办法在 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。

4

3 回答 3

4

并不难;访问VarCorr器方法正是为了恢复这些信息而设计的。它比应该的要难一点,因为该VarCorr方法将方差协方差作为字符矩阵而不是数字返回(我storage.mode用来转换为数字而不丢失结构,并suppressWarnings忽略有关 NA 的警告)

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

于 2013-05-20T14:59:50.797 回答
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 
于 2013-05-20T14:00:50.733 回答
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"
于 2014-04-19T20:18:31.807 回答