Estimating Mixed Logit Models

This vignette demonstrates an example of how to use the logitr() function to estimate mixed logit (MXL) models with preference space and WTP space utility parameterizations.

The data

This example uses the yogurt data set from Jain et al. (1994). The data set contains 2,412 choice observations from a series of yogurt purchases by a panel of 100 households in Springfield, Missouri, over a roughly two-year period. The data were collected by optical scanners and contain information about the price, brand, and a “feature” variable, which identifies whether a newspaper advertisement was shown to the customer. There are four brands of yogurt: Yoplait, Dannon, Weight Watchers, and Hiland, with market shares of 34%, 40%, 23% and 3%, respectively.

In the utility models described below, the data variables are represented as follows:

Symbol Variable
\(p\) The price in US dollars.
\(x_{j}^{\mathrm{Feat}}\) Dummy variable for whether the newspaper advertisement was shown to the customer.
\(x_{j}^{\mathrm{Hiland}}\) Dummy variable for the “Highland” brand.
\(x_{j}^{\mathrm{Weight}}\) Dummy variable for the “Weight Watchers” brand.
\(x_{j}^{\mathrm{Yoplait}}\) Dummy variable for the “Yoplait” brand.

Preference space model

This example will estimate the following mixed logit model in the preference space:

\[\begin{equation} u_{j} = \alpha p_{j} + \beta_1 x_{j}^{\mathrm{Feat}} + \beta_2 x_{j}^{\mathrm{Hiland}} + \beta_3 x_{j}^{\mathrm{Weight}} + \beta_4 x_{j}^{\mathrm{Yoplait}} + \varepsilon_{j} \label{eq:mnlPrefExample} \end{equation}\]

where the parameters \(\alpha\), \(\beta_1\), \(\beta_2\), \(\beta_3\), and \(\beta_4\) have units of utility. In the example below, we will model \(\beta_1\), \(\beta_2\), \(\beta_3\), and \(\beta_4\) as normally distributed across the population. As a result, the model will estimate two parameters for each of these coefficients: a mean (par_mu) and a standard deviation (par_sigma).

To estimate the model, first load the logitr package:

library(logitr)

Estimate the model using the logitr() function:

mxl_pref <- logitr(
  data     = yogurt,
  choice   = 'choice',
  obsID    = 'obsID',
  pars     = c('price', 'feat', 'brand'),
  randPars = c(feat = 'n', brand = 'n'),
  # You should run a multistart for MXL models since they are non-convex
  numMultiStarts = 10
)
#> Running Multistart 1 of 10...
#> Running Multistart 2 of 10...
#> Running Multistart 3 of 10...
#> Running Multistart 4 of 10...
#> Running Multistart 5 of 10...
#> Running Multistart 6 of 10...
#> Running Multistart 7 of 10...
#> Running Multistart 8 of 10...
#> Running Multistart 9 of 10...
#> Running Multistart 10 of 10...
#> Done!

Print a summary of the results:

summary(mxl_pref)
#> =================================================
#> Call:
#> logitr(data = yogurt, choice = "choice", obsID = "obsID", pars = c("price", 
#>     "feat", "brand"), randPars = c(feat = "n", brand = "n"), 
#>     numMultiStarts = 10)
#> 
#> Frequencies of alternatives:
#>        1        2        3        4 
#> 0.402156 0.029436 0.229270 0.339138 
#> 
#> Summary Of Multistart Runs:
#>    Log Likelihood Iterations Exit Status
#> 1       -2641.243         38           3
#> 2       -2642.049         42           3
#> 3       -2641.503         40           3
#> 4       -2642.048         35           3
#> 5       -2641.512         29           3
#> 6       -2641.251         28           3
#> 7       -2641.501         36           3
#> 8       -2641.373         37           3
#> 9       -2641.502         33           3
#> 10      -2641.373         28           3
#> 
#> Use statusCodes() to view the meaning of each status code
#> 
#> Exit Status: 3, Optimization stopped because ftol_rel or ftol_abs was reached.
#>                              
#> Model Type:       Mixed Logit
#> Model Space:       Preference
#> Model Run:            1 of 10
#> Iterations:                38
#> Elapsed Time:        0h:0m:2s
#> Algorithm:     NLOPT_LD_LBFGS
#> Weights Used?:          FALSE
#> Robust?                 FALSE
#> 
#> Model Coefficients: 
#>                     Estimate Std. Error  z-value  Pr(>|z|)    
#> price              -0.438379   0.038877 -11.2762 < 2.2e-16 ***
#> feat_mu             0.382304   0.223530   1.7103 0.0872096 .  
#> brandhiland_mu     -4.146425   0.215717 -19.2216 < 2.2e-16 ***
#> brandweight_mu     -1.306121   0.345491  -3.7805 0.0001565 ***
#> brandyoplait_mu     0.843576   0.125763   6.7076 1.978e-11 ***
#> feat_sigma          2.426846   0.506472   4.7917 1.654e-06 ***
#> brandhiland_sigma   0.022985   0.886992   0.0259 0.9793267    
#> brandweight_sigma   2.115097   0.651051   3.2487 0.0011592 ** 
#> brandyoplait_sigma  0.295287   0.594273   0.4969 0.6192679    
#> ---
#> Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
#>                                      
#> Log-Likelihood:         -2641.2433390
#> Null Log-Likelihood:    -3343.7419990
#> AIC:                     5300.4866780
#> BIC:                     5352.5806000
#> McFadden R2:                0.2100936
#> Adj McFadden R2:            0.2074020
#> Number of Observations:  2412.0000000
#> 
#> Summary of 10k Draws for Random Coefficients: 
#>                             Min.    1st Qu.     Median       Mean    3rd Qu.
#> feat (normal)         -9.0501506 -1.2562283  0.3804493  0.3789060  2.0165682
#> brandhiland (normal)  -4.2344624 -4.1619360 -4.1464324 -4.1464463 -4.1309337
#> brandweight (normal)  -9.4453960 -2.7338212 -1.3078558 -1.3093844  0.1185107
#> brandyoplait (normal) -0.2827217  0.6439945  0.8430701  0.8428169  1.0422049
#>                            Max.
#> feat (normal)          9.146030
#> brandhiland (normal)  -4.064810
#> brandweight (normal)   6.083848
#> brandyoplait (normal)  1.846463

