Multiple Regression in Rstan with factors

You should do

mf <- model.frame(price ~ carat + cut, data = diamonds)
y <- mf$price
x <- model.matrix(price ~ carat + cut, data = mf)

or at least that is basically what the rstanarm and brms R packages do when fitting models like this. If you want to get rid of those Helmert contrasts, you can first coerce cut to an unordered factor or call

options(contrasts = c(unordered = 'contr.treatment',
                        ordered = 'contr.treatment'))
3 Likes