How to fit multivariate multinomial model

I’m trying to move a multinomial model from brms to PyMC3, but I’m struggling to work out the syntax.

I have this code in BRMS (which works and gives satisfactory results):

df$responses <- cbind(df$response_1, df$response_2, df$response_3, df$response_4)
df$total_responses <- df$response_1 + df$response_2 + df$response_3 + df_response_4
brm(responses | trials(total_responses) ~ predictor_1 + (1 | county / state), data = df, family = "multinomial")

My priors are defaults for now, but I’m really struggling to work out how to write the model in PyMC3. My understanding is that I need to create a matrix for the design matrix, then specify each of the priors and then the likelihood, but I do not understand how to write that out in PyMC3 code.

1 Like

Welcome!

You can check out the code associated with chapter 22 of Kruschke’s DBDA book. The code doesn’t reflect recent versions of pymc, but it makes a decent intro. There have also been a few discussions of multinomial regression models, for example, this one and this this one. If those don’t help or if you need help adapting the specifics of your model (e.g., the random intercepts), feel free to ask.

@mr_penguin were you able to solve this problem? Bambi has a multinomial family now which works quite similar to brms.

Hi, yes I found that - if anyone else stumbles on this, the formula syntax would be c(cat_1, cat_2, cat_3) ~ predictors.

1 Like

Glad you found it!

For the record, this is still not very well documented (sorry!). But you can see the example in the PR where we add this family Add multinomial family by tomicapretto · Pull Request #490 · bambinos/bambi · GitHub

1 Like

Oh, so new. This syntax is better than brms imo - its much cleaner and the computer does more of the routine calculations for me, which is good.

1 Like