在R中绘制混合模型的回归系数 [英] Plotting mixed models' regression coefficients in R

查看:646
本文介绍了在R中绘制混合模型的回归系数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 sj.plot 包从四个非嵌套线性随机效应模型中尝试生成具有四个系数'图的网格很困难。
我没有嫁给这个包,所以随意建议其他路线( ggplot2 解决方案比 coefplot2 :: coefplot2

期望的输出:一个四个系数相邻的网格。

复制模型:

  data(sleepstudy)
sleepstudy $ var2 < - rnorm(n = nrow (sleepstudy),mean = 0,sd = 1)
sleepstudy $ var3 < - rnorm(n = nrow(sleepstudy),mean = 10,sd = 5)
M1 < - lmer (反应〜天+ var2 +(1 |主体),数据=睡眠研究,REML =假)$ b $(天〜+(1 |主体),数据= sleepstudy,REML =假)
M2 < (反应〜天+ var3 +(1 |主体),数据=睡眠研究,REML =假)
M4 < - lmer(反应〜天+ var2 + var3 + ,data = sleepstudy,REML = FALSE)

重现问题。尝试#1( sjp.lmm

 > sjp.lmm(M1,M2,M3,M4)
通过Kenward-Roger近似计算p值。如果计算时间过长,请使用`p.kr = FALSE`。
通过Kenward-Roger近似计算p值。如果计算时间过长,请使用`p.kr = FALSE`。
data.frame中的错误(beta,p = ps,pa = palpha,shape = pointshapes,grp = fitcnt,:
参数意味着不同的行数:3,2,1

重现问题。尝试#2( sjp.lmer + plot_grid

  plot.1 < -  sjp.lmer(fit = M1,type =fe.std,
p.kr = FALSE,
sort.est =sort.all,
y.offset = 0.4,
褪色。 ns = TRUE,
facet.grid = T)
plot.2 < - sjp.lmer(fit = M2,type =fe.std,
p.kr = FALSE,
sort.est =sort.all,
y.offset = 0.4,
fade.ns = TRUE,
facet.grid = T)
图。 3< - sjp.lmer(fit = M3,type =fe.std,
p.kr = FALSE,
sort.est =sort.all,
y。 offset = 0.4,
fade.ns = TRUE,
facet.grid = T)
plot.4< - sjp.lmer(fit = M4,type =fe.std,
p.kr = FALSE,
sort.est =sort.all,
y.offset = 0.4,
fade.ns = TRUE,
facet.grid = T)
plot_grid(list( plot.1,plot.2,plot.3,plot.4))

> plot_grid(list(plot.1,plot.2,plot.3,plot.4))
gList中的错误(list(wrapvp = list(x = 0.5,y = 0.5,width = 1,height = 1) ,:只有
在gList中允许使用'grobs'

有没有办法获得这个情节?
版本: [6] sjPlot_2.1.1 ggplot2_2.1.0 lme4_1.1-12 sjmisc_2.0.1 gridExtra_2.2.1 dplyr_0.5.0

解决方案

sjPlot函数的返回值返回这是一个数据框和plot-object,所以你必须在参数中访问plot-object:

  plot_grid(list (plot.1 $ plot,plot.2 $ plot,plot.3 $ plot,plot.4 $ plot))



编辑:
我看见你找到了错误在 sjp.lmm()<中/ code>函数并且可以修复它。如果您从GitHub下载最新的快照(


I am having hard time trying to produce a grid with four coefficients' plot from four non-nested linear random effects models using the sj.plot package. I am not married to this package so feel free to suggest other routes (ggplot2 solutions better than coefplot2::coefplot2).

Desired output: a grid with the four coefficients' plots next to each other.

Reproducing the models:

data("sleepstudy")
sleepstudy$var2 <- rnorm(n=nrow(sleepstudy), mean=0, sd=1)
sleepstudy$var3 <- rnorm(n=nrow(sleepstudy), mean=10, sd=5)
M1 <- lmer(Reaction ~ Days + (1|Subject), data=sleepstudy, REML = FALSE)
M2 <- lmer(Reaction ~ Days + var2 + (1|Subject), data=sleepstudy, REML = FALSE)
M3 <- lmer(Reaction ~ Days + var3 + (1|Subject), data=sleepstudy, REML = FALSE)
M4 <- lmer(Reaction ~ Days + var2 + var3 + (1|Subject), data=sleepstudy, REML = FALSE)

Reproducing the problems. Attempt #1 (sjp.lmm)

> sjp.lmm(M1, M2, M3, M4)
Computing p-values via Kenward-Roger approximation. Use `p.kr = FALSE` if computation takes too long.
Computing p-values via Kenward-Roger approximation. Use `p.kr = FALSE` if computation takes too long.
Error in data.frame(betas, p = ps, pa = palpha, shape = pointshapes, grp = fitcnt,  : 
  arguments imply differing number of rows: 3, 2, 1

Reproducing the problems. Attempt #2 (sjp.lmer + plot_grid)

plot.1 <- sjp.lmer(fit=M1,type="fe.std",
                   p.kr=FALSE,
                   sort.est = "sort.all",
                   y.offset = 0.4,
                   fade.ns = TRUE,
                   facet.grid = T)
plot.2 <- sjp.lmer(fit=M2,type="fe.std",
                   p.kr=FALSE,
                   sort.est = "sort.all",
                   y.offset = 0.4,
                   fade.ns = TRUE,
                   facet.grid = T)
plot.3 <- sjp.lmer(fit=M3,type="fe.std",
                   p.kr=FALSE,
                   sort.est = "sort.all",
                   y.offset = 0.4,
                   fade.ns = TRUE,
                   facet.grid = T)
plot.4 <- sjp.lmer(fit=M4,type="fe.std",
                   p.kr=FALSE,
                   sort.est = "sort.all",
                   y.offset = 0.4,
                   fade.ns = TRUE,
                   facet.grid = T)
plot_grid(list(plot.1,plot.2,plot.3,plot.4))

> plot_grid(list(plot.1,plot.2,plot.3,plot.4))
Error in gList(list(wrapvp = list(x = 0.5, y = 0.5, width = 1, height = 1,  : 
  only 'grobs' allowed in "gList"

Is there a way to obtain this plot? Versions: [6] sjPlot_2.1.1, ggplot2_2.1.0, lme4_1.1-12, sjmisc_2.0.1, gridExtra_2.2.1, dplyr_0.5.0.

解决方案

The return values of the sjPlot-functions return both a data frame and the plot-object, so you have to access the plot-object in the arguments:

plot_grid(list(plot.1$plot, plot.2$plot, plot.3$plot, plot.4$plot))

Edit: I saw you found a bug in the sjp.lmm() function and could fix it. If you download the latest snapshot from GitHub (https://github.com/sjPlot/devel), this will work:

sjp.lmm(M1, M2, M3, M4)

这篇关于在R中绘制混合模型的回归系数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