# Help speeding up a slow hierarchical model?

**URL:** <https://discourse.mc-stan.org/t/help-speeding-up-a-slow-hierarchical-model/981>\
**Category:** Modeling\
**Created:** [June 21, 2017, 12:40am UTC](https://discourse.mc-stan.org/t/help-speeding-up-a-slow-hierarchical-model/981 "2017-06-21T00:40:16Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![jfiel](https://avatars.discourse-cdn.com/v4/letter/j/ecccb3/32.png) [@jfiel](https://discourse.mc-stan.org/u/jfiel)\
**Post date:** [June 21, 2017, 12:40am UTC](https://discourse.mc-stan.org/t/help-speeding-up-a-slow-hierarchical-model/981/1 "2017-06-21T00:40:16Z")

</div>

Hi,

I am relatively new to Stan and to Bayesian analysis generally. I have been working on a multi-step problem involving estimating intergenerational income elasticities. The first step is to account for measurement error (transitory variance) in log-income as a child to estimate long-term “permanent childhood income.”

I have multiple measures (at different ages) for each child, and multiple children per family. In the data, there are about 100,000 person-year observations, nested within about 9,000 persons, nested within about 4,000 families. I control for age and estimate “true” permanent income using the family and person random effects. The model has been painfully slow. I am using rstan in Rstudio.

I have created a smaller artificial data set (6000 observations, 1500 persons, 500 families) to validate the model, and it does a nice job recovering the parameters in a reasonable time frame (\<1hr for 5,000 iterations). But when applied to the real data, it is a day-long ordeal, if not longer (e.g., with 5,000 or 10,000 iterations).

I am pasting the model in below, hoping for some guidance on speeding things up. I have done my best to use vectors rather than loops, and to reparameterize some of the hierarchical parameters.

```
data {
	int<lower=1> Np; // number of person-year observations
	int<lower=1> J; // number of persons
	int<lower=1> F; // number of families
	int<lower=1> Kp; // number of control variables
	vector[Np] Yp; // outcome
	matrix[Np,Kp] Xp; // controls
	int<lower=1, upper=J> SubjP[Np]; // person IDs
	int<lower=1, upper=F> FamP[Np]; // family IDs
	int<lower=1, upper=J> Subj2[J]; // person IDs
	int<lower=1, upper=F> Fam2[J]; // family IDs
}
parameters {
	// parent income model
	real alphaP; // intercept
	vector[Kp] betaP; // control coefficients 
	vector[J] aP_i_raw; // person effects
	vector[F] aP_f_raw; // family effects
	real<lower=0> sP_e; // within-person sd
	real<lower=0> sP_i; // between-person sd
	real<lower=0> sP_f; // between-family sd
}
transformed parameters {
	vector[J] aP_i;						
	vector[F] aP_f;						
	vector[J] Xtrue; // "true" person-specific outcome
	aP_i = 0 + sP_i*aP_i_raw; // implies aP_i ~ normal(0, sP_i)
	aP_f = 0 + sP_f*aP_f_raw; // implies aP_f ~ normal(0, sP_f)
	Xtrue = alphaP + aP_f[Fam2] + aP_i[Subj2]; 
}
model {
	vector[Np] muP;
	alphaP ~ normal(0, 5); 
	betaP ~ normal(0, 1); 
	aP_i_raw ~ normal(0, 1); 	
	aP_f_raw ~ normal(0, 1); 	
	sP_i ~ normal(0, 2);
	sP_f ~ normal(0, 2);
	sP_e ~ normal(0, 2);
	muP = alphaP + Xp*betaP + aP_f[FamP] + aP_i[SubjP];    
	Yp ~ normal(muP, sP_e); 
}

```

[edit: set code as code]

---

<div class="post-metadata">

**Author:** ![Bob\_Carpenter](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/bob_carpenter/32/9230_2.png) [@Bob\_Carpenter](https://discourse.mc-stan.org/u/Bob_Carpenter)\
**Post date:** [July 17, 2017, 10:59pm UTC](https://discourse.mc-stan.org/t/help-speeding-up-a-slow-hierarchical-model/981/2 "2017-07-17T22:59:04Z")

</div>

I missed that post ages ago, but your program’s already written about as efficiently as possible.

P.S. Sorry for the slow response—@betanalpha just fixed my settings and now I’m seeing all the old messages I missed.

---

<div class="post-metadata">

**Author:** ![Bob\_Carpenter](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/bob_carpenter/32/9230_2.png) [@Bob\_Carpenter](https://discourse.mc-stan.org/u/Bob_Carpenter)\
**Post date:** [July 17, 2017, 11:00pm UTC](https://discourse.mc-stan.org/t/help-speeding-up-a-slow-hierarchical-model/981/3 "2017-07-17T23:00:50Z")

</div>

I should’ve added that if you have a large number of observations, the centered parameterization can be faster to mix.

And I don’t know how many iterations you’re targeting, but you shouldn’t need an n\_eff in the thousands for most inferential purposes.

---

<div class="post-metadata">

**Author:** ![jfiel](https://avatars.discourse-cdn.com/v4/letter/j/ecccb3/32.png) [@jfiel](https://discourse.mc-stan.org/u/jfiel)\
**Post date:** [July 19, 2017, 12:08am UTC](https://discourse.mc-stan.org/t/help-speeding-up-a-slow-hierarchical-model/981/4 "2017-07-19T00:08:02Z")

</div>

Thanks for following up! There are a few parameters (especially some of the variance parameters) that have low n\_eff values. This seems to be more of a problem in one data set with relatively few observations per group (family). A larger data set with more observations per group seems to require much fewer iterations.

But overall, I think things are manageable at this point, and I have ironed out enough of the kinks with my models using fewer numbers of iterations that I don’t mind waiting for them to re-run for a longer period of time.
