Hi,
It's been ages since I've used Stan, and I've become stuck on what should be a trivial model. Can somebody take a look and tell me what I'm missing here.
This is a poisson regression for student enrollments. I have 27 predictor variables and 13 campuses. There is a row of data for each county in the study (about 2,000) The dependent variable is an integer count of student enrollment.
Inputs:
N = number of rows in the data set
J = number of campuses
campus = a vector of length N with an integer for each campus
X = my covariates
I'm trying to make this a multi-level model, where the campus effect is sampled from a normal distribution. (later, I might force that effect to be centered on 0) Also, from the look of the data, I might want to expand this to a zero-inflated model. But first, I want to a basic hierarchical poisson model working.
Attempting to do this all in RStan to make life easier.
Stan gives me an error for the line "theta[campus[i]]" The error is "indexes inappropriate for expression".
Code is below.
Thanks!
Enter code here...data {
int<lower=0> N;
int <lower=0> J;
vector[N] Y;
int<lower=0> campus;
matrix[N,27] X;
}
parameters {
vector[27] betas;
vector[J] theta;
real<lower=0> sigma;
real<lower=0> tau;
}
transformed parameters {
vector[N] Xbeta;
for(i in 1:N){
Xbeta[i] <- theta[campus[i]] + X[i] * betas;
}
}
model{
sigma ~ gamma(0.01, 0.01);
tau ~ gamma(0.01,0.01);
betas ~ normal(0, sigma);
for (j in 1:J){
theta[j] ~ normal(0, tau);
}
Y ~ poisson(Xbeta);
}