C++ error: which failed in stPGOcc

23 views
Skip to first unread message

Gemma Badia

unread,
May 15, 2025, 4:11:38 PMMay 15
to spOccupancy and spAbundance users
Hi all,

I'm working on fitting a single-species, multi-season occupancy model. As occupancy covariates I have 6 different variables, five of which are static and one is dynamic, having different values for each year. To incorporate it in the model I have created a matrix with rows corresponding to sites (cameras) and columns corresponding to years. 

When I try running the model I encounter the following error message:  Error in stPGOcc(occ.formula = ~ruggedness + forest + specific_vegetation + : c++ error: which failed. I have tried running the model without the dynamic covariate and it works just fine, so I assume it's something related with the data structure of that covariate. 

The model I'm trying to run is the following: 

model_HZop1 <- stPGOcc(
  occ.formula = ~ ruggedness + forest + specific_vegetation + hunting_zone_op1 + (1|IDcam),
  det.formula = ~ effort + forest + specific_vegetation + ruggedness + year,
  data = data_list,
  n.batch = 1500,      
  batch.length = 50,  
  n.burn = 20000,      
  n.thin = 2,
  n.chains = 3,
  verbose = TRUE,
)

I've aslo attached a picture below where you can visualize the data structure. Do you have any idea of what is causing this error and how I could fix it? There are no missing values in the dynamic covariate and I also don't have any duplicated coordinates.

Thank you in advance!

Kind regards,
Gemma
Data_structure.png

Jeffrey Doser

unread,
May 16, 2025, 4:18:49 AMMay 16
to Gemma Badia, spOccupancy and spAbundance users
Hi Gemma,

Thanks for the question. It looks like your dynamic covariate "hunting_zone_op1" is a matrix with 78 rows and 2 columns. However, your detection-nondetection data are organized in a three-dimensional 78x9x2 array. In this format, this tells me that there are 78 sites, 9 years, and 2 repeat visits within a year. So, if that is the correct format, then the hunting_zone_op1 should be 78 rows by 9 columns. If you have 2 years and 9 visits within a year, then you should restructure "y" to be 78x2x9 (as well as all the detection covariates) and then I think things should work for you.

Hope that helps,

Jeff

--
You received this message because you are subscribed to the Google Groups "spOccupancy and spAbundance users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to spocc-spabund-u...@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/spocc-spabund-users/95c0baea-0609-42f2-9282-3b9e06767431n%40googlegroups.com.


--
Jeffrey W. Doser, Ph.D.
Assistant Professor
Department of Forestry and Environmental Resources
North Carolina State University
Pronouns: he/him/his

Gemma Badia

unread,
May 18, 2025, 8:39:18 AMMay 18
to spOccupancy and spAbundance users
Hi Jeff,

Thank you for your fast reply! That's right, I had misplaced the years for the replicates, now it works! 

I have a couple more questions:
- Regarding hunting_zone_op1, it's a categorical variable, with values 0, 1 and 2. Do I need to specify that somehow in the model or it already interprets it as a factor? When converting it into a factor it changes into a vector format, instead of an array. 
- How can I interpret the results for the spatial covariance? What are the meanings for sigma and phi?  
Results_model1.png

Thank you again for all the support,

Gemma



El dia divendres, 16 de maig del 2025 a les 10:18:49 UTC+2, jwd...@ncsu.edu va escriure:

Jeffrey Doser

unread,
May 20, 2025, 5:52:37 AMMay 20
to spOccupancy and spAbundance users
Hi Gemma,

Glad that solved the issue. For the categorical variable, it looks like it is currently being interpreted as numeric instead of categorical, so in the model formula you should switch "hunting_zone_op1" to "factor(hunting_zone_op1)" and that should work for you.

For the spatial covariance parameters, sigma.sq is the spatial variance and phi is the spatial decay. sigma.sq is just like any other random effect variance. The larger it is, the more variation there is in the spatial random effect across different sites. phi controls how far across space the correlation between two sites goes. Assuming you are using the exponential spatial covariance function (the default), the easiest way to interpret phi is is to take 3 / phi (so 3 / 0.03 in your case), which corresponds to the effective spatial range. In your case, the effective spatial range is about 100, which means that the spatial correlation between two sites effectively becomes very small (0.05) at a distance of 100. The units of the 100 distance are in whatever units your coords were specified in. That can help give you a sense of whether the spatial autocorrelation is fine-scale or broad-scale. I will say that I often don't put an extremely large amount of interpretation on phi and sigma.sq, and instead may find plotting the resulting spatial random effects (e.g., the output from w.samples) to be more informative on the pattern of the spatial autocorrelation. You could take a look at the "04a-spatial-european-goldfinch-ex.R" script at this repository for some code to do that.

Hope that helps,

Jeff
Reply all
Reply to author
Forward
0 new messages