biodiverse / ubms

Fit models to data from unmarked animals using Stan. Uses a similar interface to the R package 'unmarked', while providing the advantages of Bayesian inference and allowing estimation of random effects.
https://hmecology.github.io/ubms/
GNU General Public License v3.0
35 stars 8 forks source link

Error in plot_residual on occupancy parameter #34

Closed gpandradep closed 3 years ago

gpandradep commented 3 years ago

Best regards Ken, First I wanted to congratulate and thank you for your work on unmarked as well as this new package. I am working with basicoccupancy models for camera trap data. I have 67 sites with 27 sampling events, 110 detections, 1038 no-detections and 661 NAs. Everything works perfect when fitting the models with ubms and I have very similar results to unmarked. My best model was this

(mg5_stan <- stan_occu(~ Slope~ 1, data=gf, chains=3, iter=600)) # Only 600 iterations for the purposes of the exercise.

I have no problem generating the residuals plots for the detection parameter

plot_residuals(mg5_stan, submodel="det")

plot_residuals(mg5_stan, submodel="det", covariate="Slope")

But the problem comes when I want to make the residuals diagnostic plots for the occupancy parameter

plot_residuals(mg5_stan, submodel="state") Error: Couldn't find working breakpoints

I am new to Bayesian approach and the Wright et al 2019 definition of residuals. which doesn't give me much clue as to what might be generating this error. is it a bug or am I missing something?

kenkellner commented 3 years ago

Since the response variable for the occupancy process is binary (0/1), regular residuals aren't very informative. Thus plot_residuals calculates binned residuals (see Gelman and Hill 2007 for more info). Binned residuals work by dividing up your datapoints into bins based on their fitted values, which in this case would be predicted occupancy for each datapoint/site. If you don't have any covariates in your occupancy model (as you don't), predicted occupancy will be the same for all sites. In that case it will be impossible to divide up the fitted values into logical bins since they all have identical values, and you get that error. Residuals are not likely to be a helpful diagnostic for the occupancy submodel in this case.