The Stan model (with minor modifications from that generated from brms) is:
functions {
/* compute correlated group-level effects
* Args:
* z: matrix of unscaled group-level effects
* SD: vector of standard deviation parameters
* L: cholesky factor correlation matrix
* Returns:
* matrix of scaled group-level effects
*/
matrix scale_r_cor(matrix z, vector SD, matrix L) {
// r is stored in another dimension order than z
return transpose(diag_pre_multiply(SD, L) * z);
}
/* integer sequence of values
* Args:
* start: starting integer
* end: ending integer
* Returns:
* an integer sequence from start to end
*/
int[] sequence(int start, int end) {
int seq[end - start + 1];
for (n in 1:num_elements(seq)) {
seq[n] = n + start - 1;
}
return seq;
}
// compute partial sums of the log-likelihood
real partial_log_lik_lpmf(int[] seq, int start, int end, data int ncat, data int[] Y, data matrix X_btc, vector b_btc, data matrix X_a1, vector b_a1, data matrix X_a2, vector b_a2, data matrix X_a3, vector b_a3, data matrix X_b1tt, vector b_b1tt, data matrix X_b2tt, vector b_b2tt, data matrix X_b3tt, vector b_b3tt, data matrix X_b4tt, vector b_b4tt, data vector C_mu0_1, data vector C_mu0_2, data vector C_mu1_1, data vector C_mu2_1, data vector C_mu3_1, data int[] J_1, data vector Z_1_btc_1, data vector Z_1_btc_2, data vector Z_1_btc_3, data vector Z_1_btc_4, data vector Z_1_btc_5, data vector Z_1_btc_6, vector r_1_btc_1, vector r_1_btc_2, vector r_1_btc_3, vector r_1_btc_4, vector r_1_btc_5, vector r_1_btc_6, data int[] J_2, data vector Z_2_btc_1, vector r_2_btc_1, data int[] J_3, data vector Z_3_a1_1, data vector Z_3_a1_2, data vector Z_3_a1_3, data vector Z_3_a1_4, data vector Z_3_a1_5, data vector Z_3_a1_6, vector r_3_a1_1, vector r_3_a1_2, vector r_3_a1_3, vector r_3_a1_4, vector r_3_a1_5, vector r_3_a1_6, data int[] J_4, data vector Z_4_a1_1, vector r_4_a1_1, data int[] J_5, data vector Z_5_a2_1, data vector Z_5_a2_2, data vector Z_5_a2_3, data vector Z_5_a2_4, data vector Z_5_a2_5, data vector Z_5_a2_6, vector r_5_a2_1, vector r_5_a2_2, vector r_5_a2_3, vector r_5_a2_4, vector r_5_a2_5, vector r_5_a2_6, data int[] J_6, data vector Z_6_a2_1, vector r_6_a2_1, data int[] J_7, data vector Z_7_a3_1, data vector Z_7_a3_2, data vector Z_7_a3_3, data vector Z_7_a3_4, data vector Z_7_a3_5, data vector Z_7_a3_6, vector r_7_a3_1, vector r_7_a3_2, vector r_7_a3_3, vector r_7_a3_4, vector r_7_a3_5, vector r_7_a3_6, data int[] J_8, data vector Z_8_a3_1, vector r_8_a3_1, data int[] J_9, data vector Z_9_b1tt_1, data vector Z_9_b1tt_2, data vector Z_9_b1tt_3, data vector Z_9_b1tt_4, data vector Z_9_b1tt_5, data vector Z_9_b1tt_6, vector r_9_b1tt_1, vector r_9_b1tt_2, vector r_9_b1tt_3, vector r_9_b1tt_4, vector r_9_b1tt_5, vector r_9_b1tt_6, data int[] J_10, data vector Z_10_b1tt_1, vector r_10_b1tt_1, data int[] J_11, data vector Z_11_b2tt_1, data vector Z_11_b2tt_2, data vector Z_11_b2tt_3, data vector Z_11_b2tt_4, data vector Z_11_b2tt_5, data vector Z_11_b2tt_6, vector r_11_b2tt_1, vector r_11_b2tt_2, vector r_11_b2tt_3, vector r_11_b2tt_4, vector r_11_b2tt_5, vector r_11_b2tt_6, data int[] J_12, data vector Z_12_b2tt_1, vector r_12_b2tt_1, data int[] J_13, data vector Z_13_b3tt_1, data vector Z_13_b3tt_2, data vector Z_13_b3tt_3, data vector Z_13_b3tt_4, data vector Z_13_b3tt_5, data vector Z_13_b3tt_6, vector r_13_b3tt_1, vector r_13_b3tt_2, vector r_13_b3tt_3, vector r_13_b3tt_4, vector r_13_b3tt_5, vector r_13_b3tt_6, data int[] J_14, data vector Z_14_b3tt_1, vector r_14_b3tt_1, data int[] J_15, data vector Z_15_b4tt_1, data vector Z_15_b4tt_2, data vector Z_15_b4tt_3, data vector Z_15_b4tt_4, data vector Z_15_b4tt_5, data vector Z_15_b4tt_6, vector r_15_b4tt_1, vector r_15_b4tt_2, vector r_15_b4tt_3, vector r_15_b4tt_4, vector r_15_b4tt_5, vector r_15_b4tt_6, data int[] J_16, data vector Z_16_b4tt_1, vector r_16_b4tt_1) {
real ptarget = 0;
int N = end - start + 1;
// initialize linear predictor term
vector[N] nlp_btc = X_btc[start:end] * b_btc;
// initialize linear predictor term
vector[N] nlp_a1 = X_a1[start:end] * b_a1;
// initialize linear predictor term
vector[N] nlp_a2 = X_a2[start:end] * b_a2;
// initialize linear predictor term
vector[N] nlp_a3 = X_a3[start:end] * b_a3;
// initialize linear predictor term
vector[N] nlp_b1tt = X_b1tt[start:end] * b_b1tt;
// initialize linear predictor term
vector[N] nlp_b2tt = X_b2tt[start:end] * b_b2tt;
// initialize linear predictor term
vector[N] nlp_b3tt = X_b3tt[start:end] * b_b3tt;
// initialize linear predictor term
vector[N] nlp_b4tt = X_b4tt[start:end] * b_b4tt;
// initialize non-linear predictor term
vector[N] mu0;
// initialize non-linear predictor term
vector[N] mu1;
// initialize non-linear predictor term
vector[N] mu2;
// initialize non-linear predictor term
vector[N] mu3;
// linear predictor matrix
vector[ncat] mu[N];
for (n in 1:N) {
// add more terms to the linear predictor
int nn = n + start - 1;
nlp_btc[n] += exp(r_1_btc_1[J_1[nn]] * Z_1_btc_1[nn] + r_1_btc_2[J_1[nn]] * Z_1_btc_2[nn] + r_1_btc_3[J_1[nn]] * Z_1_btc_3[nn] + r_1_btc_4[J_1[nn]] * Z_1_btc_4[nn] + r_1_btc_5[J_1[nn]] * Z_1_btc_5[nn] + r_1_btc_6[J_1[nn]] * Z_1_btc_6[nn] + r_2_btc_1[J_2[nn]] * Z_2_btc_1[nn]);
}
for (n in 1:N) {
// add more terms to the linear predictor
int nn = n + start - 1;
nlp_a1[n] += r_3_a1_1[J_3[nn]] * Z_3_a1_1[nn] + r_3_a1_2[J_3[nn]] * Z_3_a1_2[nn] + r_3_a1_3[J_3[nn]] * Z_3_a1_3[nn] + r_3_a1_4[J_3[nn]] * Z_3_a1_4[nn] + r_3_a1_5[J_3[nn]] * Z_3_a1_5[nn] + r_3_a1_6[J_3[nn]] * Z_3_a1_6[nn] + r_4_a1_1[J_4[nn]] * Z_4_a1_1[nn];
}
for (n in 1:N) {
// add more terms to the linear predictor
int nn = n + start - 1;
nlp_a2[n] += r_5_a2_1[J_5[nn]] * Z_5_a2_1[nn] + r_5_a2_2[J_5[nn]] * Z_5_a2_2[nn] + r_5_a2_3[J_5[nn]] * Z_5_a2_3[nn] + r_5_a2_4[J_5[nn]] * Z_5_a2_4[nn] + r_5_a2_5[J_5[nn]] * Z_5_a2_5[nn] + r_5_a2_6[J_5[nn]] * Z_5_a2_6[nn] + r_6_a2_1[J_6[nn]] * Z_6_a2_1[nn];
}
for (n in 1:N) {
// add more terms to the linear predictor
int nn = n + start - 1;
nlp_a3[n] += r_7_a3_1[J_7[nn]] * Z_7_a3_1[nn] + r_7_a3_2[J_7[nn]] * Z_7_a3_2[nn] + r_7_a3_3[J_7[nn]] * Z_7_a3_3[nn] + r_7_a3_4[J_7[nn]] * Z_7_a3_4[nn] + r_7_a3_5[J_7[nn]] * Z_7_a3_5[nn] + r_7_a3_6[J_7[nn]] * Z_7_a3_6[nn] + r_8_a3_1[J_8[nn]] * Z_8_a3_1[nn];
}
for (n in 1:N) {
// add more terms to the linear predictor
int nn = n + start - 1;
nlp_b1tt[n] += r_9_b1tt_1[J_9[nn]] * Z_9_b1tt_1[nn] + r_9_b1tt_2[J_9[nn]] * Z_9_b1tt_2[nn] + r_9_b1tt_3[J_9[nn]] * Z_9_b1tt_3[nn] + r_9_b1tt_4[J_9[nn]] * Z_9_b1tt_4[nn] + r_9_b1tt_5[J_9[nn]] * Z_9_b1tt_5[nn] + r_9_b1tt_6[J_9[nn]] * Z_9_b1tt_6[nn] + r_10_b1tt_1[J_10[nn]] * Z_10_b1tt_1[nn];
}
for (n in 1:N) {
// add more terms to the linear predictor
int nn = n + start - 1;
nlp_b2tt[n] += r_11_b2tt_1[J_11[nn]] * Z_11_b2tt_1[nn] + r_11_b2tt_2[J_11[nn]] * Z_11_b2tt_2[nn] + r_11_b2tt_3[J_11[nn]] * Z_11_b2tt_3[nn] + r_11_b2tt_4[J_11[nn]] * Z_11_b2tt_4[nn] + r_11_b2tt_5[J_11[nn]] * Z_11_b2tt_5[nn] + r_11_b2tt_6[J_11[nn]] * Z_11_b2tt_6[nn] + r_12_b2tt_1[J_12[nn]] * Z_12_b2tt_1[nn];
}
for (n in 1:N) {
// add more terms to the linear predictor
int nn = n + start - 1;
nlp_b3tt[n] += r_13_b3tt_1[J_13[nn]] * Z_13_b3tt_1[nn] + r_13_b3tt_2[J_13[nn]] * Z_13_b3tt_2[nn] + r_13_b3tt_3[J_13[nn]] * Z_13_b3tt_3[nn] + r_13_b3tt_4[J_13[nn]] * Z_13_b3tt_4[nn] + r_13_b3tt_5[J_13[nn]] * Z_13_b3tt_5[nn] + r_13_b3tt_6[J_13[nn]] * Z_13_b3tt_6[nn] + r_14_b3tt_1[J_14[nn]] * Z_14_b3tt_1[nn];
}
for (n in 1:N) {
// add more terms to the linear predictor
int nn = n + start - 1;
nlp_b4tt[n] += r_15_b4tt_1[J_15[nn]] * Z_15_b4tt_1[nn] + r_15_b4tt_2[J_15[nn]] * Z_15_b4tt_2[nn] + r_15_b4tt_3[J_15[nn]] * Z_15_b4tt_3[nn] + r_15_b4tt_4[J_15[nn]] * Z_15_b4tt_4[nn] + r_15_b4tt_5[J_15[nn]] * Z_15_b4tt_5[nn] + r_15_b4tt_6[J_15[nn]] * Z_15_b4tt_6[nn] + r_16_b4tt_1[J_16[nn]] * Z_16_b4tt_1[nn];
}
for (n in 1:N) {
int nn = n + start - 1;
// compute non-linear predictor values
mu0[n] = nlp_btc[n] * (nlp_a1[n] + nlp_b1tt[n] * C_mu0_1[nn] + C_mu0_2[nn]);
}
for (n in 1:N) {
int nn = n + start - 1;
// compute non-linear predictor values
mu1[n] = nlp_btc[n] * (nlp_a2[n] + nlp_b2tt[n] * C_mu1_1[nn]);
}
for (n in 1:N) {
int nn = n + start - 1;
// compute non-linear predictor values
mu2[n] = nlp_btc[n] * (nlp_a3[n] + nlp_b3tt[n] * C_mu2_1[nn]);
}
for (n in 1:N) {
int nn = n + start - 1;
// compute non-linear predictor values
mu3[n] = nlp_btc[n] * (nlp_b4tt[n] * C_mu3_1[nn]);
}
for (n in 1:N) {
mu[n] = transpose([mu0[n], mu1[n], mu2[n], mu3[n]]);
}
for (n in 1:N) {
int nn = n + start - 1;
ptarget += categorical_logit_lpmf(Y[nn] | mu[n]);
}
return ptarget;
}
}
data {
int<lower=1> N; // total number of observations
int<lower=2> ncat; // number of categories
int Y[N]; // response variable
int<lower=1> K_btc; // number of population-level effects
matrix[N, K_btc] X_btc; // population-level design matrix
int<lower=1> K_a1; // number of population-level effects
matrix[N, K_a1] X_a1; // population-level design matrix
int<lower=1> K_a2; // number of population-level effects
matrix[N, K_a2] X_a2; // population-level design matrix
int<lower=1> K_a3; // number of population-level effects
matrix[N, K_a3] X_a3; // population-level design matrix
int<lower=1> K_b1tt; // number of population-level effects
matrix[N, K_b1tt] X_b1tt; // population-level design matrix
int<lower=1> K_b2tt; // number of population-level effects
matrix[N, K_b2tt] X_b2tt; // population-level design matrix
int<lower=1> K_b3tt; // number of population-level effects
matrix[N, K_b3tt] X_b3tt; // population-level design matrix
int<lower=1> K_b4tt; // number of population-level effects
matrix[N, K_b4tt] X_b4tt; // population-level design matrix
// covariate vectors for non-linear functions
vector[N] C_mu0_1;
vector[N] C_mu0_2;
// covariate vectors for non-linear functions
vector[N] C_mu1_1;
// covariate vectors for non-linear functions
vector[N] C_mu2_1;
// covariate vectors for non-linear functions
vector[N] C_mu3_1;
int grainsize; // grainsize for threading
// data for group-level effects of ID 1
int<lower=1> N_1; // number of grouping levels
int<lower=1> M_1; // number of coefficients per level
int<lower=1> J_1[N]; // grouping indicator per observation
// group-level predictor values
vector[N] Z_1_btc_1;
vector[N] Z_1_btc_2;
vector[N] Z_1_btc_3;
vector[N] Z_1_btc_4;
vector[N] Z_1_btc_5;
vector[N] Z_1_btc_6;
int<lower=1> NC_1; // number of group-level correlations
// data for group-level effects of ID 2
int<lower=1> N_2; // number of grouping levels
int<lower=1> M_2; // number of coefficients per level
int<lower=1> J_2[N]; // grouping indicator per observation
// group-level predictor values
vector[N] Z_2_btc_1;
// data for group-level effects of ID 3
int<lower=1> N_3; // number of grouping levels
int<lower=1> M_3; // number of coefficients per level
int<lower=1> J_3[N]; // grouping indicator per observation
// group-level predictor values
vector[N] Z_3_a1_1;
vector[N] Z_3_a1_2;
vector[N] Z_3_a1_3;
vector[N] Z_3_a1_4;
vector[N] Z_3_a1_5;
vector[N] Z_3_a1_6;
int<lower=1> NC_3; // number of group-level correlations
// data for group-level effects of ID 4
int<lower=1> N_4; // number of grouping levels
int<lower=1> M_4; // number of coefficients per level
int<lower=1> J_4[N]; // grouping indicator per observation
// group-level predictor values
vector[N] Z_4_a1_1;
// data for group-level effects of ID 5
int<lower=1> N_5; // number of grouping levels
int<lower=1> M_5; // number of coefficients per level
int<lower=1> J_5[N]; // grouping indicator per observation
// group-level predictor values
vector[N] Z_5_a2_1;
vector[N] Z_5_a2_2;
vector[N] Z_5_a2_3;
vector[N] Z_5_a2_4;
vector[N] Z_5_a2_5;
vector[N] Z_5_a2_6;
int<lower=1> NC_5; // number of group-level correlations
// data for group-level effects of ID 6
int<lower=1> N_6; // number of grouping levels
int<lower=1> M_6; // number of coefficients per level
int<lower=1> J_6[N]; // grouping indicator per observation
// group-level predictor values
vector[N] Z_6_a2_1;
// data for group-level effects of ID 7
int<lower=1> N_7; // number of grouping levels
int<lower=1> M_7; // number of coefficients per level
int<lower=1> J_7[N]; // grouping indicator per observation
// group-level predictor values
vector[N] Z_7_a3_1;
vector[N] Z_7_a3_2;
vector[N] Z_7_a3_3;
vector[N] Z_7_a3_4;
vector[N] Z_7_a3_5;
vector[N] Z_7_a3_6;
int<lower=1> NC_7; // number of group-level correlations
// data for group-level effects of ID 8
int<lower=1> N_8; // number of grouping levels
int<lower=1> M_8; // number of coefficients per level
int<lower=1> J_8[N]; // grouping indicator per observation
// group-level predictor values
vector[N] Z_8_a3_1;
// data for group-level effects of ID 9
int<lower=1> N_9; // number of grouping levels
int<lower=1> M_9; // number of coefficients per level
int<lower=1> J_9[N]; // grouping indicator per observation
// group-level predictor values
vector[N] Z_9_b1tt_1;
vector[N] Z_9_b1tt_2;
vector[N] Z_9_b1tt_3;
vector[N] Z_9_b1tt_4;
vector[N] Z_9_b1tt_5;
vector[N] Z_9_b1tt_6;
int<lower=1> NC_9; // number of group-level correlations
// data for group-level effects of ID 10
int<lower=1> N_10; // number of grouping levels
int<lower=1> M_10; // number of coefficients per level
int<lower=1> J_10[N]; // grouping indicator per observation
// group-level predictor values
vector[N] Z_10_b1tt_1;
// data for group-level effects of ID 11
int<lower=1> N_11; // number of grouping levels
int<lower=1> M_11; // number of coefficients per level
int<lower=1> J_11[N]; // grouping indicator per observation
// group-level predictor values
vector[N] Z_11_b2tt_1;
vector[N] Z_11_b2tt_2;
vector[N] Z_11_b2tt_3;
vector[N] Z_11_b2tt_4;
vector[N] Z_11_b2tt_5;
vector[N] Z_11_b2tt_6;
int<lower=1> NC_11; // number of group-level correlations
// data for group-level effects of ID 12
int<lower=1> N_12; // number of grouping levels
int<lower=1> M_12; // number of coefficients per level
int<lower=1> J_12[N]; // grouping indicator per observation
// group-level predictor values
vector[N] Z_12_b2tt_1;
// data for group-level effects of ID 13
int<lower=1> N_13; // number of grouping levels
int<lower=1> M_13; // number of coefficients per level
int<lower=1> J_13[N]; // grouping indicator per observation
// group-level predictor values
vector[N] Z_13_b3tt_1;
vector[N] Z_13_b3tt_2;
vector[N] Z_13_b3tt_3;
vector[N] Z_13_b3tt_4;
vector[N] Z_13_b3tt_5;
vector[N] Z_13_b3tt_6;
int<lower=1> NC_13; // number of group-level correlations
// data for group-level effects of ID 14
int<lower=1> N_14; // number of grouping levels
int<lower=1> M_14; // number of coefficients per level
int<lower=1> J_14[N]; // grouping indicator per observation
// group-level predictor values
vector[N] Z_14_b3tt_1;
// data for group-level effects of ID 15
int<lower=1> N_15; // number of grouping levels
int<lower=1> M_15; // number of coefficients per level
int<lower=1> J_15[N]; // grouping indicator per observation
// group-level predictor values
vector[N] Z_15_b4tt_1;
vector[N] Z_15_b4tt_2;
vector[N] Z_15_b4tt_3;
vector[N] Z_15_b4tt_4;
vector[N] Z_15_b4tt_5;
vector[N] Z_15_b4tt_6;
int<lower=1> NC_15; // number of group-level correlations
// data for group-level effects of ID 16
int<lower=1> N_16; // number of grouping levels
int<lower=1> M_16; // number of coefficients per level
int<lower=1> J_16[N]; // grouping indicator per observation
// group-level predictor values
vector[N] Z_16_b4tt_1;
int prior_only; // should the likelihood be ignored?
}
transformed data {
int seq[N] = sequence(1, N);
}
parameters {
vector[K_btc] b_btc; // population-level effects
vector[K_a1] b_a1; // population-level effects
vector[K_a2] b_a2; // population-level effects
vector[K_a3] b_a3; // population-level effects
vector[K_b1tt] b_b1tt; // population-level effects
vector[K_b2tt] b_b2tt; // population-level effects
vector[K_b3tt] b_b3tt; // population-level effects
vector[K_b4tt] b_b4tt; // population-level effects
vector<lower=0>[M_1] sd_1; // group-level standard deviations
matrix[M_1, N_1] z_1; // standardized group-level effects
cholesky_factor_corr[M_1] L_1; // cholesky factor of correlation matrix
vector<lower=0>[M_2] sd_2; // group-level standard deviations
vector[N_2] z_2[M_2]; // standardized group-level effects
vector<lower=0>[M_3] sd_3; // group-level standard deviations
matrix[M_3, N_3] z_3; // standardized group-level effects
cholesky_factor_corr[M_3] L_3; // cholesky factor of correlation matrix
vector<lower=0>[M_4] sd_4; // group-level standard deviations
vector[N_4] z_4[M_4]; // standardized group-level effects
vector<lower=0>[M_5] sd_5; // group-level standard deviations
matrix[M_5, N_5] z_5; // standardized group-level effects
cholesky_factor_corr[M_5] L_5; // cholesky factor of correlation matrix
vector<lower=0>[M_6] sd_6; // group-level standard deviations
vector[N_6] z_6[M_6]; // standardized group-level effects
vector<lower=0>[M_7] sd_7; // group-level standard deviations
matrix[M_7, N_7] z_7; // standardized group-level effects
cholesky_factor_corr[M_7] L_7; // cholesky factor of correlation matrix
vector<lower=0>[M_8] sd_8; // group-level standard deviations
vector[N_8] z_8[M_8]; // standardized group-level effects
vector<lower=0>[M_9] sd_9; // group-level standard deviations
matrix[M_9, N_9] z_9; // standardized group-level effects
cholesky_factor_corr[M_9] L_9; // cholesky factor of correlation matrix
vector<lower=0>[M_10] sd_10; // group-level standard deviations
vector[N_10] z_10[M_10]; // standardized group-level effects
vector<lower=0>[M_11] sd_11; // group-level standard deviations
matrix[M_11, N_11] z_11; // standardized group-level effects
cholesky_factor_corr[M_11] L_11; // cholesky factor of correlation matrix
vector<lower=0>[M_12] sd_12; // group-level standard deviations
vector[N_12] z_12[M_12]; // standardized group-level effects
vector<lower=0>[M_13] sd_13; // group-level standard deviations
matrix[M_13, N_13] z_13; // standardized group-level effects
cholesky_factor_corr[M_13] L_13; // cholesky factor of correlation matrix
vector<lower=0>[M_14] sd_14; // group-level standard deviations
vector[N_14] z_14[M_14]; // standardized group-level effects
vector<lower=0>[M_15] sd_15; // group-level standard deviations
matrix[M_15, N_15] z_15; // standardized group-level effects
cholesky_factor_corr[M_15] L_15; // cholesky factor of correlation matrix
vector<lower=0>[M_16] sd_16; // group-level standard deviations
vector[N_16] z_16[M_16]; // standardized group-level effects
}
transformed parameters {
matrix[N_1, M_1] r_1; // actual group-level effects
// using vectors speeds up indexing in loops
vector[N_1] r_1_btc_1;
vector[N_1] r_1_btc_2;
vector[N_1] r_1_btc_3;
vector[N_1] r_1_btc_4;
vector[N_1] r_1_btc_5;
vector[N_1] r_1_btc_6;
vector[N_2] r_2_btc_1; // actual group-level effects
matrix[N_3, M_3] r_3; // actual group-level effects
// using vectors speeds up indexing in loops
vector[N_3] r_3_a1_1;
vector[N_3] r_3_a1_2;
vector[N_3] r_3_a1_3;
vector[N_3] r_3_a1_4;
vector[N_3] r_3_a1_5;
vector[N_3] r_3_a1_6;
vector[N_4] r_4_a1_1; // actual group-level effects
matrix[N_5, M_5] r_5; // actual group-level effects
// using vectors speeds up indexing in loops
vector[N_5] r_5_a2_1;
vector[N_5] r_5_a2_2;
vector[N_5] r_5_a2_3;
vector[N_5] r_5_a2_4;
vector[N_5] r_5_a2_5;
vector[N_5] r_5_a2_6;
vector[N_6] r_6_a2_1; // actual group-level effects
matrix[N_7, M_7] r_7; // actual group-level effects
// using vectors speeds up indexing in loops
vector[N_7] r_7_a3_1;
vector[N_7] r_7_a3_2;
vector[N_7] r_7_a3_3;
vector[N_7] r_7_a3_4;
vector[N_7] r_7_a3_5;
vector[N_7] r_7_a3_6;
vector[N_8] r_8_a3_1; // actual group-level effects
matrix[N_9, M_9] r_9; // actual group-level effects
// using vectors speeds up indexing in loops
vector[N_9] r_9_b1tt_1;
vector[N_9] r_9_b1tt_2;
vector[N_9] r_9_b1tt_3;
vector[N_9] r_9_b1tt_4;
vector[N_9] r_9_b1tt_5;
vector[N_9] r_9_b1tt_6;
vector[N_10] r_10_b1tt_1; // actual group-level effects
matrix[N_11, M_11] r_11; // actual group-level effects
// using vectors speeds up indexing in loops
vector[N_11] r_11_b2tt_1;
vector[N_11] r_11_b2tt_2;
vector[N_11] r_11_b2tt_3;
vector[N_11] r_11_b2tt_4;
vector[N_11] r_11_b2tt_5;
vector[N_11] r_11_b2tt_6;
vector[N_12] r_12_b2tt_1; // actual group-level effects
matrix[N_13, M_13] r_13; // actual group-level effects
// using vectors speeds up indexing in loops
vector[N_13] r_13_b3tt_1;
vector[N_13] r_13_b3tt_2;
vector[N_13] r_13_b3tt_3;
vector[N_13] r_13_b3tt_4;
vector[N_13] r_13_b3tt_5;
vector[N_13] r_13_b3tt_6;
vector[N_14] r_14_b3tt_1; // actual group-level effects
matrix[N_15, M_15] r_15; // actual group-level effects
// using vectors speeds up indexing in loops
vector[N_15] r_15_b4tt_1;
vector[N_15] r_15_b4tt_2;
vector[N_15] r_15_b4tt_3;
vector[N_15] r_15_b4tt_4;
vector[N_15] r_15_b4tt_5;
vector[N_15] r_15_b4tt_6;
vector[N_16] r_16_b4tt_1; // actual group-level effects
// compute actual group-level effects
r_1 = scale_r_cor(z_1, sd_1, L_1);
r_1_btc_1 = r_1[, 1];
r_1_btc_2 = r_1[, 2];
r_1_btc_3 = r_1[, 3];
r_1_btc_4 = r_1[, 4];
r_1_btc_5 = r_1[, 5];
r_1_btc_6 = r_1[, 6];
r_2_btc_1 = (sd_2[1] * (z_2[1]));
// compute actual group-level effects
r_3 = scale_r_cor(z_3, sd_3, L_3);
r_3_a1_1 = r_3[, 1];
r_3_a1_2 = r_3[, 2];
r_3_a1_3 = r_3[, 3];
r_3_a1_4 = r_3[, 4];
r_3_a1_5 = r_3[, 5];
r_3_a1_6 = r_3[, 6];
r_4_a1_1 = (sd_4[1] * (z_4[1]));
// compute actual group-level effects
r_5 = scale_r_cor(z_5, sd_5, L_5);
r_5_a2_1 = r_5[, 1];
r_5_a2_2 = r_5[, 2];
r_5_a2_3 = r_5[, 3];
r_5_a2_4 = r_5[, 4];
r_5_a2_5 = r_5[, 5];
r_5_a2_6 = r_5[, 6];
r_6_a2_1 = (sd_6[1] * (z_6[1]));
// compute actual group-level effects
r_7 = scale_r_cor(z_7, sd_7, L_7);
r_7_a3_1 = r_7[, 1];
r_7_a3_2 = r_7[, 2];
r_7_a3_3 = r_7[, 3];
r_7_a3_4 = r_7[, 4];
r_7_a3_5 = r_7[, 5];
r_7_a3_6 = r_7[, 6];
r_8_a3_1 = (sd_8[1] * (z_8[1]));
// compute actual group-level effects
r_9 = scale_r_cor(z_9, sd_9, L_9);
r_9_b1tt_1 = r_9[, 1];
r_9_b1tt_2 = r_9[, 2];
r_9_b1tt_3 = r_9[, 3];
r_9_b1tt_4 = r_9[, 4];
r_9_b1tt_5 = r_9[, 5];
r_9_b1tt_6 = r_9[, 6];
r_10_b1tt_1 = (sd_10[1] * (z_10[1]));
// compute actual group-level effects
r_11 = scale_r_cor(z_11, sd_11, L_11);
r_11_b2tt_1 = r_11[, 1];
r_11_b2tt_2 = r_11[, 2];
r_11_b2tt_3 = r_11[, 3];
r_11_b2tt_4 = r_11[, 4];
r_11_b2tt_5 = r_11[, 5];
r_11_b2tt_6 = r_11[, 6];
r_12_b2tt_1 = (sd_12[1] * (z_12[1]));
// compute actual group-level effects
r_13 = scale_r_cor(z_13, sd_13, L_13);
r_13_b3tt_1 = r_13[, 1];
r_13_b3tt_2 = r_13[, 2];
r_13_b3tt_3 = r_13[, 3];
r_13_b3tt_4 = r_13[, 4];
r_13_b3tt_5 = r_13[, 5];
r_13_b3tt_6 = r_13[, 6];
r_14_b3tt_1 = (sd_14[1] * (z_14[1]));
// compute actual group-level effects
r_15 = scale_r_cor(z_15, sd_15, L_15);
r_15_b4tt_1 = r_15[, 1];
r_15_b4tt_2 = r_15[, 2];
r_15_b4tt_3 = r_15[, 3];
r_15_b4tt_4 = r_15[, 4];
r_15_b4tt_5 = r_15[, 5];
r_15_b4tt_6 = r_15[, 6];
r_16_b4tt_1 = (sd_16[1] * (z_16[1]));
}
model {
// likelihood including constants
target += reduce_sum(partial_log_lik_lpmf, seq, grainsize, ncat, Y, X_btc, b_btc, X_a1, b_a1, X_a2, b_a2, X_a3, b_a3, X_b1tt, b_b1tt, X_b2tt, b_b2tt, X_b3tt, b_b3tt, X_b4tt, b_b4tt, C_mu0_1, C_mu0_2, C_mu1_1, C_mu2_1, C_mu3_1, J_1, Z_1_btc_1, Z_1_btc_2, Z_1_btc_3, Z_1_btc_4, Z_1_btc_5, Z_1_btc_6, r_1_btc_1, r_1_btc_2, r_1_btc_3, r_1_btc_4, r_1_btc_5, r_1_btc_6, J_2, Z_2_btc_1, r_2_btc_1, J_3, Z_3_a1_1, Z_3_a1_2, Z_3_a1_3, Z_3_a1_4, Z_3_a1_5, Z_3_a1_6, r_3_a1_1, r_3_a1_2, r_3_a1_3, r_3_a1_4, r_3_a1_5, r_3_a1_6, J_4, Z_4_a1_1, r_4_a1_1, J_5, Z_5_a2_1, Z_5_a2_2, Z_5_a2_3, Z_5_a2_4, Z_5_a2_5, Z_5_a2_6, r_5_a2_1, r_5_a2_2, r_5_a2_3, r_5_a2_4, r_5_a2_5, r_5_a2_6, J_6, Z_6_a2_1, r_6_a2_1, J_7, Z_7_a3_1, Z_7_a3_2, Z_7_a3_3, Z_7_a3_4, Z_7_a3_5, Z_7_a3_6, r_7_a3_1, r_7_a3_2, r_7_a3_3, r_7_a3_4, r_7_a3_5, r_7_a3_6, J_8, Z_8_a3_1, r_8_a3_1, J_9, Z_9_b1tt_1, Z_9_b1tt_2, Z_9_b1tt_3, Z_9_b1tt_4, Z_9_b1tt_5, Z_9_b1tt_6, r_9_b1tt_1, r_9_b1tt_2, r_9_b1tt_3, r_9_b1tt_4, r_9_b1tt_5, r_9_b1tt_6, J_10, Z_10_b1tt_1, r_10_b1tt_1, J_11, Z_11_b2tt_1, Z_11_b2tt_2, Z_11_b2tt_3, Z_11_b2tt_4, Z_11_b2tt_5, Z_11_b2tt_6, r_11_b2tt_1, r_11_b2tt_2, r_11_b2tt_3, r_11_b2tt_4, r_11_b2tt_5, r_11_b2tt_6, J_12, Z_12_b2tt_1, r_12_b2tt_1, J_13, Z_13_b3tt_1, Z_13_b3tt_2, Z_13_b3tt_3, Z_13_b3tt_4, Z_13_b3tt_5, Z_13_b3tt_6, r_13_b3tt_1, r_13_b3tt_2, r_13_b3tt_3, r_13_b3tt_4, r_13_b3tt_5, r_13_b3tt_6, J_14, Z_14_b3tt_1, r_14_b3tt_1, J_15, Z_15_b4tt_1, Z_15_b4tt_2, Z_15_b4tt_3, Z_15_b4tt_4, Z_15_b4tt_5, Z_15_b4tt_6, r_15_b4tt_1, r_15_b4tt_2, r_15_b4tt_3, r_15_b4tt_4, r_15_b4tt_5, r_15_b4tt_6, J_16, Z_16_b4tt_1, r_16_b4tt_1);
// priors including constants
target += normal_lpdf(b_btc | 0, 2.5);
target += normal_lpdf(b_a1 | 0, 2.5);
target += normal_lpdf(b_a2 | 0, 2.5);
target += normal_lpdf(b_a3 | 0, 2.5);
target += normal_lpdf(b_b1tt | 0, 2.5);
target += normal_lpdf(b_b2tt | 0, 2.5);
target += normal_lpdf(b_b3tt | 0, 2.5);
target += normal_lpdf(b_b4tt | 0, 2.5);
target += student_t_lpdf(sd_1 | 3, 0, 2.5)
- 6 * student_t_lccdf(0 | 3, 0, 2.5);
target += std_normal_lpdf(to_vector(z_1));
target += lkj_corr_cholesky_lpdf(L_1 | 1);
target += student_t_lpdf(sd_2 | 3, 0, 2.5)
- 1 * student_t_lccdf(0 | 3, 0, 2.5);
target += std_normal_lpdf(z_2[1]);
target += student_t_lpdf(sd_3 | 3, 0, 2.5)
- 6 * student_t_lccdf(0 | 3, 0, 2.5);
target += std_normal_lpdf(to_vector(z_3));
target += lkj_corr_cholesky_lpdf(L_3 | 1);
target += student_t_lpdf(sd_4 | 3, 0, 2.5)
- 1 * student_t_lccdf(0 | 3, 0, 2.5);
target += std_normal_lpdf(z_4[1]);
target += student_t_lpdf(sd_5 | 3, 0, 2.5)
- 6 * student_t_lccdf(0 | 3, 0, 2.5);
target += std_normal_lpdf(to_vector(z_5));
target += lkj_corr_cholesky_lpdf(L_5 | 1);
target += student_t_lpdf(sd_6 | 3, 0, 2.5)
- 1 * student_t_lccdf(0 | 3, 0, 2.5);
target += std_normal_lpdf(z_6[1]);
target += student_t_lpdf(sd_7 | 3, 0, 2.5)
- 6 * student_t_lccdf(0 | 3, 0, 2.5);
target += std_normal_lpdf(to_vector(z_7));
target += lkj_corr_cholesky_lpdf(L_7 | 1);
target += student_t_lpdf(sd_8 | 3, 0, 2.5)
- 1 * student_t_lccdf(0 | 3, 0, 2.5);
target += std_normal_lpdf(z_8[1]);
target += student_t_lpdf(sd_9 | 3, 0, 2.5)
- 6 * student_t_lccdf(0 | 3, 0, 2.5);
target += std_normal_lpdf(to_vector(z_9));
target += lkj_corr_cholesky_lpdf(L_9 | 1);
target += student_t_lpdf(sd_10 | 3, 0, 2.5)
- 1 * student_t_lccdf(0 | 3, 0, 2.5);
target += std_normal_lpdf(z_10[1]);
target += student_t_lpdf(sd_11 | 3, 0, 2.5)
- 6 * student_t_lccdf(0 | 3, 0, 2.5);
target += std_normal_lpdf(to_vector(z_11));
target += lkj_corr_cholesky_lpdf(L_11 | 1);
target += student_t_lpdf(sd_12 | 3, 0, 2.5)
- 1 * student_t_lccdf(0 | 3, 0, 2.5);
target += std_normal_lpdf(z_12[1]);
target += student_t_lpdf(sd_13 | 3, 0, 2.5)
- 6 * student_t_lccdf(0 | 3, 0, 2.5);
target += std_normal_lpdf(to_vector(z_13));
target += lkj_corr_cholesky_lpdf(L_13 | 1);
target += student_t_lpdf(sd_14 | 3, 0, 2.5)
- 1 * student_t_lccdf(0 | 3, 0, 2.5);
target += std_normal_lpdf(z_14[1]);
target += student_t_lpdf(sd_15 | 3, 0, 2.5)
- 6 * student_t_lccdf(0 | 3, 0, 2.5);
target += std_normal_lpdf(to_vector(z_15));
target += lkj_corr_cholesky_lpdf(L_15 | 1);
target += student_t_lpdf(sd_16 | 3, 0, 2.5)
- 1 * student_t_lccdf(0 | 3, 0, 2.5);
target += std_normal_lpdf(z_16[1]);
}
generated quantities {
// compute group-level correlations
corr_matrix[M_1] Cor_1 = multiply_lower_tri_self_transpose(L_1);
vector<lower=-1,upper=1>[NC_1] cor_1;
// compute group-level correlations
corr_matrix[M_3] Cor_3 = multiply_lower_tri_self_transpose(L_3);
vector<lower=-1,upper=1>[NC_3] cor_3;
// compute group-level correlations
corr_matrix[M_5] Cor_5 = multiply_lower_tri_self_transpose(L_5);
vector<lower=-1,upper=1>[NC_5] cor_5;
// compute group-level correlations
corr_matrix[M_7] Cor_7 = multiply_lower_tri_self_transpose(L_7);
vector<lower=-1,upper=1>[NC_7] cor_7;
// compute group-level correlations
corr_matrix[M_9] Cor_9 = multiply_lower_tri_self_transpose(L_9);
vector<lower=-1,upper=1>[NC_9] cor_9;
// compute group-level correlations
corr_matrix[M_11] Cor_11 = multiply_lower_tri_self_transpose(L_11);
vector<lower=-1,upper=1>[NC_11] cor_11;
// compute group-level correlations
corr_matrix[M_13] Cor_13 = multiply_lower_tri_self_transpose(L_13);
vector<lower=-1,upper=1>[NC_13] cor_13;
// compute group-level correlations
corr_matrix[M_15] Cor_15 = multiply_lower_tri_self_transpose(L_15);
vector<lower=-1,upper=1>[NC_15] cor_15;
// extract upper diagonal of correlation matrix
for (k in 1:M_1) {
for (j in 1:(k - 1)) {
cor_1[choose(k - 1, 2) + j] = Cor_1[j, k];
}
}
// extract upper diagonal of correlation matrix
for (k in 1:M_3) {
for (j in 1:(k - 1)) {
cor_3[choose(k - 1, 2) + j] = Cor_3[j, k];
}
}
// extract upper diagonal of correlation matrix
for (k in 1:M_5) {
for (j in 1:(k - 1)) {
cor_5[choose(k - 1, 2) + j] = Cor_5[j, k];
}
}
// extract upper diagonal of correlation matrix
for (k in 1:M_7) {
for (j in 1:(k - 1)) {
cor_7[choose(k - 1, 2) + j] = Cor_7[j, k];
}
}
// extract upper diagonal of correlation matrix
for (k in 1:M_9) {
for (j in 1:(k - 1)) {
cor_9[choose(k - 1, 2) + j] = Cor_9[j, k];
}
}
// extract upper diagonal of correlation matrix
for (k in 1:M_11) {
for (j in 1:(k - 1)) {
cor_11[choose(k - 1, 2) + j] = Cor_11[j, k];
}
}
// extract upper diagonal of correlation matrix
for (k in 1:M_13) {
for (j in 1:(k - 1)) {
cor_13[choose(k - 1, 2) + j] = Cor_13[j, k];
}
}
// extract upper diagonal of correlation matrix
for (k in 1:M_15) {
for (j in 1:(k - 1)) {
cor_15[choose(k - 1, 2) + j] = Cor_15[j, k];
}
}
}