Parameter is nan, even after trying to pass initial values

Hi. I’m having trouble initializing my program.
Here’s my sessionInfo from Rstudio (version 1.1.463):
R version 3.5.2 (2018-12-20)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows >= 8 x64 (build 9200)
rstan version 2.19.2
StanHeaders version 2.18.1-10

I receive this:
Chain 1: Rejecting initial value:
Chain 1: Error evaluating the log probability at the initial value.
Chain 1: Exception: validate transformed params: Ez[i_0__][i_1__] is nan, but must be greater than or equal to 0 (in ‘model42642385cfd_dynex1’ at line 54)

Chain 1:
Chain 1: Initialization between (-2, 2) failed after 100 attempts.
Chain 1: Try specifying initial values, reducing ranges of constrained values, or reparameterizing the model.
[1] “Error in sampler$call_sampler(args_list[[i]]) : Initialization failed.”
[1] “error occurred during calling the sampler; sampling not done”

when running my rstan script, with data that reproduce the error:

library(rstan)
library(Matrix)
options(mc.cores = parallel::detectCores())
rstan_options(auto_write = TRUE)
setwd()

y<-matrix(rbinom(700,prob=0.4,size=25),nrow=100,ncol=7)
y[c(sample(1:700,90))]<-0
time<-c(14:20)
nneg<-matrix(rbinom(700,prob=0.4,size=25),nrow=100,ncol=7)
nneg[c(sample(c(which(y==0)),50))]<-0
nrep<-y+nneg
zone<-c(rep(1:10,10))
nzones<-10
ncells<-100
ntime<-7
m<-ceiling(y*1.15)
search<-m
search[which(search>=1)]<-1
known<-y
known[c(sample(c(which(y==0)),15))]<-1
known[which(known>=1)]<-1
hpd<-rpois(100,1000)
air<-matrix(rbinom(700,prob=0.4,size=1),nrow=100,ncol=7)
distSq<-as.matrix(forceSymmetric(matrix(ceiling(rexp(100,0.05)),nrow=100,ncol=100)))
diag(distSq)<-0
muFPC<-asin(sqrt(rbeta(100,1,9)))
muIMP<-asin(sqrt(rbeta(100,3,7)))

forstan<-list(“y”=y,“time”=time,“nrep”=nrep,“zone”=zone,“m”=m,“search”=search,
“known”=known,“nzones”=nzones,“ncells”=ncells,“ntime”=ntime,
“hpd”=hpd,“air”=air,“distSq”=distSq,“muFPC”=muFPC,
“muIMP”=muIMP,“nNeighbors”=ncells)

search[c(which(search==1))]<-1.0
search[c(which(search==0))]<-0.0

Ezinits<-known
Ezinits[c(which(Ezinits==1))]<-0.85
Ezinits[c(which(Ezinits==0))]<-0.25
Ninits<-m

init_fn ← function() {
list(Ez=Ezinits,N=Ninits)
}

parms<-c(“delta1”)
memory.limit(size=80000000)
#starttime<-Sys.time()

mod ← stan(file = ‘dynex1.stan’, data = forstan,init=init_fn,chains = 1,iter=2000,pars=parms)

How can I make sure the transformed parameter Ez is initialized properly?

I’ve attached my stan program.

I’m afraid I don’t know how else to ask, or what to ask (long-time Jags-user here, doing my best to get better).

Many thanks, and very sincerely,

Matt
dynex1.stan (5.2 KB)

You can only supply initial values for symbols declared in the parameters block, and even then, you almost never need to for Stan. When I run it without initial values, I get

Chain 1: Rejecting initial value:
Chain 1:   Error evaluating the log probability at the initial value.
Chain 1: Exception: poisson_lcdf: Rate parameter is -1.8884, but must be >= 0!  (in 'model5bb2276a1fd4_dynex1' at line 62)

which appears to be due to not having a <lower = 0> in the declaration of real N[ncells,ntime];.

Once I add that, I get the error you are getting. After adding

      if (is_nan(Ez[j,t])) {
        reject("this is wrong: phi = ", phi[j,t-1], "; gamma = ", gamma[j,t], "; lagEz = ", Ez[j,t-1]);
      }

after line 101, it appears that gamma[j,t] is NaN. Digging further, this is due to prod(gammaDistPairs[t,j,1:nNeighbors]) being NaN, due to gammaDistPairs being a function of as-yet-undefined Ez[c,t-1] on line 87.

So, I’m not sure what you were trying to do, but your code is circular as written. And it is much too complicated for an initial attempt. Try to get something working with a much, much simpler model and build up to the model you are trying to estimate

2 Likes

Thank you, Dr. Goodrich!