# How to vectorize random effects model?

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;
}
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);
}

``````

Plz help me. plz

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
thank you. i will

@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.

1 Like

thank you. i try brms.
Since, I am working on a more complex model than this above example, I would like to know how to non-centered parameterisation.

I think you need to review the users guide on non-centering:

the principal is a general one. Once you get your head around it you can find ways to apply it in different circumstances.

Maybe also check out the example here

1 Like

thank you.

I see 404 error

I tried to make_stancode it in brm, but I couldn’t decipher it. lol

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.

This is a bit off topic, but is it possible to specify a design matrix in brm?

for instance,If I want to specify a model with only intercept and linear parameters,following

``````y = [1,0    * [intercept     + sigma
1,1      [linear_param]
1,2]

``````

I’m not familiar with the syntax you’re using. Brms uses R’s formula syntax, so maybe start here then see here

1 Like