Hi Daniel,
Thanks for the response, and I appreciate I should have given a little mode details.
The caveat you illustrated is precisely what my issue is, as my detection data generating function are designed to create data gaps (to be consistent with real-life monitoring data).
See below the chunk of code related to the initial model fitting and simulation loop (ignore the low number of iteration and simulations, this was just for testing purpose).
The error I get is:
Error in mcmc$run(niter, nburnin = nburnin, thin = thinToUseVec[1], thin2 = thinToUseVec[2], :
in binary sampler, all log probability density values are negative infinity and sampling cannot proceed
Which I believe is related to lack of updating of the sampling strategy as you described above.
The model takes a while to compile, so I think the gain in speed could be substantial, but Can't quite figure out how to set it up properly.
Thanks!
############################################################
## 9. BUILD MODEL ONCE
############################################################
# Constants
constants <- list(
N=N, T=T,
nScout=nScout, nDrone=nDrone,
maxCam=maxCam, nWeek=nWeek,
W=W
)
# --- Simulate initial landscape ---
land <- simulate_landscape(forest_prop, smooth_strength)
z <- simulate_occupancy(land, true)
detec <- simulate_detection(
z,
prop_cell_scout_gap, prop_scout_replicate,
prop_cell_drone_gap, prop_drone_replicate,
pWeekActive, siteActiveProb, seasonGapProb,
maxGapLength, siteWideWeekMiss,
ViewMaps
)
# --- Initialize latent occupancy based on any positive detection ---
z_inits <- matrix(0, N, T)
for(i in 1:N){
for(t in 1:T){
det <- any(detec$yScout[i,t,]==1, na.rm=TRUE) ||
any(detec$yDrone[i,t,]==1, na.rm=TRUE) ||
any(detec$yCam[i,t,,]==1, na.rm=TRUE)
z_inits[i,t] <- ifelse(det, 1, rbinom(1,1,0.3))
}
}
data <- c(land,detec)
# --- Initial values ---
inits <- list(
z = z_inits,
beta0=0, beta1=0, beta2=0,
alpha0=0, alphaNbr=0, alpha2=0, alpha3=0, alphaControl=0,
delta0=0, delta1=0,
theta0=0, theta_adequate=0, theta_good=0,
eta0=0, eta_adequate=0, eta_good=0,
phi0=0, phi_old=0, phi_fresh=0
)
# --- Build and compile Nimble model ---
model <- nimbleModel(code, constants=constants, data=data, inits=inits)
cmodel <- compileNimble(model)
conf <- configureMCMC(model, monitors=c(
"beta0","beta1","beta2",
"alpha0","alphaNbr","alpha2","alpha3","alphaControl",
"delta0","delta1",
"theta0","theta_adequate","theta_good",
"eta0","eta_adequate","eta_good",
"phi0","phi_old","phi_fresh"
))
mcmc <- buildMCMC(conf)
cmcmc <- compileNimble(mcmc, project=model)
############################################################
## 10. SIMULATION LOOP
############################################################
results_A_effL <- list()
for(sim in 1:nSim){
cat("Simulation", sim, "\n")
# --- Simulate landscape, occupancy, detection ---
land <- simulate_landscape(forest_prop, smooth_strength)
z <- simulate_occupancy(land, true)
detec <- simulate_detection(
z,
prop_cell_scout_gap, prop_scout_replicate,
prop_cell_drone_gap, prop_drone_replicate,
pWeekActive, siteActiveProb, seasonGapProb,
maxGapLength, siteWideWeekMiss,
ViewMaps
)
# --- Update model data ---
cmodel$forest[] <- land$forest
cmodel$distCrop[] <- land$distCrop
cmodel$control[,] <- land$control
cmodel$scoutCond[,,] <- detec$scoutCond
cmodel$droneCond[,,] <- detec$droneCond
cmodel$camFOV[,,,] <- detec$camFOV
# --- Update detection data ---
cmodel$setData(list(
yScout = detec$yScout,
yDrone = detec$yDrone,
yCam = detec$yCam
))
# --- Re-initialize latent states based on detection ---
z_init <- matrix(0, N, T)
for(i in 1:N){
for(t in 1:T){
det <- any(detec$yScout[i,t,]==1, na.rm=TRUE) ||
any(detec$yDrone[i,t,]==1, na.rm=TRUE) ||
any(detec$yCam[i,t,,]==1, na.rm=TRUE)
z_init[i,t] <- ifelse(det, 1, rbinom(1,1,0.3))
}
}
cmodel$z[,] <- z_init
# --- Run MCMC ---
samples <- runMCMC(
cmcmc,
niter=2000,
nburnin=1000,
nchains=1,
samplesAsCodaMCMC=TRUE
)
results_A_effL[[sim]] <- samples
}