Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DHARMa: residuals and predictor do not have the same length. #424

Closed
Murphorado opened this issue Jul 18, 2024 · 7 comments
Closed

DHARMa: residuals and predictor do not have the same length. #424

Murphorado opened this issue Jul 18, 2024 · 7 comments

Comments

@Murphorado
Copy link

Murphorado commented Jul 18, 2024

Hi Florian,

Hope you are well. After diagnosing the following linear mixed model with the DHARMa package:

fed_mod.cat.tree <- lmer(fed~best_site_avg_temps+deviations+log1p(caterpillars_per_tree)+site_avg_fbb+fbb_deviations+(1|year)+(1|site)+(1|ring), fed_best_window.cat.tree, REML=TRUE)

I got the following output:

Rplot

I don't think this indicates the model is hugely problematic, however I also wanted to plot the residuals against specific predictors from my dataset. When I attempt to do this - plotResiduals(simulationOutput, form = best_site_avg_temps) - I am met with the following error message:

Error in ensurePredictor(simulationOutput, form) :
DHARMa: residuals and predictor do not have the same length. The issue is possibly that you have NAs in your predictor that were removed during the model fit. Remove the NA values from your predictor.

I have removed all NA values from the "best_site_avg_temps" column within the "fed_best_window.cat.tree" dataset yet I am still getting the same error message. I am not sure why this is. Could it be something to do with the fact I have a nested experimental design, with different first egg dates (my response variable - fed) within the same site, therefore corresponding to the same value of "best_site_avg_temps"?

Any help would be much appreciated.

All the best,
Murphy

@Murphorado
Copy link
Author

Update: I averaged across "site_year" to check the nested design wasn't a problem. I got a much better model fit but I'm still getting the error message when I try to plot the residuals against the predictor variables.

@florianhartig
Copy link
Owner

Hello,

this is very likely due to the fact that the model will remove observations if there is an NA in ANY of the predictors, not just in your focal predictor.

The easiest way to get the right predictors is to to run

x = model.frame(fed_mod.cat.tree)

which should extract the data that is used on your model, and then use

x$best_site_avg_temps

as your predictor.

@Murphorado
Copy link
Author

Ok - thanks Florian!

@Kbhandari1904
Copy link

Kbhandari1904 commented Aug 2, 2024

I'm having the same issue, but it's trying to calculate the residuals for my fixed factors (Width, Sex and Season) My model looks like this:

par.full <- glm(Paramikrocytos ~ Width + Sex + Site/Season,
data = crab, family = "binomial")

I have no NA values in my dataset. I had no issues getting scaled quantile residuals, checking normality and zero inflation. This is what I try to run before the error message:
plotResiduals(Par.qr,
form = crab$Width,
xlab = "Rank-transformed Width")

@VanCamp-K
Copy link

Hi Kbhandari1904, have you tried implementing florianhartig's suggestion?

I had a similar problem as the original poster, which sounds similar to yours as well, and the suggested workaround worked great for me!

The easiest way to get the right predictors is to to run

x = model.frame(fed_mod.cat.tree)

which should extract the data that is used on your model, and then use

x$best_site_avg_temps

as your predictor.

For your model & parameters, this would translate to:

x = model.frame(par.full)

which should extract the data that is used on your model, and then use

x$Width

as your predictor.

ex: plotResiduals(par.full, form = x$Width)

Hope that helps!

@florianhartig
Copy link
Owner

Thanks @VanCamp-K for the explanations

This is obviously a work-around, I hope we will manage to automatise this for the user with one of the next releases (filed as #425).

@Kbhandari1904
Copy link

Thanks, it worked!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants