if (FALSE) {
# the example below fits community occupancy models to the sample data in camtrapR
# models are fit both in JAGS and Nimble
# The data set only contains 5 species and 3 stations, so the results will be nonsense.
# It is only a technical demonstration with the camtrapR workflow
# for more complete examples, see vignette 5
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
species_to_include <- unique(recordTableSample$Species)
DetHist_list <- detectionHistory(
recordTable = recordTableSample,
camOp = camop_no_problem,
stationCol = "Station",
speciesCol = "Species",
recordDateTimeCol = "DateTimeOriginal",
species = species_to_include,
occasionLength = 7,
day1 = "station",
datesAsOccasionNames = FALSE,
includeEffort = TRUE,
scaleEffort = TRUE,
timeZone = "Asia/Kuala_Lumpur"
)
# 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 = DetHist_list$detection_history,
siteCovs = sitecovs,
obsCovs = list(effort = DetHist_list$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)
}
Run the code above in your browser using DataLab