<p><div><div><div><div data-width="100" data-show-faces="false" data-send="true" data-layout="button_count" data-height="21" data-href="https://www.r-bloggers.com/crossed-and-nested-hierarchical-models-with-stan-and-r/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://biologyforfun.wordpress.com/2016/12/08/crossed-and-nested-hierarchical-models-with-stan-and-r/"> R – biologyforfun</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>Below I will expand on previous posts on bayesian regression modelling using STAN (see previous instalments <a target="_blank" rel="nofollow" href="http://datascienceplus.com/bayesian-regression-with-stan-part-1-normal-regression/">here</a>, <a target="_blank" rel="nofollow" href="http://datascienceplus.com/bayesian-regression-with-stan-beyond-normality/">here</a>, and <a target="_blank" rel="nofollow" href="http://wp.me/p2SacB-j8">here</a>). Topic of the day is modelling crossed and nested design in hierarchical models using STAN in R.</p>
<p>Crossed design appear when we have more than one grouping variable and when data are recorded for each combination of the grouping variables. For example say that we measured the growth of a fungi on different Petri dishes and that you took several samples from each dishes. In this example we have two grouping variables: the Petri dish and the sample. Since we have observations for each combination of the two grouping variables we are in a crossed design. We can model this using a hierarchical model with an intercept representing the average growth, a parameter representing the deviation from this average for each Petri dish and an additional parameter representing the deviation from the average for each sample. Below is the corresponding model in STAN code:</p>
<pre>/*A simple example of an crossed hierarchical model
*based on the Penicillin data from the lme4 package
*/

data {
  int&lt;lower=0> N;//number of observations
  int&lt;lower=0> n_sample;//number of samples
  int&lt;lower=0> n_plate;//number of plates
  int&lt;lower=1,upper=n_sample> sample_id[N];//vector of sample indeces
  int&lt;lower=1,upper=n_plate> plate_id[N];//vector of plate indeces
  vector[N] y;
}
parameters {
  vector[n_sample] gamma;//vector of sample deviation from the average 
  vector[n_plate] delta;//vector of plate deviation from the average
  real&lt;lower=0> mu;//average diameter value
  real&lt;lower=0> sigma_gamma;//standard deviation of the gamma coeffs
  real&lt;lower=0> sigma_delta;//standard deviation of the delta coeffs
  real&lt;lower=0> sigma_y;//standard deviation of the observations
}
transformed parameters {
  vector[N] y_hat;

  for (i in 1:N)
    y_hat[i] = mu + gamma[sample_id[i]] + delta[plate_id[i]];
}
model {
  //prior on the scale coefficient
//weakly informative priors, see section 6.9 in STAN user guide
  sigma_gamma ~ cauchy(0,2.5);
  sigma_delta ~ cauchy(0,2.5);
  sigma_y ~ gamma(2,0.1);
  //get sample and plate level deviation
  gamma ~ normal(0, sigma_gamma);
  delta ~ normal(0, sigma_delta);
  //likelihood
  y ~ normal(y_hat, sigma_y);
}
generated quantities {
//sample predicted values from the model for posterior predictive checks
  real y_rep[N];
  for(n in 1:N)
    y_rep[n] = normal_rng(y_hat[n],sigma_y);
}</pre>
<p>Pasting and saving this code into a .stan file we now turn to R using the Penicillin dataset from the lme4 package as (real-life) example:</p>
<pre>library(lme4)
library(rstan)
library(shinystan)#for great model viz
library(ggplot2)#for great viz in general
data(Penicillin)
#look if we have sample for each combination
xtabs(~plate+sample,Penicillin)
#create the plate and sample index
plate_id&lt;-as.numeric(Penicillin$plate)
sample_id&lt;-as.numeric(Penicillin$sample)
#the model matrix (just an intercept in this case)
X&lt;-matrix(rep(1,dim(Penicillin)[1]),ncol=1)

#fit the model
m_peni&lt;-stan(file = &quot;crossed_penicillin.stan&quot;,
data=list(N=dim(Penicillin)[1],n_sample=length(unique(sample_id)),
n_plate=length(unique(plate_id)),sample_id=sample_id,
plate_id=plate_id,y=Penicillin$diameter))

