LMER随机截距和斜率的自定义图

发布于 2025-01-21 23:56:12 字数 1486 浏览 4 评论 0 原文

我想找到一种方法来修改 didzis elferts提供的解决方案响应 to-mak"> thistist

我有一个具有随机截距和随机斜率的模型,而在原始问题中是关于仅随机截距的模型。我不知道如何从随机拦截斜率的答案中修改代码。

我之所以不使用 dotplot() sjplot 自动完成所有这些操作,是因为我想构造一个更自定义的绘图,并在一个随机的拦截和斜坡上绘制更有条理的方式,在反映随机组所属的固定效应组的方面。我看到能够做到这一点的唯一方法是提取随机斜率,截距及其差异,并将固定效应类别作为数据框中的其他列。因此,如果某人有一种简单的方法来自定义dotplot或其他类型的情节以按固定效果类别进行排序,那么我全都是耳朵!

我已经调整了可再现的代码以表明我的意思:

yestuff 数据集中添加一些变量,该变量将用作随机斜率变量

library("lme4")
Dyestuff$variable <- rep(c("X","Y"), each = 15)

运行随机截距和随机斜率模型(而不是只是随机截距模型)

fit1 <- lmer(Yield ~ variable + (variable|Batch), Dyestuff) 

该代码的其余部分与上一篇文章相同,除了我使用condvar = true的例外,因为自从提出了原始问题以来,后面已经贬值;但是请注意,该属性仍然需要命名为

randoms<-ranef(fit1, condVar = TRUE)
qq <- attr(ranef(fit1, condVar = TRUE)[[1]], "postVar")

str(qq)

rand.interc<-randoms$Batch



df<-data.frame(Intercepts=randoms$Batch[,1],
               sd.interc=2*sqrt(qq[,,1:length(qq)]),
               lev.names=rownames(rand.interc))

QQ,SD.Interc中随机截距和斜率不起作用的随机截距和斜率。我收到了此消息:

Error in qq[, , 1:length(qq)] : subscript out of bounds

我已经玩过它,但似乎无法正确。

I would like to find a way to modify the solution provided by Didzis Elferts in response to this post.

I have a model with a random intercept and random slope, whereas in the original question was about a model with just a random intercept. I can't figure out how to modify the code from the answer for random intercepts and slopes.

The reason I am not using dotplot() or sjPlot to do all this automatically is that I wanted to construct a more customized plot, with the random intercepts and slopes plotted in a more organized manner, under facets that reflect the fixed effect groups to which the random groups belong. The only way I see being able to do this is extracting the random slopes, intercepts, and their variances and having my fixed effect categories as the other columns in my data frame. So alternatively if someone has an easy way to customize a dotplot or other type of plot to sort by fixed effect categories, I'm all ears!

I've tweaked the reproducible code to show what I mean:

Add some variable to Dyestuff dataset, which will be used as the random slope variable

library("lme4")
Dyestuff$variable <- rep(c("X","Y"), each = 15)

Run a random intercept and random slope model (instead of just random intercept model)

fit1 <- lmer(Yield ~ variable + (variable|Batch), Dyestuff) 

the rest of the code is the same as in the previous post, with the exception that I used condVar=TRUE because postVar has been depreciated since the original question was asked; note though, that the attribute still needs to be named postVar

randoms<-ranef(fit1, condVar = TRUE)
qq <- attr(ranef(fit1, condVar = TRUE)[[1]], "postVar")

str(qq)

rand.interc<-randoms$Batch



df<-data.frame(Intercepts=randoms$Batch[,1],
               sd.interc=2*sqrt(qq[,,1:length(qq)]),
               lev.names=rownames(rand.interc))

with both random intercept and slope in qq, sd.interc doesn't work. I get this message:

Error in qq[, , 1:length(qq)] : subscript out of bounds

I've played around with it but can't seem to get it right.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

乱了心跳 2025-01-28 23:56:12

我认为,如果您想要截距的条件模式的标准偏差,则应使用 sqrt(qq [1,1,]);这将提取条件变化阵列的每个“切片”的(1,1)元素。

df <- data.frame(Intercepts=randoms$Batch[,1],
                 sd.interc=2*sqrt(qq[1,1,]),
                 lev.names=rownames(rand.interc))

(我不确定为什么您将这些值加倍,我想是将它们用作信心栏的半宽度的初步?)

但是,现在可以使用更轻松的工具。

broom.mixed::tidy(fit1, "ran_vals", conf.int = TRUE)

给出

# A tibble: 12 × 8
   effect   group level term        estimate std.error conf.low conf.high
   <chr>    <chr> <chr> <chr>          <dbl>     <dbl>    <dbl>     <dbl>
 1 ran_vals Batch A     (Intercept)  -12.2        14.8    -41.2     16.8 
 2 ran_vals Batch B     (Intercept)   -1.93       14.8    -30.9     27.0 
 3 ran_vals Batch C     (Intercept)   14.1        14.8    -14.9     43.1 
...

as.data.frame(ranef(fit1))(甚至内置在 lme4 中)

I think if you want the standard deviations of the conditional modes for the intercepts you should use sqrt(qq[1,1,]); this extracts the (1,1) element of each "slice" of the conditional-variance array.

df <- data.frame(Intercepts=randoms$Batch[,1],
                 sd.interc=2*sqrt(qq[1,1,]),
                 lev.names=rownames(rand.interc))

(I'm not sure why you doubled these values, I guess as a preliminary to using them as half-widths of a confidence bar?)

However, there are easier tools to use now.

broom.mixed::tidy(fit1, "ran_vals", conf.int = TRUE)

gives

# A tibble: 12 × 8
   effect   group level term        estimate std.error conf.low conf.high
   <chr>    <chr> <chr> <chr>          <dbl>     <dbl>    <dbl>     <dbl>
 1 ran_vals Batch A     (Intercept)  -12.2        14.8    -41.2     16.8 
 2 ran_vals Batch B     (Intercept)   -1.93       14.8    -30.9     27.0 
 3 ran_vals Batch C     (Intercept)   14.1        14.8    -14.9     43.1 
...

or as.data.frame(ranef(fit1)) (this is even built into lme4)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文