I am attempting to recreate an example Hierarchical model in cmdstanr from this blog post (of which I am very grateful for). I keep getting errors related to my data type statements.
My initial STAN code:
data {
int N_obs; // number of observations
int N_pts; // number of participants
int K; // number of predictors + intercept
int pid[N_obs]; // participant id vector
matrix[N_obs, K] x; // matrix of predictors
real y[N_obs]; // y vector
}
parameters {
vector[K] beta_p[N_pts]; // ind participant intercept and slope coefficients by group
vector<lower=0>[K] sigma_p; // sd for intercept and slope
vector[K] beta; // intercept and slope hyper-priors
corr_matrix[K] Omega; // correlation matrix
real<lower=0> sigma; // population sigma
}
model {
vector[N_obs] mu;
// priors
beta ~ normal(0, 1);
Omega ~ lkj_corr(2);
sigma_p ~ exponential(1);
sigma ~ exponential(1);
beta_p ~ multi_normal(beta, quad_form_diag(Omega, sigma_p));
// likelihood
for(i in 1:N_obs) {
mu[i] = x[i] * (beta_p[pid[i]]); // * is matrix multiplication in this context
}
y ~ normal(mu, sigma);
}
I then re-wrote the data and parameters piece to be:
data {
int N_obs; // number of observations
int N_pts; // number of participants
int K; // number of predictors + intercept
array[N_obs] int<lower=1, upper=N_pts> pid; // particpant id
matrix[N_obs, K] x; // matrix of predictors
vector[N_obs] y;
}
parameters {
matrix[K, N_pts] z_1;
vector[N_pts] beta_p; // ind participant intercept and slope coefficients by group
vector<lower=0>[K] sigma_p; // sd for intercept and slope
vector[K] beta; // intercept and slope hyper-priors
corr_matrix[K] Omega; // correlation matrix
real<lower=0> sigma; // population sigma
}
but Iām still getting the following error:
27: mu[i] = x[i] * (beta_p[pid[i]]); // * is matrix multiplication in this context
^
28: }
29:
-------------------------------------------------
Ill-typed arguments supplied to assignment operator =:
The left hand side has type
real
and the right hand side has type
row_vector
Can someone help me understand how to re-write this code with the new STAN syntax, please?