#launch_shinystan(m_peni)</pre>
<p>The model seem to fit pretty nicely, all chains converged for all parameters (Rhat around 1), we have decent posterior distribution (top panel in the figure below) and also good correlation between observed and fitted data (bottom panel figure below).</p>
<p><img sizes="(max-width: 900px) 100vw, 900px" alt="check" class="wp-image-1413" src="https://i1.wp.com/biologyforfun.files.wordpress.com/2016/12/check.png?w=456&ssl=1" data-recalc-dims="1"></p>
<p>In a next step we can look at the deviation form the average diameter for each sample and each plate (Petri dish):</p>
<pre>#make caterpillar plot
mcmc_peni&lt;-extract(m_peni)
sample_eff&lt;-apply(mcmc_peni$gamma,2,quantile,probs=c(0.025,0.5,0.975))
df_sample&lt;-data.frame(ID=unique(Penicillin$sample),Group=&quot;Sample&quot;,
LI=sample_eff[1,],Median=sample_eff[2,],HI=sample_eff[3,])
plate_eff&lt;-apply(mcmc_peni$delta,2,quantile,probs=c(0.025,0.5,0.975))
df_plate&lt;-data.frame(ID=unique(Penicillin$plate),Group=&quot;Plate&quot;,
LI=plate_eff[1,],Median=plate_eff[2,],HI=plate_eff[3,])
df_all&lt;-rbind(df_sample,df_plate)
ggplot(df_all,aes(x=ID,y=Median))+geom_point()+
 geom_linerange(aes(ymin=LI,ymax=HI))+facet_wrap(~Group,scales=&quot;free&quot;)+
 geom_hline(aes(yintercept=0),color=&quot;blue&quot;,linetype=&quot;dashed&quot;)+
 labs(y=&quot;Regression parameters&quot;)</pre>
<p><img sizes="(max-width: 800px) 100vw, 800px" alt="cater_peni" class="wp-image-1412" src="https://i0.wp.com/biologyforfun.files.wordpress.com/2016/12/cater_peni.png?w=456&ssl=1" data-recalc-dims="1"></p>
<p>We can compare this figure to Figure 2.2 in <a target="_blank" rel="nofollow" href="http://lme4.r-forge.r-project.org/book/Ch2.pdf">here</a> where the same model was fitted to the data using lmer.</p>
<p>I now turn to nested design. Nested design occur when there is more than one grouping variable and when there is a hierarchy in these variables with categories from lower variables only being present at one level from higher variables. For examples if we measured student scores within classes within schools we would have a nested hierarchical design. In the following I will use the Arabidopsis dataset from the lme4 package. Arabidopsis plants from different regions (Netherlands, Spain and Sweden) and from different populations within these regions (nested design) were collected and the researchers looked at the effect of herbivory and nutrient addition on the number of fruits produced per plants. Below is the corresponding STAN code:</p>
<pre>/*Nested regression example
*Three-levels with varying-intercept
*based on: https://rpubs.com/kaz_yos/stan-multi-2
*and applied to the Arabidopsis data from lme4
*/

data {
  int&lt;lower=1> N; //number of observations
  int&lt;lower=1> P; //number of populations
  int&lt;lower=1> R; //number of regions
//population ID
  int&lt;lower=1,upper=P> PopID[N]; 
 //index of population appertenance to a specific region
  int&lt;lower=1,upper=R> PopWithinReg[P]; 

  int&lt;lower=0> Fruit[N]; //the response variable
  real AMD[N]; //predictor variable, whether the apical meristem was unclipped (0) or clipped (1)
  real nutrient[N]; //predictor variable, whether nutrient level were control (0) or higher (1)
}

parameters {
  //regression slopes
  real beta_0; //intercept
  real beta_1; //effect of clipping apical meristem on number of fruits
  real beta_2; //effect of increaing nutrient level on number of fruits

  //the deviation from the intercept at the different levels
  real dev_pop[P]; //deviation between the populations within a region
  real dev_reg[R]; //deviation between the regions

  //the standard deviation for the deviations
  real&lt;lower=0> sigma_pop;
  real&lt;lower=0> sigma_reg;
}

transformed parameters {
  //varying intercepts
  real beta_0pop[P];
  real beta_0reg[R];

  //the linear predictor for the observations
  real&lt;lower=0> lambda[N];

  //compute the varying intercept at the region level
  for(r in 1:R){
    beta_0reg[r] = beta_0 + dev_reg[r];}

  //compute varying intercept at the population within region level
  for(p in 1:P){
     beta_0pop[p] = beta_0reg[PopWithinReg[p]] + dev_pop[p];}

  //the linear predictor
  for(n in 1:N){
     lambda[n] = beta_0pop[PopID[n]] + beta_1 * AMD[n] + beta_2 * nutrient[n];}
}

