【问题标题】:Extracting results from a `lmermod` object从“lmermod”对象中提取结果
【发布时间】:2014-12-17 18:33:31
【问题描述】:

我想从 lmermod 对象中提取一些结果

require(lme4)
(fm1 <- lmer(Yield ~ 1|Batch, Dyestuff))

这会产生

Linear mixed model fit by REML ['lmerMod']
Formula: Yield ~ 1 | Batch
   Data: Dyestuff
REML criterion at convergence: 319.6543
Random effects:
 Groups   Name        Std.Dev.
 Batch    (Intercept) 42.00   
 Residual             49.51   
Number of obs: 30, groups:  Batch, 6
Fixed Effects:
(Intercept)  
       1527 

特别是,我想提取 2 个随机效应的标准偏差,即 42.00 和 49.51。我想可能有一个内置的方法可以做到这一点,但我找不到它,所以我想我应该能够通过使用str(fm1) 找到它,它会产生这个:

Formal class 'lmerMod' [package "lme4"] with 13 slots
  ..@ resp   :Reference class 'lmerResp' [package "lme4"] with 9 fields
  .. ..$ Ptr    :<externalptr> 
  .. ..$ mu     : num [1:30] 1510 1510 1510 1510 1510 ...
  .. ..$ offset : num [1:30] 0 0 0 0 0 0 0 0 0 0 ...
  .. ..$ sqrtXwt: num [1:30] 1 1 1 1 1 1 1 1 1 1 ...
  .. ..$ sqrtrwt: num [1:30] 1 1 1 1 1 1 1 1 1 1 ...
  .. ..$ weights: num [1:30] 1 1 1 1 1 1 1 1 1 1 ...
  .. ..$ wtres  : num [1:30] 35.1 -69.9 -69.9 10.1 70.1 ...
  .. ..$ y      : num [1:30] 1545 1440 1440 1520 1580 ...
  .. ..$ REML   : int 1
  .. ..and 26 methods, of which 14 are possibly relevant:
  .. ..  allInfo, copy#envRefClass, initialize, initialize#lmResp,
  .. ..  initializePtr, initializePtr#lmResp, objective, ptr, ptr#lmResp,
  .. ..  setOffset, setResp, setWeights, updateMu, wrss
  ..@ Gp     : int [1:2] 0 6
  ..@ call   : language lmer(formula = Yield ~ 1 | Batch, data = Dyestuff)
  ..@ frame  :'data.frame':     30 obs. of  2 variables:
  .. ..$ Yield: num [1:30] 1545 1440 1440 1520 1580 ...
  .. ..$ Batch: Factor w/ 6 levels "A","B","C","D",..: 1 1 1 1 1 2 2 2 2 2 ...
  .. ..- attr(*, "terms")=Classes 'terms', 'formula' length 3 Yield ~ 1 + Batch
  .. .. .. ..- attr(*, "variables")= language list(Yield, Batch)
  .. .. .. ..- attr(*, "factors")= int [1:2, 1] 0 1
  .. .. .. .. ..- attr(*, "dimnames")=List of 2
  .. .. .. .. .. ..$ : chr [1:2] "Yield" "Batch"
  .. .. .. .. .. ..$ : chr "Batch"
  .. .. .. ..- attr(*, "term.labels")= chr "Batch"
  .. .. .. ..- attr(*, "order")= int 1
  .. .. .. ..- attr(*, "intercept")= int 1
  .. .. .. ..- attr(*, "response")= int 1
  .. .. .. ..- attr(*, ".Environment")=<environment: R_GlobalEnv> 
  .. .. .. ..- attr(*, "predvars")= language list(Yield, Batch)
  .. .. .. ..- attr(*, "dataClasses")= Named chr [1:2] "numeric" "factor"
  .. .. .. .. ..- attr(*, "names")= chr [1:2] "Yield" "Batch"
  .. .. .. ..- attr(*, "predvars.fixed")= language list(Yield)
  .. ..- attr(*, "formula")=Class 'formula' length 3 Yield ~ 1 | Batch
  .. .. .. ..- attr(*, ".Environment")=<environment: R_GlobalEnv> 
  ..@ flist  :List of 1
  .. ..$ Batch: Factor w/ 6 levels "A","B","C","D",..: 1 1 1 1 1 2 2 2 2 2 ...
  .. ..- attr(*, "assign")= int 1
  ..@ cnms   :List of 1
  .. ..$ Batch: chr "(Intercept)"
  ..@ lower  : num 0
  ..@ theta  : num 0.848
  ..@ beta   : num 1527
  ..@ u      : num [1:6] -20.755 0.461 33.669 -27.212 66.877 ...
  ..@ devcomp:List of 2
  .. ..$ cmp : Named num [1:10] 9.15 1.88 61495.41 9590.84 71086.25 ...
  .. .. ..- attr(*, "names")= chr [1:10] "ldL2" "ldRX2" "wrss" "ussq" ...
  .. ..$ dims: Named int [1:12] 30 30 1 29 1 6 1 1 0 1 ...
  .. .. ..- attr(*, "names")= chr [1:12] "N" "n" "p" "nmp" ...
  ..@ pp     :Reference class 'merPredD' [package "lme4"] with 18 fields
  .. ..$ Lambdat:Formal class 'dgCMatrix' [package "Matrix"] with 6 slots
  .. .. .. ..@ i       : int [1:6] 0 1 2 3 4 5
  .. .. .. ..@ p       : int [1:7] 0 1 2 3 4 5 6
  .. .. .. ..@ Dim     : int [1:2] 6 6
  .. .. .. ..@ Dimnames:List of 2
  .. .. .. .. ..$ : NULL
  .. .. .. .. ..$ : NULL
  .. .. .. ..@ x       : num [1:6] 0.848 0.848 0.848 0.848 0.848 ...
  .. .. .. ..@ factors : list()
  .. ..$ LamtUt :Formal class 'dgCMatrix' [package "Matrix"] with 6 slots
  .. .. .. ..@ i       : int [1:30] 0 0 0 0 0 1 1 1 1 1 ...
  .. .. .. ..@ p       : int [1:31] 0 1 2 3 4 5 6 7 8 9 ...
  .. .. .. ..@ Dim     : int [1:2] 6 30
  .. .. .. ..@ Dimnames:List of 2
  .. .. .. .. ..$ : NULL
  .. .. .. .. ..$ : NULL
  .. .. .. ..@ x       : num [1:30] 0.848 0.848 0.848 0.848 0.848 ...
  .. .. .. ..@ factors : list()
  .. ..$ Lind   : int [1:6] 1 1 1 1 1 1
  .. ..$ Ptr    :<externalptr> 
  .. ..$ RZX    : num [1:6, 1] 1.98 1.98 1.98 1.98 1.98 ...
  .. ..$ Ut     :Formal class 'dgCMatrix' [package "Matrix"] with 6 slots
  .. .. .. ..@ i       : int [1:30] 0 0 0 0 0 1 1 1 1 1 ...
  .. .. .. ..@ p       : int [1:31] 0 1 2 3 4 5 6 7 8 9 ...
  .. .. .. ..@ Dim     : int [1:2] 6 30
  .. .. .. ..@ Dimnames:List of 2
  .. .. .. .. ..$ : chr [1:6] "A" "B" "C" "D" ...
  .. .. .. .. ..$ : NULL
  .. .. .. ..@ x       : num [1:30] 1 1 1 1 1 1 1 1 1 1 ...
  .. .. .. ..@ factors : list()
  .. ..$ Utr    : num [1:6] 6384 6481 6634 6354 6787 ...
  .. ..$ V      : num [1:30, 1] 1 1 1 1 1 1 1 1 1 1 ...
  .. ..$ VtV    : num [1, 1] 30
  .. ..$ Vtr    : num 45825
  .. ..$ X      : num [1:30, 1] 1 1 1 1 1 1 1 1 1 1 ...
  .. .. ..- attr(*, "dimnames")=List of 2
  .. .. .. ..$ : chr [1:30] "1" "2" "3" "4" ...
  .. .. .. ..$ : chr "(Intercept)"
  .. .. ..- attr(*, "assign")= int 0
  .. ..$ Xwts   : num [1:30] 1 1 1 1 1 1 1 1 1 1 ...
  .. ..$ Zt     :Formal class 'dgCMatrix' [package "Matrix"] with 6 slots
  .. .. .. ..@ i       : int [1:30] 0 0 0 0 0 1 1 1 1 1 ...
  .. .. .. ..@ p       : int [1:31] 0 1 2 3 4 5 6 7 8 9 ...
  .. .. .. ..@ Dim     : int [1:2] 6 30
  .. .. .. ..@ Dimnames:List of 2
  .. .. .. .. ..$ : chr [1:6] "A" "B" "C" "D" ...
  .. .. .. .. ..$ : NULL
  .. .. .. ..@ x       : num [1:30] 1 1 1 1 1 1 1 1 1 1 ...
  .. .. .. ..@ factors : list()
  .. ..$ beta0  : num 0
  .. ..$ delb   : num 1527
  .. ..$ delu   : num [1:6] -20.755 0.461 33.669 -27.212 66.877 ...
  .. ..$ theta  : num 0.848
  .. ..$ u0     : num [1:6] 0 0 0 0 0 0
  .. ..and 42 methods, of which 30 are possibly relevant:
  .. ..  b, beta, CcNumer, copy#envRefClass, initialize, initializePtr,
  .. ..  installPars, L, ldL2, ldRX2, linPred, P, ptr, RX, RXdiag, RXi,
  .. ..  setBeta0, setDelb, setDelu, setTheta, solve, solveU, sqrL, u, unsc,
  .. ..  updateDecomp, updateL, updateLamtUt, updateRes, updateXwts
  ..@ optinfo:List of 7
  .. ..$ optimizer: chr "bobyqa"
  .. ..$ control  :List of 1
  .. .. ..$ iprint: int 0
  .. ..$ derivs   :List of 2
  .. .. ..$ gradient: num 1.61e-07
  .. .. ..$ Hessian : num [1, 1] 14.1
  .. ..$ conv     :List of 2
  .. .. ..$ opt : int 0
  .. .. ..$ lme4: list()
  .. ..$ feval    : int 16
  .. ..$ warnings : list()
  .. ..$ val      : num 0.848

