Get startedGet started for free

Choice models - under the hood

1. Choice models - under the hood

After doing all that data prep, we are ready to fit a choice model. We did this before in Chapter 1, but let's take a more careful look "under the hood" to understand how choice models and the mlogit() function work.

2. Multinomial logit model

A statistical model is a mathematical story describing where data comes from. Let's explain the story behind the multinomial logit with a bit of R code. The model begins with a linear equation for the utility for each alternative. For instance, if alternative 1 has 4 seats and a price of 100, then v1 is alpha times 4 + beta times 100. We add an error term to each of the v's that follows an extreme value distribution and is independent across alternatives to get what we call the utility. We never get to observe this utility, but we assume that the decision maker chooses the alternative that has the greatest utility. Assuming the alternative with the greatest utility is chosen, you can actually compute the probability that the user chooses each option. This involves an integral, and when you solve it you get simple formulae for the probabilities. The probability of choosing the first alternative is the exponential of v1 divided by the sum of the exponentials of v1, v2, and v3. From these formulae, you can see that the probability of choosing each alternative depends on the utilities of the other alternatives. This is a defining characteristic of choices. For example, you might be willing to buy an expensive white-chocolate bar, if the other options are even worse. Conveniently, the probabilities p1, p2, and p3 always sum up to one. And that's our model for where the data comes from. We assume this model is true or at least a reasonable representation of the choice process. The parameters of this model are alpha and beta and we don't know what they are.

3. Estimating a multinomial logit model with mlogit()

When we call mlogit(), we give it a formula that defines which variables are used to compute the v's. Here we've included seat and price to match the model on the previous slide. The alpha and beta are not included in the formula, but they are implicitly there. For reasons we'll explain later, we usually remove the intercept by including 0 + at the start of the formula. When we run mlogit(), it finds the values of the parameters that maximize the probabilities of the choices observed in our data. This is called maximum likelihood estimation. mlogit() does this numerically, by starting with the parameters all at zero, computing a gradient of the likelihood and then moving to parameters that give higher and higher likelihoods for the observed choices. If you include the option print.level equals 3 mlogit() will report each step in this process. At the first iteration, it tries alpha equals 0-point-113 and beta equals -0-point-103. By the third iteration it is at alpha equals 0-point-121 and beta equals -0-point-109.

4. Summary of mlogit() output

When we look at the summary of the mlogit() output, we can see the values of alpha and beta that maximize the likelihood of the choices in the column labeled Estimate in the Coefficients table towards the bottom of the output. The final values are alpha equals 0-point-1209849 multiplying seat and beta equals -0-point-1086995 multiplying price.

5. Indexed choice data

There is one other important thing we skipped over in chapter 1. mlogit() requires you to pass in the data in a special indexed format. To create it, you use a function called dfidx(). Here we're passing a data frame called sportscar-dot-df to dfidx() to create a new indexed object called sportscar. dfidx() needs two things from you. The first is idx, a pair of column names that index the data: the first one identifies the choice question, and the second one identifies the alternative within that question. The second is choice, which is the name of the column where the observed choice is stored. Our raw data doesn't have a column that numbers the choice questions, so on the first line we create one and call it chid. mlogit() will only accept data in this indexed form, so you always have to do this before you call mlogit().

6. Let's fit some choice models to the chocolate data!

Now, you can give it a try with the chocolate data.

Create Your Free Account

or

By continuing, you accept our Terms of Use, our Privacy Policy and that your data is stored in the USA.