How to get nicely named parameters in Stan?

I have two models which run nicely and give me good posterior distributions.

  • Model 1 has the parameters hard coded and not done via a matrix, so it gives me outputs with names of the input variable (b_group_1_intercept).
  • Model 2 has the parameters submitted via a matrix, so it is more efficient, but the parameter names are b_group_1[1], b_group_1[2] etc.
  • Model 3 is the BRMS version of my model, which gives me the useful name muprop1_Intercept and the efficiency of the matrix.

My question is how do I get a model with the matrix’s efficiency and the nice naming system? Can I do still do this by sending independent variables one column at a time and building the design matrix in the transformed data block?

In Stan we’ve been adding vectorized glms for common models, and if you use them, you will get the distinction between the intercept and the slope terms. Here are a few examples:

The naming convention here is <distribution>_<link function>_glm - I’m not sure we have a comprehensive list of all the _glm distributions anywhere, nor is this mentioned in the Stan User’s Guide and it should be (will file an issue on this).

Here’s an example of how to set this up for simple linear regression with 2 predictors - this is the non-hierarchical version of the model for the radon dataset from Gelman and Hill 2007, chapter 12.

data {
  int<lower=1> N;  // observations
  int<lower=1> J;  // counties
  array[N] int<lower=1, upper=J> county;
  vector[N] y;     // radon
  vector[N] x;     // floor
  vector[N] u;     // uranium
}
transformed data {
  matrix[N, 2] xs = [x', u']';
}
parameters {
  vector[J] alpha;
  vector[2] beta;
  real<lower=0> sigma;
}
transformed parameters {
  vector[N] alphas = alpha[county];
}
model {
  y ~ normal_id_glm(xs, alphas, beta, sigma);  
  alpha ~ normal(0, 2.5);
  beta ~ normal(0, 2.5);
  sigma ~ normal(0, 5);
}

BRMS is manipulating the generated stanfit object and renaming the parameters for you. cf brms/rename_pars.R at 811d68a447823c29c9af9dc8fd6cfbebe39cb249 · paul-buerkner/brms · GitHub

(update: filed an issue on the docs for this: give examples and cross-reference vectorized glms in SUG · Issue #593 · stan-dev/docs · GitHub)

1 Like