The above summary table prints summaries of the estimated coefficients as well as a summary table of the distribution of 10,000 population draws for each normally-distributed model coefficient. The results show that the feat attribute has a significant standard deviation coefficient, suggesting that there is considerable heterogeneity across the population for this attribute. In contrast, the brand coefficients have small and insignificant standard deviation coefficients.

Compute the WTP implied from the preference space model:

wtp_mxl_pref <- wtp(mxl_pref, price =  "price")
wtp_mxl_pref
#>                     Estimate Std. Error  z-value  Pr(>|z|)    
#> lambda              0.438379   0.038877  11.2761 < 2.2e-16 ***
#> feat_mu             0.872085   0.527562   1.6530 0.0983209 .  
#> brandhiland_mu     -9.458547   0.674975 -14.0132 < 2.2e-16 ***
#> brandweight_mu     -2.979435   0.732157  -4.0694 4.714e-05 ***
#> brandyoplait_mu     1.924308   0.316252   6.0847 1.167e-09 ***
#> feat_sigma          5.535958   1.328044   4.1685 3.066e-05 ***
#> brandhiland_sigma   0.052431   2.052480   0.0255 0.9796201    
#> brandweight_sigma   4.824817   1.370845   3.5196 0.0004322 ***
#> brandyoplait_sigma  0.673590   1.422720   0.4735 0.6358905    
#> ---
#> Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

WTP space model

This example will estimate the following mixed logit model in the WTP space:

\[\begin{equation} u_{j} = \lambda ( \omega_1 x_{j}^{\mathrm{Feat}} + \omega_2 x_{j}^{\mathrm{Hiland}} + \omega_3 x_{j}^{\mathrm{Weight}} + \omega_4 x_{j}^{\mathrm{Yoplait}} - p_{j}) + \varepsilon_{j} \label{eq:mnlWtpExample} \end{equation}\]

where the parameters \(\omega_1\), \(\omega_2\), \(\omega_3\), and \(\omega_4\) have units of dollars and \(\lambda\) is the scale parameter. In the example below, we will model \(\omega_1\), \(\omega_2\), \(\omega_3\), and \(\omega_4\) as normally distributed across the population. As a result, the model will estimate two parameters for each of these coefficients: a mean (par_mu) and a standard deviation (par_sigma).

To estimate the model, first load the logitr package:

library(logitr)

Estimate the model using the logitr() function:

mxl_wtp <- logitr(
  data       = yogurt,
  choice     = 'choice',
  obsID      = 'obsID',
  pars       = c('feat', 'brand'),
  price      = 'price',
  randPars   = c(feat = 'n', brand = 'n'),
  modelSpace = 'wtp',
  # You should run a multistart for MXL models since they are non-convex
  numMultiStarts = 10,
  # Use the computed WTP from the preference space model as the starting
  # values for the first run:
  startVals = wtp_mxl_pref$Estimate
)
#> Running Multistart 1 of 10...
#> NOTE: Using user-provided starting values for this run
#> Running Multistart 2 of 10...
#> Running Multistart 3 of 10...
#> Running Multistart 4 of 10...
#> Running Multistart 5 of 10...
#> Running Multistart 6 of 10...
#> Running Multistart 7 of 10...
#> Running Multistart 8 of 10...
#> Running Multistart 9 of 10...
#> Running Multistart 10 of 10...
#> Done!

Print a summary of the results:

