I’m trying to fit a choice modeling example that works in a frequentist framework and move it to a Bayesian framework via the `brms`

package. This is an example from a textbook co-authored by @eleafeit:

## Background of frequentist approach

Here’s the data:

```
# Read in data
cbc.df <- read.csv("http://goo.gl/5xQObB",
colClasses = c(seat = "factor", price = "factor",
choice="integer"))
# Format
cbc.df$eng <- factor(cbc.df$eng, levels=c("gas", "hyb", "elec"))
```

I next loaded a package to run a frequentist version of the choice model (`mlogit`

package) and formatted the data for that package:

```
# Add library
library(mlogit)
# Format data
cbc.mlogit <- mlogit.data(data=cbc.df, choice="choice", shape="long",
varying=3:6, alt.levels=paste("pos",1:3), id.var="resp.id")
# Fit model
m1 <- mlogit(choice ~ 0 + seat + cargo + eng + price, data = cbc.mlogit)
```

So what I’m trying to do now is figure out how to get the preference shares for different combinations of the features `seat`

, `cargo`

, `eng`

, and `price`

. This is the function that is used in the textbook I’m working from:

```
# Predicting shares
predict.mnl <- function(model, data) {
# Function for predicting shares from a multinomial logit model
# model: mlogit object returned by mlogit()
# data: a data frame containing the set of designs for which you want to
# predict shares. Same format at the data used to estimate model.
data.model <- model.matrix(update(model$formula, 0 ~ .), data = data)[ , -1]
utility <- data.model%*%model$coef
share <- exp(utility)/sum(exp(utility))
cbind(share, data)
}
```

So I can take m1 and get predicted vote shares for participants:

```
# Define combinations of variables
attrib <- list(seat = c("6", "7", "8"),
cargo = c("2ft", "3ft"),
eng = c("gas", "hyb", "elec"),
price = c("30", "35", "40"))
# Select some of those combinations to get predictions for
(new.data <- expand.grid(attrib)[c(8, 1, 3, 41, 49, 26), ]) # find attrib at top
# Generate shares
predict.mnl(m1, new.data)
share seat cargo eng price
8 0.11273356 7 2ft hyb 30
1 0.43336911 6 2ft gas 30
3 0.31917819 8 2ft gas 30
41 0.07281396 7 3ft gas 40
49 0.01669280 6 2ft elec 40
26 0.04521237 7 2ft hyb 35
```

So you can see that `share`

sums to 1.0 and that each row predicts the proportion of people that would choose that option if presented with these few combinations.

## My Question: How do I get shares if I fit the model in brms?

So I’m pretty sure that it’s straightforward to fit the frequentist version of model `m1`

in `brms`

:

```
b1 <- brm (choice ~ 0 + seat + cargo + eng + price, data=cbc.df,
family="categorical", chains = 2, cores = 2)
```

But can anyone help be determine how I can get shares from this model? How can do the equivalent to what’s being accomplished in this function (from above)?:

```
# Predicting shares
predict.mnl <- function(model, data) {
# Function for predicting shares from a multinomial logit model
# model: mlogit object returned by mlogit()
# data: a data frame containing the set of designs for which you want to
# predict shares. Same format at the data used to estimate model.
data.model <- model.matrix(update(model$formula, 0 ~ .), data = data)[ , -1]
utility <- data.model%*%model$coef
share <- exp(utility)/sum(exp(utility))
cbind(share, data)
}
```