I would like to vectorize the random effects model because it is running slowly, but I don’t know how to do it.
data {
int N; //size
int M; // num of predictor (include intercept)
int G; // num of team
vector[N] y ; // outcome
matrix[N, M] x; // independent variable
int teamID[N] // team label
}
parameters{
vector[M] beta[G]; //random effects
vector[M] gamma; // fix effects
real<lower=0> sigma; sd
vector<lower=0> [M] tau; random sd vector
corr_matrix[M] omega;
}
transformed parameters{
cov_matrix[M] Tau;
Tau = quad_form_diag(omega, tau);
}
model{
vector[N] mu;
for (i in 1:N){
mu[i] = x[i] *beta[teamID[i]];
}
y ~ normal(mu, sigma);
beta ~ multi_normal(gamma, Tau);
gamma~ normal(0, 100);
sigma~ cauchy(0, 5);
tau ~ cauchy(0, 5);
omega ~lkj_corr(1);
}
First have a look at using cholesky factors for your covariance matrix, and using the non-centered parameterisation outlined here: 24.7 Reparameterization | Stan User’s Guide
@andrjohns
I looked at the url, but there were no perfect fit examples and I couldn’t find a specific method.
Can you please tell me how to make the above code non-centered parameterisation…
Another introductory idea for the model you are running might be to look at brms. The stan code is automatically generated for you with non-centering included.
I found the following link for you:
Which shows how to use brms for the case you have. I suggest specifying a simple example and then inspecting the stancode using the make_stancode command. The syntax uses a more standard notation for models like this which reduces the amount of time spent coding in my opinion.
Now that I think of it, you might also look at the more complex model here
It’s hard to tell from your OP given that some folks use different terms for things and it’s the contents of the predictor matrix that actually determine the proper model structure. See here for elaboration.
In brm, can’t I specify a variable that is not in the data frame?
I want to assign the variables estimated in formula 1 to formula 2, as in the regression calibrartion.