Hi!
I’m trying to fit a model to some data in stan. I’ve got some weakly informative priors already based off of some early data analysis and have a good idea of what the parameters in the model should be, but I’m running into some issues with my non-centered hierarchical model. I know exactly where the issue is, but I’m looking for some guidance on how to try and get past it. I imagine I’ll need some sort of transformation or rescaling somewhere but I’m not sure how best to do this without drastically changing the model itself. I’ve recreated the crux of the problem:
data{
int<lower = 1> N; //Sample Size
int<lower = 1> n_rep; //Number of replicates
array[n_rep, N] real y; // data
array[N] real x; //Predictor array, all positive
}
parameters{
real<lower=0> mu_theta;
real<lower = 0> tau;
array[n_rep] real theta_tilde;
real<lower = 0> mu_sigma;
real<lower = 0> xi;
array[n_rep] real sigma_tilde;
}
transformed parameters{
array[n_rep] real theta;
array[n_rep] real<lower = 0> sigma;
for (n in 1:n_rep){
theta[n] = mu_theta + tau * theta_tilde[n];
sigma[n] = mu_sigma + xi * sigma_tilde[n];
}
}
model{
mu_theta ~ normal(0.1,0.1);
tau ~ normal(0,0.1);
mu_sigma ~ normal(0,0.1);
xi ~ normal(0,0.1);
for (n in 1:n_rep){
theta_tilde[n] ~ std_normal();
sigma_tilde[n] ~ std_normal();
for(j in 1:N){
y[n,j] ~ normal(theta[n]*x, sigma[n]);
}
}
}
I know that the values for theta for each replicate should be around 0.1 and that the variance is relatively small here too. The issue is that during the step in the transformed parameters block, since, as I understand it, theta_tilde should not be constrained to be positive, this often comes out to be a negative value, the location parameter comes out as negative and I get the “location parameter is nan, but should be finite!” error, presumably as a result of the log of this negative value being taken.
I’ve implemented a near identical model for another dataset with larger parameters and had no issues in the past, so I’d be grateful for any input or suggestions!