pbreheny / visreg

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

Issue with gamm4 not fixed by FAQ #91

Closed utooley closed 3 years ago

utooley commented 3 years ago

I think this is similar to #19 #25 but following the steps outlined did not help. Wondering if something changed or if I'm doing something wrong? I tried updating both gamm4 and visreg, just in case, but didn't help.

model<- gamm4(meas~s(age, k=4) + individual_ses_income_and_ed_t1 + rsfmri_c_ngd_meanmotion + sex, random = ~ (1 | mri_info_deviceserialnumber) + (1 | rel_family_id/ID), data=df_main_SES_filtered_training, method="REML"))

visreg(model$gam, "age")
Error in eval(inp, data, env) : object 'Default.Default' not found

model$gam$data <- df_main_SES_filtered_training
visreg(model$gam, "age")
Error in y + rr : non-conformable arrays
In addition: Warning message:
In Response(fit, x, trans, alpha, ...) :
  Residuals do not match data; have you changed the original data set?  If so, visreg is probably not displaying the residuals for the data set that was actually used to fit the model.
pbreheny commented 3 years ago

Kind of hard to diagnose without a reproducible example, but what happens if you call residuals(model)? Is it an n x 1 vector?

utooley commented 3 years ago

Thanks for responding! That put me on track to fixing one issue, which was that one of my grouping variables had NAs and variables were being dropped from gamm4, so residuals(model) wasn't the same size as model$gam$data.

I can't seem to replicate the issue with a reproducible example, though, which makes me think I'm overlooking something about my model/data. I got to the error below after fixing the NAs issue, but haven't had a lot of success googling why this might occur. I'd guess this is user error and I'll figure out out eventually, so feel free to close, but if this gives any hints as to the source, your insight would be appreciated!

model$gam$data <- correct_dataset
visreg(model$gam, "age")
Error in update.default(fit, formula = formula(fit), data = Data) : 
  need an object with call component
pbreheny commented 3 years ago

Well, one reason why gamm4 models are problematic is that they don't include the call, so methods like update(fit) don't work. The problem might be how sex is coded: is it a logical or character value? If so, try recoding it to a factor before fitting the gamm4 model (usually this conversion happens automatically, but the error message you're getting seems to indicate that gamm4 isn't giving R enough information to make this conversion automatic).