Manuel Böhm
Manuel Böhm

Reputation: 1

How can I include data from a dataframe when plotting a lmer-model (in R)?

1 Time instead of Time4

I calculated a Multilevel Model and included (among other IVs), time4 as time^4. Now, I would like to plot the effect of time on my DV. However, I would like to include the variable "time" which was not included in the model. Basically, I only need to change the axis labels... The resulting graph should look like this one:

plot_model(rq1_model2, type = "pred", show.values = TRUE)

Perceived Learning predicted by time^4 (Graph, Picture)

I only want to include the fixed effects and keep the model simple.

How do I do that?

2 Adding a second graph to the plot

Additionally, I would like to include a second similar model in the same window / plot for comparison. This second graph should only be depicted for values of time from 0 onwards (there are negative values for time for the first value). Actually, the model only contains values for the time4-values referring to values of time from 0 onwards.

Thank you for any ideas in advance!

3 What I already tried

plot_model(rq1_model2, type = "pred", show.values = TRUE)

But I don't know how to include data from a data frame additional to the model.

I also tried including the variable time with "^4" instead of time4 (where I calculated the variable before). However, the result is not right.

My model:

rq1_model2 <- lmerTest::lmer(proc_learn ~ ... + time4 + (1| code),
                             data = data, REML = FALSE)

Another option would be to only include data from a data frame. But then I would still need to get the regression results and insert the variable time. I could see exporting the regression results into a data frame and adding the variable time. However, I don't know how to get to the regression graph.

A Minimal Reproducible Example (Thanks to @Ben Bolker)

library(lmerTest)
ss <- transform(sleepstudy, Days4 = Days^4)
m <- lmer(Reaction ~ Days4 + (1|Subject), ss) 

Upvotes: 0

Views: 75

Answers (1)

Ben Bolker
Ben Bolker

Reputation: 226741

I'm afraid this is rather complicated, but it might do what you want.

set up example, fit two models

library(lmerTest)
ss <- within(sleepstudy,
{
    Dayminus <- Days - 4
    Days4 <- Dayminus^4
})
m1 <- lmer(Reaction ~ Days4 + (1|Subject), ss)
m2 <- update(m1, subset = (Dayminus > 0))

set up new data frame for predictions

You can predict based on the original data, but this is better if your original data are sparse/out of order/etc.. You will also have to set any non-focal values in your model to their mean values (or whatever reference value you want to use).

library(ggplot2)
pframe <- with(ss,
               data.frame(Dayminus = seq(min(Dayminus), max(Dayminus),
                                         length.out = 101)))
pframe <- transform(pframe, Days4 = Dayminus^4)

make predictions; get SEs and construct CIs

Set up a utility function because we have to do the same thing twice.

mkpred <- function(m) {
    pp <- predict(m, newdata = pframe, se.fit = TRUE, re.form = NA)
    pframe <- with(pp,
                   cbind(pframe, ## uses external pframe
                         data.frame(pred = fit,
                                    lwr = fit - 1.96*se.fit,
                                    upr = fit + 1.96*se.fit)))
    return(pframe)
}
pred1frame <- mkpred(m1)
pred2frame <- mkpred(m2)
## restrict predictions for the second model to positive values of 'Dayminus'
pred2frame <- pred2frame[pred2frame$Dayminus>0,]

create the plot

Draw a line and a semi-transparent ribbon for each model. (If you have several models, or if you want to have an automatic legend, you need to row-bind the two data frames along with a factor variable which says model 1, model 2 ...)

ggplot(pred1frame, aes(Dayminus, pred)) +
    geom_line() +
    geom_line(data=pred2frame, col = "red") +
    geom_ribbon(aes(ymin=lwr, ymax = upr), alpha = 0.2, color=NA, fill = "black") +
    geom_ribbon(data =pred2frame,
                aes(ymin=lwr, ymax = upr), alpha = 0.2, color=NA, fill = "red") 

enter image description here

Upvotes: 0

Related Questions