I want to construct hierarchical ordered logit model. I use several ways but get a lot of warnings. I use induced dirichlet prior for threshold and reduce sum to speed up. My stan code like this.
functions {
real partial_sum(
int[] slice_y,
int start, int end,
matrix x1,matrix x2, vector[] thresh,
vector beta1, vector[] beta2, int[] g
)
{
real lp = 0;
for(i in start:end)
lp += ordered_logistic_lpmf(slice_y[i-start+1] |x1[i]*beta1+x2[i]*beta2[g[i]] , thresh[g[i]]);
return lp;
}
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);
}
}
data {
int<lower=1> N; // Number of observations
int<lower=1> K; // Number of ordinal categories
int<lower=1> D; //Number of covariates
int<lower=1> DN; //Number of covariates to estimate use time-varying beta
array[N] int<lower=1, upper=K> y; // Observed ordinals
matrix[N,D-DN] x1;
matrix[N,DN] x2;
int g[N]; //time indicator
int<lower=1> P; //P different time
}
parameters {
vector[D-DN] beta1;
vector[DN] beta2[P]; //time varying beta
ordered[K - 1] thresh[P]; //time varying threshold
real<lower=0> sigma;
vector<lower=0,upper=15>[DN] Omega;
}
model {
vector[DN] Zero= rep_vector(0,DN);
beta1~ normal(0,10);
beta2[1] ~normal(0,10);
//tau ~ cauchy(0,2.5);
//Omega ~ lkj_corr(2);
thresh[1] ~ induced_dirichlet(rep_vector(1, K), 0);
for (i in 1: (P-1)){
(thresh[i+1] - thresh[i]) ~ normal(0,sigma);
//(beta2[i+1]-beta2[i]) ~ normal(0,omega[i]);
(beta2[i+1]-beta2[i]) ~ normal(Zero,Omega);
}
target += reduce_sum(partial_sum, y, 1, x1,x2, thresh, beta1,beta2, g);
}
I have got the warnings like this:
Warning messages:
1: There were 152 divergent transitions after warmup. See
https://mc-stan.org/misc/warnings.html#divergent-transitions-after-warmup
to find out why this is a problem and how to eliminate them.
2: Examine the pairs() plot to diagnose sampling problems
3: Bulk Effective Samples Size (ESS) is too low, indicating posterior means and medians may be unreliable.
Running the chains for more iterations may help. See
https://mc-stan.org/misc/warnings.html#bulk-ess
4: Tail Effective Samples Size (ESS) is too low, indicating posterior variances and tail quantiles may be unreliable.
Running the chains for more iterations may help. See
https://mc-stan.org/misc/warnings.html#tail-ess
And my estimate result is
mean se_mean sd 2.5% 25% 50% 75% 97.5% n_eff Rhat
beta1[1] -17.71 0.20 9.61 -36.27 -24.02 -17.88 -11.39 1.90 2418 1.00
beta1[2] -4.67 0.18 9.95 -23.83 -11.40 -4.60 2.07 15.01 2919 1.00
beta1[3] -19.19 0.24 9.22 -36.77 -25.41 -19.40 -13.25 -0.33 1427 1.00
beta1[4] -0.56 0.02 0.44 -1.53 -0.83 -0.54 -0.24 0.21 824 1.00
beta2[1,1] 7.89 0.17 5.44 -3.72 4.40 8.47 11.79 17.12 1016 1.00
beta2[1,2] -3.45 0.03 1.44 -6.06 -4.47 -3.54 -2.51 -0.47 2296 1.00
beta2[1,3] -0.28 0.00 0.10 -0.46 -0.34 -0.28 -0.22 -0.07 2083 1.00
beta2[1,4] 2.73 0.01 0.25 2.29 2.55 2.71 2.88 3.25 1564 1.00
beta2[1,5] 2.17 0.03 0.83 0.96 1.49 2.05 2.72 3.99 815 1.01
beta2[1,6] 1.33 0.01 0.52 0.35 0.99 1.30 1.64 2.43 2979 1.00
beta2[1,7] -0.13 0.01 0.28 -0.66 -0.31 -0.14 0.03 0.50 2876 1.00
beta2[2,1] 12.75 0.08 4.64 3.59 9.82 12.51 15.34 23.07 3000 1.00
beta2[2,2] -5.68 0.05 1.84 -9.82 -6.78 -5.39 -4.39 -2.67 1286 1.00
beta2[2,3] -0.30 0.00 0.11 -0.54 -0.36 -0.30 -0.24 -0.09 2335 1.00
beta2[2,4] 2.62 0.00 0.20 2.24 2.49 2.61 2.74 3.06 1898 1.00
beta2[2,5] 1.56 0.01 0.51 0.61 1.23 1.52 1.88 2.64 2063 1.00
beta2[2,6] 0.82 0.01 0.49 -0.23 0.50 0.87 1.17 1.67 1735 1.00
beta2[2,7] -0.24 0.01 0.30 -0.95 -0.39 -0.22 -0.07 0.32 2532 1.00
beta2[3,1] 14.35 0.08 3.71 7.68 11.81 14.12 16.58 22.22 2279 1.00
beta2[3,2] -3.61 0.02 1.07 -5.56 -4.39 -3.64 -2.90 -1.40 1852 1.00
beta2[3,3] -0.30 0.00 0.07 -0.43 -0.34 -0.30 -0.25 -0.16 2156 1.00
beta2[3,4] 2.61 0.00 0.15 2.33 2.51 2.61 2.71 2.93 2004 1.00
beta2[3,5] 1.19 0.01 0.31 0.55 0.99 1.20 1.39 1.78 1862 1.00
beta2[3,6] 1.45 0.01 0.34 0.84 1.22 1.42 1.68 2.16 2620 1.00
beta2[3,7] -0.15 0.00 0.21 -0.53 -0.29 -0.16 -0.02 0.30 2377 1.00
beta2[4,1] 12.17 0.07 3.79 4.77 9.72 12.11 14.64 19.68 3236 1.00
beta2[4,2] -6.45 0.06 1.72 -10.21 -7.59 -6.28 -5.16 -3.69 851 1.00
beta2[4,3] -0.30 0.00 0.09 -0.47 -0.35 -0.30 -0.25 -0.10 2597 1.00
beta2[4,4] 2.69 0.01 0.20 2.36 2.54 2.66 2.81 3.16 1135 1.00
beta2[4,5] 1.33 0.01 0.34 0.66 1.12 1.30 1.53 2.06 2198 1.00
beta2[4,6] 0.86 0.01 0.37 0.11 0.61 0.89 1.13 1.51 1726 1.00
beta2[4,7] -0.17 0.00 0.23 -0.59 -0.33 -0.19 -0.04 0.34 2767 1.00
beta2[5,1] 12.68 0.08 3.48 5.81 10.41 12.62 14.93 19.54 1941 1.00
beta2[5,2] -4.76 0.02 0.90 -6.56 -5.37 -4.76 -4.15 -3.01 3032 1.00
beta2[5,3] -0.32 0.00 0.07 -0.46 -0.37 -0.32 -0.28 -0.20 2368 1.00
beta2[5,4] 2.41 0.00 0.16 2.10 2.30 2.41 2.51 2.72 1462 1.00
beta2[5,5] 1.17 0.01 0.27 0.62 0.98 1.18 1.35 1.67 1938 1.00
beta2[5,6] 1.44 0.01 0.28 0.93 1.25 1.43 1.63 2.00 1908 1.00
beta2[5,7] -0.35 0.00 0.20 -0.77 -0.48 -0.34 -0.22 0.00 2124 1.00
thresh[1,1] -1.91 0.03 1.21 -4.33 -2.72 -1.86 -1.09 0.39 1695 1.00
thresh[1,2] 1.03 0.04 1.28 -1.57 0.16 1.03 1.91 3.46 1148 1.00
thresh[1,3] 4.45 0.04 1.38 1.68 3.54 4.44 5.40 7.17 1094 1.00
thresh[1,4] 7.26 0.04 1.41 4.42 6.35 7.26 8.25 9.89 1142 1.00
thresh[2,1] -2.03 0.03 1.39 -4.98 -2.93 -1.95 -1.11 0.50 1747 1.00
thresh[2,2] 1.79 0.03 1.22 -0.62 0.96 1.80 2.59 4.16 1798 1.00
thresh[2,3] 5.72 0.03 1.22 3.42 4.89 5.69 6.52 8.16 1718 1.00
thresh[2,4] 8.30 0.03 1.25 5.91 7.43 8.30 9.12 10.80 1696 1.00
thresh[3,1] -1.75 0.04 1.30 -4.28 -2.63 -1.75 -0.92 0.80 1324 1.01
thresh[3,2] 2.75 0.03 1.22 0.46 1.91 2.72 3.53 5.20 1262 1.01
thresh[3,3] 6.07 0.03 1.23 3.79 5.21 6.05 6.88 8.62 1359 1.01
thresh[3,4] 8.55 0.03 1.26 6.20 7.68 8.53 9.36 11.14 1376 1.00
thresh[4,1] -2.42 0.04 1.52 -5.54 -3.37 -2.36 -1.44 0.50 1622 1.00
thresh[4,2] 2.71 0.04 1.37 0.30 1.76 2.63 3.51 5.61 1189 1.01
thresh[4,3] 6.92 0.05 1.45 4.40 5.93 6.82 7.81 9.95 1034 1.01
thresh[4,4] 9.19 0.05 1.47 6.66 8.19 9.10 10.07 12.27 1051 1.01
thresh[5,1] -2.88 0.04 1.72 -6.66 -3.82 -2.78 -1.76 0.21 1704 1.00
thresh[5,2] 3.50 0.06 1.69 0.68 2.36 3.31 4.48 7.29 778 1.01
thresh[5,3] 7.42 0.06 1.75 4.51 6.22 7.23 8.47 11.43 758 1.01
thresh[5,4] 10.01 0.07 1.83 6.97 8.74 9.79 11.08 14.14 734 1.01
sigma 1.05 0.03 0.73 0.17 0.54 0.88 1.36 2.89 566 1.01
Omega[1] 5.65 0.16 3.87 0.33 2.46 4.99 8.27 14.10 621 1.00
Omega[2] 3.73 0.10 2.90 0.19 1.50 3.10 5.15 11.28 890 1.00
Omega[3] 0.11 0.00 0.14 0.00 0.03 0.07 0.14 0.48 921 1.00
Omega[4] 0.35 0.01 0.32 0.02 0.15 0.27 0.45 1.15 1350 1.00
Omega[5] 0.83 0.03 0.83 0.03 0.28 0.61 1.12 2.96 738 1.01
Omega[6] 1.08 0.03 1.02 0.06 0.42 0.82 1.42 3.81 1313 1.00
Omega[7] 0.39 0.02 0.57 0.02 0.12 0.25 0.48 1.53 859 1.00
lp__ -920.76 0.60 10.95 -940.60 -928.41 -921.10 -914.03 -896.96 330 1.01
I don’t know why I can get these errors. And if my model has some errors to cause the identification problem?