Title: | Multi-Species N-Mixture (MNM) Models with 'nimble' |
---|---|
Description: | Simulating data and fitting multi-species N-mixture models using 'nimble'. Includes features for handling zero-inflation and temporal correlation, Bayesian inference, model diagnostics, parameter estimation, and predictive checks. Designed for ecological studies with zero-altered or time-series data. Mimnagh, N., Parnell, A., Prado, E., & Moral, R. A. (2022) <doi:10.1007/s10651-022-00542-7>. Royle, J. A. (2004) <doi:10.1111/j.0006-341X.2004.00142.x>. |
Authors: | Niamh Mimnagh [aut, cre], Rafael de Andrade Moral [aut] |
Maintainer: | Niamh Mimnagh <[email protected]> |
License: | GPL (>= 3) |
Version: | 0.1.0 |
Built: | 2025-03-20 05:22:26 UTC |
Source: | https://github.com/niamhmimnagh/multinmix |
Computes the Akaike Information Criterion (AIC) for an object of class "MNM"
.
AIC is a metric used for model comparison, balancing goodness of fit and model complexity.
The formula for AIC is:
where:
is the log-likelihood of the model.
is the number of parameters in the model.
## S4 method for signature 'MNM' AIC(object)
## S4 method for signature 'MNM' AIC(object)
object |
An object of class |
A numeric value representing the AIC of the fitted model.
Computes the Bayesian Information Criterion (BIC) for an object of class "MNM"
.
BIC is a metric used for model comparison, accounting for goodness of fit and the size of the dataset.
The formula for BIC is:
where:
is the log-likelihood of the model.
is the number of observations in the dataset.
is the number of parameters in the model.
## S4 method for signature 'MNM' BIC(object)
## S4 method for signature 'MNM' BIC(object)
object |
An object of class |
A numeric value representing the BIC of the fitted model.
#' This dataset is a subset of the North American Breeding Bird Survey, containing data collected at 15 routes in Michigan, USA. Each route has 10 stops, and the dataset includes counts for 10 bird species.
birds
birds
A data frame with 600 rows and 13 columns:
An identifier for the 15 survey sites.
The year of the survey (numeric).
The English common name of the bird species surveyed (character).
Count for replicate 1 at the site (numeric).
Count for replicate 2 at the site (numeric).
Count for replicate 3 at the site (numeric).
Count for replicate 4 at the site (numeric).
Count for replicate 5 at the site (numeric).
Count for replicate 6 at the site (numeric).
Count for replicate 7 at the site (numeric).
Count for replicate 8 at the site (numeric).
Count for replicate 9 at the site (numeric).
Count for replicate 10 at the site (numeric).
North American Breeding Bird Survey (https://www.pwrc.usgs.gov/BBS/)
# MNM Model #' # Example: Hurdle Model # Data must first be reformatted to an array of dimension (R,T,S,K) R <- 15 T <- 10 S <- 10 K <- 4 # Ensure data is ordered consistently birds <- birds[order(birds$Route, birds$Year, birds$English_Common_Name), ] # Create a 4D array with proper dimension Y <- array(NA, dim = c(R, T, S, K)) # Map route, species, and year to indices route_idx <- as.numeric(factor(birds$Route)) species_idx <- as.numeric(factor(birds$English_Common_Name)) year_idx <- as.numeric(factor(birds$Year)) # Populate the array stop_data <- as.matrix(birds[, grep("^Stop", colnames(birds))]) for (i in seq_len(nrow(birds))) { Y[route_idx[i], , species_idx[i], year_idx[i]] <- stop_data[i, ] } # Assign dimnames dimnames(Y) <- list( Route = sort(unique(birds$Route)), Stop = paste0("Stop", 1:T), Species = sort(unique(birds$English_Common_Name)), Year = sort(unique(birds$Year)) ) # Selecting only 5 bird species and 1 year for analysis: Y<-Y[,,1:5,1] model<-MNM_fit(Y=Y, AR=FALSE, Hurdle=FALSE)
# MNM Model #' # Example: Hurdle Model # Data must first be reformatted to an array of dimension (R,T,S,K) R <- 15 T <- 10 S <- 10 K <- 4 # Ensure data is ordered consistently birds <- birds[order(birds$Route, birds$Year, birds$English_Common_Name), ] # Create a 4D array with proper dimension Y <- array(NA, dim = c(R, T, S, K)) # Map route, species, and year to indices route_idx <- as.numeric(factor(birds$Route)) species_idx <- as.numeric(factor(birds$English_Common_Name)) year_idx <- as.numeric(factor(birds$Year)) # Populate the array stop_data <- as.matrix(birds[, grep("^Stop", colnames(birds))]) for (i in seq_len(nrow(birds))) { Y[route_idx[i], , species_idx[i], year_idx[i]] <- stop_data[i, ] } # Assign dimnames dimnames(Y) <- list( Route = sort(unique(birds$Route)), Stop = paste0("Stop", 1:T), Species = sort(unique(birds$English_Common_Name)), Year = sort(unique(birds$Year)) ) # Selecting only 5 bird species and 1 year for analysis: Y<-Y[,,1:5,1] model<-MNM_fit(Y=Y, AR=FALSE, Hurdle=FALSE)
This dataset is a subset of the North American Breeding Bird Survey, containing data collected at 24 routes in Michigan, USA. Each route has 10 stops, and the dataset includes counts for 20 bird species.
birds_ZI
birds_ZI
A data frame with 2,880 rows and 13 columns:
An identifier for the 24 survey sites.
The year of the survey (numeric).
The English common name of the bird species surveyed (character).
Count for replicate 1 at the site (numeric).
Count for replicate 2 at the site (numeric).
Count for replicate 3 at the site (numeric).
Count for replicate 4 at the site (numeric).
Count for replicate 5 at the site (numeric).
Count for replicate 6 at the site (numeric).
Count for replicate 7 at the site (numeric).
Count for replicate 8 at the site (numeric).
Count for replicate 9 at the site (numeric).
Count for replicate 10 at the site (numeric).
This dataset represents a subset of the North American Breeding Bird Survey. Data was collected in Michigan over six years, with observations for 20 bird species recorded at 24 routes, each surveyed 10 times. The dataset is used to study avian biodiversity and population trends.
North American Breeding Bird Survey (https://www.pwrc.usgs.gov/BBS/)
data(birds_ZI) head(birds_ZI) # Example: Hurdle Model # Data must first be reformatted to an array of dimension (R,T,S,K) R <- 24 T <- 10 S <- 20 K <- 6 # Ensure data is ordered consistently birds_ZI <- birds_ZI[order(birds_ZI$Route, birds_ZI$Year, birds_ZI$English_Common_Name), ] # Create a 4D array with proper dimension Y <- array(NA, dim = c(R, T, S, K)) # Map route, species, and year to indices route_idx <- as.numeric(factor(birds_ZI$Route)) species_idx <- as.numeric(factor(birds_ZI$English_Common_Name)) year_idx <- as.numeric(factor(birds_ZI$Year)) # Populate the array stop_data <- as.matrix(birds_ZI[, grep("^Stop", colnames(birds))]) for (i in seq_len(nrow(birds))) { Y[route_idx[i], , species_idx[i], year_idx[i]] <- stop_data[i, ] } # Assign dimnames dimnames(Y) <- list( Route = sort(unique(birds_ZI$Route)), Stop = paste0("Stop", 1:T), Species = sort(unique(birds_ZI$English_Common_Name)), Year = sort(unique(birds_ZI$Year)) ) # Selecting only 5 bird species for analysis: Y<-Y[,,1:5,] model<-MNM_fit(Y=Y, AR=TRUE, Hurdle=TRUE)
data(birds_ZI) head(birds_ZI) # Example: Hurdle Model # Data must first be reformatted to an array of dimension (R,T,S,K) R <- 24 T <- 10 S <- 20 K <- 6 # Ensure data is ordered consistently birds_ZI <- birds_ZI[order(birds_ZI$Route, birds_ZI$Year, birds_ZI$English_Common_Name), ] # Create a 4D array with proper dimension Y <- array(NA, dim = c(R, T, S, K)) # Map route, species, and year to indices route_idx <- as.numeric(factor(birds_ZI$Route)) species_idx <- as.numeric(factor(birds_ZI$English_Common_Name)) year_idx <- as.numeric(factor(birds_ZI$Year)) # Populate the array stop_data <- as.matrix(birds_ZI[, grep("^Stop", colnames(birds))]) for (i in seq_len(nrow(birds))) { Y[route_idx[i], , species_idx[i], year_idx[i]] <- stop_data[i, ] } # Assign dimnames dimnames(Y) <- list( Route = sort(unique(birds_ZI$Route)), Stop = paste0("Stop", 1:T), Species = sort(unique(birds_ZI$English_Common_Name)), Year = sort(unique(birds_ZI$Year)) ) # Selecting only 5 bird species for analysis: Y<-Y[,,1:5,] model<-MNM_fit(Y=Y, AR=TRUE, Hurdle=TRUE)
A generic function for checking the convergence of an "MNM"
object.
check_convergence(object)
check_convergence(object)
object |
An object of class |
A numeric value indicating the number of parameters that meet the convergence criterion (Rhat < 1.1).
Checks the convergence status of an object of class "MNM"
based on convergence diagnostics (e.g., Rhat values).
Returns the number of parameters that meet the convergence criterion (Rhat < 1.1).
## S4 method for signature 'MNM' check_convergence(object)
## S4 method for signature 'MNM' check_convergence(object)
object |
An object of class |
A numeric value indicating the number of converged parameters.
Generates the density plot for a specified parameter stored in the MNM object.
## S4 method for signature 'MNM' density(x, param, ...)
## S4 method for signature 'MNM' density(x, param, ...)
x |
An object of class |
param |
The name of the parameter to plot (e.g., |
... |
Additional arguments (not used). |
No return value; displays the density plot for the specified parameter.
# Calling the density function # density(y, "N[10, 1]")
# Calling the density function # density(y, "N[10, 1]")
A named list where each key is a valid Nimble distribution, and the value is a vector of the required arguments.
distribution_args
distribution_args
A named list of length 19.
This object is used internally to validate user-specified prior distributions and their arguments in Nimble models.
Retrieves the log-likelihood value from an object of class "MNM"
.
## S4 method for signature 'MNM' logLik(object)
## S4 method for signature 'MNM' logLik(object)
object |
An object of class |
A numeric value representing the log-likelihood of the fitted model.
Fits a multi-species N-mixture (MNM) model to observed count data, leveraging Nimble for Bayesian inference. This model accounts for variation in detection probability and abundance across multiple species and sites.
MNM( Y = NULL, iterations = 60000, burnin = 20000, thin = 10, Xp = NULL, Xn = NULL, verbose = TRUE, ... )
MNM( Y = NULL, iterations = 60000, burnin = 20000, thin = 10, Xp = NULL, Xn = NULL, verbose = TRUE, ... )
Y |
An array of observed count data of dimension (R, T, S), where:
This array is typically produced by the |
iterations |
Integer. Total number of MCMC iterations to run. Default is 60,000. |
burnin |
Integer. Number of initial MCMC iterations to discard as burn-in. Default is 20,000. |
thin |
Integer. Thinning interval for MCMC samples to reduce autocorrelation. Default is 10. |
Xp |
An array of covariates affecting detection probability, with dimensions (R, S, P1), where:
See examples for implementation details. |
Xn |
An array of covariates affecting abundance, with dimensions (R, S, P2), where:
See examples for implementation details. |
verbose |
Control the level of output displayed during function execution. Default is TRUE. |
... |
Additional arguments passed for prior distribution specification. Supported distributions include dnorm, dexp, dgamma, dbeta, dunif, dlnorm, dbern, dpois, dbinom, dcat, dmnorm, dwish, dchisq, dinvgamma, dt, dweib, ddirch, dmulti, dmvt. Default prior distributions are:
See Nimble (r-nimble.org) documentation for distribution details. |
This function takes observed count data and covariates, then fits an MNM model using Nimble. The model estimates species-specific detection probabilities and abundances, allowing for covariate effects. The function also supports posterior predictive checks by comparing observed counts with predicted values.
An MNM object that contains the following components:
summary: Nimble model summary (mean, standard deviation, standard error, quantiles, effective sample size and Rhat value for all monitored values)
n_parameters: Number of parameters in the model (for use in calculating information criteria)
data: Observed abundances
fitted_Y: Predicted values for Y. Posterior predictive checks can be performed by comparing fitted_Y with the observed data.
logLik: Log-likelihood of the observed data (Y) given the model parameters.
n_converged: Number of parameters with successful convergence (Rhat < 1.1).
plot: traceplots and density plots for all monitored variables.
Ensure that the dimensions of Y
, Xp
, and Xn
match the requirements specified above. Mismatched dimensions will result in errors during model fitting.
Royle, J. A. (2004). N-mixture models for estimating population size from spatially replicated counts. Biometrics, 60(1), 108-115.
Mimnagh, N., Parnell, A., Prado, E., & Moral, R. D. A. (2022). Bayesian multi-species N-mixture models for unmarked animal communities. Environmental and Ecological Statistics, 29(4), 755-778.
simulateData
: For generating example datasets compatible with this function.
# Example 1: # Covariates must be of dimension (R, S, P1/P2). If covariates of an alternative dimension # are used, they must first be coerced into the right format. # If we have two abundance-covariates, one site-level covariate and one species-level # covariate, they may be combined as follows: R <- 10 # Number of sites S <- 5 # Number of species T<-5 Y <- array(sample(0:10, 100, replace = TRUE), dim = c(R, T, S)) covariate_1 <- runif(R) # Site-level covariate covariate_2 <- runif(S) # Species-level covariate # Expand covariate_1 to have S columns expanded_covariate_1 <- matrix(rep(covariate_1, S), nrow = R, ncol = S) # Expand covariate_2 to have R rows expanded_covariate_2 <- t(matrix(rep(covariate_2, R), nrow = S, ncol = R)) # Combine into an array of dimensions (R, S, 2) Xn <- array(c(expanded_covariate_1, expanded_covariate_2), dim = c(R, S, 2)) dim(Xn) # this is now in the correct format and can be used. result <- MNM(Y, Xn = Xn) # nimble creates auxiliary functions that may be removed after model run is complete # str_objects <- ls(pattern = "^str") # rm(list = str_objects) print(result@summary) #' data(birds) # Example 2: North American Breeding Bird Data # Data must first be reformatted to an array of dimension (R,T,S,K) R <- 15 T <- 10 S <- 10 K <- 4 # Ensure data is ordered consistently birds <- birds[order(birds$Route, birds$Year, birds$English_Common_Name), ] # Create a 4D array with proper dimension Y <- array(NA, dim = c(R, T, S, K)) # Map route, species, and year to indices route_idx <- as.numeric(factor(birds$Route)) species_idx <- as.numeric(factor(birds$English_Common_Name)) year_idx <- as.numeric(factor(birds$Year)) # Populate the array stop_data <- as.matrix(birds[, grep("^Stop", colnames(birds))]) for (i in seq_len(nrow(birds))) { Y[route_idx[i], , species_idx[i], year_idx[i]] <- stop_data[i, ] } # Assign dimnames dimnames(Y) <- list( Route = sort(unique(birds$Route)), Stop = paste0("Stop", 1:T), Species = sort(unique(birds$English_Common_Name)), Year = sort(unique(birds$Year)) ) # Selecting only 5 bird species and 1 year for analysis: Y<-Y[,,1:5,1] model<-MNM_fit(Y=Y, AR=FALSE, Hurdle=FALSE, iterations=5000, burnin=1000)
# Example 1: # Covariates must be of dimension (R, S, P1/P2). If covariates of an alternative dimension # are used, they must first be coerced into the right format. # If we have two abundance-covariates, one site-level covariate and one species-level # covariate, they may be combined as follows: R <- 10 # Number of sites S <- 5 # Number of species T<-5 Y <- array(sample(0:10, 100, replace = TRUE), dim = c(R, T, S)) covariate_1 <- runif(R) # Site-level covariate covariate_2 <- runif(S) # Species-level covariate # Expand covariate_1 to have S columns expanded_covariate_1 <- matrix(rep(covariate_1, S), nrow = R, ncol = S) # Expand covariate_2 to have R rows expanded_covariate_2 <- t(matrix(rep(covariate_2, R), nrow = S, ncol = R)) # Combine into an array of dimensions (R, S, 2) Xn <- array(c(expanded_covariate_1, expanded_covariate_2), dim = c(R, S, 2)) dim(Xn) # this is now in the correct format and can be used. result <- MNM(Y, Xn = Xn) # nimble creates auxiliary functions that may be removed after model run is complete # str_objects <- ls(pattern = "^str") # rm(list = str_objects) print(result@summary) #' data(birds) # Example 2: North American Breeding Bird Data # Data must first be reformatted to an array of dimension (R,T,S,K) R <- 15 T <- 10 S <- 10 K <- 4 # Ensure data is ordered consistently birds <- birds[order(birds$Route, birds$Year, birds$English_Common_Name), ] # Create a 4D array with proper dimension Y <- array(NA, dim = c(R, T, S, K)) # Map route, species, and year to indices route_idx <- as.numeric(factor(birds$Route)) species_idx <- as.numeric(factor(birds$English_Common_Name)) year_idx <- as.numeric(factor(birds$Year)) # Populate the array stop_data <- as.matrix(birds[, grep("^Stop", colnames(birds))]) for (i in seq_len(nrow(birds))) { Y[route_idx[i], , species_idx[i], year_idx[i]] <- stop_data[i, ] } # Assign dimnames dimnames(Y) <- list( Route = sort(unique(birds$Route)), Stop = paste0("Stop", 1:T), Species = sort(unique(birds$English_Common_Name)), Year = sort(unique(birds$Year)) ) # Selecting only 5 bird species and 1 year for analysis: Y<-Y[,,1:5,1] model<-MNM_fit(Y=Y, AR=FALSE, Hurdle=FALSE, iterations=5000, burnin=1000)
Fits a multi-species N-mixture (MNM) model with an autoregressive component (AR-1) using Nimble. This model is suitable for time-series data collected over long periods and includes covariates for abundance and detection probability.
MNM_AR( Y = NULL, iterations = 60000, burnin = 20000, thin = 10, Xp = NULL, Xn = NULL, verbose = TRUE, ... )
MNM_AR( Y = NULL, iterations = 60000, burnin = 20000, thin = 10, Xp = NULL, Xn = NULL, verbose = TRUE, ... )
Y |
Array of observed counts, with dimensions (R, T, S, K), where:
|
iterations |
Integer. Number of MCMC iterations to run. Defaults to 60,000. |
burnin |
Integer. Number of iterations to discard as burn-in. Defaults to 20,000. |
thin |
Integer. Thinning interval for saving MCMC samples. Defaults to 10. |
Xp |
Array of detection covariates with dimensions
|
Xn |
Array of abundance covariates with dimensions
|
verbose |
Control the level of output displayed during function execution. Default is TRUE. |
... |
Additional arguments passed for prior distribution specification. Supported distributions include dnorm, dexp, dgamma, dbeta, dunif, dlnorm, dbern, dpois, dbinom, dcat, dmnorm, dwish, dchisq, dinvgamma, dt, dweib, ddirch, dmulti, dmvt. Default prior distributions are:
See Nimble (r-nimble.org) documentation for distribution details. |
The model incorporates temporal and species-level covariates and accounts for serial correlation in abundance using an AR(1) process. The input data should be structured as an array with dimensions (R,T,S,K). See parameter descriptions for details.
Features include:
Posterior predictive checks via predicted vs observed abundance.
Calculation of log-likelihood for model evaluation.
Automatic monitoring of parameter convergence.
An MNM object that contains the following components:
summary: Nimble model summary (mean, standard deviation, standard error, quantiles, effective sample size and Rhat value for all monitored values).
n_parameters: Number of parameters in the model (for use in calculating information criteria).
data: Observed abundances.
fitted_Y:Predicted values for Y: posterior predictive checks can be performed by comparing fitted_Y with the observed data.
logLik:Log-likelihood of the observed data (Y) given the model parameters.
n_converged: Number of parameters with successful convergence (Rhat < 1.1).
plot: traceplots and density plots for all monitored variables.
Ensure that the dimensions of Y
, Xp
, and Xn
match the requirements specified above. Mismatched dimensions will result in errors during model fitting.
Royle, J. A. (2004). N-mixture models for estimating population size from spatially replicated counts. Biometrics, 60(1), 108-115.
Mimnagh, N., Parnell, A., Prado, E., & Moral, R. D. A. (2022). Bayesian multi-species N-mixture models for unmarked animal communities. Environmental and Ecological Statistics, 29(4), 755-778.
simulateData
: For generating example datasets compatible with this function.
MNM
: For details on creating covariate arrays Xp and Xn.
# Example 1: Y <- array(rpois(1000, lambda = 10), dim = c(10, 10, 5, 2)) Xp <- array(runif(500), dim = c(10, 5, 2, 3)) Xn <- array(runif(1000), dim = c(10, 5, 2, 4)) # Fit the AR-1 model result <- MNM_AR(Y = Y, Xp = Xp, Xn = Xn) # nimble creates auxiliary functions that may be removed after model run is complete # str_objects <- ls(pattern = "^str") # rm(list = str_objects) # Check fitted vs observed abundance plot(result@data, result@fitted_Y) #' data(birds) # Example 2: North American Breeding Bird Data # Data must first be reformatted to an array of dimension (R,T,S,K) R <- 15 T <- 10 S <- 10 K <- 4 # Ensure data is ordered consistently birds <- birds[order(birds$Route, birds$Year, birds$English_Common_Name), ] # Create a 4D array with proper dimension Y <- array(NA, dim = c(R, T, S, K)) # Map route, species, and year to indices route_idx <- as.numeric(factor(birds$Route)) species_idx <- as.numeric(factor(birds$English_Common_Name)) year_idx <- as.numeric(factor(birds$Year)) # Populate the array stop_data <- as.matrix(birds[, grep("^Stop", colnames(birds))]) for (i in seq_len(nrow(birds))) { Y[route_idx[i], , species_idx[i], year_idx[i]] <- stop_data[i, ] } # Assign dimnames dimnames(Y) <- list( Route = sort(unique(birds$Route)), Stop = paste0("Stop", 1:T), Species = sort(unique(birds$English_Common_Name)), Year = sort(unique(birds$Year)) ) # Selecting only 5 bird species for analysis: Y<-Y[,,1:5,] model<-MNM_fit(Y=Y, AR=TRUE, Hurdle=FALSE, iterations=10000, burnin=2000)
# Example 1: Y <- array(rpois(1000, lambda = 10), dim = c(10, 10, 5, 2)) Xp <- array(runif(500), dim = c(10, 5, 2, 3)) Xn <- array(runif(1000), dim = c(10, 5, 2, 4)) # Fit the AR-1 model result <- MNM_AR(Y = Y, Xp = Xp, Xn = Xn) # nimble creates auxiliary functions that may be removed after model run is complete # str_objects <- ls(pattern = "^str") # rm(list = str_objects) # Check fitted vs observed abundance plot(result@data, result@fitted_Y) #' data(birds) # Example 2: North American Breeding Bird Data # Data must first be reformatted to an array of dimension (R,T,S,K) R <- 15 T <- 10 S <- 10 K <- 4 # Ensure data is ordered consistently birds <- birds[order(birds$Route, birds$Year, birds$English_Common_Name), ] # Create a 4D array with proper dimension Y <- array(NA, dim = c(R, T, S, K)) # Map route, species, and year to indices route_idx <- as.numeric(factor(birds$Route)) species_idx <- as.numeric(factor(birds$English_Common_Name)) year_idx <- as.numeric(factor(birds$Year)) # Populate the array stop_data <- as.matrix(birds[, grep("^Stop", colnames(birds))]) for (i in seq_len(nrow(birds))) { Y[route_idx[i], , species_idx[i], year_idx[i]] <- stop_data[i, ] } # Assign dimnames dimnames(Y) <- list( Route = sort(unique(birds$Route)), Stop = paste0("Stop", 1:T), Species = sort(unique(birds$English_Common_Name)), Year = sort(unique(birds$Year)) ) # Selecting only 5 bird species for analysis: Y<-Y[,,1:5,] model<-MNM_fit(Y=Y, AR=TRUE, Hurdle=FALSE, iterations=10000, burnin=2000)
This function generates Nimble code for fitting various Multi-Species N-Mixture (MNM) models. These models can include standard MNM, hurdle components, autoregressive (AR) components, or both hurdle and AR components.
MNM_control( model = "MNM", prior_detection_probability = "dnorm(0, 0.001)", prior_precision = "dwish(Omega[1:S,1:S], df)", prior_mean = "dnorm(0,0.001)", prior_mean_AR = "dnorm(0,0.001)", prior_sd_AR = "dexp(1)", prior_hurdle = "dbeta(1,1)", Xp = NULL, Xn = NULL )
MNM_control( model = "MNM", prior_detection_probability = "dnorm(0, 0.001)", prior_precision = "dwish(Omega[1:S,1:S], df)", prior_mean = "dnorm(0,0.001)", prior_mean_AR = "dnorm(0,0.001)", prior_sd_AR = "dexp(1)", prior_hurdle = "dbeta(1,1)", Xp = NULL, Xn = NULL )
model |
Character. Specifies the type of MNM model to generate. Options are:
|
prior_detection_probability |
Character. Prior distribution for the detection probability intercept ( |
prior_precision |
Character. Prior distribution for the precision matrix for the species-level random effect. Default is |
prior_mean |
Character. Prior distribution for the mean of the species-level random effect ( |
prior_mean_AR |
Character. Prior distribution for the mean of the autoregressive random effect ( |
prior_sd_AR |
Character. Prior distribution for the standard deviation of the autoregressive random effect ( |
prior_hurdle |
Character. Prior distribution for |
Xp |
Array. Covariates influencing detection probability. Dimensions depend on the model:
|
Xn |
Array. Covariates influencing abundance. Dimensions depend on the model:
|
The generated Nimble code can be used to fit Bayesian hierarchical MNM models. Users can specify prior distributions for key model parameters and provide covariate arrays that influence detection probability and abundance. The function validates prior specifications and adapts model code based on the selected model type. Supported models include:
MNM: Standard MNM model
Hurdle: MNM model with a hurdle component
AR: MNM model with an autoregressive component
HurdleAR: MNM model with both hurdle and AR components
This is an internal function, needed for implementing MNM models using the MNM_fit function. While the MNM_fit function allows the user to define prior distributions and linear covariates on detection probability and abundance, if the user wishes to implement more complex models in Nimble (via unsupported prior distributions or non-linear covariate effects), the Nimble model code may be extracted as in examples below, and modified by the user.
Character. Nimble code for the specified MNM model, which can be used for further analysis or fitting.
# Example # In order to implement scenarios involving nonlinear covariate # effects or complex interaction terms, # the user is invited to extract and modify the MNM Nimble code: model<-MNM_control(model="MNM") cat(model)
# Example # In order to implement scenarios involving nonlinear covariate # effects or complex interaction terms, # the user is invited to extract and modify the MNM Nimble code: model<-MNM_control(model="MNM") cat(model)
Fits a Multi-Species N-Mixture (MNM) model to observed count data using Nimble, with options to include autocorrelation (AR) and/or hurdle components for advanced ecological modeling.
MNM_fit( Y = NULL, AR = FALSE, Hurdle = FALSE, Xp = NULL, Xn = NULL, verbose = TRUE, ... )
MNM_fit( Y = NULL, AR = FALSE, Hurdle = FALSE, Xp = NULL, Xn = NULL, verbose = TRUE, ... )
Y |
Array of observed count data:
|
AR |
Logical. Indicates whether to include an autocorrelation component in the model. Defaults to |
Hurdle |
Logical. Indicates whether to include a hurdle component in the model. Defaults to |
Xp |
An array of covariates affecting detection probability, with dimensions (R, S, P1), where:
|
Xn |
An array of covariates affecting abundance, with dimensions (R, S, P2), where:
|
verbose |
Control the level of output displayed during function execution. Default is TRUE. |
... |
Additional arguments for prior distributions. Supported priors include:
|
This function implements the Bayesian MNM model to estimate latent species abundances and inter-species correlations based on observed count data. Extensions include options for incorporating autocorrelation (AR) to account for temporal dependencies and a hurdle model to handle zero inflation in the data. The input data and covariates must conform to specific dimensional requirements as described below.
The MNM model (Mimnagh et al., 2022) builds upon Royle's (2004) N-mixture model by allowing simultaneous modeling of multiple species, enabling inference about inter-species relationships and correlations.
An MNM object that contains the following components:
summary: Nimble model summary (mean, standard deviation, standard error, quantiles, effective sample size and Rhat value for all monitored values)
n_parameters: Number of parameters in the model (for use in calculating information criteria)
data: Observed abundances
fitted_Y: Predicted values for Y. Posterior predictive checks can be performed by comparing fitted_Y with the observed data.
logLik: Log-likelihood of the observed data (Y) given the model parameters.
n_converged: Number of parameters with successful convergence (Rhat < 1.1).
Ensure that the dimensions of Y
, Xp
, and Xn
match the requirements specified above. Mismatched dimensions will result in errors during model fitting.#'
Royle, J. A. (2004). N-mixture models for estimating population size from spatially replicated counts. Biometrics, 60(1), 108-115.
Mimnagh, N., Parnell, A., Prado, E., & Moral, R. D. A. (2022). Bayesian multi-species N-mixture models for unmarked animal communities. Environmental and Ecological Statistics, 29(4), 755-778.
simulateData
: For generating example datasets compatible with this function.
MNM
: For details on creation of covariate arrays Xp and Xn.
# Example 1: Fit a standard MNM model Y <- array(data = rpois(60, lambda = 5), dim = c(3, 5, 4)) # Simulated counts Xp <- array(data = rnorm(60), dim = c(3, 4, 2)) # Detection covariates Xn <- array(data = rnorm(60), dim = c(3, 4, 2)) # Abundance covariates model <- MNM_fit(Y = Y, AR = FALSE, Hurdle = FALSE, Xp = Xp, Xn = Xn) # nimble creates auxiliary functions that may be removed after model run is complete # str_objects <- ls(pattern = "^str") # rm(list = str_objects) #' # Example 2: Fit an MNM model with AR-1 component Y <- array(data = rpois(180, lambda = 5), dim = c(3, 5, 4, 3)) # Simulated counts Xp <- array(data = rnorm(180), dim = c(3, 4, 3, 2)) # Detection covariates Xn <- array(data = rnorm(180), dim = c(3, 4, 3, 2)) # Abundance covariates model <- MNM_fit(Y = Y, AR = TRUE, Hurdle = FALSE, Xp = Xp, Xn = Xn) # Example 3: Fit an MNM model with user-specified prior distributions Y <- array(data = rpois(60, lambda = 5), dim = c(3, 5, 4)) # Simulated counts Xp <- array(data = rnorm(60), dim = c(3, 4, 2)) # Detection covariates Xn <- array(data = rnorm(60), dim = c(3, 4, 2)) # Abundance covariates model <- MNM_fit(Y = Y, AR = FALSE, Hurdle = TRUE, Xp = Xp, Xn = Xn, prior_detection_probability="dnorm(0.01,0.01)") # Access traceplots and density plots tracePlot(model, "N[3, 1]") density(model, "N[3, 1]")
# Example 1: Fit a standard MNM model Y <- array(data = rpois(60, lambda = 5), dim = c(3, 5, 4)) # Simulated counts Xp <- array(data = rnorm(60), dim = c(3, 4, 2)) # Detection covariates Xn <- array(data = rnorm(60), dim = c(3, 4, 2)) # Abundance covariates model <- MNM_fit(Y = Y, AR = FALSE, Hurdle = FALSE, Xp = Xp, Xn = Xn) # nimble creates auxiliary functions that may be removed after model run is complete # str_objects <- ls(pattern = "^str") # rm(list = str_objects) #' # Example 2: Fit an MNM model with AR-1 component Y <- array(data = rpois(180, lambda = 5), dim = c(3, 5, 4, 3)) # Simulated counts Xp <- array(data = rnorm(180), dim = c(3, 4, 3, 2)) # Detection covariates Xn <- array(data = rnorm(180), dim = c(3, 4, 3, 2)) # Abundance covariates model <- MNM_fit(Y = Y, AR = TRUE, Hurdle = FALSE, Xp = Xp, Xn = Xn) # Example 3: Fit an MNM model with user-specified prior distributions Y <- array(data = rpois(60, lambda = 5), dim = c(3, 5, 4)) # Simulated counts Xp <- array(data = rnorm(60), dim = c(3, 4, 2)) # Detection covariates Xn <- array(data = rnorm(60), dim = c(3, 4, 2)) # Abundance covariates model <- MNM_fit(Y = Y, AR = FALSE, Hurdle = TRUE, Xp = Xp, Xn = Xn, prior_detection_probability="dnorm(0.01,0.01)") # Access traceplots and density plots tracePlot(model, "N[3, 1]") density(model, "N[3, 1]")
This function fits a multi-species N-mixture (MNM) model incorporating a Hurdle component to handle zero-inflated data, allowing for robust estimation of abundance and detection probabilities across multiple species and sites.
MNM_Hurdle( Y = NULL, iterations = 60000, burnin = 20000, thin = 10, Xp = NULL, Xn = NULL, verbose = TRUE, ... )
MNM_Hurdle( Y = NULL, iterations = 60000, burnin = 20000, thin = 10, Xp = NULL, Xn = NULL, verbose = TRUE, ... )
Y |
Array of observed counts, with dimensions (R, T, S, K), where:
|
iterations |
Integer. Number of iterations to be used in the JAGS model. Defaults to 60,000. |
burnin |
Integer. Number of iterations to be discarded as burn-in. Defaults to 20,000. |
thin |
Integer. Thinning interval for the MCMC chains. Defaults to 10. |
Xp |
Array of detection covariates with dimensions
|
Xn |
Array of abundance covariates with dimensions
|
verbose |
Control the level of output displayed during function execution. Default is TRUE. |
... |
Additional arguments passed for prior distribution specification. Supported distributions include dnorm, dexp, dgamma, dbeta, dunif, dlnorm, dbern, dpois, dbinom, dcat, dmnorm, dwish, dchisq, dinvgamma, dt, dweib, ddirch, dmulti, dmvt. Default prior distributions are:
See Nimble (r-nimble.org) documentation for distribution details. |
This function uses the Nimble framework to fit a Hurdle model, which combines a truncated Poisson distribution for non-zero counts with a separate process for modeling zero counts. The model is particularly suitable for ecological data with excess zeros, such as species occurrence data.
The model supports covariates influencing both abundance and detection probabilities, and outputs posterior distributions for model parameters, derived quantities, and predicted values. Convergence diagnostics and posterior predictive checks can also be performed using the returned results.
An MNM object that contains the following components:
summary: Nimble model summary statistics (mean, standard deviation, standard error, quantiles, effective sample size and Rhat value for all monitored values)
n_parameters: Number of parameters in the model (for use in calculating information criteria).
data: Observed abundances.
fitted_Y: Predicted values for Y. Posterior predictive checks can be performed by comparing fitted_Y with the observed data.
logLik: Log-likelihood of the observed data (Y) given the model parameters.
n_converged: Number of parameters with successful convergence (Rhat < 1.1).
plot: traceplots and density plots for all monitored variables.
Ensure that the dimensions of Y
, Xp
, and Xn
match the requirements specified above. Mismatched dimensions will result in errors during model fitting.
Royle, J. A. (2004). N-mixture models for estimating population size from spatially replicated counts. Biometrics, 60(1), 108-115.
Mimnagh, N., Parnell, A., Prado, E., & Moral, R. D. A. (2022). Bayesian multi-species N-mixture models for unmarked animal communities. Environmental and Ecological Statistics, 29(4), 755-778.
simulateData
: For generating example datasets compatible with this function.
MNM
: For details on creating covariate arrays Xp and Xn.
# Example 1: Y <- array(rpois(100, lambda = 5), dim = c(10, 5, 2)) Xp <- array(runif(100), dim = c(10, 2, 5)) Xn <- array(runif(100), dim = c(10, 2, 3)) model <- MNM_Hurdle(Y = Y, Xp = Xp, Xn = Xn) # nimble creates auxiliary functions that may be removed after model run is complete # str_objects <- ls(pattern = "^str") # rm(list = str_objects) # Accessing results print(model@summary) #' data(birds) # Example 2: North American Breeding Bird Data # Data must first be reformatted to an array of dimension (R,T,S,K) R <- 15 T <- 10 S <- 10 K <- 4 # Ensure data is ordered consistently birds <- birds[order(birds$Route, birds$Year, birds$English_Common_Name), ] # Create a 4D array with proper dimension Y <- array(NA, dim = c(R, T, S, K)) # Map route, species, and year to indices route_idx <- as.numeric(factor(birds$Route)) species_idx <- as.numeric(factor(birds$English_Common_Name)) year_idx <- as.numeric(factor(birds$Year)) # Populate the array stop_data <- as.matrix(birds[, grep("^Stop", colnames(birds))]) for (i in seq_len(nrow(birds))) { Y[route_idx[i], , species_idx[i], year_idx[i]] <- stop_data[i, ] } # Assign dimnames dimnames(Y) <- list( Route = sort(unique(birds$Route)), Stop = paste0("Stop", 1:T), Species = sort(unique(birds$English_Common_Name)), Year = sort(unique(birds$Year)) ) # Selecting only 5 bird species and 1 year for analysis: Y<-Y[,,1:5,1] model<-MNM_fit(Y=Y, AR=FALSE, Hurdle=TRUE, iterations=5000, burnin=1000)
# Example 1: Y <- array(rpois(100, lambda = 5), dim = c(10, 5, 2)) Xp <- array(runif(100), dim = c(10, 2, 5)) Xn <- array(runif(100), dim = c(10, 2, 3)) model <- MNM_Hurdle(Y = Y, Xp = Xp, Xn = Xn) # nimble creates auxiliary functions that may be removed after model run is complete # str_objects <- ls(pattern = "^str") # rm(list = str_objects) # Accessing results print(model@summary) #' data(birds) # Example 2: North American Breeding Bird Data # Data must first be reformatted to an array of dimension (R,T,S,K) R <- 15 T <- 10 S <- 10 K <- 4 # Ensure data is ordered consistently birds <- birds[order(birds$Route, birds$Year, birds$English_Common_Name), ] # Create a 4D array with proper dimension Y <- array(NA, dim = c(R, T, S, K)) # Map route, species, and year to indices route_idx <- as.numeric(factor(birds$Route)) species_idx <- as.numeric(factor(birds$English_Common_Name)) year_idx <- as.numeric(factor(birds$Year)) # Populate the array stop_data <- as.matrix(birds[, grep("^Stop", colnames(birds))]) for (i in seq_len(nrow(birds))) { Y[route_idx[i], , species_idx[i], year_idx[i]] <- stop_data[i, ] } # Assign dimnames dimnames(Y) <- list( Route = sort(unique(birds$Route)), Stop = paste0("Stop", 1:T), Species = sort(unique(birds$English_Common_Name)), Year = sort(unique(birds$Year)) ) # Selecting only 5 bird species and 1 year for analysis: Y<-Y[,,1:5,1] model<-MNM_fit(Y=Y, AR=FALSE, Hurdle=TRUE, iterations=5000, burnin=1000)
Fits a multi-species N-mixture model (MNM) with an autoregressive (AR-1) component and a Hurdle (zero-altered) component using Nimble. This model is suitable for zero-inflated data and data collected over extended time periods.
MNM_Hurdle_AR( Y = NULL, iterations = 60000, burnin = 20000, thin = 10, Xp = NULL, Xn = NULL, verbose = TRUE, ... )
MNM_Hurdle_AR( Y = NULL, iterations = 60000, burnin = 20000, thin = 10, Xp = NULL, Xn = NULL, verbose = TRUE, ... )
Y |
Array of observed counts with dimensions
|
iterations |
Number of MCMC iterations for model fitting. Default is |
burnin |
Number of initial iterations to discard as burn-in. Default is |
thin |
Thinning interval for the MCMC chains. Default is |
Xp |
Array of detection covariates with dimensions
|
Xn |
Array of abundance covariates with dimensions
|
verbose |
Control the level of output displayed during function execution. Default is TRUE. |
... |
Additional arguments passed for prior distribution specification. Supported distributions include dnorm, dexp, dgamma, dbeta, dunif, dlnorm, dbern, dpois, dbinom, dcat, dmnorm, dwish, dchisq, dinvgamma, dt, dweib, ddirch, dmulti, dmvt. Default prior distributions are:
See Nimble (r-nimble.org) documentation for distribution details. |
The MNM_Hurdle_AR model extends the standard N-mixture model by incorporating:
Hurdle (zero-altered) component: Handles zero-inflated data by modelling excess zeros separately.
Autoregressive (AR-1) component: Accounts for temporal dependencies in abundance data.
The model is fitted to data formatted as produced by the simulateData
function. Covariates affecting detection probability and abundance may also be provided. Results include posterior summaries, model diagnostics, and predictions for posterior predictive checks.
An object of class MNM
with the following components:
summary: Summary statistics for monitored parameters, including mean, standard deviation, standard error, quantiles, effective sample size, and Rhat values.
n_parameters: Number of parameters in the model (useful for calculating information criteria).
data: Observed abundances (Y
).
fitted_Y: Predicted values for Y
. Use these for posterior predictive checks by comparing them with observed data.
logLik: Log-likelihood of the observed data (Y
) given the model parameters.
n_converged: Number of parameters with successful convergence (Rhat < 1.1).
plot: traceplots and density plots for all monitored variables.
Ensure that the dimensions of Y
, Xp
, and Xn
match the requirements specified above. Mismatched dimensions will result in errors during model fitting.
Royle, J. A. (2004). N-mixture models for estimating population size from spatially replicated counts. Biometrics, 60(1), 108-115.
Mimnagh, N., Parnell, A., Prado, E., & Moral, R. D. A. (2022). Bayesian multi-species N-mixture models for unmarked animal communities. Environmental and Ecological Statistics, 29(4), 755-778.
simulateData
: For generating example datasets compatible with this function.
MNM
: For details on creating covariate arrays Xp and Xn.
# Example 1: Simulate data and fit the model # Simulating example data set.seed(42) R <- 5 # Number of sites T <- 10 # Number of replicates S <- 3 # Number of species K <- 2 # Number of time periods P1 <- 2 # Number of detection covariates P2 <- 3 # Number of abundance covariates x<-simulateData(model="HurdleAR", R=R, T=T, ,S=S, K=K) Xp <- array(runif(R * S * K * P1), dim = c(R, S, K, P1)) Xn <- array(runif(R * S * K * P2), dim = c(R, S, K, P2)) # Fit the MNM_Hurdle_AR model result <- MNM_Hurdle_AR(Y = x[["Y"]], Xp = Xp, Xn = Xn) # nimble creates auxiliary functions that may be removed after model run is complete # str_objects <- ls(pattern = "^str") # rm(list = str_objects) # Access results print(result@summary) #' data(birds) # Example 2: North American Breeding Bird Data # Data must first be reformatted to an array of dimension (R,T,S,K) R <- 15 T <- 10 S <- 10 K <- 4 # Ensure data is ordered consistently birds <- birds[order(birds$Route, birds$Year, birds$English_Common_Name), ] # Create a 4D array with proper dimension Y <- array(NA, dim = c(R, T, S, K)) # Map route, species, and year to indices route_idx <- as.numeric(factor(birds$Route)) species_idx <- as.numeric(factor(birds$English_Common_Name)) year_idx <- as.numeric(factor(birds$Year)) # Populate the array stop_data <- as.matrix(birds[, grep("^Stop", colnames(birds))]) for (i in seq_len(nrow(birds))) { Y[route_idx[i], , species_idx[i], year_idx[i]] <- stop_data[i, ] } # Assign dimnames dimnames(Y) <- list( Route = sort(unique(birds$Route)), Stop = paste0("Stop", 1:T), Species = sort(unique(birds$English_Common_Name)), Year = sort(unique(birds$Year)) ) # Selecting only 5 bird species for analysis: Y<-Y[,,1:5,] model<-MNM_fit(Y=Y, AR=TRUE, Hurdle=TRUE, iterations=5000, burnin=1000)
# Example 1: Simulate data and fit the model # Simulating example data set.seed(42) R <- 5 # Number of sites T <- 10 # Number of replicates S <- 3 # Number of species K <- 2 # Number of time periods P1 <- 2 # Number of detection covariates P2 <- 3 # Number of abundance covariates x<-simulateData(model="HurdleAR", R=R, T=T, ,S=S, K=K) Xp <- array(runif(R * S * K * P1), dim = c(R, S, K, P1)) Xn <- array(runif(R * S * K * P2), dim = c(R, S, K, P2)) # Fit the MNM_Hurdle_AR model result <- MNM_Hurdle_AR(Y = x[["Y"]], Xp = Xp, Xn = Xn) # nimble creates auxiliary functions that may be removed after model run is complete # str_objects <- ls(pattern = "^str") # rm(list = str_objects) # Access results print(result@summary) #' data(birds) # Example 2: North American Breeding Bird Data # Data must first be reformatted to an array of dimension (R,T,S,K) R <- 15 T <- 10 S <- 10 K <- 4 # Ensure data is ordered consistently birds <- birds[order(birds$Route, birds$Year, birds$English_Common_Name), ] # Create a 4D array with proper dimension Y <- array(NA, dim = c(R, T, S, K)) # Map route, species, and year to indices route_idx <- as.numeric(factor(birds$Route)) species_idx <- as.numeric(factor(birds$English_Common_Name)) year_idx <- as.numeric(factor(birds$Year)) # Populate the array stop_data <- as.matrix(birds[, grep("^Stop", colnames(birds))]) for (i in seq_len(nrow(birds))) { Y[route_idx[i], , species_idx[i], year_idx[i]] <- stop_data[i, ] } # Assign dimnames dimnames(Y) <- list( Route = sort(unique(birds$Route)), Stop = paste0("Stop", 1:T), Species = sort(unique(birds$English_Common_Name)), Year = sort(unique(birds$Year)) ) # Selecting only 5 bird species for analysis: Y<-Y[,,1:5,] model<-MNM_fit(Y=Y, AR=TRUE, Hurdle=TRUE, iterations=5000, burnin=1000)
The "MNM"
class represents a multi-species N-mixture model object fitted using Nimble.
It contains key model outputs, including parameter estimates, input data, predictions,
log-likelihood, convergence diagnostics, and model summaries.
summary
A data.frame
summarizing the model output, including posterior means, standard deviations, and convergence diagnostics (e.g., Rhat values).
n_parameters
A numeric value indicating the number of parameters estimated in the model.
estimates
Mean estimates for all monitored parameters.
fitted_Y
An array containing the fitted values (predicted responses) for the model.
data
An array containing the input data used to fit the model.
logLik
A numeric value representing the log-likelihood of the model.
n_converged
A numeric value indicating the number of parameters with Rhat < 1.1, showing convergence.
plot
A list containing traceplots and density plots for all monitored variables.
Returns the predicted (fitted) values, denoted as , from an object of class
"MNM"
.
predictY(object) ## S4 method for signature 'MNM' predictY(object)
predictY(object) ## S4 method for signature 'MNM' predictY(object)
object |
An object of class |
An array containing the predicted (fitted) values from the model.
predictY(MNM)
: Predict fitted values for the "MNM" class.
Simulates multi-species correlated abundance data for various Multi-Species N-Mixture (MNM) model types, including standard MNM, Hurdle, AR (autoregressive), and HurdleAR models.
simulateData( model = "MNM", R = 10, S = 2, T = 5, prob = "all", abundance = "small", K = 4, theta = 0.5 )
simulateData( model = "MNM", R = 10, S = 2, T = 5, prob = "all", abundance = "small", K = 4, theta = 0.5 )
model |
Character. Specifies the model type. Options are |
R |
Integer. Number of sites. Default is |
S |
Integer. Number of species. Default is |
T |
Integer. Number of replicates. Default is |
prob |
Character. Specifies the range of detection probabilities:
|
abundance |
Character. Specifies the abundance size:
|
K |
Integer. Number of time points (used for AR models). Default is |
theta |
Numeric. Probability of zero-inflation (used for hurdle models). Default is |
This function generates abundance data for multi-species N-mixture models under different configurations:
MNM: Standard multi-species N-mixture model.
Hurdle: Includes a hurdle component to model zero-inflated data.
AR: Includes an autoregressive (AR) component for temporal dependencies.
HurdleAR: Combines hurdle and AR components for zero-inflation and temporal dependencies. The output includes observed and true abundances, detection probabilities, latent variables, and covariance information for the random effects.
A list containing:
Y: Array of observed abundances.
N: Array of true abundances.
p: Array of detection probabilities.
Sigma: Covariance matrix for the multivariate normal variable a
.
mu: Mean vector for the multivariate normal variable a
.
lambda: Latent abundance rate parameter.
correlation: Correlation matrix derived from Sigma
.
R, T, S, K: Number of sites, sampling occasions, species, and time points.
Additional elements depending on the model type:
phi: Autoregression parameter (AR and HurdleAR models).
muPhi: Mean of the autoregressive parameter (AR and HurdleAR models).
varPhi: Variance of the autoregressive parameter (AR and HurdleAR models).
zeros: Matrix of zero-indicators for hurdle models.
theta: Zero-inflation parameter for hurdle models.
simulateData_MNM
: Helper function for simulating standard MNM data.
simulateData_Hurdle
: Helper function for simulating hurdle MNM data.
simulateData_AR
: Helper function for simulating AR MNM data.
simulateData_Hurdle_AR
: Helper function for simulating hurdle AR MNM data.
# Simulate data for a standard MNM model data <- simulateData(model = "MNM", R = 10, S = 3, T = 5, prob = "all", abundance = "small") # Simulate data for a hurdle model data <- simulateData(model = "Hurdle", R = 10, S = 3, T = 5, prob = "large", abundance = "large", theta = 0.3) # Simulate data for an autoregressive model data <- simulateData(model = "AR", R = 10, S = 2, T = 5, K = 4, prob = "small", abundance = "small") # Simulate data for a hurdle autoregressive model data <- simulateData(model = "HurdleAR", R = 10, S = 3, T = 5, K = 4, prob = "all", abundance = "large", theta = 0.5)
# Simulate data for a standard MNM model data <- simulateData(model = "MNM", R = 10, S = 3, T = 5, prob = "all", abundance = "small") # Simulate data for a hurdle model data <- simulateData(model = "Hurdle", R = 10, S = 3, T = 5, prob = "large", abundance = "large", theta = 0.3) # Simulate data for an autoregressive model data <- simulateData(model = "AR", R = 10, S = 2, T = 5, K = 4, prob = "small", abundance = "small") # Simulate data for a hurdle autoregressive model data <- simulateData(model = "HurdleAR", R = 10, S = 3, T = 5, K = 4, prob = "all", abundance = "large", theta = 0.5)
The tracePlot
function generates trace plots for parameters in objects. This
documentation includes the generic and the method for the "MNM"
class.
tracePlot(x, param, ...) ## S4 method for signature 'MNM' tracePlot(x, param, ...)
tracePlot(x, param, ...) ## S4 method for signature 'MNM' tracePlot(x, param, ...)
x |
An object of class |
param |
The name of the parameter to plot (e.g., |
... |
Additional arguments (not used for |
No return value; displays a trace plot for the specified parameter.
tracePlot(MNM)
: Method for objects of class "MNM"
.
# Assuming `y` is an object of class "MNM" with plots stored # tracePlot(y, "N[8, 1]") Generates a trace plot for parameter N[8, 1]
# Assuming `y` is an object of class "MNM" with plots stored # tracePlot(y, "N[8, 1]") Generates a trace plot for parameter N[8, 1]
Validates a set of user-defined priors to ensure that they specify valid distributions supported by Nimble and have the correct parameters for each distribution.
validate_all_priors(priors)
validate_all_priors(priors)
priors |
A named list of priors specified as strings, where the name is the prior name (e.g., |
The function checks the following:
Whether the specified distribution is supported by Nimble.
Whether the correct number of parameters is provided for the distribution.
If any prior is invalid, the function throws an informative error with details about the issue.
Returns TRUE
if all priors are valid. Throws an error if any prior is invalid.
NIMBLE Development Team (2021). NIMBLE: An R Package for Programming with BUGS Models. https://r-nimble.org/
Nimble documentation for a full list of supported distributions.
Validates a single user-defined prior to ensure it specifies a valid distribution supported by Nimble and has the correct parameters for the distribution.
validate_prior(prior_name, prior_string)
validate_prior(prior_name, prior_string)
prior_name |
A string representing the name of the prior (e.g., |
prior_string |
A string specifying the prior distribution (e.g., |
Returns TRUE
if the prior is valid. Throws an error if the prior is invalid.
validate_prior("prior_mean", "dnorm(0, 0.001)")
validate_prior("prior_mean", "dnorm(0, 0.001)")