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 with an error message when using the function intMsPGOcc() to do Occupancy with multiple data sources #28

Closed lnovo98 closed 1 year ago

lnovo98 commented 1 year ago

Good evening, It's my first time using GitHub, sorry if I am not explaining my issue clearly enough.

I am currently trying to run a code using the intMsPGOcc() function from the spOccupancy() package to calculate occupancy values for mammal species from two different dataset: camera traps and foot survey

I already used the spatial function spMsPGOcc() in the past for the camera traps and it worked well !

However, this time using both camera traps and foot survey data it is not working anymore at a certain point. I followed the function description and coded my arguments the same way as the one provided in the spOccupancy package example. However, I keep getting the same error message: Error in chol.default(W) : the leading minor of order 59 is not positive definite

When only running the function with one chain it works fine, but as soon as I increase the chain to 2 or more the error message pops up.

Here is how my data and code look: Camera traps: 39 species for 183 sites with a repeat visit of 4 (=number of weeks cameras were active) Foot survey: 26 species for 136 transects with a repeat visit of 2 (=beginning and end of the month)

data.list <- list(y = y, a list of 2 objects containing respectively an array for the camera traps data source (39x183x4), and an array for the foot survey (26x136x2) occ.covs = mat.env, numerical matrix [1:319, 1:15], that contains the total of sites (cameras + transects) and the considered environmental covariates (15) that could be influencing occupancy and will be written in the formulas det.covs = det.covs.L, a list of two other lists (one for cameras, one for transects) that each contains the detection covariates as numerical vectors that we believe will influence the species detection, and as well will be included in the formula sites = sites.int, a list of 2 numerical vectors with the site number, one for cameras and one for transects species = species.int) a list of 2 character vectors that both contains the 39 species name for camera traps and the 26 species name for the transects

occ.formula.int <- ~ scaled_drivers_km Formula for occupancy, scaled_drivers_km = minimum distance in km of a site from the river, simplified for model trials det.formula.int = list(C = ~CtDays, Formula for camera (C) detection, CtDays = number of active days T = ~Gbs + Wo ) #Gbs + Wo) Formula for foot survey (T) detection, Gbs = proportion of open grassland, Wo = proportion of open woodland

prior.list <- list(beta.comm.normal = list(mean = 0,var = 2.73), alpha.comm.normal = list(mean = list(0, 0), var = list(2.72, 2.72)), tau.sq.beta.ig = list(a = 0.1, b = 0.1), tau.sq.alpha.ig = list(a = list(0.1, 0.1), b = list(0.1, 0.1)))

inits.list <- list(alpha.comm = list(0,0), beta.comm = 0, alpha = list(0, 0), beta = 0, tau.sq.beta = 1, z = matrix(1,46,J), J = 319, number of total sites fix = T)

outputs <- intMsPGOcc(occ.formula = occ.formula.int, det.formula = det.formula.int, inits = inits.list, priors = prior.list, data = data.list, n.samples = 1000, n.omp.threads = 1, verbose = TRUE, n.report = 10, n.burn = 100, n.thin = 30, n.chains = 2)

summary(outputs, level = 'community')

I already tried multiple combinations with the number of sample, burn and thinning but the error message keeps happening

outputs <- intMsPGOcc(occ.formula = occ.formula.int,

  • det.formula = det.formula.int,
  • inits = inits.list,
  • priors = prior.list,
  • data = data.list,
  • n.samples = 1000,
  • n.omp.threads = 1,
  • verbose = TRUE,
  • n.report = 10,
  • n.burn = 100,
  • n.thin = 30,
  • n.chains = 2)

    Preparing to run the model

    tau.sq.beta is not specified in initial values. Setting initial values to random values between 0.5 and 10

Fixing initial values across all chains


Model description

Integrated Multispecies Occupancy Model with Polya-Gamma latent variable fit with 319 sites and 46 species. I believe 46 species are the common species between the 39 detected with camera traps and the 26 species detected with transects

Integrating 2 occupancy data sets.

Samples per Chain: 1000 Burn-in: 100 Thinning Rate: 30 Number of Chains: 2 Total Posterior Samples: 60

Source compiled with OpenMP support and model fit using 1 thread(s).


Chain 1

Sampling ... Sampled: 10 of 1000, 1.00%

Sampled: 20 of 1000, 2.00%

Sampled: 30 of 1000, 3.00%

Sampled: 40 of 1000, 4.00%

Sampled: 50 of 1000, 5.00%

Sampled: 60 of 1000, 6.00%

Sampled: 70 of 1000, 7.00%

Sampled: 80 of 1000, 8.00%

Sampled: 90 of 1000, 9.00%

Sampled: 100 of 1000, 10.00%

Sampled: 110 of 1000, 11.00%

Sampled: 120 of 1000, 12.00%

Sampled: 130 of 1000, 13.00%

Sampled: 140 of 1000, 14.00%

Sampled: 150 of 1000, 15.00%

Sampled: 160 of 1000, 16.00%

