I am currently trying to approximate the Poisson distribution with Normal for my prior in the Bayesian model. The code has no error, but then I keep encountering this error, which I think is due to the very sparse data feed into the model.
data {
int<lower=1> ncol;
int<lower=1> nrow;
vector[ncol] yH;
vector[nrow] x;
#int x[nrow];
matrix[nrow, ncol] A;
vector[nrow] sigma_x;
vector[ncol] sigma_y;
#matrix[nrow,ncol] sigma_x;
#matrix[nrow,ncol] sigma_y;
vector[nrow] epsilon;
}
parameters {
vector<lower = 0>[ncol] yT;
}
model {
yT ~ normal(yH, sqrt(yH)); //to approximate the fact that yT ~ poisson(yH)
x ~ normal(A*yT + epsilon, sigma_x);
//x ~ multi_normal(A*yT + epsilon, sigma_x);
}
I used the following R code to feed in the data and run:
foreach(i = 1:20) %dopar% {
nrow = nrow(rte_m[[i]]);
ncol = ncol(rte_m[[i]]);
A â as.matrix(rte_m[[i]]);
sigma_x â as.vector(sample.int(10, nrow(kf_vect[[i]]), replace=TRUE))
sigma_y â as.vector(eps_vect[[i]])
yH â as.vector(dh_vect[[i]]$X);
yT â yH + as.vector(eps_vect[[i]]);
epsilon â sample.int(15, nrow(kf_vect[[i]]), replace=TRUE)
x â round(as.vector(as.matrix(rte_m[[i]])%*%yT) + epsilon)
iterations = 200;
#input it into our Stan model file âstamodeling.stanâ
stanmodel1 â stan_model(file = âpoissnorm.stanâ,
model_name = âstanmodel1â);#NUTS (No U-Turn) sampler to generate posterior distribution
stanfit â sampling(stanmodel1, cores = parallel::detectCores(), data = list(ncol = ncol,nrow = nrow,
yH = yH,
x=x, epsilon = epsilon,
A = A, sigma_y = sigma_y)
,iter=iterations, chains = 3, control = list(max_treedepth=13));
print(i)
When I run, it starts very fast, but then it gave me this error message that I cannot fix:
Rejecting initial value:
Error evaluating the log probability at the initial value.
Exception: normal_lpdf: Random variable[1] is nan, but must not be nan! (in âmodel23728163056_stanmodel1â at line 24)
I remember I could add something to change the initial starting point of MCMC, but I am not sure if that even helps in this case. Can someone please help me on this issue? I encountered it twice, but failed to overcome it:((