# Mixed Logit Model

**URL:** <https://discourse.mc-stan.org/t/mixed-logit-model/3513>\
**Category:** Modeling\
**Created:** [March 8, 2018, 4:22pm UTC](https://discourse.mc-stan.org/t/mixed-logit-model/3513 "2018-03-08T16:22:56Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![TOM](https://avatars.discourse-cdn.com/v4/letter/t/3d9bf3/32.png) [@TOM](https://discourse.mc-stan.org/u/TOM)\
**Post date:** [March 8, 2018, 4:22pm UTC](https://discourse.mc-stan.org/t/mixed-logit-model/3513/1 "2018-03-08T16:22:56Z")

</div>

Hi  
Before setting up my own mixed logit model I wanted to ask if such a model already exists. By mixed logit I understand a multi-logit regression (see stan manual page 133) that includes both, alternative specific variables as well as individual variables.  
Thanks for any hints,  
Tom

---

<div class="post-metadata">

**Author:** ![martinmodrak](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/martinmodrak/32/133_2.png) [@martinmodrak](https://discourse.mc-stan.org/u/martinmodrak)\
**Post date:** [March 10, 2018, 12:37pm UTC](https://discourse.mc-stan.org/t/mixed-logit-model/3513/2 "2018-03-10T12:37:55Z")

</div>

I am not sure I understand what kind of model you mean (I don’t know what you mean by “alternative specific” and “individual” variables). I would however definitely check if your use case isn’t covered by [brms](https://github.com/paul-buerkner/brms). At the very least, you could build a model as close to your aims with brms and then use the code generated by brms as a starting point.

---

<div class="post-metadata">

**Author:** ![TOM](https://avatars.discourse-cdn.com/v4/letter/t/3d9bf3/32.png) [@TOM](https://discourse.mc-stan.org/u/TOM)\
**Post date:** [March 13, 2018, 4:36pm UTC](https://discourse.mc-stan.org/t/mixed-logit-model/3513/3 "2018-03-13T16:36:49Z")

</div>

There seems to be a lot of confusion about terminology, at least for me. By mixed logit I refer to what others call a general model that combines features of a multinomial logit and a conditional logit. A formulation that may help to clarify what I mean is given [here](http://data.princeton.edu/wws509/notes/c6s3.html) (see section 6.3.4).

@martinmodrak I certainly will look into brms.

---

<div class="post-metadata">

**Author:** ![James\_Savage](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/james_savage/32/26_2.png) [@James\_Savage](https://discourse.mc-stan.org/u/James_Savage)\
**Post date:** [March 14, 2018, 3:01am UTC](https://discourse.mc-stan.org/t/mixed-logit-model/3513/4 "2018-03-14T03:01:42Z")

</div>

Hi Tom,

Yep – very straightforward. Here’s a gist that describes implementing with only choice-level variables that vary across choices

> <https://gist.github.com/khakieconomics/0333a54dff4fabf6204080ca5bf92cb6>

If you want to include variables that vary by the individual, then you simply need to include on line 37 those variables multiplied through by their coefficient matrix, as with multinomial logit.

Here’s a slide-deck that might help.

[http://rpubs.com/jimsavage/sawtoothcon](http://rpubs.com/jimsavage/sawtoothcon)

---

<div class="post-metadata">

**Author:** ![TOM](https://avatars.discourse-cdn.com/v4/letter/t/3d9bf3/32.png) [@TOM](https://discourse.mc-stan.org/u/TOM)\
**Post date:** [March 14, 2018, 12:19pm UTC](https://discourse.mc-stan.org/t/mixed-logit-model/3513/5 "2018-03-14T12:19:25Z")

</div>

@James_Savage Thanks for the hint. Is there toy data to play around with the model? I’m asking because it is difficult to follow how you subset your data in line 37.

---

<div class="post-metadata">

**Author:** ![James\_Savage](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/james_savage/32/26_2.png) [@James\_Savage](https://discourse.mc-stan.org/u/James_Savage)\
**Post date:** [March 14, 2018, 4:33pm UTC](https://discourse.mc-stan.org/t/mixed-logit-model/3513/6 "2018-03-14T16:33:03Z")

</div>

Hi Tom,

Here are a couple of worked examples for you.

[http://rpubs.com/jimsavage/using\_decisionmaker\_variables\_in\_mixed\_logit](http://rpubs.com/jimsavage/using_decisionmaker_variables_in_mixed_logit)

If you don’t want to include individual-level parameters on the choice-level attributes you can basically delete the second half of the definition of `beta_individual` (make sure to remove the `tau` and `L_Omega` parameters in that case).

Note I _strongly_ recommend the second version of the model, as it’s more flexible, both in terms of the types of choice patterns it can fit but also the fact that in each task/market/comparison set you can have different possible choices characterised only by their attributes _and still_ allow demographics to influence decision probabilities.

---

<div class="post-metadata">

**Author:** ![TOM](https://avatars.discourse-cdn.com/v4/letter/t/3d9bf3/32.png) [@TOM](https://discourse.mc-stan.org/u/TOM)\
**Post date:** [March 15, 2018, 1:19pm UTC](https://discourse.mc-stan.org/t/mixed-logit-model/3513/7 "2018-03-15T13:19:50Z")

</div>

Hi James

Thanks!

I had a hard time getting my head around your indexing part. So I tried it with a different style of indexing. If I’m not mistaken, it includes individual data (demographics) as well as attributes of the alternatives. My model looks like this:

> <https://github.com/ThomasWilli/mixed-logit/blob/master/stan/mnl_cl.stan>

It seems to me that this model is much more sparse… In other words what am I missing here?

---

<div class="post-metadata">

**Author:** ![James\_Savage](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/james_savage/32/26_2.png) [@James\_Savage](https://discourse.mc-stan.org/u/James_Savage)\
**Post date:** [March 15, 2018, 5:06pm UTC](https://discourse.mc-stan.org/t/mixed-logit-model/3513/8 "2018-03-15T17:06:19Z")

</div>

Hi Tom -

First, this model is not identified. As you have it, individual attributes affect the utility of all choices to the same degree (ie simply result in a shift in utility for all choices that does not change their rank ordering). Your beta needs to be a matrix (probably with the P-1 parameterization, which can be achieved by setting the last row as 0).

Second, it assumes that all individuals the same marginal utilities, and so you’ll get the red bus/blue bus problem. That mightn’t be a concern for you.

Third, with logits, normal(0, 5) is an enormously wide prior.

Hope this helps!

---

<div class="post-metadata">

**Author:** ![jeremy.koster](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/jeremy.koster/32/5756_2.png) [@jeremy.koster](https://discourse.mc-stan.org/u/jeremy.koster)\
**Post date:** [March 15, 2018, 9:16pm UTC](https://discourse.mc-stan.org/t/mixed-logit-model/3513/9 "2018-03-15T21:16:52Z")

</div>

Hi James,

This is great stuff and potentially very useful in a variety of contexts. One thing that social scientists often encounter with their data is repeated observations of the same individuals. To borrow from your beer example (in the slides), imagine that you have data on the age and salary of the individuals consuming the beer. And then imagine that you see their respective beer choices on approximately five separate occasions, perhaps each characterized by time-varying covariates (amount of stress reported by beer drinker _i_ before making the choice).

What are the chances of expanding the example to account for these variables (presumably with random effects for the repeated observations of individuals)?

---

<div class="post-metadata">

**Author:** ![James\_Savage](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/james_savage/32/26_2.png) [@James\_Savage](https://discourse.mc-stan.org/u/James_Savage)\
**Post date:** [March 15, 2018, 9:24pm UTC](https://discourse.mc-stan.org/t/mixed-logit-model/3513/10 "2018-03-15T21:24:33Z")

</div>

Hey Jeremy –

That’s precisely the sort of model I fit everyday! See the second example here, which says that individual characteristics -\> preferences over choice attributes, which when combined with choice attributes give us choice probabilities.

[http://rpubs.com/jimsavage/using\_decisionmaker\_variables\_in\_mixed\_logit](http://rpubs.com/jimsavage/using_decisionmaker_variables_in_mixed_logit)

---

<div class="post-metadata">

**Author:** ![jeremy.koster](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/jeremy.koster/32/5756_2.png) [@jeremy.koster](https://discourse.mc-stan.org/u/jeremy.koster)\
**Post date:** [March 15, 2018, 9:28pm UTC](https://discourse.mc-stan.org/t/mixed-logit-model/3513/11 "2018-03-15T21:28:35Z")

</div>

Oh cool. One slight hurdle, though. I started working through your simulated data script and hit an error in the creation of the indexes data frame:

Error in data\_frame(individual = rep(1:I, each = K \* 10), task = rep(1:T, :  
could not find function “%\>%”

Am I overlooking a needed package? Or is there something else about the syntax that I’m overlooking?

---

<div class="post-metadata">

**Author:** ![James\_Savage](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/james_savage/32/26_2.png) [@James\_Savage](https://discourse.mc-stan.org/u/James_Savage)\
**Post date:** [March 15, 2018, 9:32pm UTC](https://discourse.mc-stan.org/t/mixed-logit-model/3513/12 "2018-03-15T21:32:13Z")

</div>

Ah – you’ll need to `install.packages("dplyr")` and `library(dplyr)` before you run the script.

---

<div class="post-metadata">

**Author:** ![jeremy.koster](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/jeremy.koster/32/5756_2.png) [@jeremy.koster](https://discourse.mc-stan.org/u/jeremy.koster)\
**Post date:** [March 15, 2018, 9:35pm UTC](https://discourse.mc-stan.org/t/mixed-logit-model/3513/13 "2018-03-15T21:35:47Z")

</div>

Got it. Thanks.

---

<div class="post-metadata">

**Author:** ![jeremy.koster](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/jeremy.koster/32/5756_2.png) [@jeremy.koster](https://discourse.mc-stan.org/u/jeremy.koster)\
**Post date:** [March 15, 2018, 9:54pm UTC](https://discourse.mc-stan.org/t/mixed-logit-model/3513/14 "2018-03-15T21:54:13Z")

</div>

If I’m following along correctly, the attributes of the products vary across tasks (that is, there are time-varying covariates for the products). But the X2 variables for the 50 individuals are assumed to be time-invariant, correct?

How straightforward is to add time-varying covariates for the individuals – perhaps measures of emotional states or variation in monetary endowments at time _t_ or something like that?

---

<div class="post-metadata">

**Author:** ![James\_Savage](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/james_savage/32/26_2.png) [@James\_Savage](https://discourse.mc-stan.org/u/James_Savage)\
**Post date:** [March 15, 2018, 10:14pm UTC](https://discourse.mc-stan.org/t/mixed-logit-model/3513/15 "2018-03-15T22:14:23Z")

</div>

Hey Jeremy,

Yep – difficult, but in theory at least possible. I guess you could model each individual-time pair, and presumably have an individual-level hyperprior.

Something like

beta\_it ~ normal(beta\_i + Gamma \* Z\_it’, sigma)  
beta\_i ~ normal(beta, sigma\_2)

I’m pretty sure it would work, but might take some time!

---

<div class="post-metadata">

**Author:** ![jeremy.koster](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/jeremy.koster/32/5756_2.png) [@jeremy.koster](https://discourse.mc-stan.org/u/jeremy.koster)\
**Post date:** [March 15, 2018, 10:33pm UTC](https://discourse.mc-stan.org/t/mixed-logit-model/3513/16 "2018-03-15T22:33:32Z")

</div>

In their 1989 book, McCullagh and Nelder note the equivalence of conditional multinomial models and Poisson models. Prior to seeing your post, I was looking into the possibility of using a Poisson parameterization. The downside seems to be the need for binary dummy variables corresponding to the choices (equal to the number of people making the choices, that is, which becomes unwieldy with large sample sizes).

Your comment about the challenges of adding time-varying individual-level predictors makes me wonder if the Poisson might have some advantages . . . even though my hunch is that your approach remains preferable. I was also looking into multilevel conditional logit models using gllamm in Stata.

Meanwhile, my initial read on the individual-time effect is that it has the advantage of conservatism, almost like a random coefficient (or random slopes) model. But different disciplines might vary in terms of its necessity.

---

<div class="post-metadata">

**Author:** ![James\_Savage](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/james_savage/32/26_2.png) [@James\_Savage](https://discourse.mc-stan.org/u/James_Savage)\
**Post date:** [March 16, 2018, 6:38pm UTC](https://discourse.mc-stan.org/t/mixed-logit-model/3513/17 "2018-03-16T18:38:33Z")

</div>

Hi Jeremy,

It really depends what you are modeling. In Stan at least, I’ve found the Poisson implementation to be a fair bit slower than the multinomial likelihood, but that might just be my coding of it. But that’s not the model–the model is one of individual choice. And you want to choose the model that makes best sense of your data. In most discrete choice problems this will be conditional logit with parameters varying at the level of the individual and the possibility of unobserved demand shocks that correlate with your choice attributes. If you only have aggregate counts data, you can still fit the model as illustrated here:

> **[Aggregate random coefficients logit—a generative approach](http://modernstatisticalworkflow.blogspot.com/2017/03/aggregate-random-coefficients-logita.html)**
>
> Jim Savage, Michael Betancourt and Shoshana Vasserman 17 March 2017 This post illustrates how to fit aggregate random coefficient l...

Cheers,  
Jim

---

<div class="post-metadata">

**Author:** ![jeremy.koster](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/jeremy.koster/32/5756_2.png) [@jeremy.koster](https://discourse.mc-stan.org/u/jeremy.koster)\
**Post date:** [March 16, 2018, 8:08pm UTC](https://discourse.mc-stan.org/t/mixed-logit-model/3513/18 "2018-03-16T20:08:04Z")

</div>

Cool. And the translation of terminology across disciplines is helpful.

Thanks.

---

<div class="post-metadata">

**Author:** ![daniel\_guhl](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/daniel_guhl/32/2003_2.png) [@daniel\_guhl](https://discourse.mc-stan.org/u/daniel_guhl)\
**Post date:** [March 18, 2018, 2:07pm UTC](https://discourse.mc-stan.org/t/mixed-logit-model/3513/19 "2018-03-18T14:07:29Z")

</div>

Hi Tom,  
Once the `X` matrix is specified correctly, estimating models with alternative- and individual-specific variables is easy. One simple solution that is often applied in the econometrics literature is to use one alternative as a reference (e.g., `j = 1`). That means, you build for each individual-specific variable `J -1` alternative-specific versions (e.g., `income_2`, …,`income_J`) that are zero for each alternative other than j.

I’ve added an example using `R` and the `TravelMode` data from the `AER` package. As a comparison a used mlogit. `mFormula()` makes it very easy to specify a formula with both kinds of variables and then you can use `model.matrix()` to create `X`. I hope using `R` for this example is ok for you. The Stan program is included in the `R` file and works for multinomial, conditional, and “mixed” logit models, as long as you specific `X` correctly. Please also have a look at the [vignette](https://cran.r-project.org/web/packages/mlogit/vignettes/mlogit.pdf) from `mlogit` regarding setting up `X`.

[https://gist.github.com/dhguhl/46bd5dabeb5bb1931e81e1b0354cfe3f](https://gist.github.com/dhguhl/46bd5dabeb5bb1931e81e1b0354cfe3f)

PS: I do not like the term “mixed logit” for this type of model because this is also (maybe more often) used for multinomial logit model with individual-specific parameters. However, my econometrics Prof. also always used it ambiguously… very confusing.

PPS: I’m not saying my prior choice is the best, the stan program is perfect, or assuming homogenous preferences is a good idea… just tried to keep the example as simple as possible!

Best,  
Daniel

---

<div class="post-metadata">

**Author:** ![TOM](https://avatars.discourse-cdn.com/v4/letter/t/3d9bf3/32.png) [@TOM](https://discourse.mc-stan.org/u/TOM)\
**Post date:** [March 19, 2018, 10:05am UTC](https://discourse.mc-stan.org/t/mixed-logit-model/3513/20 "2018-03-19T10:05:08Z")

</div>

Hi Daniel

Thanks for sharing this. If I understood you correctly, it is indeed very easy to estimate such a model. I guess your approach has the same effect of what @James_Savage does by setting “outside options” to zero (I’m still working on implementing his approach, though). I adapted your example to my data and used `R` as well (using `mlogit.data`, `mFormula` and `model.matrix`). Additionally, I’ve added a vector, that sets alternatives that are not viable to individual `i` to zero.

> <https://github.com/ThomasWilli/mixed-logit/blob/master/stan/mnl_cl_v2.stan>

PS: The terminology is indeed confusing.

[Next page](https://discourse.mc-stan.org/t/mixed-logit-model/3513.md?page=2)