summary(mxl_wtp)
#> =================================================
#> Call:
#> logitr(data = yogurt, choice = "choice", obsID = "obsID", pars = c("feat", 
#>     "brand"), price = "price", randPars = c(feat = "n", brand = "n"), 
#>     modelSpace = "wtp", numMultiStarts = 10, startVals = wtp_mxl_pref$Estimate)
#> 
#> Frequencies of alternatives:
#>        1        2        3        4 
#> 0.402156 0.029436 0.229270 0.339138 
#> 
#> Summary Of Multistart Runs:
#>    Log Likelihood Iterations Exit Status
#> 1       -2670.953         95           3
#> 2       -2641.501        108           3
#> 3       -2641.501        131           3
#> 4       -2641.243         84           3
#> 5       -2656.821         97          -1
#> 6       -2641.243        103           3
#> 7       -2654.573         77           3
#> 8       -2794.884         79           3
#> 9       -2641.501        117           3
#> 10      -2654.593         93           3
#> 
#> Use statusCodes() to view the meaning of each status code
#> 
#> Exit Status: 3, Optimization stopped because ftol_rel or ftol_abs was reached.
#>                                  
#> Model Type:           Mixed Logit
#> Model Space:   Willingness-to-Pay
#> Model Run:                4 of 10
#> Iterations:                    84
#> Elapsed Time:            0h:0m:7s
#> Algorithm:         NLOPT_LD_LBFGS
#> Weights Used?:              FALSE
#> Robust?                     FALSE
#> 
#> Model Coefficients: 
#>                     Estimate Std. Error  z-value  Pr(>|z|)    
#> lambda              0.437860   0.038044  11.5094 < 2.2e-16 ***
#> feat_mu             0.875735   0.519639   1.6853 0.0919353 .  
#> brandhiland_mu     -9.461679   0.642863 -14.7180 < 2.2e-16 ***
#> brandweight_mu     -2.992112   0.726701  -4.1174 3.832e-05 ***
#> brandyoplait_mu     1.932994   0.306809   6.3003 2.971e-10 ***
#> feat_sigma          5.546823   1.205093   4.6028 4.168e-06 ***
#> brandhiland_sigma   0.027317   1.991724   0.0137 0.9890571    
#> brandweight_sigma   4.842786   1.352934   3.5795 0.0003443 ***
#> brandyoplait_sigma  0.634387   1.318731   0.4811 0.6304747    
#> ---
#> Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
#>                                      
#> Log-Likelihood:         -2641.2426652
#> Null Log-Likelihood:    -3343.7419990
#> AIC:                     5300.4853303
#> BIC:                     5352.5792000
#> McFadden R2:                0.2100938
#> Adj McFadden R2:            0.2074022
#> Number of Observations:  2412.0000000
#> 
#> Summary of 10k Draws for Random Coefficients: 
#>                              Min.   1st Qu.     Median       Mean    3rd Qu.
#> feat (normal)         -20.6831755 -2.869309  0.8714971  0.8679698  4.6110267
#> brandhiland (normal)   -9.5663109 -9.480114 -9.4616877 -9.4617042 -9.4432675
#> brandweight (normal)  -21.6280284 -6.261015 -2.9960845 -2.9995844  0.2697649
#> brandyoplait (normal)  -0.4867112  1.504219  1.9319076  1.9313636  2.3597231
#>                            Max.
#> feat (normal)         20.906195
#> brandhiland (normal)  -9.364680
#> brandweight (normal)  13.928170
#> brandyoplait (normal)  4.087568

If you want to compare the WTP from the two different model spaces, use the wtpCompare() function:

wtpCompare(mxl_pref, mxl_wtp, price = 'price')
#>                              pref            wtp  difference
#> lambda                 0.43837864     0.43785975 -0.00051889
#> feat_mu                0.87208545     0.87573543  0.00364998
#> brandhiland_mu        -9.45854712    -9.46167890 -0.00313178
#> brandweight_mu        -2.97943538    -2.99211210 -0.01267671
#> brandyoplait_mu        1.92430825     1.93299371  0.00868547
#> feat_sigma             5.53595796     5.54682266  0.01086470
#> brandhiland_sigma      0.05243094     0.02731711 -0.02511383
#> brandweight_sigma      4.82481684     4.84278559  0.01796875
#> brandyoplait_sigma     0.67359004     0.63438718 -0.03920286
#> logLik             -2641.24333902 -2641.24266515  0.00067387

Note that the WTP will not necessarily be the same between preference space and WTP space MXL models. This is because the distributional assumptions in MXL models imply different distributions on WTP depending on the model space. See Train and Weeks (2005) and Sonnier, Ainslie, and Otter (2007) for details on this topic.

References

Jain, Dipak C, Naufel J Vilcassim, and Pradeep K Chintagunta. 1994. “A Random-Coefficients Logit Brand-Choice Model Applied to Panel Data.” Journal of Business & Economic Statistics 12 (3): 317–28.
Sonnier, Garrett, Andrew Ainslie, and Thomas Otter. 2007. Heterogeneity distributions of willingness-to-pay in choice models.” Quant. Mark. Econ. 5 (3): 313–31. https://doi.org/10.1007/s11129-007-9024-6.
Train, Kenneth E., and Melvyn Weeks. 2005. Discrete Choice Models in Preference and Willingness-to-Pay Space.” In Appl. Simul. Methods Environ. Resour. Econ., 1–16.