Validate transformed params: std[2] is nan, but must be greater than or equal to 0

Part of my code is as follows

`parameters{
corr_matrix[2] omega; //Correlation matrix
matrix[n_p,2] person_offset; //
}
transformed parameters{
vector[n_p] theta;
vector[n_p] tau;
vector<lower=0>[2] std; //standard deviation of theta and tau
std[1]=1; //I want to fix the standard deviation of theta to be 1
matrix[n_p,2] person_parameters;

for (n in 1:n_p) {
person_parameters[n,] =  person_offset[n,] * quad_form_diag(omega, std);
}
theta[1:n_p] = person_parameters[1:n_p,1];
tau[1:n_p] = person_parameters[1:n_p,2];                                        //theta and tau obey the multivariate normal distribution

}

model{
omega ~ lkj_corr(1);
std[2] ~ normal(0, 2.5);
for(n in 1:n_p){
person_offset[n,] ~ normal(0,1);
}
}`

When I run all of my code, I will encounter the following problems:
Chain 1: Rejecting initial value:
Chain 1: Error evaluating the log probability at the initial value.
Chain 1: Exception: validate transformed params: std[2] is nan, but must be greater than or equal to 0 (in ‘model304c3c6a54c6_seq12’ at line 82)

Does anyone know how to solve it?

Yanbin,

I believe that, in your code, std[2] must receive a value in transformed parameters if you are going to put a prior on it in the model block.

I suspect that you want std[2] to be a parameter instead of a “transformed” parameter. So the parameters block could have an extra line:

real std2;

And then, in transformed parameters, you could have

std[2] = std2;

Or you use std2 alone and maybe avoid defining the std vector in transformed parameters.

1 Like

thank you! I will try it