pbreheny / visreg

Visualization of regression functions
http://pbreheny.github.io/visreg/
61 stars 18 forks source link

Overlaying mixed effects model results from different models / dataframes #86

Closed mcgregorian1 closed 4 years ago

mcgregorian1 commented 4 years ago

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.

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.

pbreheny commented 4 years ago

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?

mcgregorian1 commented 4 years ago

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.

pbreheny commented 4 years ago

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?

mcgregorian1 commented 4 years ago

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

mcgregorian1 commented 4 years ago

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?

pbreheny commented 4 years ago

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.

mcgregorian1 commented 4 years ago

Gotcha, perfect. Thank you again.