pbreheny / visreg

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

visreg error with lme input: unused argument (formula = formula(fit)) #97

Closed elslabbert closed 3 years ago

elslabbert commented 3 years ago

I would like to use the <visreg> package for a mixed effects model but have run into an error about unused formula argument that I is baffling me. I have read over the post on https://rdrr.io/cran/visreg/man/visreg-faq.html, and this ones special topics section for mixed models (https://pbreheny.github.io/visreg/gg.html), but still getting this error despite making various adjustments.

My model structure is as follows:

modMD_3 <- nlme::lme(mean_multidiversity ~ mass_fl_pa +
                       scale(C_mg.100g) + scale(K_mg.100g) +
                       scale(hillshade) +
                       scale(spring_cv_avgTemp),
                     random = ~1|Site, data = dat_full_set_merged)
visreg(fit = modMD_3, xvar = "C_mg.100g", type = "contrast", data = dat_full_set_merged)

This gives me the following error: Error in lme.formula(fixed = mean_multidiversity ~ mass_fl_pa + scale(C_mg.100g) + : unused argument (formula = formula(fit))

I checked the <visreg> code and don't see this error message specified there. So I suppose its from another embedded function, but I don't know where to start looking to resolved the issue.

Some advice on how to solve the issue would be much appreciated.

pbreheny commented 3 years ago

Sorry, I can't reproduce the error. The following works fine for me:

library(visreg)

X <- matrix(rnorm(100*3), 100, 3)
colnames(X) <- sprintf('V%d', 1:3)
DF <- data.frame(X, Site=factor(sample(LETTERS[1:5], 100, replace=TRUE)), y=rnorm(100))

fit <- nlme::lme(y ~ V1 + scale(V2) + scale(V3), random = ~1|Site, data=DF)
visreg(fit, 'V2', type='contrast')

Maybe you need to update your version of visreg?

elslabbert commented 3 years ago

Thanks for the feedback.

I back tracked and found that the lme model was not working properly - random effect was not properly accounted for. So perhaps the error was more related to that issue. Changing to the lme4 package fixed this issue and then visreg worked just fine. Thank you!