# NOT RUN {
# }
# NOT RUN {
data("camtraps")
# create camera operation matrix
camop_no_problem <- cameraOperation(CTtable = camtraps,
stationCol = "Station",
setupCol = "Setup_date",
retrievalCol = "Retrieval_date",
hasProblems = FALSE,
dateFormat = "dmy"
)
data("recordTableSample")
# make list of detection histories
DetHist_list <- lapply(unique(recordTableSample$Species), FUN = function(x) {
detectionHistory(
recordTable = recordTableSample,
camOp = camop_no_problem,
stationCol = "Station",
speciesCol = "Species",
recordDateTimeCol = "DateTimeOriginal",
species = x,
occasionLength = 7,
day1 = "station",
datesAsOccasionNames = FALSE,
includeEffort = TRUE,
scaleEffort = TRUE,
timeZone = "Asia/Kuala_Lumpur"
)}
)
# assign species names to list items
names(DetHist_list) <- unique(recordTableSample$Species)
# extract detection histories (omit effort matrices)
ylist <- lapply(DetHist_list, FUN = function(x) x$detection_history)
# create some fake covariates for demonstration
sitecovs <- camtraps[, c(1:3)]
sitecovs$elevation <- c(300, 500, 600)
# scale numeric covariates
sitecovs[, c(2:4)] <- scale(sitecovs[,-1])
# bundle input data for communityModel
data_list <- list(ylist = ylist,
siteCovs = sitecovs,
obsCovs = list(effort = DetHist_list[[1]]$effort))
# create community model for JAGS
modelfile1 <- tempfile(fileext = ".txt")
mod.jags <- communityModel(data_list,
occuCovs = list(fixed = "utm_y", ranef = "elevation"),
detCovsObservation = list(fixed = "effort"),
intercepts = list(det = "ranef", occu = "ranef"),
modelFile = modelfile1)
summary(mod.jags)
# fit in JAGS
fit.jags <- fit(mod.jags,
n.iter = 1000,
n.burnin = 500,
chains = 3)
summary(fit.jags)
# response curves (= marginal effect plots)
plot_effects(mod.jags,
fit.jags,
submodel = "state")
plot_effects(mod.jags,
fit.jags,
submodel = "det")
# effect sizes plot
plot_coef(mod.jags,
fit.jags,
submodel = "state")
plot_coef(mod.jags,
fit.jags,
submodel = "det")
# create community model for Nimble
modelfile2 <- tempfile(fileext = ".txt")
mod.nimble <- communityModel(data_list,
occuCovs = list(fixed = "utm_x", ranef = "utm_y"),
detCovsObservation = list(fixed = "effort"),
intercepts = list(det = "ranef", occu = "ranef"),
modelFile = modelfile2,
nimble = TRUE) # set nimble = TRUE
# load nimbleEcology package
# currently necessary to do explicitly, to avoid additional package dependencies
require(nimbleEcology)
# fit uncompiled model in Nimble
fit.nimble.uncomp <- fit(mod.nimble,
n.iter = 10,
chains = 1)
# fit compiled model in Nimble
fit.nimble.comp <- fit(mod.nimble,
n.iter = 5000,
n.burnin = 2500,
chains = 3,
compile = TRUE)
# parameter summary statistics
summary(fit.nimble.comp)
# response curves (= marginal effect plots)
plot_effects(mod.nimble,
fit.nimble.comp,
submodel = "state")
plot_effects(mod.nimble,
fit.nimble.comp,
submodel = "det")
# effect sizes plot
plot_coef(mod.nimble,
fit.nimble.comp,
submodel = "state")
plot_coef(mod.nimble,
fit.nimble.comp,
submodel = "det")
# traceplots
plot(fit.nimble.comp)
# }
# NOT RUN {
# }
Run the code above in your browser using DataLab