Hi,
I read lots of conversations to see if I could find a solution for the errors I get running nimble. It seems that people had similar problems than me however I wasn't able to solve mine by following the corresponding tips (code and data set at the end).
1. warnings while running the model.
I have several warnings when I am running the model:
* Warning: R object of different size than NimArrDouble. R obj has size 1 but NimArrDbl has size 4.
Warning: R object of different size than NimArrDouble. R obj has size 1 but NimArrDbl has size 2.
Warning: R object of different size than NimArrDouble. R obj has size 1 but NimArrDbl has size 2.
* warning: value of deterministic node phi[278, 9]: value is NA or NaN even after trying to calculate.
* warning: problem initializing stochastic node z[672, 27]: logProb is -Inf.
* warning: problem initializing stochastic node z[278, 10]: logProb is NA or NaN.
The model results of p and phi are coherent, so it seems that the model is working. Are these warnings a problem or they are just informative ? Do you identify something wrong in my code or these warnings are due to my dataset?
2. error in visualising results
I can't use the nimble functions to visualise my data.
Neither MCMCsummary(), MCMCtrace(), or plot() are working.
I already tried to look at mcmc.phitpt$samples but it still creates the following error message :
Error in quantile.default(newX[, i], ...) :
missing values and NaN's not allowed if 'na.rm' is FALSE
It does not surprise me, I have NAs in my data set.
As I was unable to find the solution, I tried to calculate the summary by myself by calculating the mean, sd, quantile of the model output. However, I didn't find any solution to calculate Rhat and n.eff.
I found some function on internet that could have helped but again, I am having errors.
I tried for n.eff:
coda::effectiveSize(mcmc.phitpt$samples)
for Rhat:
coda::gelman.diag(mcmc.phitpt, confidence = 0.95, transform=FALSE, autoburnin=TRUE,
multivariate=TRUE)
Error message is the same for both:
Error in mcmc.list(x) : Arguments must be mcmc objects
I converted mcmc.phitpt$samples into a mcmc object using coda::as.mcmc() but I keep having the same error.
Could you help please ?
My code and data :
My data set is a table of 6+28 columns (corresponding to 6 variables + 28 sampling occasions) and 730 individuals.
Each individual was tagged in a specific site (4 sites), at a specific year (2 years). There are 6 or 8 receivers on each site.
I was only able to develop the model by having a specific column for each receiver, therefore I have 28 columns in total with NAs (I created a summarised my data set in joined file - blanks are NAs).
I also tried to have only 8 columns to avoid too much NAs but then I would have had p[site (s), receiver (r), year (t)] instead of p[r, t] that complicated again the extraction of the results.
Thank you in advance for your help. It would be very helpful as I tried everything I was thinking of.
Céline
### IMPORT DATA ----
Estuary <- read.csv("data set.csv", sep = ",")
site <- Estuary$River
sex <- Estuary$Sex
species <- Estuary$Species
size <- Estuary$ZSize
WL <- Estuary$ZresWL
y <- select(Estuary, -c(1:6))
y <- as.matrix(y)
### MODEL HMM VF ----
#---
phitpt <- nimbleCode({
# coefficients
alpha ~ dnorm(mean = 0, sd = 1.5)
beta_site[1] <- 0
beta_site[2] ~ dnorm(0, 1.5)
beta_site[3] ~ dnorm(0, 1.5)
beta_site[4] ~ dnorm(0, 1.5)
beta_species[1] <- 0
beta_species[2] ~ dnorm(0, 1.5)
beta_sex[1] <- 0
beta_sex[2] ~ dnorm(0, 1.5)
beta_size ~ dnorm(0, 1.5)
beta_WL ~ dnorm(0, 1.5)
beta_dist ~ dnorm(0, 1.5)
delta[1] <- 1
delta[2] <- 0
# phi: coded to vary in between receivers (r)
for (i in 1:N) {
for (r in first[i]:last[i]-1) {
logit(phi[i, r]) <- alpha + beta_site[site[i]] + beta_species[species[i]] + beta_sex[sex[i]] +
(beta_size * size[i]) + (beta_WL * WL[i])
}
}
# p: vary between receivers (r) and year of study (t)
for (r in 1:T-1){
for (t in 1:2){
p[t, r] ~ dunif(0,1)
}
}
# transition matrices
for (i in 1:N){
for (r in first[i]:last[i]-1){
gamma[1, 1, i, r] <- phi[i, r] # alive at t-1 & alive at t
gamma[1, 2, i, r] <- 1 - phi[i, r] # alive at t-1 & dead at t
gamma[2, 1, i, r] <- 0 # dead at t-1 & alive at t
gamma[2, 2, i, r] <- 1 # dead at t-1 & dead at t
omega[1, 1, i, r] <- 1 - p[year[i], r] # alive & detected at rec a
omega[1, 2, i, r] <- p[year[i], r] # alive & not detected at rec a
omega[2, 1, i, r] <- 1 # dead & detected at rec a
omega[2, 2, i, r] <- 0 # dead & not detected at rec r
}
}
# state-space model likelihood
for (i in 1:N) {
z[i, first[i]] ~ dcat(delta[1:2])
for (r in (first[i]+1):last[i]) {
z[i, r] ~ dcat(gamma[z[i, r - 1], 1:2, i, r - 1])
y[i, r] ~ dcat(omega[z[i, r], 1:2, i, r - 1])
}
}
})
## data
my.data <- list(y = y + 1)
## constants
first <- apply(y, 1, function(x) min(which(x !=0)))
last <- apply(y, 1, function(x) max(which(!
is.na(x))))
my.constants <- list(N = nrow(y),
T = ncol(y),
site = site,
year = year,
first = first,
size = size,
species = species,
sex = sex,
WL = WL,
last = last)
## Set initial values
zinits <- y + 1 # non-detection -> alive
zinits[zinits == 2] <- 1 # dead -> alive
initial.values <- function() list(alpha = rnorm(1,0,1),
beta_site = rnorm(1,0,1),
beta_species = rnorm(1,0,1),
beta_sex = rnorm(1,0,1),
beta_size = rnorm(1,0,1),
beta_WL = rnorm(1,0,1),
beta_dist = rnorm(1,0,1),
z = zinits)
# Define parameters to save
#---
parameters.to.save <- c("phi","p", "z", "beta_site", "beta_species", "beta_sex", "beta_size",
"beta_WL", "beta_dist")
## mcmc details
n.iter <- 5000
n.burnin <- 1000
n.chains <- 2
n.thin <- 1
## fit
mcmc.phitpt <- nimbleMCMC(code = phitpt,
constants = my.constants,
data = my.data,
monitors = parameters.to.save,
inits = initial.values,
thin = n.thin,
niter = n.iter,
nburnin = n.burnin,
nchains = n.chains,
WAIC = TRUE)