Get startedGet started for free

Heterogeneity in preferences for other features

1. Heterogeneity in preference for other features

In the last video we saw that price sensitivity barely varied across sports car buyers, while in the exercises you found a lot of variation in price sensitivity among the chocolate buyers. Both are real results, and either way price is only one attribute, so we should also think about heterogeneity in preferences for the other attributes. We would expect that people have different preferences for the features of sports cars or the different types of chocolates and we can incorporate this into our choice models. But these other attributes are factors and when we fit a hierarchical model, it is important to think about how those factors are coded. So, before we get to a model with heterogeneity in all the parameters, let's take a minute and talk about a different way to code factors that works better for hierarchical models.

2. A different way to code factors

In the last chapter, we used dummy coding. For example, the seat factor was represented by two variables: seat4 and seat5 where seat4 is one if the car has four seats and zero otherwise. In the table, the rows represent the original levels of the seat factor and the columns represent our dummy coded variables that will be included in the model. With effects coding, we still have two variables, but watch which level gets left out. R's dummy coding leaves out the first level, which is two seats, while effects coding leaves out the last one, which is five seats. So the first variable takes the value of one if the car has two seats, zero if it has four seats, and -1 if it has five seats, as you can see here. This coding may look a bit funny, but what it does is make the coefficients for seat2 and seat4 relative to the average of all the levels instead of relative to the zero level.

3. Changing the coding for a factor

Every R factor has a set of contrasts, which define the coding scheme that you want to use for each factor. R's default is dummy coding, but we can change that. In this line of code, contr-dot-sum(levels(sportscar$seat)) we create an effects coding scheme for the seat factor. When we assign the output of contr-dot-sum() to contrasts(sportscar$seat), we are storing the effects coding scheme with the data frame. That way, when we use this data later, the modeling function will know how we want this factor coded. This works for most modeling functions in R including lm(), glm() and mlogit(). Unfortunately, contr-dot-sum() doesn't label the new variables very nicely, so with this second line of code, we're relabeling to make it a bit easier for us to read the results later. Once we run this code, we can see the new coding scheme by typing contrasts(sportscar$seat).

4. Making all the coefficients heterogeneous

If we want all the parameters in our model to be normally distributed across people, we need to change rpar to a vector with 5 "n"'s - one for each coefficient in the model. Each element in my_rpar should have a name corresponding to one of the model coefficients. This is a bit tricky when you have factor predictors because new coefficient names get generated for the coded variables when mlogit() does the factor coding. But we can fit a non-hierarchical model and then use that to get the names for the my_rpar vector. First we fit m3, which is non-hierarchical. Then we take the names of the coefficients from m3 and assign it to the names of my_rpar. When we print my_rpar, we can see that it has the names of the coefficients. We can then pass my_rpar into mlogit() to fit the hierarchical model m8.

5. Hierarchical model parameters

If we plot the estimated parameters for seat2 and seat4, we get a density plot for each parameter. We can see the seat4 parameters range from about -0-point-8 to +0-point-4 across people. This model suggests that there are some people who like four seats and others who dislike them.

6. Coefficient for the base level

There is one other cool feature of effects coding. When you have dummy codes the value of the left-out level is zero. When we dummy coded the seat variable, the seat4 and the seat5 variables took the value of 0 for 2-seat sports cars. With effects codes, the left-out level is five seats, and 5-seat cars are assigned -1 for both the seat2 and seat4 variables. So the value of five-seat sports cars isn't zero - under effects coding it is the negative sum of the coefficients for the other levels. So we can compute the population average value of five seats and see that it is around 0-point-36, which means five seats are preferred on average.

7. Let's try it with the `chocolate` data!

Let's try more random coefficients 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.