Chain 1: Initialization between (-2, 2) failed after 100 attempts

Hello, all. I am encountering some difficulties in running my codes.
The following is my Rstan codes
//

data {
int<lower=1> N;
int<lower=1> n;
vector[n] y;
vector[n] t;
vector[n] pi_vector;
int<lower=1> yr[n];
vector[n] m3_vector; // constant, 1.5, length = n
vector[n] m6_vector; // constant, 3.0, length = n
}

parameters {

real mu_m1;
real mu_m2;
real mu_m4;
real mu_m5;
real mu_m7;

real<lower=0> common_sigma;

real<lower=0> tau_1;
real<lower=0> tau_2;
real<lower=0> tau_3;
real<lower=0> tau_4;
real<lower=0> tau_5;
real<lower=0> tau_6;
real<lower=0> tau_7;

}

transformed parameters {
vector[N] m1;
vector[N] m2;
vector[N] m4;
vector[N] m5;
vector[N] m7;
}

model {
for (i in 1:n){
y[i] ~ normal(m1[yr[i]]+m2[yr[i]]cos(2pi_vector[i]*m3_vector[yr[i]]*t[i])+m4[yr[i]]sin(2pi_vector[i]*m3_vector[yr[i]]*t[i])+m5[yr[i]]cos(2pi_vector[i]*m6_vector[yr[i]]*t[yr[i]])+m7[yr[i]]sin(2pi_vector[i]*m6_vector[yr[i]]*t[i]), common_sigma);
}

mu_m1 ~ normal(0.0001, 1);
mu_m2 ~ normal(0.0002, 1);
mu_m4 ~ normal(0.0002, 1);
mu_m5 ~ normal(0.0001, 1);
mu_m7 ~ normal(0.0008, 1);
tau_1 ~ inv_gamma(0.1,0.1);
tau_2 ~ inv_gamma(0.1,0.1);
tau_3 ~ inv_gamma(0.1,0.1);
tau_4 ~ inv_gamma(0.1,0.1);
tau_5 ~ inv_gamma(0.1,0.1);
tau_6 ~ inv_gamma(0.1,0.1);
tau_7 ~ inv_gamma(0.1,0.1);

common_sigma ~ inv_gamma(0.1,0.1);

for (j in 1:N){
m1[j] ~ normal(mu_m1,tau_1);
m2[j] ~ normal(mu_m2,tau_2);
m4[j] ~ normal(mu_m4,tau_4);
m5[j] ~ normal(mu_m5,tau_5);
m7[j] ~ normal(mu_m7,tau_7);
}
}

generated quantities {
real y_rep[n];
for (k in 1:n) {
y_rep[k] = m1[yr[k]]+m2[yr[k]]cos(2pi_vector[k]*m3_vector[yr[k]]*t[k])+m4[yr[k]]sin(2pi_vector[k]*m3_vector[yr[k]]*t[k])+m5[yr[k]]cos(2pi_vector[k]*m6_vector[yr[k]]*t[yr[k]])+m7[yr[k]]sin(2pi_vector[k]*m6_vector[yr[k]]*t[k]);
}
}

//I do not know what is wrong with my codes. I hope someone can provide suggestions. Thank you very much in advance.

Hey

Something which immediately stands out to me is that you have transformed parameters but you do not do any parameter transformations? The transformed parameter block is to transform a parameters using already defined parameters. For instance taking the inverse of a parameter and sampling the inverse instead of the original parameter

In your case it seems like these m1, … parameters are your actual parameters and the mu_m parameters are just used as prior information for which we also place a prior? If I understand your Bayesian model correctly you have something like:

P(m, \mu_m, \tau | y) \propto P(y|m) P(m|\mu_m, \tau) P(\mu_m) P(\tau)

Right? For simplicity I just denote m as all the m values you defined and \mu_m as all the prior means. In this case both m and \mu_m are parameters of your model and you would need to specify it in the parameters block.

I am not completely sure but I think the values for m are being initialized to nan and since you do not assign any values to it, it generates errors (i.e. stan only initializes parameters in the parameters block and then does computations for the transformed parameters). If you print the console messages using show_console argument and it gives something alongs the lines of “random variable is nan” this would confirm my beliefs.

Hi Garren Hermanus,

Thanks for figuring out the errors. Indeed, I am using Rstand to build a simple Bayesian Hierarchical Model. I am new to Rstand programming. Could you tell me how to rewrite the codes? Thanks in advance.