biodiverse / spOccupancy

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

Issue running gaussian covariance model with spPGOcc (c++ error: dpotrf failed) #22

Closed KBrunk01 closed 1 year ago

KBrunk01 commented 1 year ago

Hi there,

I am trying to fit a spatial model to my data, and I was interested in experimenting with the different covariance functions. I have been able to successfully fit a spatial mode with the exponential function, but when I try to run the same model with a gaussian function for the covariance, it gives me a rather cryptic error:

Error in spPGOcc(occ.formula = M, det.formula = yewa_Det, data = YEWA, : c++ error: dpotrf failed

A bit of googling around has not presented an obvious solution, any ideas?

Thanks much!

doserjef commented 1 year ago

Hi Kristin,

Thanks for opening this issue, and apologies for the cryptic error. This error indicates that there is a problem when trying to calculate the spatial covariance matrix. The Gaussian covariance function can be a bit finnicky, as it sometimes requires additional effort up front to the get the model to work. Sometimes I've found it doesn't fit successfully at all, depending on the data set. This may be related to the fact that this covariance function results in extremely smooth estimates of the spatial autocorrelation, which may not always be realistic or possible depending on how many sites there are in the data set. Here are a few things I'll suggest you try out:

  1. Manually specify the initial value for the "phi" (spatial decay) parameter. Assuming that you are using the default prior for phi, a good way to specify the initial value would be as follows:
# This should be the "inits" argument of spPGOcc
inits = list(phi = 3 / mean(dist(YEWA$coords))
  1. You may also need to specify the "tuning" value for phi. This is in the "tuning" argument of spPGOcc. The default value is 1, so you I might suggest first trying a value substantially smaller than that, followed by one larger than that if that doesn't work. You can do this by specifying the tuning argument as tuning = list(phi = 0.1) or tuning = list(phi = 2.5).
  2. Take a look at the units of your coordinates in the YEWA$coords object. If this is in meters, I might suggest converting to km and then trying to refit the model.

If none of those work, feel free to send me the code/data and I'll give it a look.

Jeff

KBrunk01 commented 1 year ago

Hi Jeff,

Thanks so much for the speedy reply! I was already running with your first and second suggestions met, specifically, I had set an initial value for phi of 3/mean(dist(YEWA$coords)), and tuning = 0.1. I tried setting the tuning to a high value, and got the same error. I also had my spatial coordinates in UTM, so I divided by 1000 to make them kilometers, to no avail.

I do have encounter histories ready to go for several species, though, with Yellow Warbler being one of the most sparse in terms of actual detections. Naive occupancy for YEWA is only about 4% across my study area (47 occupied sites/1276 total sites). I tried running the gaussian covariance model with another more common species (Hermit Warbler - 59% naive occupancy), and it seems to run just fine. It seems sparsity of detections/low occurrence may be my issue...

doserjef commented 1 year ago

Interesting, thanks for the additional info, Kristin! Sorry those suggestions didn't work for you. It could certainly be related to the rarity of the species. As I mentioned the Gaussian covariance structure can sometimes be difficult to estimate because of how smooth it is, and depending on the spatial structure of where those few occurrence of YEWA were, that could be leading to the problem. One other potential thing to try would be to fit the model with the exponential covariance function, then use the mean estimates of the parameters from that model fit as starting values for a model using the Gaussian covariance structure.

Glad to hear you've got it to work with other species! It does seem like this is just a specific scenario with the YEWA dataset, so I'll go ahead and close the issue, but feel free to comment again and re-open if necessary.