I’m trying to simulate CR (and ultimately IPM) data from a nimble. While the CJS model worked fine, for some reason, I can’t get the multistate model to work: I keep getting multiple rows of the following warnings
[Warning] Dynamic index out of bounds: dcat(prob = S[z[i, t_minus_1], i, t_minus_1, 1:3])
[Warning] Dynamic index out of bounds: dcat(prob = E[z[i, t], i, t_minus_1, 1:3])
(with S and E being my transition and observation matrix, respectively).
I am fairly certain that my indexing is fine, and that I have provided all constants used in the loops (the nimble model code works fine when fitted to data simulated using R code). I checked whether I would be able to simulate data using the nimble model despite the warnings, but unsurprisingly, it doesn’t work: I get NA on the occasion of first capture, and NaN on all the following occasions in both the matrix of capture histories and the matix of latent states. I imagine I should specify the latent states at first capture or something along these lines, but I’m not sure how. What am I missing?
The code is attached.
Many thanks in advance,
Marwan
--
You received this message because you are subscribed to the Google Groups "nimble-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to nimble-users...@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/nimble-users/c5844d43-c19e-4596-95b3-3b22db79113bn%40googlegroups.com.
Hi Chris,
Many thanks for your answer! I have now (i) added `includeData=TRUE` when calling ` simulate `, (ii) added `“y” ` in `dependencies`, and (iii) added initial value of y (on “first” only).
This indeed got rid of the warning messages, and the z’s are now simulated correctly. However, the y’s on occasions following first capture are not simulated, in spite of `includeData=TRUE`(in fact I get the same result as when `includeData=F`). Any idea why?
The updated code is attached
As always, thanks a lot for taking time to help,
Marwan
To view this discussion visit https://groups.google.com/d/msgid/nimble-users/c697246c-c0af-4d45-b68c-bf651885e982n%40googlegroups.com.
Hi Perry,
Thank you for your answer. I have tried both options but none seem to work (unless I did something wrong). Whether or not non-NA values are set for all nodes of y, and whether or not I specify "includePredictive=TRUE" when I call getDependencies, the y’s never appear in the result of getDependencies, and predictably are not simulated when I call simulate.
One thing I noticed is that the z’s are simulated even though they are flagged as posterior predictive nodes (whether or not I specify "includePredictive=TRUE" when I call getDependencies).
Any idea what is going wrong for the y’s?
The reproducible example is attached.
Many thanks,
Marwan
To view this discussion visit https://groups.google.com/d/msgid/nimble-users/4fbcde2c-5562-4ef7-89f4-eeadce14a34dn%40googlegroups.com.
Hi Perry,
Thank you for your answer.
Ah I should have seen the typo, I was too quick.
Indeed, now the y’s appear in the result of getDependencies. However, they are still not simulated and now even the z’s are not simulated anymore. Including “y” back in the ‘dependencies’ vector (now renamed ‘parameters’ as you suggested) seems to ensure that the z’s are simulated (though the pre-defined y values are still not simulated over).
Do you see what is wrong (the code is below, and the model is toy-sized now)? Sorry for the hassle.
Many thanks,
Marwan
--------------------------------------------------------
library(nimble)
multistate_code <- nimbleCode({
# Priors and constraints
for (i in 1:n_ind){
for (t in 1:(n_occasions-1)){
phi[i, t] <- mu_phi
beta[i, t] <- mu_beta
p[i, t] <- mu_p
} #t
} #i
mu_phi ~ dunif(0, 1) # Prior for mean survival prob
mu_beta ~ dunif(0, 1) # Prior for mean breeding prob
mu_p ~ dunif(0, 1) # Prior for mean resighting prob
# Define state-transition and observation matrices
for (i in 1:n_ind){
# Define probabilities of state S(t+1) given S(t)
for (t in first[i]:(n_occasions-1)){
S[1, i, t, 1] <- phi[i, t]*(1-beta[i, t])
S[1, i, t, 2] <- phi[i, t]*beta[i, t]
S[1, i, t, 3] <- 1-phi[i, t]
S[2, i, t, 1] <- phi[i, t]*(1-beta[i, t])
S[2, i, t, 2] <- phi[i, t]*beta[i, t]
S[2, i, t, 3] <- 1-phi[i, t]
S[3, i, t, 1] <- 0
S[3, i, t, 2] <- 0
S[3, i, t, 3] <- 1
# Define probabilities of O(t) given S(t)
E[1, i, t, 1] <- p[i, t]
E[1, i, t, 2] <- 0
E[1, i, t, 3] <- 1-p[i, t]
E[2, i, t, 1] <- 0
E[2, i, t, 2] <- p[i, t]
E[2, i, t, 3] <- 1-p[i, t]
E[3, i, t, 1] <- 0
E[3, i, t, 2] <- 0
E[3, i, t, 3] <- 1
} #t
} #i
# Likelihood
for (i in 1:n_ind){
# Define latent state at first capture
z[i, first[i]] <- y[i, first[i]]
for (t in (first[i]+1):n_occasions){
# State process: draw S(t) given S(t-1)
z[i, t] ~ dcat(S[z[i, t-1], i, t-1, 1:3])
# Observation process: draw O(t) given S(t)
y[i, t] ~ dcat(E[z[i, t], i, t-1, 1:3])
} #t
} #i
})
# ~ 2. Define parameters -------------------------------------------------------
{
n_occasions <- 3
n_marked <- 3
n_ind <- n_marked*n_occasions - n_marked
mu_phi <- 0.7
mu_beta <- 0.6
mu_p <- 0.4
}
# Bundle constants for the model
constants <- list(n_ind = n_ind,
n_occasions = n_occasions,
first = rep(1:(n_occasions-1), each = n_marked)
)
# Build the model
multistate_model <- nimbleModel(multistate_code,
constants = constants)
# ~ 3. Simulate the data -------------------------------------------------------
# First option from Perry ++++++++++++++++++++++++++++++++++++++++++++++++++++++
# Initialize y on the first capture, put 1 on all following occasions
y <- matrix(data = NA, nrow = n_ind, ncol = n_occasions)
first <- rep(1:(n_occasions-1), each = n_marked)
for (i in 1:n_ind) {
y[i, first[i]] <- sample(x = 1:2, size = 1, prob = c(0.4, 0.6))
y[i, (first[i]+1):n_occasions] <- 1
}
# Set y as data
multistate_model$setData(list(y = y))
multistate_model$isData("y") # y is marked as data
# Set initial values
initial_values <- list(mu_phi = mu_phi,
mu_beta = mu_beta,
mu_p = mu_p)
multistate_model$setInits(initial_values)
parameters <- c("mu_phi", "mu_beta", "mu_p", "y")
nodesToSim <- multistate_model$getDependencies(parameters,
self = F,
downstream = T)
# Compile the model
c_multistate_model <- compileNimble(multistate_model)
# simulate
list.simul <- list()
# simulate 2 datasets a
for (i in 1:2) {
set.seed(i)
c_multistate_model$simulate(nodes = nodesToSim,
includeData = T) # forced simulation for nodes flagged as data
list.simul[[i]] <- list(y = c_multistate_model$y,
z = c_multistate_model$z)
print(i)
}
list.simul[[1]]$y
list.simul[[1]]$z
-----------------------------------------------------
To view this discussion visit https://groups.google.com/d/msgid/nimble-users/12d68f43-fc35-47e2-a1f9-1a6cf25291c8n%40googlegroups.com.