pbreheny/visreg

Overlaying mixed effects model results from different models / dataframes

Closed this issue · 7 comments

Goal

I want to directly compare visreg results from different models. visregList helps with this, but it is limited to making sure the models use the exact same group of variables.

  • I want to produce a plot like below, but where Time comes from two models with different numbers of variables.
    image

Explanation

this code works

For example, the following works, as both fit1 and fit2 have models that include Diet, Time, and Cow.

library(lme4, quietly=TRUE)
data(Milk, package="nlme")
ctrl <- lmerControl(optCtrl=list(xtol_rel=1e-6)) # Warning otherwise
fit1 <- lmer(protein ~ Diet + Time + (Time|Cow), data=Milk)
fit2 <- lmer(protein ~ Diet + poly(Time, 2) + (Time|Cow), data=Milk)

library(visreg)
v <- visregList(visreg(fit1, 'Time', plot=FALSE),
                 visreg(fit2, 'Time', plot=FALSE),
                 labels=c('Model 1', 'Model 2'),
                 collapse=TRUE)
plot(v, overlay=TRUE)

This presents an output visual like this:
image

this code does not

However, if let's say I don't include Diet in fit1. The visregList(..., collapse=TRUE) does not work because visreg(fit1) and visreg(fit2) have underlying dataframes that have differing number of columns. In other words, as fit1 didn't include Diet, it is dropped from the resultant data.

library(lme4, quietly=TRUE)
data(Milk, package="nlme")
ctrl <- lmerControl(optCtrl=list(xtol_rel=1e-6)) # Warning otherwise
fit1 <- lmer(protein ~ Time + (Time|Cow), data=Milk)
fit2 <- lmer(protein ~ Diet + poly(Time, 2) + (Time|Cow), data=Milk)

library(visreg)
v <- visregList(visreg(fit1, 'Time', plot=FALSE),
                visreg(fit2, 'Time', plot=FALSE),
                labels=c('Model 1', 'Model 2'),
                collapse=TRUE)
plot(v, overlay=TRUE)

image
image

The error is a simple rbind one.
image

I tried manually adding in a Diet column to fit1 (fit1@frame$Diet <- "Barley"), but this did not work.

I believe the most recent commit fixes this issue; can you try again and let me know if you are now able to do what you want?

visregList now works, but when I try plotting with ...overlay=TRUE), it still plots each one separately instead of combining onto one plot with different colors.

I'm not sure if the functionality of this should be using by= __ . I did try doing plot(v, by=v, overlay=TRUE) but that did not work.

Maybe I don't understand what you're trying to do, then. On my machine, the following code works and produces an overlaid plot with different colors for the two models, like you have above:

library(lme4, quietly=TRUE)
data(Milk, package="nlme")
fit1 <- lmer(protein ~ Time + (Time|Cow), data=Milk)
fit2 <- lmer(protein ~ Diet + poly(Time, 2) + (Time|Cow), data=Milk)

library(visreg)
v <- visregList(visreg(fit1, 'Time', plot=FALSE),
                visreg(fit2, 'Time', plot=FALSE),
                labels=c('Model 1', 'Model 2'),
                collapse=TRUE)
plot(v, overlay=TRUE)

Does this not work on your machine? Or it runs, but the output isn't what you're looking for?

Ah my bad, I had forgot to include labels= and collapse=. This works perfectly, thank you!

One last question: as I understand it, to change the colors of the lines, my best option would be to do gg=TRUE and alter using ggplot methods?

You can change colors as you would ordinarily, e.g.:

plot(v, overlay=TRUE, line.par=list(col=c('green', 'purple')))

Whether you use ggplot or not is up to you. Admittedly, this is slightly cumbersome, as you have to separately specify the colors for both points and lines. There is likely a more user-friendly way to do this, but that's how it works in the current version.

Gotcha, perfect. Thank you again.