gpt4 book ai didi

r - 提取 lmer 随机效应的置信区间;用 dotplot(ranef()) 绘制

转载 作者:行者123 更新时间:2023-12-04 14:47:14 24 4
gpt4 key购买 nike

我正在尝试提取用 dotplot(ranef()) 绘制的置信区间和截距值.我怎样才能做到这一点?

attach(sleepstudy)
library(lme4)
fm1 <- lmer(Reaction ~ Days + (Days | Subject), sleepstudy)
lattice::dotplot(ranef(fm1, condVar=TRUE))
enter image description here
我尝试探索列表对象 fm1但找不到 CI。

最佳答案

rr <- ranef(fm1)  ## condVar = TRUE has been the default for a while
as.data.frame :给出条件模式和 SD,您可以从中计算间隔(从技术上讲,这些不是“置信区间”,因为 BLUP/条件模式的值不是参数......)
dd <- as.data.frame(rr)
transform(dd, lwr = condval - 1.96*condsd, upr = condval + 1.96*condsd)
或与 broom.mixed::tidy :
broom.mixed::tidy(m1, effects = "ran_vals", conf.int = TRUE)
broom.mixed::tidy()用途 as.data.frame.ranef.mer() (由 as.data.frame 调用的方法)内部:该函数采用 ?lme4::ranef 中描述的相当复杂的数据结构并以更用户友好的格式提取条件模式和标准偏差:

If ‘condVar’ is ‘TRUE’ the ‘"postVar"’attribute is an array of dimension j by j by k (or a list of sucharrays). The kth face of this array is a positive definitesymmetric j by j matrix. If there is only one grouping factor inthe model the variance-covariance matrix for the entire randomeffects vector, conditional on the estimates of the modelparameters and on the data, will be block diagonal; this j by jmatrix is the kth diagonal block. With multiple grouping factorsthe faces of the ‘"postVar"’ attributes are still the diagonalblocks of this conditional variance-covariance matrix but thematrix itself is no longer block diagonal.


在这种特殊情况下,您需要执行以下操作来复制 condsd as.data.frame()的栏目:
## get the 'postVar' attribute of the first (and only) RE term
aa <- attr(rr$Subject, "postVar")
## for each slice of the array, extract the diagonal;
## transpose and drop dimensions;
## take the square root
sqrt(c(t(apply(aa, 3, diag))))

关于r - 提取 lmer 随机效应的置信区间;用 dotplot(ranef()) 绘制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69805532/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com