CompSocialSciR
CompSocialSciR

Reputation: 593

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.

Upvotes: 2

Views: 1186

Answers (2)

Daniel
Daniel

Reputation: 7822

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

Upvotes: 2

Gregor Thomas
Gregor Thomas

Reputation: 145755

I don't know anything about this sjPlot class, but it looks like it bundles a bunch of stuff to the plot in a list. plot_grid, grid.arrange and the like don't know how to deal with the extra stuff, but they can handle the ggplot part, which is called plot:

plot_grid(lapply(list(plot.1,plot.2,plot.3,plot.4), "[[", "plot"))

I am a bit surprised because it seems like at least some of the bundled information is duplicative. For example, plot.1$data has a small data frame which seems to be a copy (or subset?) of the data that is already bundled with the plot, plot.1$plot$data. Maybe it's more different and there's good reason for it in more complex cases.

Upvotes: 0

Related Questions