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)
Run the code above in your browser using DataLab