Is it possible to plot density surfaces using the groups argument?

39 views
Skip to first unread message

Milena Ribeiro

unread,
Mar 30, 2025, 12:17:22 PMMar 30
to secr
Hello, 

When using the "groups" argument in secr.fit, is it possible to plot distinct density surfaces for each of the groups, i.e. one density surface for each sex (F and M)?

I am using the following command:
surface.sex <- predictDsurface(fit.sex.D)
plot(surface.sex)

And I get the error:
Error in plot.Dsurface(surface.sex) : covariate D.0 not found

I understand that when I use the "groups" argument in secr.fit(), secr fits separate models for each group and stores the density estimates separately. However, I do not know how to solve the problem in the script and if it is possible to solve it.

Thank you for your attention,

Milena Ribeiro.

Murray Efford

unread,
Mar 30, 2025, 11:08:42 PMMar 30
to secr
For groups, density predictDsurface() saves density as one covariate for each group. If the groups are 'f' and 'm' then these are named 'D.f' and 'D.m'. 
To plot a particular group 'f' you can then
(i) specify e.g. plot(surface.sex, 'D.f') where the second argument is the covariate name, or more directly 
(ii) specify e.g. plot(surface.sex, group = 'f').  The group argument is documented in  ?plot.Dsurface.
Murray

# I tested with:
library(secr)
morning <- subset(housemouse, occ = c(1,3,5,7,9)) # includes one mouse with sex unknown
groupx   <- secr.fit(morningx, model = D ~ g + x, groups = 'sex', buffer = 25, biasLimit = NA, trace = FALSE)
Ds <- predictDsurface(groupx)
par(mfrow=c(1,2))
plot(Ds, group = 'f')
plot(Ds, group = 'm')

Milena Ribeiro

unread,
Apr 5, 2025, 2:54:36 PMApr 5
to secr

Hello, Efford,

Thank you for your response. I tried what you recommended, as well as some variations for testing purposes, and in all cases I received the following error:

r
CopyEdit
Error in seq.default(covrange[1], covrange[2], length.out = breaks + 1): 'from' must be a finite number In addition: Warning messages: 1: In min(x): no non-missing arguments to min; returning Inf 2: In max(x): no non-missing arguments to max; returning -Inf

When I run the command covariates(surface.sex), I get the columns corresponding to my groups, but all values in the rows (i.e., the mask points) are NA, which I believe is what’s causing the error above. Is there any solution to this?

Thank you again for your help.

Best regards,
Milena Ribeiro

Murray Efford

unread,
Apr 5, 2025, 5:26:17 PMApr 5
to secr
So predictDsurface is not working for your model and mask, for some reason we cannot see. I think you will need to troubleshoot that, looking at the model and the fitted coefficients coef( fit.sex.D) and the covariates of the original mask covariates(fit.sex.D$mask). It worked for me, as you can see by running the example.
Murray
Reply all
Reply to author
Forward
0 new messages