Nested model in STAN?

Let's say I want to simulate a random effect on two levels, i.e. I have two levels of nesting: individuals in the parent group and parent groups in the group of grandparents. I know how to write a base model for one random effect (below) from examples like these , but I don't know how to write an equivalent

lmer(resp ~ (1|a/b), data = DAT) 

in lmer.

STAN code for one RE. The question is how to embed a in a higher level b ?

 data{ int<lower=0> N; int<lower=0> K; matrix[N,K] X; vector[N] price; int J; int<lower=1,upper=J> re[N]; } parameters{ vector[J] a; real mu_a; real tau; real<lower=0> sigma_a; real<lower=0> sigma; vector[K] beta; } transformed parameters{ vector[N] mu_hat; for(i in 1:N) mu_hat[i] <- a[re[i]]; } model { mu_a ~ normal(0,10); tau ~ cauchy(0,5); a ~ normal(mu_a,sigma_a); for(i in 1:N) price[i] ~ normal(X[i]*beta + mu_hat[i], sigma); } " 
+6
source share
2 answers

I'm not sure that the a / b notation is in lmer, but if you want the nested levels to be multi-layered, then this is easy to do with a predictor. Say you have an IRT model with students (j in 1: J) invested in schools (school [j] in 1: S), and schools invested in cities (city [s] in 1: C).

[April 14, 2017 update]

Now you can vectorize everything. So instead:

 for (j in 1:J) theta[j] ~ normal(alpha[school[j]], sigma_theta); for (s in 1:S) alpha[s] ~ normal(beta[city[s]], sigma_alpha); beta ~ normal(0, 5); 

you may have

 theta ~ normal(alpha[school], sigma_theta); alpha ~ normal(beta[city], sigma_alpha); beta ~ normal(0, 5); 
+4
source

If your model is simple, the brms package brms worth a look. It compiles your formulas to stan and runs the model. It also has expressive syntax borrowed from lmer. I like that you can compile the model as a stan file and then build on it if you already have lmer formula

And, of course, it has an additional advantage (from stan) that the confusing difference in evaluating the “fixed effect” and “random effects” is gone, and both of them are evaluated essentially as parameters with subsequent distributions.

0
source

Source: https://habr.com/ru/post/984528/


All Articles