Example with family "multinomial"

Hi,
Can anyone suggest an example including r-code that uses the family “multinomial” in brms?

I cannot find anywhere how to specify the model.
Thanks

  • Operating System: windows 10
  • brms Version: 2.8.0

Here is an example with dummy data:

N <- 15
dat <- data.frame(
    y1 = rbinom(N, 10, 0.3), y2 = rbinom(N, 10, 0.5), 
    y3 = rbinom(N, 10, 0.7), x = rnorm(N)
)
dat$size <- with(dat, y1 + y2 + y3)
dat$y <- with(dat, cbind(y1, y2, y3))

prior <- prior(normal(0, 10), "b", dpar = muy2) +
    prior(cauchy(0, 1), "Intercept") +
    prior(normal(0, 2), "Intercept", dpar = muy3)
fit <- brm(bf(y | trials(size)  ~ 1, muy2 ~ x), data = dat, 
                family = multinomial(), prior = prior)

You may also bind y1 to y3 together within the formula as long as you avoid using cbind which is (currently still) reseverd form multivariate models.

4 Likes

Thank you very much for taking the time.

I cannot fully understand the model because I do not get what you did here:

I tried to look up

> dpar

in

set_prior {brms}

But I could not find how to define it and what, muy2/muy3 are.
I understood they are not arbitrary names because changing them I get the following error message:

> xxx is not a valid distributional or non-linear parameter

Could you tell me where to find these info?

Thank you again

The name of the distributional parameters in multinomial models are mu<category name> (whereas the first category will serve as the reference by default). You only need to use those if you want to fit predictor terms of different structure to different categories. If you want to apply the same structure to all categories (except for the reference of course), you can simply write

bf(y | trials(size) ~ <predictor term>)
2 Likes

How do you bind the Ys directly in the formula if you cannot use cbind?

Uhm, using cmdstan as backend I get this error:

Semantic error in '/var/folders/v5/hz8q76_n1g1b7v_bmyfcp1qw0000gn/T/RtmpuzrsEJ/model-41b04ae911d8.stan', line 10, column 8 to column 30:
   -------------------------------------------------
     8:     *   a scalar to be added to the log posterior 
     9:     */ 
    10:     real multinomial_logit_lpmf(int[] y, vector mu) {
                 ^
    11:       return multinomial_lpmf(y | softmax(mu));
    12:     }
   -------------------------------------------------

Identifier 'multinomial_logit_lpmf' clashes with Stan Math library function.

make: *** [/var/folders/v5/hz8q76_n1g1b7v_bmyfcp1qw0000gn/T/RtmpuzrsEJ/model-41b04ae911d8.hpp] Error 1
Errore: An error occured during compilation! See the message above for more information.
Inoltre: Warning message:
Specifying global priors for regression coefficients in categorical models is deprecated. Please specify priors separately for each response category. 

If I don’t use cmdstan instead I get the same but as a warning:

When you compile models, you are also contributing to development of the NEXT
Stan compiler. In this version of rstan, we compile your model as usual, but
also test our new compiler on your syntactically correct model. In this case,
the new compiler did not work like we hoped. By filing an issue at
https://github.com/stan-dev/stanc3/issues with your model
or a minimal example that shows this warning you will be contributing
valuable information to Stan and ensuring your models continue working. Thank you!
This message can be avoided by wrapping your function call inside suppressMessages()
 or by first calling rstan_options(javascript = FALSE).
Semantic error in 'string', line 10, column 8 to column 30: 
Identifier 'multinomial_logit_lpmf' clashes with Stan Math library function.

Sorry, I confused multinomial with multivariate!

As far as I know, for multinomial you have to cbind outside of the formula then use the bound variable as the response.

uhm, could you make an example?

Yep, but Paul said " You may also bind y1 to y3 together within the formula as long as you avoid using cbind" but didn’t say how.

Anyway, the bigger problem now is making it work with cmdstan.

for example use cbind2 ← cbind

1 Like

ah!

@paul.buerkner I saw there’s an issue on github about this but it’s solved… I’m updating from cmdstan 2.26 → 2.26.1 but I don’t think that’s the cause.

UPDATE: indeed it’s not
UPDATE: updating from brms 2.14.4 → 2.15 solved the problem!