Package 'nimbleJSextras'

Title: Aadditional Functions and Distributions For Bayesian Inference Of Jolly-Seber Models For The NIMBLE Package
Description: This package provides functions and simulation capability for modeling and testing code when analyzing capture-recapture data using Jolly-Seber type models.
Authors: Devin Johnson [aut, cre]
Maintainer: Devin Johnson <[email protected]>
License: CC0
Version: 0.1.9007
Built: 2026-11-09 19:14:00 UTC
Source: https://github.com/dsjohnson/nimbleJSextras

Help Index


Collapse all capture histories to only unique histories

Description

Collapse all individuals to unique histories and number of times each history occurs

Usage

collapse_ch(ch_matrix)

Arguments

ch_matrix

A matrix of capture histories where each row represents a single individual.


Hidden Markov Model Distributions for Binomial, Poisson, and Marked Capture Histories

Description

Density and random generation functions for Hidden Markov Model (HMM) distributions with binomial, Poisson, or marked continuous observation distributions.

Usage

dhmm_binom(x, init, prob, size, probTrans, len = 0, log = 0)

rhmm_binom(n, init, prob, size, probTrans, len = 0)

dhmm_pois(x, init, rate, probTrans, len = 0, log = 0)

rhmm_pois(n, init, rate, probTrans, len = 0)

dhmm_mark(x, mark, init, prob, probTrans, knots, sigma_knot, len = 0, log = 0)

rhmm_mark(n, mark, init, prob, probTrans, len = 0)

Arguments

x

Vector of observed counts or capture status for an individual across time (length 'K').

init

Vector of initial state probabilities at time 't = 1' of length 'J+2' (where 'J' is the number of intermediate active states, plus 1 initial unentered and 1 final exited state).

prob

2D array ('K x J') containing detection probabilities for each intermediate/active state 'J' at time 't'.

size

Vector of sizes (number of trials) for the binomial observation at each time (length 'K').

probTrans

Time-dependent 3D array of system state transition probabilities with dimensions '(J+2) x (J+2) x K' (where 'J+2' is the total number of system states and 'K' is number of occasions). 'probTrans[r, s, t]' is the probability of transitioning from state 'r' at time 't' to state 's' at time 't+1'.

len

Length of sequence 'x' ('K').

log

Logical; if 'TRUE', log-probabilities/densities are returned.

n

Number of random draws, each returning a vector of length 'len'. Currently only 'n = 1' is supported.

rate

2D array ('K x J') containing Poisson detection rates for each intermediate/active state 'J' at time 't'.

mark

Vector of continuous observations/marks across time (length 'K').

knots

Vector of knot locations/means corresponding to each intermediate state 'J' (length 'J').

sigma_knot

Standard deviation for the normal distribution around the knot locations.

Value

For 'dhmm_*', the likelihood (or log-likelihood if 'log = TRUE') evaluated as a numeric scalar. For 'rhmm_*', a numeric vector of generated values of length 'len'. Note that 'rhmm_mark' currently returns a zero vector placeholder.

—————————————————————————– Binomial emission HMM distribution —————————————————————————–


Jolly-Seber distributions For nimble models

Description

dJS_ms provides multistate Jolly-Seber (JS) distributions, dJS_binom provides binomial JS distributions for standard JS and robust design JS models. dJS_pois provides JS distributions for Poisson robust design, i.e., continuous-time secondary occasions. pstar_** functions calculate the overall probability of detection and are necessary for the JS model distributions.

Usage

dJS_ms(x, init, probObs, probTrans, pstar, weight = 1, len = 1, log = 0)

rJS_ms(n, init, probObs, probTrans, pstar = 0, weight = 1, len)

pstar_ms(init, probObs, probTrans, len)

dJS_binom(x, init, prob, size, probTrans, pstar, weight = 1, len = 0, log = 0)

rJS_binom(n, init, prob, size, probTrans, pstar = 0, weight = 1, len)

pstar_binom(init, prob, size, probTrans, len)

dJS_pois(x, init, rate, probTrans, pstar, weight = 1, len = 0, log = 0)

rJS_pois(n, init, rate, probTrans, pstar = 0, weight = 1, len)

pstar_pois(init, rate, probTrans, len)

