jeff-hughes / reghelper

R package with regression helper functions
5 stars 5 forks source link

Issue in simple_slopes with lme4 models when weights are used - object not found #8

Closed LukasWallrich closed 3 years ago

LukasWallrich commented 3 years ago

When I try to use simple_slopes on a lmer-model with weights, it fails with an object not found error. I think this is because the call inside the function contains the reference to the original weights variable, while mdata contains a (weights) variable instead.

Maybe all that is needed is this line in simple_slopes.merMod?

if(!is.null(call[["weights"]])) call[["weights"]] <- quote(`(weights)`)

Minimal example:

mtcars$svy_wt <- 1
mod <- lme4::lmer(mpg ~ wt*disp + (1|am), data = mtcars, weights = svy_wt)
simple_slopes(mod)

I just found the following workaround, but would be ideal if it worked with the original variable names.

mtcars$`(weights)` <- 1
mod <- lme4::lmer(mpg ~ wt*disp + (1|am), data = mtcars, weights = `(weights)`)
simple_slopes(mod)
jeff-hughes commented 3 years ago

Thanks for noting this. To be honest, I don't really use weights in my own research, so this was simply something I never looked into. But your proposed change seems to fix the issue. Looks like it also impacts lm models in the same way, but interestingly, not nlme models.

Anyway, I will take a look at see whether there are other places where weights may be an issue, and hopefully push an update within the next week or so. Thanks again!