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

Attempting to explore the spatial autocorrelation of residuals #57

Closed gpandradep closed 2 years ago

gpandradep commented 2 years ago

Hi Ken, I am interested in exploring the spatial patterns of residuals of some occupancy models. However, I am somewhat lost when it comes to understanding the matrix created by the residuals() function of an ubm object. I fit a model with covariates to a matrix with 30 sites and 7 survey occasions, all well so far. Once I try to extract the residuals with res <- residuals(fit_ubms, submodel= "state") I get a res matrix of [1:3000, 1:9]. This is where I'm confused, I don't know if the rows represent my 30 sites100 times, and I figure the columns will be the draws? When I extract the residuals for the detection submodel I now get a matrix of [1:3000, 1:63]. Am I correct in assuming that is my 30sites X 100 and my 7 survey occasionsX 9?

If so, to proceed to construct a Moran I correlogram plot I should use the mean of all repetitions per site, right?

An apology for how silly the question may be and thank you very much for your time.

kenkellner commented 2 years ago

The occupancy residuals matrix should be (number of total iterations) x (number of sites). I'm guessing you fit 3 chains of 2000 iterations (1000 warmup 1000 saved) each so the number of rows makes sense, but something seems wrong with the number of sites - ubms seems to think there are 9 instead of 30? The detection residuals also suggest ubms thinks there are 9 sites - 3000 total iterations x (number of sites number of occasions) would be 3000 x (79).

If you have missing values in site covariates you are using in your model, ubms may be dropping those sites. Alternatively there might be a bug here somewhere. Can you e-mail me the code/data to generate the model so I can look at it, or perhaps the fitted model as an Rds or Rdata?

gpandradep commented 2 years ago

Hi Ken, thanks for your reply. I was playing with the crossbill data and it works very well. After your explanation, it is very clear to me the structure of the residuals matrix.

In the example I mentioned to you I was using a subset of the habDat data from the stocc package you use in the vignette of the spatial occupancy models. There are indeed several sites with NAs that I did not look at (in total there are 9 sites left with some observation), so the function does well in assuming 9 sites. Sorry for the clumsiness, but thank you very much for your explanation it was very helpful to now apply it to my data and explore if there is a spatial structure or autocorrelation in the residuals.