I have a model in mind but I am not sure how to write it.
Each unit i \in 1..5 gets discrete value g_i \in 1..3 with G_j \sim \text{Categorical}. Then for each i a h_i \in 1..4 is selected randomly with H_k|G_{j} \sim \text{Categorical}_{j}. Then Y_i|H_k \sim \text{Normal}_k.
But I am a beginner so I would like to ask for some help. How can I write the model I described?
data {
int<lower=0> N;
int<lower=0> K;
int<lower=0> h;
int g[N];
int h[N];
vector[N] y;
}
parameters {
vector[K] alpha;
real<lower=0> sigma;
simplex[K] theta;
simplex[J] phi[K];
vector[K] v;
vector[J] w;
}
model {
// Assign a mean for each group.
for (j in 1:J) {
alpha[j] ~ normal(0, sigma);
}
for (k in 1:K) {
phi[k] ~ dirichlet(w);
}
for (n in 1:N) {
// Choose a group.
g[n] ~ categorical(theta);
// Choose a group.
h[n] ~ categorical(phi[g[n]]);
// Distribution centered at the group mean.
y[n] ~ normal(alpha[h[n]], sigma);
}
}
If both g and h are observed then you don’t need to sum over discrete choices. Those tricks are only needed for inferring unknown discrete quantities.
Your model looks correct but needs priors for w or v. Actually, v doesn’t do anything so it’s totally unconstrained by the model.
I guess it’s saying there’s an out-of-bounds access somewhere?
data {
int<lower=0> N;
int<lower=0> K;
int<lower=0> J;
int g[N];
int h[N];
vector[N] y;
}
parameters {
vector[K] alpha;
real<lower=0> sigma;
simplex[K] theta;
simplex[J] phi[K];
vector[K] v;
vector[J] w;
}
model {
// Assign a mean for each group.
for (j in 1:J) {
alpha[j] ~ normal(0, sigma);
}
for (j in 1:J) {
w[j] ~ exponential(1);
}
for (k in 1:K) {
phi[k] ~ dirichlet(w);
}
for (n in 1:N) {
// Choose a group.
g[n] ~ categorical(theta);
// Choose a group.
h[n] ~ categorical(phi[g[n]]);
// Distribution centered at the group mean.
y[n] ~ normal(alpha[h[n]], sigma);
}
}
Exception: vector[uni] indexing: accessing element out of range. index 4 out of range; expecting index to be between 1 and 3 (in ‘bounds.stan’, line 20, column 8 to column 36)
I expected sigma_across to be ~10, sigma_within to be ~1, and the alphas to be 10,20,30,40. But the results are very different.
data {
int<lower=0> N;
int<lower=0> K;
int<lower=0> J;
int g[N];
int h[N];
vector[N] y;
}
parameters {
vector[J] alpha;
real<lower=0> sigma_within;
real<lower=0> sigma_across;
simplex[K] theta;
simplex[J] phi[K];
vector<lower=0>[J] w;
}
model {
sigma_across ~ exponential(10);
sigma_within ~ exponential(10);
// Assign a mean for each group.
for (j in 1:J) {
alpha[j] ~ normal(0, sigma_across);
}
for (j in 1:J) {
w[j] ~ exponential(10);
}
for (k in 1:K) {
phi[k] ~ dirichlet(w);
}
for (n in 1:N) {
// Choose a group.
g[n] ~ categorical(theta);
// Choose a group.
h[n] ~ categorical(phi[g[n]]);
// Distribution centered at the group mean.
y[n] ~ normal(alpha[h[n]], sigma_within);
}
}
17% divergent transitions is very bad. The sampler is not able to explore the posterior distribution adequately. I wouldn’t trust those results.
The exponential prior for w concentrates near zero and that’s where phi’s Dirichlet prior has very difficult geometry, especially if you don’t have much data. Unless you have a good reason to believe w is near zero you should consider a different prior, for example log-normal.