Potential Bug with site-level variation

92 views
Skip to first unread message

Heather Gaya

unread,
Dec 11, 2023, 9:49:43 AM12/11/23
to secr
Hi all,

I'm running a model with data from 10 different sites across 2 years, where density is modeled as a function of both spatial covariates and a site-level effect. The models run great and are able to get an estimate for the betas. However, region.N and predictD give the error: "Error in mat %*% betaD : non-conformable arguments"

Using the source code on Github, I've tracked down the error. It comes  from the model.matrix() or general.model.matrix() functions or right around there. The model gives the density formula as ~opnearl_wg84 + site. General.model.matrix() interprets this as 3 betas - the intercept, the beta for opnearl_wg84 and a beta for site, but it actually needs to be interpreted as a factor and turn into 11 betas (intercept/reference level, opnearl_wg84 and 9 sites) so that it matches the dimensions of betaD. Trying to get around this by expanding out the factor levels into binary variables in the mask doesn't help, since region.N filters out all covariates that aren't relevant, and it doesn't recognize "siteSITE1" or "siteSITE2" as connected to the "site" covariate used in density estimation.

I've created a workaround for my very specific problem, but just wanted to let everyone know that this might come up in the future. 

-Heather 

Murray Efford

unread,
Dec 13, 2023, 7:10:04 PM12/13/23
to secr
Thanks for the report - will check this out soon.

Murray Efford

unread,
Dec 20, 2023, 1:47:23 PM12/20/23
to secr
Hi Heather
Can you please provide more code and context? I do not see a problem with this fake example (arbitrarily generating 'sites' from the grids of possumCH).
Murray

library(secr)
grid <- substring(rownames(traps(possumCH)), 1,2)
covariates(possummask)$grid <- factor(grid[nearesttrap(possummask, traps(possumCH))])

plot(possummask, cov='grid')
plot(traps(possumCH), add=T)

fit <- secr.fit(possumCH, mask = possummask, D ~ x + y + grid)
coef(fit)
#                 beta    SE.beta        lcl        ucl
# D         0.30025341 0.26994201 -0.2288232  0.8293300
# D.x       0.22512618 0.48666096 -0.7287118  1.1789641
# D.y       0.04205227 0.37118572 -0.6854584  0.7695629
# D.gridG2 -0.09976331 0.56668561 -1.2104467  1.0109201
# D.gridG3  0.17377280 0.63202951 -1.0649823  1.4125279
# D.gridG4  0.14544947 0.78357986 -1.3903388  1.6812378
# D.gridG5  0.67232770 0.72558724 -0.7497972  2.0944526
# g0       -1.43600678 0.14054143 -1.7114629 -1.1605506
# sigma     3.94227490 0.04967478  3.8449141  4.0396357

masklist <- split(possummask, covariates(possummask)$grid)
lapply(masklist, region.N, object = fit)

plot(predictDsurface(fit))
plot(traps(possumCH), add=T)

On Tuesday, December 12, 2023 at 3:49:43 AM UTC+13 heather...@gmail.com wrote:
Message has been deleted

Murray Efford

unread,
Dec 21, 2023, 10:56:05 PM12/21/23
to secr

Heather

Thanks for sending your data offline. I have revised my earlier diagnosis [now deleted] as follows.

Your ‘site’ predictor is a character mask covariate. You provide different masks for each year, and exclude from the first mask two sites not sampled in that year. As a result, when the session-specific site covariate is converted internally from character to factor, factor levels do not match across years, and the beta parameters for the site covariate in the first year do not match those in the second year.

The solution is to apply shareFactorLevels() to the multi-session mask _before_ fitting the model (and there’s really no need for a multi-session mask). Applying this retrospectively, as I suggested earlier, gives the _wrong_ answer because it mismatches sites and coefficients. Your workaround is also wrong for the same reason.

Why is the problem not flagged by secr.fit()? The verify function applied to a multi-session mask does throw a warning and suggest using shareFactorLevels(), but within secr.fit() verify is mistakenly applied only to each separate session-specific mask, and therefore fails to find the inconsistency.

Murray

heather...@gmail.com

unread,
Dec 22, 2023, 6:26:43 PM12/22/23
to secr
Thank you Murray! That solved my problem, and I'm glad to see it was mostly an error on how we setup our mask. 

As a follow up question, would this still work if my covariates (in this case, landcover) had different values for my different sessions? I'm imagining a scenario where we measure vegetation each year and then use that in the mask, therefore necessitating a multi-session mask. This is purely theoretical, but just curious if that's available in SECR at this time. 

-Heather 

Sent from my iPhone; please excuse any typos

On Dec 21, 2023, at 10:56 PM, Murray Efford <murray...@gmail.com> wrote:


--
You received this message because you are subscribed to a topic in the Google Groups "secr" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/secrgroup/6WFWDf98Nz4/unsubscribe.
To unsubscribe from this group and all its topics, send an email to secrgroup+...@googlegroups.com.
To view this discussion on the web, visit https://groups.google.com/d/msgid/secrgroup/0b29273c-4751-4abf-bb6c-bfd2c18be8adn%40googlegroups.com.

Murray Efford

unread,
Dec 22, 2023, 7:45:12 PM12/22/23
to secr
I'm glad that worked. It is not necessary for all factor levels to be represented in every session, only that a factor covariate has a set of levels that is constant across sessions, so that there is a consistent relationship between levels and beta parameters (coefficients). As I understand it, the scenario you describe is really just a variation on your original one, where not all levels of 'site' were sampled in the first year (or not all vegetation classes appear in each year).
Murray
Reply all
Reply to author
Forward
0 new messages