Hi, forum,
So I am trying to fit a random intercept model that has a random intercept for each unit and each year in the data which looks like:
model {
data {
int<lower=0> N; // Number of rows in data
int<lower=0> P; // Number of cols in X (variables in model)
int<lower=0> C; // Number of units (eg: countries)
int<lower=0> T; // Number of time periods (eg: years)
int y[N]; // // Target
matrix[N, P] x; // input features
int<lower=1, upper=C> unit[N]; //map obs to unit
int<lower=1, upper=T> time[N]; //map obs to time
}
parameters {
vector[C] alpha2; // random unit intercept
vector[T] alpha3; // random time intercept
real alpha; // fixed intercept
vector[P] beta; // coeficient for variables
real <lower=0> sigma; // error sd
real <lower=0> sigma_u; // unit hyperprior sd
real <lower=0> sigma_t; // time hyperprior sd
real <lower=0> sigma_i; // fixed hyperprior sd
real mu_u; // unit hyperprior mean
real mu_t; // time hyperprior mean
real mu_i; // fixed hyperprior mean
}
model {
target += normal_lpdf(mu_u|0,1);
target += normal_lpdf(mu_t|0,1);
target += normal_lpdf(mu_i|0,1);
target += cauchy_lpdf(sigma_t|0,1);
target += cauchy_lpdf(sigma_u|0,1);
target += cauchy_lpdf(sigma_i|0,1);
target += cauchy_lpdf(sigma|0,1);
target += normal_lpdf(alpha|mu_i,sigma_i);
target += normal_lpdf(alpha2|mu_u,sigma_u);
target += normal_lpdf(alpha3|mu_t,sigma_t);
target += normal_lpdf(beta|0,1);
for (n in 1:N) {
real ystar;
ystar = alpha + alpha2[unit[n]] + alpha3[time[n]] + x[n]*beta; //
target += normal_lpdf(y[n]| ystar,sigma);
}
}
}
The model above works fine, however, I am having trouble getting the model to converge (I am using 2000 iterations, going up in numbers does help but I suspect this is not the best solution.) So I decided to try using a non-centered reparametrization. Since most of the convergence problems seem to be on the intercept parameters, I suspect these are the parameters that need to be reparameterized. I coded up the mode as:
model {
data {
int<lower=0> N; // Number of rows in data
int<lower=0> P; // Number of cols in X (variables in model)
int<lower=0> C; // Number of units (eg: countries)
int<lower=0> T; // Number of time periods (eg: years)
int y[N]; // // Target
matrix[N, P] x; // input features
int<lower=1, upper=C> unit[N]; //map obs to unit
int<lower=1, upper=T> time[N]; //map obs to time
}
parameters {
real alpha; // fixed intercept
vector[P] beta; // coeficient for variables
real <lower=0> sigma; // error sd
real <lower=0> sigma_u; // unit hyperprior sd
real <lower=0> sigma_t; // time hyperprior sd
real <lower=0> sigma_i; // fixed hyperprior sd
real mu_u; // unit hyperprior mean
real mu_t; // time hyperprior mean
real mu_i; // fixed hyperprior mean
vector[C] eta_u;
vector[T] eta_t;
}
transformed parameters {
vector[C] alpha2; // random unit intercept
vector[T] alpha3; // random time intercept
alpha3 = mu_t + sigma_t * eta_t;
alpha2 = mu_u + sigma_u * eta_u;
}
model {
target += normal_lpdf(eta_u|0,1);
target += normal_lpdf(eta_t|0,1);
target += normal_lpdf(mu_u|0,1);
target += normal_lpdf(mu_t|0,1);
target += normal_lpdf(mu_i|0,1);
target += cauchy_lpdf(sigma_t|0,1);
target += cauchy_lpdf(sigma_u|0,1);
target += cauchy_lpdf(sigma_i|0,1);
target += cauchy_lpdf(sigma|0,1);
target += normal_lpdf(beta|0,1);
for (n in 1:N) {
real ystar;
ystar = alpha + alpha2[unit[n]] + alpha3[time[n]] + x[n]*beta; //
target += normal_lpdf(y[n]| ystar,sigma);
}
}
}
However, this version of the model is running incredibly slow on my data. I am not sure if I made a mistake in the reparameterization or what the problem is. If anyone has any suggestions, hints, ideas, or know where I have coded something wrong please let me know. Also, let me know if more information is needed.
Thank you in advance, and sorry if my question is not clear!