Chocolate model with random price coefficient
Okay, we're ready to fit a hierarchical model to the chocolate data. Let's start with the code we had before to estimate a non-hierarchical choice model and modify it to estimate a model where the Price parameter is normally distributed. The chocolate data is still loaded.
This exercise is part of the course
Choice Modeling for Marketing in R
Exercise instructions
- A hierarchical model needs to know which person made each choice. Add the
Subjectcolumn to the first index indfidx()so that each choice question is linked to the person who answered it. - Add the
rparinput tomlogit(). It should be equal toc(Price = "n")to indicate that you want the coefficient forPriceto be normally distributed. - Add the
panel = TRUEinput tomlogit()to tell it that you want to assume eachSubjecthas his or her ownPricecoefficient. - Plot the hierarchical model by typing
plot(choc_m6).
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# add Subject to the first index of dfidx()
chocolate <- dfidx(chocolate, idx = list(c("chid", ____), "Alt"),
choice = "Selection")
# add rpar and panel inputs to mlogit call
choc_m6 <- mlogit(Selection ~ 0 + Brand + Type + Price, data = chocolate,
____, ____)
# plot the model