# 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
# using rm(list = ls(pattern = "^str"))
# 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)
Run the code above in your browser using DataLab