doserjef / spOccupancy

Single-species, Multi-species, and Integrated Spatial Occupancy Models
https://www.jeffdoser.com/files/spoccupancy-web/
GNU General Public License v3.0
49 stars 7 forks source link

spOccupancy without detection? #41

Closed devilevie closed 4 months ago

devilevie commented 4 months ago

I'm analysing the influence of climate variables on mammalian prey species distributions by using presence-absence data derived from prey range maps within predator ranges. I'd like to use a joint species distribution modelling approach and was drawn to spOccupancy as it can account for spatial autocorrelation. However, since I'm not using repeated surveys for the presence-absence data, detection would not be included in the model. Is this still a useful modelling approach?

doserjef commented 4 months ago

Hi @devilevie

Yes, spOccupancy has a function sfJSDM() that is designed exactly for what you're looking for: a spatially-explicit joint species distribution model without detection. This is described in the vignette here.

Jeff

devilevie commented 3 months ago

Thanks Jeff, that's exactly what I needed!

Cheers Evie

From: Jeff Doser @.> Sent: Wednesday, March 20, 2024 10:32 AM To: doserjef/spOccupancy @.> Cc: Jones, Evie @.>; Mention @.> Subject: Re: [doserjef/spOccupancy] spOccupancy without detection? (Issue #41)

Hi @devileviehttps://github.com/devilevie

Yes, spOccupancy has a function sfJSDM() that is designed exactly for what you're looking for: a spatially-explicit joint species distribution model without detection. This is described in the vignette herehttps://www.jeffdoser.com/files/spoccupancy-web/articles/factormodels.

Jeff

- Reply to this email directly, view it on GitHubhttps://github.com/doserjef/spOccupancy/issues/41#issuecomment-2009714768, or unsubscribehttps://github.com/notifications/unsubscribe-auth/BG7DXPNMRLXRNYF7G7ZKT6TYZGMUVAVCNFSM6AAAAABE7SNEGKVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDAMBZG4YTINZWHA. You are receiving this because you were mentioned.Message ID: @.**@.>>

devilevie commented 3 months ago

Hi Jeff,

I'm using the sfJSDM modelling approach and trying to assess model fit. Apparently the ppcOcc argument isn't set up for sfJSDM objects. Is there another way to assess model fit with sfJSDM (and not just compare models)?

Thanks for your help, Evie

From: Jones, Evie Sent: Thursday, March 21, 2024 3:45 PM To: doserjef/spOccupancy @.***> Subject: RE: [doserjef/spOccupancy] spOccupancy without detection? (Issue #41)

Thanks Jeff, that's exactly what I needed!

Cheers Evie

From: Jeff Doser @.**@.>> Sent: Wednesday, March 20, 2024 10:32 AM To: doserjef/spOccupancy @.**@.>> Cc: Jones, Evie @.**@.>>; Mention @.**@.>> Subject: Re: [doserjef/spOccupancy] spOccupancy without detection? (Issue #41)

Hi @devileviehttps://github.com/devilevie

Yes, spOccupancy has a function sfJSDM() that is designed exactly for what you're looking for: a spatially-explicit joint species distribution model without detection. This is described in the vignette herehttps://www.jeffdoser.com/files/spoccupancy-web/articles/factormodels.

Jeff

- Reply to this email directly, view it on GitHubhttps://github.com/doserjef/spOccupancy/issues/41#issuecomment-2009714768, or unsubscribehttps://github.com/notifications/unsubscribe-auth/BG7DXPNMRLXRNYF7G7ZKT6TYZGMUVAVCNFSM6AAAAABE7SNEGKVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDAMBZG4YTINZWHA. You are receiving this because you were mentioned.Message ID: @.**@.>>

doserjef commented 3 months ago

Hi @devilevie,

Unfortunately at the moment there is no built-in function in spOccupancy to do posterior predictive checks for sfJSDM model objects. I've attached a script ppcJSDM.R that contains a function to do a few different types of posterior predictive checks with sfJSDM (or lfJSDM) model objects. I've also attached a script that shows how to use it with a similar data set. You should be able to use the function for a simple posterior predictive check with your model, but let me know if you have any questions. Note that in order to attach the files on Github I had to change the file ending to ".txt", so after you download the files, resave them with a ".R" file ending instead of ".txt". I'm hoping at some point to get this functionality embedded in the package itself, just haven't gotten around to it.

Let me know if I can clarify anything.

Jeff

example-sfJSDM-ppcJSDM.txt ppcJSDM.txt

devilevie commented 3 months ago

Hi Jeff,

Thanks, that worked great. Is there any way to see the Bayesian p values for the individual species as well? The code only produces the p value for the community.

Best, Evie

From: Jeff Doser @.> Sent: Wednesday, March 27, 2024 6:53 AM To: doserjef/spOccupancy @.> Cc: Jones, Evie @.>; Mention @.> Subject: Re: [doserjef/spOccupancy] spOccupancy without detection? (Issue #41)

Hi @devileviehttps://github.com/devilevie,

Unfortunately at the moment there is no built-in function in spOccupancy to do posterior predictive checks for sfJSDM model objects. I've attached a script ppcJSDM.R that contains a function to do a few different types of posterior predictive checks with sfJSDM (or lfJSDM) model objects. I've also attached a script that shows how to use it with a similar data set. You should be able to use the function for a simple posterior predictive check with your model, but let me know if you have any questions. Note that in order to attach the files on Github I had to change the file ending to ".txt", so after you download the files, resave them with a ".R" file ending instead of ".txt". I'm hoping at some point to get this functionality embedded in the package itself, just haven't gotten around to it.

Let me know if I can clarify anything.

Jeff

example-sfJSDM-ppcJSDM.txthttps://github.com/doserjef/spOccupancy/files/14772152/example-sfJSDM-ppcJSDM.txt ppcJSDM.txthttps://github.com/doserjef/spOccupancy/files/14772153/ppcJSDM.txt

- Reply to this email directly, view it on GitHubhttps://github.com/doserjef/spOccupancy/issues/41#issuecomment-2022465502, or unsubscribehttps://github.com/notifications/unsubscribe-auth/BG7DXPOYXGGSDIMUMZ53WN3Y2KQITAVCNFSM6AAAAABE7SNEGKVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDAMRSGQ3DKNJQGI. You are receiving this because you were mentioned.Message ID: @.**@.>>

doserjef commented 3 months ago

Hi Evie,

It's not as straightforward to do that for sfJSDM compared to occupancy models since there is not an additional level of replication. I don't have an exact solution for you, but when using the ppcJSDM function I sent with group = 'species', then you can get somewhat of a sense of how good the model does for different species by looking at the fit.y.rep.group.quants and fit.y.group.quants components that come from the ppcJSDM object. Each one of those values corresponds to the fit statistic calculated for the different species. Take a look at this section in the intro vignette for some more details (there I grouped by sites, but here your grouping is by species).

Alternatively, you could generate some simple metrics for each species by comparing the total number of observations in a replicate data set compared to the true data set. You can extract a replicate data set using the fitted() function (see ?fitted.sfJSDM(). Then a simple comparison of how many detections the model predicts for a given species compared to what the actual data has could also give you an indication of how good the model is.

Jeff

devilevie commented 2 months ago

Hi Jeff,

Thanks for the tips. Hoping you may be able to help me with another issue. I ran a model with 2000 random points, 19 species, 7 predictors and 1 spatial factor and got it to converge (3 chains, 100000 samples per chain, burn-in 50000, thinning 5 = 30,000 posterior samples). However, when I ran the same model with a subset of these species (16) the model no longer converged. The spatial factor was the biggest problem, with an Rhat value of 10.5 and ESS of 22. I've been running into this issue with most iterations of the model. Any tips on how to fix this and improve convergence?

I've attached the output for the model that worked and the one that didn't. Scroll to the bottom for the results. Thanks again for your help.

Kind regards Evie

From: Jeff Doser @.> Sent: Thursday, March 28, 2024 5:12 PM To: doserjef/spOccupancy @.> Cc: Jones, Evie @.>; Mention @.> Subject: Re: [doserjef/spOccupancy] spOccupancy without detection? (Issue #41)

Hi Evie,

It's not as straightforward to do that for sfJSDM compared to occupancy models since there is not an additional level of replication. I don't have an exact solution for you, but when using the ppcJSDM function I sent with group = 'species', then you can get somewhat of a sense of how good the model does for different species by looking at the fit.y.rep.group.quants and fit.y.group.quants components that come from the ppcJSDM object. Each one of those values corresponds to the fit statistic calculated for the different species. Take a look at this section in the intro vignettehttps://www.jeffdoser.com/files/spoccupancy-web/articles/modelfitting#posterior-predictive-checks for some more details (there I grouped by sites, but here your grouping is by species).

Alternatively, you could generate some simple metrics for each species by comparing the total number of observations in a replicate data set compared to the true data set. You can extract a replicate data set using the fitted() function (see ?fitted.sfJSDM(). Then a simple comparison of how many detections the model predicts for a given species compared to what the actual data has could also give you an indication of how good the model is.

Jeff

- Reply to this email directly, view it on GitHubhttps://github.com/doserjef/spOccupancy/issues/41#issuecomment-2026135023, or unsubscribehttps://github.com/notifications/unsubscribe-auth/BG7DXPJGIMEKETRUCPOKWPLY2SBR5AVCNFSM6AAAAABE7SNEGKVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDAMRWGEZTKMBSGM. You are receiving this because you were mentioned.Message ID: @.**@.>>

doserjef commented 2 months ago

Hi Evie,

Thanks for the note. Convergence with these sorts of factor models can be tricky, as the models can often be very sensitive to the initial values used for each MCMC chain. Take a look at this vignette that talks about ways to try and improve convergence of spatial models in spOccupancy (with a focus on spatial factor models like the one you are trying to fit). You might also try specifying fix = TRUE inside of a list that you pass to the "inits" argument in sfJSDM, which will fix the initial values across all chains.

Hope that helps,

Jeff

devilevie commented 2 months ago

Hi Jeff,

Thanks for your help, fixing the initial values did the trick!

Cheers Evie

From: Jeff Doser @.> Sent: Tuesday, May 14, 2024 9:01 AM To: doserjef/spOccupancy @.> Cc: Jones, Evie @.>; Mention @.> Subject: Re: [doserjef/spOccupancy] spOccupancy without detection? (Issue #41)

Hi Evie,

Thanks for the note. Convergence with these sorts of factor models can be tricky, as the models can often be very sensitive to the initial values used for each MCMC chain. Take a look at this vignettehttps://www.jeffdoser.com/files/spoccupancy-web/articles/modelconsiderations that talks about ways to try and improve convergence of spatial models in spOccupancy (with a focus on spatial factor models like the one you are trying to fit). You might also try specifying fix = TRUE inside of a list that you pass to the "inits" argument in sfJSDM, which will fix the initial values across all chains.

Hope that helps,

Jeff

- Reply to this email directly, view it on GitHubhttps://github.com/doserjef/spOccupancy/issues/41#issuecomment-2110175086, or unsubscribehttps://github.com/notifications/unsubscribe-auth/BG7DXPJVWSGZIHITO2SPQOLZCIDHLAVCNFSM6AAAAABE7SNEGKVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDCMJQGE3TKMBYGY. You are receiving this because you were mentioned.Message ID: @.***>

devilevie commented 4 weeks ago

Hi Jeff,

I've been trying to use spOccupancy to for joint species distribution modelling to predict future species distributions with climate change. To do this, I used current climate variables at 2000 points to predict current distributions, then used the model to predict future distributions with future climate variables at the same locations (offset by +1 so the models would run, otherwise they say there are no new locations). The results basically show no change in distributions which shouldn't be the case. Your vignettes demonstrate the models work to predict current distributions at new locations, but not future distributions at the same locations. Do you have any recommendations for getting the models to work for future predictions?

Thanks for your help.

Best, Evie

From: Jones, Evie Sent: Monday, May 20, 2024 8:48 AM To: doserjef/spOccupancy @.***> Subject: RE: [doserjef/spOccupancy] spOccupancy without detection? (Issue #41)

Hi Jeff,

Thanks for your help, fixing the initial values did the trick!

Cheers Evie

From: Jeff Doser @.**@.>> Sent: Tuesday, May 14, 2024 9:01 AM To: doserjef/spOccupancy @.**@.>> Cc: Jones, Evie @.**@.>>; Mention @.**@.>> Subject: Re: [doserjef/spOccupancy] spOccupancy without detection? (Issue #41)

Hi Evie,

Thanks for the note. Convergence with these sorts of factor models can be tricky, as the models can often be very sensitive to the initial values used for each MCMC chain. Take a look at this vignettehttps://www.jeffdoser.com/files/spoccupancy-web/articles/modelconsiderations that talks about ways to try and improve convergence of spatial models in spOccupancy (with a focus on spatial factor models like the one you are trying to fit). You might also try specifying fix = TRUE inside of a list that you pass to the "inits" argument in sfJSDM, which will fix the initial values across all chains.

Hope that helps,

Jeff

- Reply to this email directly, view it on GitHubhttps://github.com/doserjef/spOccupancy/issues/41#issuecomment-2110175086, or unsubscribehttps://github.com/notifications/unsubscribe-auth/BG7DXPJVWSGZIHITO2SPQOLZCIDHLAVCNFSM6AAAAABE7SNEGKVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDCMJQGE3TKMBYGY. You are receiving this because you were mentioned.Message ID: @.**@.>>

doserjef commented 2 weeks ago

Hi @devilevie,

Very sorry for my delay. I'm just getting caught up on responding to things after being out of town for a couple weeks.

First thing I would suggest doing is re-installing spOccupancy from CRAN to make sure you have v0.7.6 of the package (the latest version). You can check this after you load the package in an R session by running sessionInfo(). Using that version of the package, you should be able to predict at the actual data locations (you shouldn't get the error saying there are no new locations) and so you won't have to do any sort of offset. If you supply new climate variables for forecasting in the X.0 portion of the predictions then everything should work according to the estimates of the model. If the variables weren't all that important (i.e., didn't have very large effect sizes) relative to the amount of residual spatial variation explained by the latent factors, then the model may just not be able to predict a lot of change. But, try doing the prediction again with v0.7.6 of the package, and if you're not seeing expected results then feel free to send me your code and I can try and take a look to see if there is anything else going on.

Jeff