This is an example taken from Jeff Gill’s Bayesian Methods: A Social and Behavioural Sciences Approach (Gill 2008). I wanted to re-implment the existing BUGS code into Stan and see if I could arrive at the same results.

Our Data

The example in the book utilises data from the US Department of Commerce’s Survey of Current Business. The data contain information by quarter from 1979 to 1989. After some hunting I found that Dr Gill graciously put the data to this book in the BaM package which I subsequently downloaded from CRAN.

data_sales <- BaM::retail.sales

And we can inspect it a little more:

data_sales

To describe the data further:

  • DSB - National Wage and Incime Disbursements (Billions)
  • EMP - Employees on non-agricultural payrolls (thousands)
  • BDG - building dealer sales (millions)
  • CAR - car dealer sales (millions)
  • FRN - home furnishings dealer sales (millions)
  • GRM - general merchanise sales in millions

Generative Model

In the case of the model for the data it is specified as

\[y_{i,j} \sim N(\beta_0[i]+\beta_1[i]x_j, \tau)\] \[\beta_0[i] \sim N(\mu_{\beta.0},\tau_{\beta.0})\] \[\beta_1[i] \sim N(\mu_{\beta.1},\tau_{\beta.1})\] \[\tau\sim\Gamma(0.01, 0.01)\]

Now to Stan

To implement the above model we would need to do the following:

// Normal Normal Model

data {
  int<lower=0> J;   // number of data items
  int<lower=0> I;   // number of predictors
  vector[J] x; // depdent variable
  matrix[J, I] y;   // predictor matrix
}
parameters {
  vector[I] beta0; //vector of beta0s
  vector[I] beta1; //vector of beta1s
  real mu_beta0; //average value of beta0
  real mu_beta1; //average value of beta1
  real tau;
  real tau_beta1;
  real tau_beta0;
  
}

transformed parameters{
  real xbar;
  xbar = mean(x); // calculate the average value of x
  
}
model {
  // priors
  tau ~ gamma(.01, .01); 
  mu_beta0 ~ normal(0, 10);
  tau_beta0 ~ gamma( .01, .01);
  mu_beta1 ~ normal(0, 10);
  tau_beta1 ~ gamma( .01, .01);
  
  //group effects for each indicator
  for(i in 1:I){
  beta0[i] ~ normal(mu_beta0, tau_beta0);
  beta1[i] ~ normal(mu_beta1, tau_beta1);
  }
  
  //model
  for(j in 1:J){
    y[j]~ normal(beta0 + beta1*(x[j]-xbar), tau);
  }
  
}

Now we can make sure that the model compiles.

library(rstan)
rstan_options(auto_write = TRUE)
nn_fit <- stan_model("stan_hlm_nn.stan")

Format our data for the modelL

data <- list(J = nrow(data_sales),
             I = 6L,
             x = data_sales$TIME,
             y = as.matrix(data_sales[,-1])/1000)

And fit our model:

fit <- sampling(nn_fit, data = data,  chains = 2, iter = 2000, refresh = 0)

Looking at the output from our model it matches that on page 405 in the second edition almost right on! Hooray!

print(fit, probs = c(0.025, 0.5, 0.975))
## Inference for Stan model: stan_hlm_nn.
## 2 chains, each with iter=2000; warmup=1000; thin=1; 
## post-warmup draws per chain=1000, total post-warmup draws=2000.
## 
##              mean se_mean    sd    2.5%     50%     98% n_eff Rhat
## beta0[1]     1.84    0.02  0.76    0.38    1.83    3.28  1763    1
## beta0[2]    96.31    0.02  0.77   94.85   96.31   97.76  2518    1
## beta0[3]    17.08    0.02  0.74   15.59   17.10   18.49  1799    1
## beta0[4]    66.04    0.01  0.73   64.62   66.05   67.45  2800    1
## beta0[5]    16.18    0.02  0.76   14.76   16.17   17.72  2192    1
## beta0[6]    37.85    0.02  0.74   36.41   37.86   39.29  2330    1
## beta1[1]     0.04    0.00  0.06   -0.07    0.04    0.15  1737    1
## beta1[2]     0.50    0.00  0.06    0.39    0.50    0.61  2884    1
## beta1[3]     0.32    0.00  0.06    0.19    0.32    0.43  2224    1
## beta1[4]     1.52    0.00  0.06    1.41    1.52    1.64  2215    1
## beta1[5]     0.37    0.00  0.06    0.25    0.37    0.48  2747    1
## beta1[6]     0.55    0.00  0.06    0.44    0.55    0.66  2137    1
## mu_beta0     9.28    0.22  9.60   -9.94    9.29   27.73  1845    1
## mu_beta1     0.56    0.01  0.25    0.07    0.55    1.11  1581    1
## tau          4.96    0.01  0.22    4.54    4.95    5.41  1865    1
## tau_beta1    0.60    0.01  0.25    0.32    0.54    1.26   603    1
## tau_beta0   49.03    0.76 18.66   26.14   45.42   91.46   607    1
## xbar        22.50     NaN  0.00   22.50   22.50   22.50   NaN  NaN
## lp__      -586.80    0.11  3.16 -593.77 -586.42 -581.69   775    1
## 
## Samples were drawn using NUTS(diag_e) at Tue Feb 26 22:02:06 2019.
## For each parameter, n_eff is a crude measure of effective sample size,
## and Rhat is the potential scale reduction factor on split chains (at 
## convergence, Rhat=1).

Posterior Model Checking

Thus we refit this model in Stan, but let’s check the trace plots and some posterior predictive checks to make sure we have a model that makes sense.

traceplot(fit, "beta0")

traceplot(fit, "beta1")

library(bayesplot)
posterior <- as.array(fit)

mcmc_areas(posterior,
           pars = c("beta1[1]","beta1[2]","beta1[3]","beta1[4]","beta1[5]","beta1[6]"),
           prob = 0.8)

posterior2 <- extract(fit, inc_warmup = TRUE, permuted = FALSE)
color_scheme_set("darkgray")
mcmc_scatter(
  as.matrix(fit),
  pars = c("tau", "beta1[1]"), 
  np = nuts_params(fit), 
  np_style = scatter_style_np(div_color = "green", div_alpha = 0.8)
)

References

Gill, Jeff. 2008. Bayesian Methods: A Social and Behavioral Sciences Approach. 2nd ed. CRC Press; CRC Press. http://www.crcpress.com/product/isbn/9781584885627.



Research and Methods Resources
me.dewitt.jr@gmail.com



Winston- Salem, NC

Michael DeWitt


Copyright © 2018 Michael DeWitt. All rights reserved.