Hi,
I have a factor with different levels and I would like to assign different priors for each level. Could you please help me with that?
As example, I have the following data. M and P are two factors and R the response. M has 5 levels and in following model I have set the same prior for all the 5 levels. However, I would like to assign different prior for each level.
M <- rep(1:5, each=6 )
P <- rep(1:3, each=10 )
R <- runif(30, 0, 2)
dat <- cbind.data.frame(M, P, R)
model.stan <- '
data{
int <lower=1> N;
int M[N];
int P[N];
vector[N] R;
}
parameters{
real M_mu[5];
real P_mu[3];
real<lower=0> sigma;
}
transformed parameters{
real mu[N];
for(n in 1:N)
mu[n] = M_mu[M[n]] + P_mu[P[n]];
}
model{
M_mu ~ normal(0,1);
P_mu ~ normal(0,1);
sigma ~ exponential(1);
R ~ normal( mu, sigma);
}
'
stan_samples <- stan(model_code = model.stan, model_name='new',
data = list(
N = nrow(dat),
M = dat $M,
P = dat $P,
R = dat $R
)
)
print(stan_samples, pars=c('M_mu', 'P_mu','sigma'), probs=c(0.05, 0.95), digits_summary=4)
Thanks