1

EDIT - I have managed to resolve this. See my comment below

I am running a Bayesian regression in R using rstanarm using priors I have set, using the following code:

priors <- rstanarm::normal(location = c(-1, 0.5, 2), scale = c(1, 2, 0.5))

bmd <- stan_glm(s01_1 ~ pc + a03 + l01, data=bes19, prior = priors, iter=1000, seed=6942)

This outputs the following error, which I have no idea how to rectify:

  Exception: mismatch in dimension declared and found in context; processing stage=data initialization; variable name=prior_scale; position=0; dims declared=(19); dims found=(3)  (in '/data/hyperparameters.stan' at line 2; included from 'model_continuous' at line 56)

failed to create the sampler; sampling not done
Error in check_stanfit(stanfit) : 
  Invalid stanfit object produced please report bug

I would be grateful for any solutions, please. Thank you.

  • 1
    I have found the solution - my independent variables were factors. Changing them to numeric using as.numeric() was sufficient and now the model works. I thought I'd post this in case anyone else stumbles on this problem. – William Wormell Jul 09 '21 at 15:29
  • Yes, in general, the `location` and `scale` arguments to a prior in rstanarm need to have length equal to the number of coefficients (or else be scalars that are internally recycled to apply to all coefficients). When some of the predictions are factors, the number of coefficients to estimate for that factor is equal to the number of levels less one. By changing the three predictors from factors to integers, you made them require only one coefficient each, although that adds an assumption that the effect of the variable is linear. – Ben Goodrich Jul 10 '21 at 17:04

0 Answers0