dJS_mark(
  x,
  mark,
  init,
  prob,
  probTrans,
  pstar,
  knots,
  sigma_knot,
  len = 0,
  log = 0
)

rJS_mark(n, mark, init, prob, probTrans, pstar, knots, sigma_knot, len = 0)

pstar_mark(init, mark, prob, probTrans, knots, sigma_knot, len)

Arguments

x

Vector of individual capture history (length 'K').

init

Vector of initial state probabilities at time 't = 1' of length 'J+2' (where 'J' is the number of intermediate active states, plus 1 initial unentered and 1 final exited state).

probObs

Time-dependent 3D array of observation probabilities. First two dimensions of 'probObs' are of size (number of possible system states) 'x' (number of possible observation classes) and the third dimension is the number of occasions. 'probObs[i, j, t]' is the probability that an individual in the 'i'th latent state is recorded as being in the 'j'th detection state at time 't'.

probTrans

Time-dependent 3D array of system state transition probabilities with dimensions '(J+2) x (J+2) x K' (where 'J+2' is the total number of system states and 'K' is number of occasions). 'probTrans[r, s, t]' is the probability of transitioning from state 'r' at time 't' to state 's' at time 't+1'.

pstar

Overall probability of detection. Calculated using the 'pstar_**' functions in nimble model code.

weight

The number of times the capture history occurs in the full data set of capture histories. Default is 1.

len

Length of sequence 'x' ('K').

log

Logical; if 'TRUE', log-probabilities/densities are returned.

n

Number of random draws, each returning a vector of length 'len'. Currently only 'n = 1' is supported, but the argument exists for standardization of 'rJS_**' functions.

prob

For 'dJS_binom', a 2D array ('K x J') containing detection probabilities for each intermediate/active state 'J' at time 't'.

size

Vector of the number of secondary occasions for each primary occasion in a robust design (length 'K').

rate

2D array ('K x J') containing Poisson detection rates for each intermediate/active state 'J' at time 't'.

mark

Vector of continuous observations/marks across time (length 'K').

knots

Vector of knot locations/means corresponding to each intermediate state 'J' (length 'J').

sigma_knot

Standard deviation around knot locations.

Value

For 'dJS_*', the likelihood (or log-likelihood if 'log = TRUE') evaluated as a numeric scalar. For 'rJS_*', a numeric vector of generated capture histories of length 'len'. For 'pstar_*', a numeric scalar representing the overall detection probability.


Marked JS Observed History Conditional State Sampler Distributions

Description

Custom NIMBLE distribution wrappers for sample_det_ms, sample_det_binom, sample_det_pois, and sample_det_mark. Provides dummy density functions (returning constant log-density 0.0) and random simulation functions for conditional latent state draws of observed individuals under various Jolly-Seber observation models.

Usage

dJSCond_ms(x, obs, init, probObs, probTrans, log = 0)

rJSCond_ms(n = 1, obs, init, probObs, probTrans)

dJSCond_binom(x, obs, init, prob, size, probTrans, log = 0)

rJSCond_binom(n = 1, obs, init, prob, size, probTrans)

dJSCond_pois(x, obs, init, rate, probTrans, log = 0)

rJSCond_pois(n = 1, obs, init, rate, probTrans)

dJSCond_mark(x, obs, mark, init, prob, probTrans, knots, sigma_knot, log = 0)

rJSCond_mark(n = 1, obs, mark, init, prob, probTrans, knots, sigma_knot)

Arguments

x

Vector of sampled latent state histories of length K.

obs

Vector of observed capture history / counts / presence of length K.

init

Vector of initial state distribution probabilities.

probObs

3D array of state-to-observation probabilities (n_states x O x K).

probTrans

3D array of state transition probability matrices ((J+2) x (J+2) x (K-1)).

log

Logical; if TRUE, returns log probability (always 0.0).

n

Integer number of draws to simulate (defaults to 1).

prob

2D matrix of state-specific detection probabilities (K x J).

size

Vector of binomial trial counts across occasions of length K.

rate

2D matrix of Poisson detection rates (K x J).

mark

Vector of continuous mark observations of length K.

knots

Vector of knot locations/means corresponding to each active state J.

sigma_knot

Standard deviation around knot locations.

Value

