Stratification in openCR models that estimate density

46 views
Skip to first unread message

Taylor Hamlin

unread,
Mar 29, 2026, 7:01:27 PM (14 days ago) Mar 29
to secr
Kia ora,

I am currently working on implementing stratification in openCR. I have three sites that I would like to implement as different strata within the same model. Each site is visited monthly (primary session) and traps are run for a non-fixed amount of days (usually around 3-7 days; secondary sessions).

I have been attempting to get the data in the correct format to start and so was systematically testing the capture histories and masks across model types. During testing, most of the spatial models ran without issue, however, I noticed that models that estimated density-related parameters (superD, BD, and D) returned the same error:

Error in if (N < ncf) return(1e+10) :
missing value where TRUE/FALSE needed


Upon further testing I found that if I ran the strata separately (no stratification) or in pairs of two, the models ran successfully (all pairwise combinations were found to work). It is only when all three strata are run in the same model do I encounter problems.

Is this an issue anyone has encountered before? Is there something I am missing about increasing the amount of stratification? I would be grateful if anyone had any insights.

All the best,
Taylor

# R-Version: 4.5.2
# openCR Version: 2.2.7

Current code:
## Draw in pre-processed capture histories, one for each site/strata
capt_hist_BP <- readRDS(file.path(path_input, "openCR", "Input", "Site_Bruie Plains Rd_CH.rds"))
capt_hist_MR <- readRDS(file.path(path_input, "openCR", "Input", "Site_Mailers Rd_CH.rds"))
capt_hist_PD <- readRDS(file.path(path_input, "openCR", "Input", "Site_Pig Dam_CH.rds"))

## All capture histories generate: "No errors found :-)"
verify(capt_hist_BP)
verify(capt_hist_MR)
verify(capt_hist_PD)

## Create individual masks for each site/strata
msk_BP <- make.mask(traps(capt_hist_BP), buffer = 200, spacing = 10, type = 'trapbuffer')
msk_MR <- make.mask(traps(capt_hist_MR), buffer = 200, spacing = 10, type = 'trapbuffer')
msk_PD <- make.mask(traps(capt_hist_PD), buffer = 200, spacing = 10, type = 'trapbuffer')

## Run each site/strata independently.
## Currently all converge and have reasonable estimates.
m_test1 <- openCR.fit(capt_hist_BP,
                      mask = msk_BP,
                      type = "secrD")
m_test2 <- openCR.fit(capt_hist_MR,
                      mask = msk_MR,
                      type = "secrD")
m_test3 <- openCR.fit(capt_hist_PD,
                      mask = msk_PD,
                      type = "secrD")

## Run pairwise combinations of all sites/strata.
## Currently all converge and have reasonable estimates
capt_hist_4 <- stratify(capt_hist_BP, capt_hist_MR)
msk_4 <- list(msk_BP, msk_MR)

m_test4 <- openCR.fit(capt_hist_4,
                      mask = msk_4,
                      type = "secrD",
                      stratified = TRUE)

capt_hist_5 <- stratify(capt_hist_BP, capt_hist_PD)
msk_5 <- list(msk_BP, msk_PD)

m_test5 <- openCR.fit(capt_hist_5,
                      mask = msk_5,
                      type = "secrD",
                      stratified = TRUE)

capt_hist_6 <- stratify(capt_hist_MR, capt_hist_PD)
msk_6 <- list(msk_MR, msk_PD)

m_test6 <- openCR.fit(capt_hist_6,
                      mask = msk_6,
                      type = "secrD",
                      stratified = TRUE)

## Run all three sites/strata in the same model.
## Generates error: "Error in if (N < ncf) return(1e+10) : missing value where TRUE/FALSE needed"
capt_hist_7 <- stratify(capt_hist_BP, capt_hist_MR, capt_hist_PD)
msk_7 <- list(msk_BP, msk_MR, msk_PD)

m_test7 <- openCR.fit(capt_hist_7,
                      mask = msk_7,
                      type = "secrD",
                      stratified = TRUE)

Murray Efford

unread,
Mar 29, 2026, 7:14:49 PM (14 days ago) Mar 29
to Taylor Hamlin, secr
Kia ora 
I'll have to work through an example to check this out, and that may take a few days. It would help if you are able to send me the data for your example offline - assuming you're comfortable with that.
Thanks for your detailed report.
Murray

--
You received this message because you are subscribed to the Google Groups "secr" group.
To unsubscribe from this group and stop receiving emails from it, send an email to secrgroup+...@googlegroups.com.
To view this discussion, visit https://groups.google.com/d/msgid/secrgroup/7b641083-8ef9-49bf-a436-08f4c6096980n%40googlegroups.com.

Taylor Hamlin

unread,
Mar 29, 2026, 7:19:14 PM (14 days ago) Mar 29
to secr

Kia ora Murray

I will double check with the owners of the dataset, but I think it should be fine. 

What is the best way to send it to you?

Cheers,
Taylor

Murray Efford

unread,
Apr 5, 2026, 3:36:25 AM (7 days ago) Apr 5
to secr
Hi Taylor

Thanks for sending the data. There was indeed a bug in openCR.fit for these stratified models that I hope I have fixed in the development version of openCR on GitHub (2.2.8). You should be able to install that with

install.packages("openCR", repos = "https://MurrayEfford.r-universe.dev")

However, openCR is currently failing checks on some systems because of a glitch in the suggested package RMark that Jeff Laake is working on. This means the fast r-universe route may not work on some Mac and Unix systems, and you may need to install from the GitHub source.

Also, I found that the default starting values did not work with your 3-stratum data (a common problem when strata are quite different). So you may need to play with starting values; for secrD these ones worked:
start = list(lambda0=0.005, superD=24, sigma = 19)
with final estimates (lambda0=0.003, superD=39, sigma = 20).

I haven't tested all your models - let me know if there are further problems.

Murray

Taylor Hamlin

unread,
Apr 6, 2026, 9:48:50 PM (6 days ago) Apr 6
to secr
Kia ora Murray

Thank-you for your prompt reply, especially over the Easter break.

I've had a chance to run a selection of the problematic models with the updated package, and it appears to have resolved the issue.

Much appreciated.

Ngā mihi,
Taylor

Reply all
Reply to author
Forward
0 new messages