brms icon indicating copy to clipboard operation
brms copied to clipboard

contrasts argument to bf/lf/nlf?

Open kevingoneill opened this issue 3 years ago • 1 comments

The lm function includes an argument "contrasts" that allows you to set contrast coding for any categorical IVs in your model formula, which is used to build the design matrix. For example, to get a model with sum-to-zero contrasts instead of the default treatment contrasts, you can run something like:

df <- data.frame(y=rnorm(n=50), x=as.factor(rbinom(n=50, size=1, prob=0.5)))
m <- lm(y ~ x, data=df, contrasts=list(x='contr.sum'))
summary(m)

As far as I understand, brms doesn't directly provide this functionality. This isn't usually ever a problem, since you can just set the proper contrasts outside of a call to brm. But I've found myself in a rather odd situation where I want to code a variable differently for different distributional parameters in a model. The gist is that I'm fitting an unequal variance signal detection model with the cumulative family (as nicely described here), and although I'd prefer to use contr.sum for the main part of the model, I need to use contr.treatment for the disc submodel to ensure that log(disc) = 0 for the reference value of my IV (whenever x = 0). Specifically, I think it'd be nice to be able to do something like this:

df <- data.frame(y=1+rbinom(50, 5, 0.5), x=as.factor(rbinom(50, 1, 0.5)))
m.f1 <- bf(y ~ x, contrasts=list(x='contr.sum'))
m.f2 <- lf(disc ~ 0 + x, cmc=FALSE, contrasts=list(x='contr.treatment'))
m <- brm(m.f1 + m.f2, data=df, family='cumulative')

Of course, one could simply define two different x variables with the same values but different contrast codings, but that feels clunky. I understand that this is definitely a low-priority issue and something that you're unlikely to ever need, but if it's easy to implement & doesn't interfere with the way bf/lf/nlf already work, then I think it'd be a nice thing to have.

While I'm here, I want to express how incredible brms is as a package! I've been using it for a lot of my recent projects, and it is mind-boggling how many things you can do with it & how quickly it's being developed.

kevingoneill avatar Apr 21 '21 21:04 kevingoneill

I like the idea and will think about it a little more before implementing it.

paul-buerkner avatar Apr 22 '21 07:04 paul-buerkner