dJSCond_* returns a scalar double (0.0). rJSCond_* returns a numeric vector of sampled latent states of length K.

—————————————————————————– Multistate JS Conditional Sampler —————————————————————————–


Marked JS Unobserved Abundance Latent State Distributions

Description

Custom NIMBLE distribution wrappers for sampling unobserved latent state histories (nu_t) across time occasions for undetected individuals in Jolly-Seber models. Provides dummy density functions (returning constant log-density 0.0) and random simulation functions for unobserved abundance states.

Usage

dJSUndet_ms(x, Nu, init, probObs, probTrans, log = 0)

rJSUndet_ms(n = 1, Nu, init, probObs, probTrans)

dJSUndet_binom(x, Nu, init, prob, size, probTrans, log = 0)

rJSUndet_binom(n = 1, Nu, init, prob, size, probTrans)

dJSUndet_pois(x, Nu, init, rate, probTrans, log = 0)

rJSUndet_pois(n = 1, Nu, init, rate, probTrans)

dJSUndet_mark(x, Nu, init, prob, probTrans, log = 0)

rJSUndet_mark(n = 1, Nu, init, prob, probTrans)

Arguments

x

Matrix of sampled unobserved latent states across intermediate states and occasions (dimension J x K).

Nu

Total number of unobserved individuals in the superpopulation.

init

Vector of initial state distribution probabilities.

probObs

3D array of state-to-observation probabilities (n_states x O x K).

probTrans

3D array of state transition probability matrices ((J+2) x (J+2) x (K-1)).

log

Logical; if TRUE, returns log probability (always 0.0).

n

Integer number of draws to simulate (defaults to 1).

prob

2D matrix of state-specific detection probabilities (K x J).

size

Vector of binomial trial counts across occasions of length K.

rate

2D matrix of Poisson detection rates (K x J).

Value

dJSUndet_* returns a scalar double (0.0). rJSUndet_* returns a 2D numeric matrix of sampled unobserved latent state counts across occasions.

—————————————————————————– Multistate JS Unobserved Sampler —————————————————————————–


Convenience functions for loading transition and observation parameter matrices for Jolly-Seber models

Description

Functions to generate the full transition matrix 'Gamma' or observation probability matrix 'P' for single-state and multistate Jolly-Seber models.

Usage

load_Gamma(xi_tilde, phi)

load_probObs(p)

load_Gamma_ms(xi_tilde, alpha, phi, Psi)

load_probObs_ms(p)

Arguments

xi_tilde

Scalar representing recruitment/entry probability.

phi

For load_Gamma: a scalar survival probability. For load_Gamma_ms: a numeric vector of length 'J' containing state-specific survival probabilities.

p

For load_probObs: a scalar capture probability. For load_probObs_ms: a numeric vector of length 'J' containing state-specific capture probabilities.

alpha

A numeric vector of length 'J' specifying the initial distribution across active states upon entry.

Psi

Matrix of dimension 'J x J' detailing movement transition probabilities between active states.

Value

For single-state models (load_Gamma, load_probObs):

  • load_Gamma: A '3 x 3' state-transition matrix.

  • load_probObs: A '3 x 2' observation probability matrix.

For multistate models (load_Gamma_ms, load_probObs_ms):

  • load_Gamma_ms: A '(J+2) x (J+2)' state-transition matrix (where 'J' is the number of active states).

  • load_probObs_ms: A '(J+2) x 2' observation probability matrix.


Sample latent states for undetected (or detected) individuals in Jolly-Seber models

Description

Forward-filtering backward-sampling functions to sample sequence of latent states given capture histories for Jolly-Seber HMM models.

Usage

sample_det_ms(x, init, probObs, probTrans)

sample_det_binom(x, init, prob, size, probTrans)

sample_det_pois(x, init, rate, probTrans)

sample_det_mark(x, mark, init, prob, probTrans, knots, sigma_knot)

Arguments

x

Vector of observed counts or capture status for an individual across time (length 'K').

init

Vector of initial state probabilities at time 't = 1' of length 'J+2' (where 'J' is the number of intermediate active states, plus 1 initial unentered and 1 final exited state).

probObs

Time-dependent 3D array of observation probabilities. First two dimensions of 'probObs' are of size (number of possible system states) 'x' (number of possible observation classes) and the third dimension is the number of occasions ('K').