model {
  //weakly informative priors on the slopes
  beta_0 ~ cauchy(0,5);
  beta_1 ~ cauchy(0,5);
  beta_2 ~ cauchy(0,5);

  //weakly informative prior on the standard deviation
  sigma_pop ~ cauchy(0,2.5);
  sigma_reg ~ cauchy(0,2.5);

  //distribution of the varying intercept
  dev_pop ~ normal(0,sigma_pop);
  dev_reg ~ normal(0,sigma_reg);

  //likelihood
  Fruit ~ poisson_log(lambda);
}

generated quantities {
//sample predicted values from the model for posterior predictive checks
 int&lt;lower=0> fruit_rep[N];
 for(n in 1:N)
   fruit_rep[n] = poisson_log_rng(lambda[n]);
}</pre>
<p>I decided to use a Poisson distribution as the response is a count variable. The only “tricky” part is the index linking a particular population to its specific region (PopWithinReg). In this model we assume that variations between populations within regions is only affecting the average number of fruits but is not affecting the plant responses to the simulated herbivory (AMD) and to increased in nutrient levels. In other words populations within region is an intercept-only “random effect”. We turn back to R:</p>
<pre> data(&quot;Arabidopsis&quot;)
#generate the IDs
pop.id &lt;- as.numeric(Arabidopsis$popu)
pop_to_reg &lt;- as.numeric(factor(substr(levels(Arabidopsis$popu),3,4)))
#create the predictor variables
amd &lt;- ifelse(Arabidopsis$amd==&quot;unclipped&quot;,0,1)
nutrient &lt;- ifelse(Arabidopsis$nutrient==1,0,1)

