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 me 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 I'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 I'll explain later, I 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-100 equals -0-point-151. By the third iteration it is at alpha equals 0-point-114 and beta equals -0-point-169.

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-1143487 multiplying seat and beta equals -0-point-1687046 multiplying price.

5. mlogit.data objects

There is one other important thing I skipped over in chapter 1. mlogit() requires you to pass in the data in a special mlogit-dot-data format. To create this format, you use a function called mlogit-dot-data(). Here I'm passing a data frame called sportscar-dot-df to mlogit-dot-data() to create a new mlogit-dot-data object called sportscar. The key inputs to this function are: the name of the data frame where your data is stored, the shape of that data: "long" or "wide", choice, which is the name of the column where the choice is stored, varying, which indicates the column numbers where the attributes of the alternatives are stored, and alt, which is a string with the name of the column where the alternative numbers are stored. mlogit() will only accept data that is in the mlogit-dot-data 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.