1

After variable selection I usually end up in a model with a numerical covariable (2nd or 3rd degree). What I want to do is to plot using emmeans package preferentially. Is there a way of doing it?

I can do it using predict:

m1 <- lm(mpg ~ poly(disp,2), data = mtcars)
df <- cbind(disp = mtcars$disp, predict.lm(m1, interval = "confidence"))
df <- as.data.frame(df)

ggplot(data = df, aes(x = disp, y = fit)) +
    geom_line() +
  geom_ribbon(aes(ymin = lwr, ymax = upr, x = disp, y = fit),alpha = 0.2)

enter image description here

I didn't figured out a way of doing it using emmip neither emtrends

For illustration purposes, how could I do it using mixed models via lme?

m1 <- lme(mpg ~ poly(disp,2), random = ~1|factor(am), data = mtcars)
  • I don't understand your question. Is the plot the desired result? If yes, why is your code not sufficient? I've shown how to create confidence bands for `lme` in this answer: https://stackoverflow.com/questions/14358811/extract-prediction-band-from-lme-fit/14435982#14435982 – Roland Jan 18 '19 at 06:59
  • Yes! The plot is the desired result, but I don't want to make it using predict (and also because there is no direct way of doing via predict using `lme` as you have shown on your another response. Fortunately after some hours of searching, I found `sjPlot`package, which does the job. I am thinking about to post this as one possible answer so far. – Guilherme Parreira Jan 18 '19 at 17:03

2 Answers2

1

I suspect that your issue is due to the fact that by default, covariates are reduced to their means in emmeans. You can use theat or cov.reduce arguments to specify a larger number of values. See the documentation for ref_grid and vignette(“basics”, “emmeans”), or the index of vignette topics.

Russ Lenth
  • 5,922
  • 2
  • 13
  • 21
  • Thanks! I did it! `em1 <- ref_grid(m1, at = list(disp = seq(min(mtcars$disp), max(mtcars$disp), 1)))` `emmip(em1, ~disp, CIs = T)` With this code I can do what I want! – Guilherme Parreira Jan 21 '19 at 17:35
0

Using sjPlot:

plot_model(m1, terms = "disp [all]", type = "pred")

gives the same graphic.

Using emmeans:

em1 <- ref_grid(m1, at = list(disp = seq(min(mtcars$disp), max(mtcars$disp), 1)))
emmip(em1, ~disp, CIs = T)

returns a graphic with a small difference in layout. An alternative is to add the result to an object and plot as the way that I want to:

d1 <- emmip(em1, ~disp, CIs = T, plotit = F)