Using hcov in secr.fit for polygon data causes R session abort

224 views
Skip to first unread message

h.tr...@gmx.de

unread,
Jun 7, 2023, 6:59:13 AM6/7/23
to secr
Hi everyone,

I am new to secr and would like to fit a hybrid mixture model to account for sex differences in my polygon data (pine marten faecal samples collected within a study area, single search).
When I include the term hcov = "sex" in secr.fit, it almost immediately terminates my complete R session saying "R Session Aborted - R encountered fatal error" (R version 4.1.3). I get no helpful error messages to do some troubleshooting.

I tried to run the model on two different mashines with the same result (operating systems windows 10 22H2 and ubuntu 22.04).
Further I tried to fit a hybrid mixture model with the polygon example data polygonCH ( I added the covariate sex with randomly created "f" and "m" as factors for each detected animal) to check whether my pine marten capthist data might be prepared wrong but again the R session terminates. However, running the example data from ?hcov for the housemouse trap data works perfectly fine.

Did anyone encounter this problem before?
Is it not possible to include hcov in polygon data?
 
Any help would be much appreciated!

Thanks,
Henriette

Murray Efford

unread,
Jun 7, 2023, 5:14:52 PM6/7/23
to h.tr...@gmx.de, secr
Henriette
I'm sorry for the trouble; this should never happen. Could you please send an offending dataset to me offline? Your modification of the polygonCH dataset would be sufficient.
Thanks
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 on the web, visit https://groups.google.com/d/msgid/secrgroup/9bf43840-702d-4a1d-b996-a825424b9312n%40googlegroups.com.

Murray Efford

unread,
Jun 9, 2023, 4:47:48 AM6/9/23
to secr
There is indeed a bug that causes secr.fit to crash when faced with polygon data and hcov. I have fixed it for the next release, but that is a few weeks away. Meantime, I suggest you think about discretizing polygon detectors: it's much faster, and the code for point (e.g., 'proximity') detectors is more stable and better tested. Compare, for example,

# 600 seconds
# after fixing 'hcov' bug
fit_hcov = secr.fit(polygonCH, buffer = 200, hcov = "value", trace = TRUE)
coef(fit_hcov)
#                 beta    SE.beta        lcl       ucl
# D         0.09845739 0.13858501 -0.1731642 0.3700790
# lambda0   1.47621420 0.09548916  1.2890589 1.6633695
# sigma     3.90088946 0.04914132  3.8045742 3.9972047
# pmix.h2m -0.03636427 0.26972486 -0.5650153 0.4922867

# 25 seconds
CH <- discretize(polygonCH, spacing = 25)
fit_hcov_discretized <- secr.fit(CH, buffer = 200, hcov = "value", trace = TRUE)
coef(fit_hcov_discretized)
#                 beta    SE.beta        lcl        ucl
# D         0.09234172 0.13872995 -0.1795640  0.3642474
# lambda0  -1.70417134 0.12836875 -1.9557695 -1.4525732
# sigma     3.90952158 0.05307972  3.8054872  4.0135559
# pmix.h2m -0.03635842 0.26972419 -0.5650081  0.4922913

h.tr...@gmx.de

unread,
Jun 12, 2023, 5:03:34 AM6/12/23
to secr
Hi Murray,
thank you very much for helping that fast and fixing the bug!
I will also look into the advice of discretizing polygon detectors.

Thanks a lot,
Henriette 

Lukas Scholz

unread,
Feb 19, 2024, 6:00:34 AMFeb 19
to secr
Dear Murray,

I'm still new to secr and was playing around a bit. I assumed R crashing was a result of my inadequate usage of the secr.fit function. However, when coming across this conversation, I thought it might be worth flagging that bug.

I have a very similar set up with a polygon detector and came across a similar or likely even the same error: 
running secr.fit with groups instead of hcov, e.g. model=g0~g, groups = "mycovariate" and/or the option to specify the covariate via model = g0~mycovariate (instead of hcov) causes R to crash.

I'm using secr 4.6.4 and sex (f, m) as my covariate. The hcov approach mentioned above runs without crashing, and when discretizing the polygon (as suggested to Henriette) the groups argument works too.


If I understand correctly, you generally recommend discretizing polygon detectors, since analysis run faster and are more robust despite being less "accurate" (for lack of better word) due to artifacts introduced from the discretization?

Many thanks,
Lukas

Murray Efford

unread,
Feb 19, 2024, 2:37:00 PMFeb 19
to secr
Lukas
Thanks for your report. I'll look into it. You should be using lambda0 not g0 as the detection parameter with polygon detectors. I assume the model fits fine if you set CL = TRUE, remove 'groups', and model lambda0~mycovariate?
Yes, discretizing polygons works well, without any 'artifacts' worth mentioning.
Murray