...但这让我一点也不聪明。

【问题讨论】:

  • 您是否尝试过使用attributes(fm1)
  • @DavidArenburg 我没有,所以我只是尝试并找到第二项为fm1@devcomp$cmp[10],但我仍然找不到第一项
  • 班级拼写为lmerMod
  • attr(summary(fm1)$varcor, "sc"), attr(summary(fm1)$varcor$Batch, "stddev")

标签: r lme4


【解决方案1】:

lme4 提供VarCorr 用于提取方差和相关分量:

varcor <- VarCorr(fm1)
as.data.frame(varcor)
#       grp        var1 var2    vcov   sdcor
#1    Batch (Intercept) <NA> 1764.05 42.0006
#2 Residual        <NA> <NA> 2451.25 49.5101

【讨论】:

  • 如果 OP 可以建议他在文档中查看/期望找到此信息的位置,我可以尝试改进文档以便更容易找到...
  • 好吧。我只是(按顺序)查看?lmer,然后查看?'lmerMod-class' 的索引并尝试?summary.lmerMod(尽管最后一个不存在。)我也考虑过vcov,但此后没有去那里上下文表明这仅适用于固定效果。似乎我可能应该去那里,因为将来一些有用的注释者可能也会在那里放一个链接。
  • 我认为不方便的是class(fm1)lmerMod,但是你想要的方法是类VarCorr.merMod.当你做methods(class="lm")时,你会得到一堆有用的功能,但是methods(class="lmerMod") 什么也不返回。除非用户知道VarCorr 函数(通过阅读文档)或在查看summary(fm1)$varcor 后提取了summary(fm1)$varcor,否则用户很难找到他们想要的信息。去年早些时候遇到这个问题时,我使用了超级贫民窟capture.output 进入文本处理方法。编辑以反映lmerMod
  • @BenBolker:当一个人转到 stats::vcov 时(来自 lme4::merMod-class 的链接,似乎没有提到“merMod”类,即使有大量其他类. 也许更新该页面也是必要的。
  • @Vlo try methods(class='merMod') 虽然我不确定没有这个讨论的天真的用户(比如我自己)怎么会知道 lmerMod 是 merMod 的一个子集。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-12-14
  • 1970-01-01
  • 2021-02-26
  • 2016-06-24
相关资源
最近更新 更多