m_arab &lt;- stan(&quot;nested_3lvl.stan&quot;,data=list(N=625,P=9,R=3,PopID=pop.id,
PopWithinReg=pop_to_reg,Fruit=Arabidopsis$total.fruits,
AMD=amd,nutrient=nutrient))
#check model
#launch_shinystan(m_arab)</pre>
<p>Rstan is warning us that we had some divergent iterations, we could correct this using non-centered re-parametrization (See <a target="_blank" rel="nofollow" href="http://wp.me/p2SacB-j8">this</a> post and the <a target="_blank" rel="nofollow" href="http://mc-stan.org/documentation/">STAN user guide</a>). More worrisome is the discrepancy between the posterior predictive data and the observed ones:</p>
<p><img sizes="(max-width: 900px) 100vw, 900px" alt="check_nest" class="wp-image-1411" src="https://i0.wp.com/biologyforfun.files.wordpress.com/2016/12/check_nest.png?w=456&ssl=1" data-recalc-dims="1"></p>
<p>We can explore these errors for each populations within regions:</p>
<pre>mcmc_arab &lt;- extract(m_arab)
#plot obs vs fitted data across groups
fit_arab &lt;- mcmc_arab$fruit_rep
#average across MCMC samples
Arabidopsis$Fit &lt;- apply(fit_arab,2,mean)
#plot obs vs fit
ggplot(Arabidopsis,aes(x=total.fruits,y=Fit,color=amd,shape=factor(nutrient)+
geom_point()+facet_wrap(~popu,scales=&quot;free&quot;)+
geom_abline(aes(intercept=0,slope=1))</pre>
<p><img sizes="(max-width: 800px) 100vw, 800px" alt="nested_hier" class="wp-image-1410" src="https://i1.wp.com/biologyforfun.files.wordpress.com/2016/12/nested_hier.png?w=456&ssl=1" data-recalc-dims="1"></p>
<p>The model predict basically four values, one for each combination of the two treatment variables. The original data are way more dispersed than the fitted ones, one could try to use negative binomial distribution while making the treatment effect also vary between the populations between the regions …</p>
<p>That’s it for this post, a great source of regression models for further examples in the <a target="_blank" rel="nofollow" href="https://github.com/stan-dev/example-models/wiki">STAN-wiki</a>.</p>
<p> </p>
<p> </p>
<p> </p>
<p>Filed under: <a target="_blank" rel="nofollow" href="https://biologyforfun.wordpress.com/category/r-and-stat/">R and Stat</a> Tagged: <a target="_blank" rel="nofollow" href="https://biologyforfun.wordpress.com/tag/bayesian/">Bayesian</a>, <a target="_blank" rel="nofollow" href="https://biologyforfun.wordpress.com/tag/r/">R</a>, <a target="_blank" rel="nofollow" href="https://biologyforfun.wordpress.com/tag/stan/">STAN</a>, <a target="_blank" rel="nofollow" href="https://biologyforfun.wordpress.com/tag/statistics/">Statistics</a> <a target="_blank" rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/biologyforfun.wordpress.com/1323/"><img alt="" src="http://feeds.wordpress.com/1.0/comments/biologyforfun.wordpress.com/1323/" border="0"></a> <a target="_blank" rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/biologyforfun.wordpress.com/1323/"><img alt="" src="http://feeds.wordpress.com/1.0/delicious/biologyforfun.wordpress.com/1323/" border="0"></a> <a target="_blank" rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/biologyforfun.wordpress.com/1323/"><img alt="" src="http://feeds.wordpress.com/1.0/facebook/biologyforfun.wordpress.com/1323/" border="0"></a> <a target="_blank" rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/biologyforfun.wordpress.com/1323/"><img alt="" src="http://feeds.wordpress.com/1.0/twitter/biologyforfun.wordpress.com/1323/" border="0"></a> <a target="_blank" rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/biologyforfun.wordpress.com/1323/"><img alt="" src="http://feeds.wordpress.com/1.0/stumble/biologyforfun.wordpress.com/1323/" border="0"></a> <a target="_blank" rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/biologyforfun.wordpress.com/1323/"><img alt="" src="http://feeds.wordpress.com/1.0/digg/biologyforfun.wordpress.com/1323/" border="0"></a> <a target="_blank" rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/biologyforfun.wordpress.com/1323/"><img alt="" src="http://feeds.wordpress.com/1.0/reddit/biologyforfun.wordpress.com/1323/" border="0"></a> <img alt="" src="https://i1.wp.com/pixel.wp.com/b.gif?resize=1%2C1&ssl=1" border="0" data-recalc-dims="1"></p>
<p>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="https://biologyforfun.wordpress.com/2016/12/08/crossed-and-nested-hierarchical-models-with-stan-and-r/"> R – biologyforfun</a></strong>.</div>
<hr>
<a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="https://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a title="The R Project for Statistical Computing" rel="nofollow" href="https://www.r-project.org/">R</a> news and <a title="R tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/tutorial">tutorials</a> on topics such as: <a title="Data science" rel="nofollow" href="https://www.r-bloggers.com/search/data%20science">Data science</a>, <a title="Big Data" rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data">Big Data, <a title="R jobs" rel="nofollow" href="https://www.r-users.com/">R jobs</a>, visualization (<a title="ggplot and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2">ggplot2</a>, <a title="Boxplots using lattice and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/boxplot">Boxplots</a>, <a title="Maps and gis" rel="nofollow" href="https://www.r-bloggers.com/search/map">maps</a>, <a title="Animation in R" rel="nofollow" href="https://www.r-bloggers.com/search/animation">animation</a>), programming (<a title="RStudio IDE for R" rel="nofollow" href="https://www.r-bloggers.com/search/RStudio">RStudio</a>, <a title="Sweave and literate programming" rel="nofollow" href="https://www.r-bloggers.com/search/sweave">Sweave</a>, <a title="LaTeX in R" rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX">LaTeX</a>, <a title="SQL and databases" rel="nofollow" href="https://www.r-bloggers.com/search/SQL">SQL</a>, <a title="Eclipse IDE for R" rel="nofollow" href="https://www.r-bloggers.com/search/eclipse">Eclipse</a>, <a title="git and github, Version Control System" rel="nofollow" href="https://www.r-bloggers.com/search/git">git</a>, <a title="Large data in R using Hadoop" rel="nofollow" href="https://www.r-bloggers.com/search/hadoop">hadoop</a>, <a title="Web Scraping of google, facebook, yahoo, twitter and more using R" rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping">Web Scraping</a>) statistics (<a title="Regressions and ANOVA analysis tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/regression">regression</a>, <a title="principal component analysis tutorial" rel="nofollow" href="https://www.r-bloggers.com/search/PCA">PCA</a>, <a title="Time series" rel="nofollow" href="https://www.r-bloggers.com/search/time+series">time series</a>, <a title="finance trading" rel="nofollow" href="https://www.r-bloggers.com/search/trading">trading</a>) and more...
<img height="1" alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/gv6qvQTtflU"></a></div>