Sampled: 170 of 1000, 17.00%

Sampled: 180 of 1000, 18.00%

Sampled: 190 of 1000, 19.00%

Sampled: 200 of 1000, 20.00%

Sampled: 210 of 1000, 21.00%

Sampled: 220 of 1000, 22.00%

Sampled: 230 of 1000, 23.00%

Sampled: 240 of 1000, 24.00%

Sampled: 250 of 1000, 25.00%

Sampled: 260 of 1000, 26.00%

Sampled: 270 of 1000, 27.00%

Sampled: 280 of 1000, 28.00%

Sampled: 290 of 1000, 29.00%

Sampled: 300 of 1000, 30.00%

Sampled: 310 of 1000, 31.00%

Sampled: 320 of 1000, 32.00%

Sampled: 330 of 1000, 33.00%

Sampled: 340 of 1000, 34.00%

Sampled: 350 of 1000, 35.00%

Sampled: 360 of 1000, 36.00%

Sampled: 370 of 1000, 37.00%

Sampled: 380 of 1000, 38.00%

Sampled: 390 of 1000, 39.00%

Sampled: 400 of 1000, 40.00%

Sampled: 410 of 1000, 41.00%

Sampled: 420 of 1000, 42.00%

Sampled: 430 of 1000, 43.00%

Sampled: 440 of 1000, 44.00%

Sampled: 450 of 1000, 45.00%

Sampled: 460 of 1000, 46.00%

Sampled: 470 of 1000, 47.00%

Sampled: 480 of 1000, 48.00%

Sampled: 490 of 1000, 49.00%

Sampled: 500 of 1000, 50.00%

Sampled: 510 of 1000, 51.00%

Sampled: 520 of 1000, 52.00%

Sampled: 530 of 1000, 53.00%

Sampled: 540 of 1000, 54.00%

Sampled: 550 of 1000, 55.00%

Sampled: 560 of 1000, 56.00%

Sampled: 570 of 1000, 57.00%

Sampled: 580 of 1000, 58.00%

Sampled: 590 of 1000, 59.00%

Sampled: 600 of 1000, 60.00%

Sampled: 610 of 1000, 61.00%

Sampled: 620 of 1000, 62.00%

Sampled: 630 of 1000, 63.00%

Sampled: 640 of 1000, 64.00%

Sampled: 650 of 1000, 65.00%

Sampled: 660 of 1000, 66.00%

Sampled: 670 of 1000, 67.00%

Sampled: 680 of 1000, 68.00%

Sampled: 690 of 1000, 69.00%

Sampled: 700 of 1000, 70.00%

Sampled: 710 of 1000, 71.00%

Sampled: 720 of 1000, 72.00%

Sampled: 730 of 1000, 73.00%

Sampled: 740 of 1000, 74.00%

Sampled: 750 of 1000, 75.00%

Sampled: 760 of 1000, 76.00%

Sampled: 770 of 1000, 77.00%

Sampled: 780 of 1000, 78.00%

Sampled: 790 of 1000, 79.00%

Sampled: 800 of 1000, 80.00%

Sampled: 810 of 1000, 81.00%

Sampled: 820 of 1000, 82.00%

Sampled: 830 of 1000, 83.00%

Sampled: 840 of 1000, 84.00%

Sampled: 850 of 1000, 85.00%

Sampled: 860 of 1000, 86.00%

Sampled: 870 of 1000, 87.00%

Sampled: 880 of 1000, 88.00%

Sampled: 890 of 1000, 89.00%

Sampled: 900 of 1000, 90.00%

Sampled: 910 of 1000, 91.00%

Sampled: 920 of 1000, 92.00%

Sampled: 930 of 1000, 93.00%

Sampled: 940 of 1000, 94.00%

Sampled: 950 of 1000, 95.00%

Sampled: 960 of 1000, 96.00%

Sampled: 970 of 1000, 97.00%

Sampled: 980 of 1000, 98.00%

Sampled: 990 of 1000, 99.00%

Sampled: 1000 of 1000, 100.00%

Chain 2

Sampling ... Sampled: 10 of 1000, 1.00%

Sampled: 20 of 1000, 2.00%

Sampled: 30 of 1000, 3.00%

Sampled: 40 of 1000, 4.00%

Sampled: 50 of 1000, 5.00%

Sampled: 60 of 1000, 6.00%

Sampled: 70 of 1000, 7.00%

Sampled: 80 of 1000, 8.00%

Sampled: 90 of 1000, 9.00%

Sampled: 100 of 1000, 10.00%

Sampled: 110 of 1000, 11.00%

Sampled: 120 of 1000, 12.00%

Sampled: 130 of 1000, 13.00%

Sampled: 140 of 1000, 14.00%

Sampled: 150 of 1000, 15.00%

Sampled: 160 of 1000, 16.00%

Sampled: 170 of 1000, 17.00%

Sampled: 180 of 1000, 18.00%

Sampled: 190 of 1000, 19.00%

