Creating detection matrix for n-mixture model

53 views
Skip to first unread message

Matthew Hyde

unread,
Apr 22, 2025, 6:18:21 PMApr 22
to hmecology: Hierarchical Modeling in Ecology
Hi all,

I'm looking to run an n-mixture model on some camera trap data I have, but I'm getting stuck at how to create the matrix when I have uneven sampling (e.g., some days a camera is off while others are on). Typically I've used CamtrapR for such applications, but it doesn't output the detection history for n-mixture models. 

Does anyone have any suggestions or code snippets they would be willing to share?

Thank you in advance,
Matt

Marc Kery

unread,
Apr 23, 2025, 5:02:15 AMApr 23
to Matthew Hyde, hmecology: Hierarchical Modeling in Ecology
Dear Matt,

if you're using unmarked or ubms, then you need to input a regular array. That is, when you don't have an identical number of occasions at all sites (i.e. your data are imbalanced), you must fill it up with missing values (NAs) to restore balance. In this, it doesn't matter whether you have the NAs interspersed within the history of a site, or whether you move all non-missing data left in a site history, leaving a tail of NAs on the right. Only, you will have to do the same treatment also to any observational covariates.

If you're using your own code in JAGS or NIMBLE, you can do the same (i.e., restore balance by filling up with NAs). This will result in neater code, though at the cost of some computational inefficiency, since the missing responses will be estimated when fitting the model. If the number of such added NAs is not too big, this cost can usually be ignored.

If you don't want this inefficiency, then you can:
  • either vectorize the analysis (see analysis of dynamic occupancy model in Section 4.10.2 in the AHM2 book for an example). Here, you string out the detection history matrix into a simple vector, toss out all missing values, and create additional vectors that index sites, years, occasions etc for each response. In the BUGS code, you then use nested indexing to tell the software from which site, year, occasion etc. a response is coming.
  • another option is to move all non-missing data in the detection history left, leaving a tail of missings on the right on each line. Then, you create a variable that contains the number of non-missing values for each site. And in the loop, where you define the likelihood of the data, you instruct BUGS to only evaluate the detection history at a site from column 1 until whatever is the number of non-missing occasions at that site.

Best regards  --- Marc



From: hmec...@googlegroups.com <hmec...@googlegroups.com> on behalf of Matthew Hyde <mhy...@gmail.com>
Sent: Wednesday, April 23, 2025 00:18
To: hmecology: Hierarchical Modeling in Ecology <hmec...@googlegroups.com>
Subject: Creating detection matrix for n-mixture model
 
--
*** Three hierarchical modeling email lists ***
(1) unmarked: for questions specific to the R package unmarked
(2) SCR: for design and Bayesian or non-bayesian analysis of spatial capture-recapture
(3) HMecology (this list): for everything else, especially material covered in the books by Royle & Dorazio (2008), Kéry & Schaub (2012), Kéry & Royle (2016, 2021) and Schaub & Kéry (2022)
---
You received this message because you are subscribed to the Google Groups "hmecology: Hierarchical Modeling in Ecology" group.
To unsubscribe from this group and stop receiving emails from it, send an email to hmecology+...@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/hmecology/f2e12a2b-3462-4fd0-bb15-a4f599c705cdn%40googlegroups.com.

Matthew Hyde

unread,
Apr 23, 2025, 12:41:36 PMApr 23
to hmecology: Hierarchical Modeling in Ecology
Hi Mark,

Thank you very much for the thorough explanation! I'm trying to fit the model in JAGS, so I'll try go the first route and I'll check out the code from AHM2. Thank you again!

Best,
Matt

Reply all
Reply to author
Forward
0 new messages