000andy8484 000andy8484 - 25 days ago 19
R Question

Plotting mixed models' regression coefficients in R

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
.

Answer

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))

enter image description here

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)

enter image description here