I want to model the hierarchical ordered logit model in stan. And I only want the threshold to vary with the group variables. I am not sure if there is an example or vignette in the hierarchical ordered logit model
First I use the brms code like this
brm(data = data,
family = cumulative(),
y|thres(gr=yr) ~ x1+x2+x3,
iter = 4000, warmup = 2000, chains = 4, cores = 4,
seed = 23
)
The yr
is group variable means I have 3 different groups.
x1,x2,x3
means predicted variables
And I use Stan to model a threshold varying hierarchical model like this
functions {
real induced_dirichlet_lpdf(vector c, vector alpha, real phi) {
int K = num_elements(c) + 1;
vector[K - 1] sigma = inv_logit(phi - c);
vector[K] p;
matrix[K, K] J = rep_matrix(0, K, K);
// Induced ordinal probabilities
p[1] = 1 - sigma[1];
for (k in 2:(K - 1))
p[k] = sigma[k - 1] - sigma[k];
p[K] = sigma[K - 1];
// Baseline column of Jacobian
for (k in 1:K) J[k, 1] = 1;
// Diagonal entries of Jacobian
for (k in 2:K) {
real rho = sigma[k - 1] * (1 - sigma[k - 1]);
J[k, k] = - rho;
J[k - 1, k] = rho;
}
return dirichlet_lpdf(p | alpha)
+ log_determinant(J);
}
}
pper=K> y[N]; // Observed ordinals
row_vector[D] x[N];
int g[N];
}
parameters {
vector[D] beta; // Latent effect
ordered[K - 1] c; // (Internal) cut points
real a[M];
real<lower=0,upper=10> sigma;
}
model {
// Prior model
//gamma ~ normal(0, 1);
a ~ normal(0,sigma);
c ~ induced_dirichlet(rep_vector(1, K), 0);
// Observational model
for (n in 1:N){
y[n] ~ ordered_logistic(x[n]* beta + a[g[n]], c);
}
}
generated quantities {
int<lower=1, upper=K> y_ppc[N];
for (n in 1:N)
y_ppc[n] = ordered_logistic_rng(x[n,]*beta, c);
}
I use awkward coding to separate data to 3 groups and input to stan.
I have two questions : 1. what is the different with brms code and stan in my hierarchical model
2. if I did wrong in writing this stan code for my model