Change contrast category in categorical regression


#1

Is it possible to change the contrast level in a categorical regression using brms? To take an example from @paul.buerkner 's GitHub, using the inhaler data,

fit <- brm(rating ~ 0 + trait + trait:(period + carry + treat) + (0+trait|subject),
data = inhaler, family = categorical(), prior = set_prior(“normal(0,5)”))

Is it possible to change the contrast level for model fitting such that it is level 4 rather than level 1, for example? Given that objects saved as factors in R can’t be processed in Stan, I’m unsurprised that the standard relevel() and C() tricks don’t work. Thanks!

  • Operating System: Windows 10 64 bit
  • brms Version: 2.3.1

#2

You have to have a factor response variable for relevel to work. However, inhaler$rating is numeric. If you transform it to a factor first and then relevel, you can change the contrast category.


#3

Thanks for the sanity check @paul.buerkner. Because brms was passing to Stan and Stan doesn’t like factors, when I saw the error

Error: Invalid response category names. Please avoid using any special characters in the names.

when attempting to run a categorical model with a factor response, I assumed it was an issue passing a factor to Stan. However, I wasn’t able to replicate the problem using the inhaler data, following your response. On revisting my code, it appears it was an issue of converting data that included -1 to a factor, which kicked back the above error message above because - is treated as a special character. Renaming the variable solved the problem.