Sampled: 200 of 1000, 20.00%

Sampled: 210 of 1000, 21.00%

Sampled: 220 of 1000, 22.00%

Sampled: 230 of 1000, 23.00%

Sampled: 240 of 1000, 24.00%

Sampled: 250 of 1000, 25.00%

Sampled: 260 of 1000, 26.00%

Sampled: 270 of 1000, 27.00%

Sampled: 280 of 1000, 28.00%

Sampled: 290 of 1000, 29.00%

Sampled: 300 of 1000, 30.00%

Sampled: 310 of 1000, 31.00%

Sampled: 320 of 1000, 32.00%

Sampled: 330 of 1000, 33.00%

Sampled: 340 of 1000, 34.00%

Sampled: 350 of 1000, 35.00%

Sampled: 360 of 1000, 36.00%

Sampled: 370 of 1000, 37.00%

Sampled: 380 of 1000, 38.00%

Sampled: 390 of 1000, 39.00%

Sampled: 400 of 1000, 40.00%

Sampled: 410 of 1000, 41.00%

Sampled: 420 of 1000, 42.00%

Sampled: 430 of 1000, 43.00%

Sampled: 440 of 1000, 44.00%

Sampled: 450 of 1000, 45.00%

Sampled: 460 of 1000, 46.00%

Sampled: 470 of 1000, 47.00%

Sampled: 480 of 1000, 48.00%

Sampled: 490 of 1000, 49.00%

Sampled: 500 of 1000, 50.00%

Sampled: 510 of 1000, 51.00%

Sampled: 520 of 1000, 52.00%

Sampled: 530 of 1000, 53.00%

Sampled: 540 of 1000, 54.00%

Sampled: 550 of 1000, 55.00%

Sampled: 560 of 1000, 56.00%

Sampled: 570 of 1000, 57.00%

Sampled: 580 of 1000, 58.00%

Sampled: 590 of 1000, 59.00%

Sampled: 600 of 1000, 60.00%

Sampled: 610 of 1000, 61.00%

Sampled: 620 of 1000, 62.00%

Sampled: 630 of 1000, 63.00%

Sampled: 640 of 1000, 64.00%

Sampled: 650 of 1000, 65.00%

Sampled: 660 of 1000, 66.00%

Sampled: 670 of 1000, 67.00%

Sampled: 680 of 1000, 68.00%

Sampled: 690 of 1000, 69.00%

Sampled: 700 of 1000, 70.00%

Sampled: 710 of 1000, 71.00%

Sampled: 720 of 1000, 72.00%

Sampled: 730 of 1000, 73.00%

Sampled: 740 of 1000, 74.00%

Sampled: 750 of 1000, 75.00%

Sampled: 760 of 1000, 76.00%

Sampled: 770 of 1000, 77.00%

Sampled: 780 of 1000, 78.00%

Sampled: 790 of 1000, 79.00%

Sampled: 800 of 1000, 80.00%

Sampled: 810 of 1000, 81.00%

Sampled: 820 of 1000, 82.00%

Sampled: 830 of 1000, 83.00%

Sampled: 840 of 1000, 84.00%

Sampled: 850 of 1000, 85.00%

Sampled: 860 of 1000, 86.00%

Sampled: 870 of 1000, 87.00%

Sampled: 880 of 1000, 88.00%

Sampled: 890 of 1000, 89.00%

Sampled: 900 of 1000, 90.00%

Sampled: 910 of 1000, 91.00%

Sampled: 920 of 1000, 92.00%

Sampled: 930 of 1000, 93.00%

Sampled: 940 of 1000, 94.00%

Sampled: 950 of 1000, 95.00%

Sampled: 960 of 1000, 96.00%

Sampled: 970 of 1000, 97.00%

Sampled: 980 of 1000, 98.00%

Sampled: 990 of 1000, 99.00%

Sampled: 1000 of 1000, 100.00% Error in chol.default(W) : the leading minor of order 61 is not positive definite

doserjef commented 1 year ago

Hello!

Thanks for the background information on your data sets, and sorry to hear you're running into this problem. That error that you are getting happens when there is a problem when calculating the Rhat (Gelman-Rubin diagnostic). This is only done when running with multiple chains, which explains why you're not seeing the problem with a single chain. Usually this occurs because there is some parameter in the model that is not being estimated properly, which could arise from a bad initial value (although the ones you supply seem fine), a problem with the data formatting, or some specific case with a given data set (i.e., extremely rare species). Could you send me your data set and script via email (doserjef@msu.edu) and I can take a look at it?

Thanks,

Jeff

lnovo98 commented 1 year ago

Hi Jeff ! Thank you so much for your reply, I am sending you my script and dataset.

Have a good day,

Cheers,

Lucy

doserjef commented 1 year ago

We figured out this error was occurring because the burn-in period was not long enough, and some very large values at the beginning of the MCMC chains were leading to an error when intMsPGOcc() tries to calculate Rhat. Running the chains for longer with a longer burn-in eliminated the error.