probTrans

Time-dependent 3D array of system state transition probabilities with dimensions '(J+2) x(J+2) x K' (where 'J+2' is the total number of system states and 'K' is number of occasions). 'probTrans[r, s, t]' is the probability of transitioning from state 'r' at time 't' to state 's' at time 't+1'.

prob

2D array ('K x J') containing detection probabilities for each intermediate/active state 'J' at time 't'.

size

Vector of sizes (number of trials) for the binomial observation at each time (length 'K').

rate

2D array ('K x J') containing Poisson detection rates for each intermediate/active state 'J' at time 't'.

mark

Vector of continuous observations/marks across time (length 'K').

knots

Vector of knot locations/means corresponding to each intermediate state 'J' (length 'J').

sigma_knot

Standard deviation around knot locations.

Value

A numeric vector of length 'K' containing the sampled latent state sequence.

—————————————————————————– Multistate JS —————————————————————————–


Sample latent states for undetected (unobserved) individuals in Jolly-Seber models

Description

Forward-filtering backward-sampling functions to simulate state trajectories for individuals that were never observed in the capture-recapture study.

Usage

sample_undet_ms(n = 1, init, probObs, probTrans)

sample_undet_binom(n = 1, init, prob, size, probTrans)

sample_undet_pois(n = 1, init, rate, probTrans)

sample_undet_mark(n = 1, init, prob, probTrans)

Arguments

n

Integer number of undetected individuals to simulate.

init

Vector of initial state probabilities at time 't = 1' of length 'J+2' (where 'J' is the number of intermediate active states, plus 1 initial unentered and 1 final exited state).

probObs

Time-dependent 3D array of observation probabilities. First two dimensions of 'probObs' are of size '(J+2) x O' ('O' = number of observation states) and the third dimension is 'K'.

probTrans

Time-dependent 3D array of system state transition probabilities with dimensions '(J+2) x (J+2) x K'. 'probTrans[r, s, t]' is the probability of transitioning from state 'r' at time 't' to state 's' at time 't+1'.

prob

2D array ('K x J') containing detection probabilities for each intermediate/active state 'J' at time 't'.

size

Vector of sizes (number of trials) for the binomial observation at each primary occasion (length 'K').

rate

2D array ('K x J') containing Poisson detection rates for each intermediate/active state 'J' at time 't'.

Value

A matrix of dimension '(J+2) x K' containing the count of undetected individuals in each system state across all occasions 'K'.

—————————————————————————– Multistate JS —————————————————————————–


Simulation output object

Description

Simulation output object

Usage

sim_js_list

Fields

ch

A matrix of observed states for the simulated data (included all zero capture hostories as well)

true_states

A matrix of true states for the simulated data

observed

An indicator vector that shows which individuals were actually captured.


Simulate Traditional Jolly-Seber Capture-Recapture Data

Description

Simulate Traditional Jolly-Seber Capture-Recapture Data

Usage

simulate_js(N, xi, phi, p)

Arguments

N

Superpopulation size

xi

Vector of entry probabilities, not necessarily normalized (length = 'K').

phi

A 'K-1' vector of survival probabilities.

p

A 'K' vector of capture probabilities for when an individual is available for capture.


Simulate Multistate Jolly-Seber Capture-Recapture Data

Description

Simulate Multistate Jolly-Seber Capture-Recapture Data

Usage

simulate_multistate_js(N, xi, alpha, phi, psi, p)

Arguments

N

Superpopulation size

xi

Vector of entry probabilities, not necessarily normalized (length = 'K').

alpha

A matrix of initial state assignment probabilites upon entry (dim = 'J-2'x'K'). The total number of states including pre-birth and death is 'J'.

phi

A 'J-2'x'K-1' matrix of survival probabilities for each of the 'J-2' alive states

psi

A 'J-2'x'J-2'x'K-1' array where 'psi[,,t]' is the probability transition matrix between the 'J-2' alive states.

p

A 'J-2'x'O'x'K' array where 'p[i,j,t]' is the probability of making observation 'j' when the individual is in true state 'i' on occasion 't'. The number of observation states is 'O'. The observation states do not need to be directly related the the true states.