# Multiple Regression in Rstan with factors

**URL:** <https://discourse.mc-stan.org/t/multiple-regression-in-rstan-with-factors/3189>\
**Category:** Modeling\
**Created:** [February 4, 2018, 3:19pm UTC](https://discourse.mc-stan.org/t/multiple-regression-in-rstan-with-factors/3189 "2018-02-04T15:19:00Z")\
**Posts on this page:** 1\
**Showing post:** 2

<div class="post-metadata">

**Author:** ![bgoodri](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/bgoodri/32/4451_2.png) [@bgoodri](https://discourse.mc-stan.org/u/bgoodri)\
**Post date:** [February 4, 2018, 7:15pm UTC](https://discourse.mc-stan.org/t/multiple-regression-in-rstan-with-factors/3189/2 "2018-02-04T19:15:45Z")

</div>

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'))

```

---

_[View the full topic](https://discourse.mc-stan.org/t/multiple-regression-in-rstan-with-factors/3189)._
