Good morning,
I am trying to run a dynamic N-mixture model to estimate trend of abundance of some amphibian species.
When running, I get this warning message (two times for each chain):
"Warning from SEXP_2_int: input element is a real with a non-integer value"
Do you know what is this warning trying to say? It seems that it might be related to non-integer counts in the negative binomial distribution, however, all counts I have are integer.
Below you find the script to run the model and I attach the data needed to run it.
All the best,
Mattia
setwd("...")
library(nimble)
load("dataset.Rdata")
model <- nimbleCode({
# Prior for the variance of CAR model
N_tau ~ T(dnorm(0, sd = 10), 0, ) # Truncated normal distribution with mean 0 and SD = 10
# Spatial effect - CAR model
N_rho[1:nsite] ~ dcar_normal(adj=adj[1:sumneigh],weights=weights[1:sumneigh],num=num_adj[1:nsite],tau=N_tau, zero_mean = 1)
# Model for abundance
for (k in 1:nyear){
for (i in 1:nsite){
lambda[i,k] <- exp(alpha + beta.trend*year[k] + eps.year[k] + N_rho[i])
}
eps.year[k] ~ dnorm(0, tau.lpsi) # Year random effect
}
alpha ~ dnorm(0, 0.01)
beta.trend ~ dnorm(0, 0.01)
tau.lpsi <- pow(sd.lpsi, -2)
sd.lpsi ~ dunif(0, 10)
# Priors for ecological submodel
v ~ dunif(0,50)
# Ecological submodel
for (i in 1:nsite){
for (k in 1:nyear){
N[i,k] ~ dnegbin(p.nb[i,k], v)
p.nb[i,k]<-v/(v+lambda[i,k])
}
}
# Priors for observational model
p ~ dunif(0, 1)
# Observational model
for (i in 1:nsite){
for (k in 1:nyear){
y[i,k] ~ dbin(p,N[i,k])
}
}
# Derived parameters
for (k in 1:nyear){
for(i in 1:nsite){
N.site[i,k] <- exp(alpha + beta.trend*year[k] + N_rho[i])
}
Ntot[k] <- sum(N[1:nsite,k]) # Total abundance in each year
N.trend[k] <- sum(N.site[1:nsite,k])
}
})
years <- as.numeric(colnames(counts))
# DATA
win.data <- list(y = counts)
constants <- list(nsite = dim(counts)[1], nyear = dim(counts)[2],
year = years-mean(c(min(years),max(years))),
adj = adiacenti,num_adj = num_adiacenti, sumneigh = sumneigh, weights = rep(1,sumneigh))
# parameters to monitor
params <- c("alpha","beta.trend","eps.year","sd.lpsi","N_rho", "N_tau","Ntot","N.trend","p","v")
# inits
inits <-list(alpha = rnorm(1,0,1), beta.trend = rnorm(1,0,1), v = runif(1,0,50),
eps.year = rnorm(dim(counts)[2],0,1), sd.lpsi = dunif(1,0,10),
p = runif(1,0,1), N_tau=runif(1,1,10),N_rho=runif(dim(counts)[1],-1,1))
# MCMC
nc <- 3
ni <- 4*10^3
nb <- ni*2/3
nt <- (ni-nb)/1000
# nimble model
Rmodel <- nimbleModel(code=model, constants=constants, data=win.data, inits = inits)
Rmodel$initializeInfo()
# mcmc
conf <- configureMCMC(Rmodel, print = F)
conf$addMonitors(params)
Rmcmc <- buildMCMC(conf)
# Compile the model and MCMC algorithm
Cmodel <- compileNimble(Rmodel)
Cmcmc <- compileNimble(Rmcmc, project = Rmodel)
# RUN THE MODEL
start.time <- Sys.time()
out <- runMCMC(mcmc = Cmcmc, niter = ni, nburnin = nb, nchains = nc, thin = nt,
samplesAsCodaMCMC = T,inits = inits)
Sys.time()-start.time
# Check results
MCMCvis::MCMCsummary(out, params = c("alpha","beta.trend",
"eps.year","sd.lpsi","N_tau",
"Ntot","N.trend","p","v"))
mcmcplots::mcmcplot(out,parms = c("alpha","beta.trend",
"eps.year","sd.lpsi","N_tau",
"Ntot","N.trend","p","v"))