Lukas Scholz

unread,
Feb 21, 2024, 7:55:58 AMFeb 21
to secr
Thank you Murray! Also, for pointing out that lambda0 (and not g0) is the appropriate detection parameter for polygon detectors (since they use hazard functions).

My previous message was a bit unclear, apologies. As mentioned, my covariate is sex (two levels: f, m; no NAs). While D~Sex works, both using sigma~Sex and (the wrong use of) g0~Sex cause R to crash. Both instances were set to CL = TRUE. The suggested use of lambda0~Sex with CL=TRUE also causes a fatal error.
Regarding the use of groups=”Sex”, I'd set CL=FALSE. Here, R crashes as well both when specifying the model with D~g; sigma~g; or lambda0~g respectively.


I’m interested in estimating densities. I expect sex differences in density as well as home range sizes, further I also suspect differences in detection.
While I went through vignettes, pdfs and conversations in this group, I’m still not sure if I understood everything correctly. Apart from modelling separate female and male sessions, there are three options for modelling the sex differences:
  1. Sex as group, i.e. groups=”Sex” & model term with ~g
  2. Sex as an individual covariate, i.e. model term with ~Sex
In the case of Sex, the individual covariate corresponds to two discrete groups, thus no preference in which approach to choose? While the group (full likelihood) models use density as a model parameter, the individual covariate (conditional likelihood) models derive density. Does that mean that in I my case, again there’s no preference in which approach to choose?
Lastly, you lean towards the use of individual covariates instead of groups, since the code is more stable?

3) Using a finite mixture models, i.e. hcov=”Sex”
Those are especially useful as ‘hybrid’ models, when there are individuals of unknown sex. Since mixture models are for detection parameters only and don’t let me model different sex specific densities, they are not suitable for my question.

Independently, I will continue with discretized polygons from now on.

Many thanks,
Lukas

Murray Efford

unread,
Feb 22, 2024, 8:33:57 PMFeb 22
to secr
Hello Lukas
Any time 'secr' causes R to crash we know there's a bug. It would help me find it if you sent a (preferably simple) example - to my private email if necessary.

All available methods for sex differences in detection give the same answers (used correctly!). It's a misconception that the hybrid mixture models do not provide density estimates for each sex. derived(fit, groups = 'Sex') will do it (for conditional likelihood or full likelihood with D~1).
Murray

Murray Efford

unread,
Feb 23, 2024, 6:52:47 PMFeb 23
to secr
Lukas
Thanks for sending a nice example offline. There was indeed a bug deep in the code that is now fixed in the working version of secr (4.6.6). This is available as Windows binary and source on the Density website, and at GitHub.

With your example CH_BR (and new function in 4.6.6 list.secr.fit() for multiple models) I get

# conditional likelihood, individual covariate
args <- list(CH_BR, buffer = 4 * initialsigma_BR, detectfn = 'HHN', CL = TRUE, trace = FALSE)
models <- list(lambda0~1, lambda0~Sex, sigma~Sex)
fits <- list.secr.fit(model = models, constant = args)

# full likelihood, groups
args <- list(CH_BR, buffer = 4 * initialsigma_BR, detectfn = 'HHN', CL = FALSE, trace = FALSE, groups = 'Sex')
models <- list(lambda0~1, lambda0~g, sigma~g)
fitsg <- list.secr.fit(model = models, constant = args)

AIC(fits)
#                    model          detectfn npar    logLik      AIC     AICc dAICc AICcwt
# fit1   lambda0~1 sigma~1 hazard halfnormal    2 -1155.676 2315.352 2315.814 0.000 0.6107
# fit3 lambda0~1 sigma~Sex hazard halfnormal    3 -1155.503 2317.007 2317.967 2.153 0.2081
# fit2 lambda0~Sex sigma~1 hazard halfnormal    3 -1155.642 2317.284 2318.244 2.430 0.1812

AIC(fitsg)
#                      model          detectfn npar    logLik      AIC     AICc dAICc AICcwt
# fit1 D~1 lambda0~1 sigma~1 hazard halfnormal    3 -1161.024 2328.047 2329.007 0.000 0.6077
# fit3 D~1 lambda0~1 sigma~g hazard halfnormal    4 -1160.770 2329.540 2331.206 2.199 0.2024
# fit2 D~1 lambda0~g sigma~1 hazard halfnormal    4 -1160.833 2329.666 2331.333 2.326 0.1899

Murray

Lukas Scholz

unread,
Feb 26, 2024, 8:46:05 AMFeb 26
to secr
Hey Murray,

perfect, will try it with the latest working version directly. Also, a massive thank you both for constantly updating the secr package and for helping people in this group.

Lukas
Reply all
Reply to author
Forward
0 new messages