# 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{Yoplait}}$$ Dummy variable for the “Yoplait” brand.
$$x_{j}^{\mathrm{Dannon}}$$ Dummy variable for the “Dannon” brand.

# Preference space model

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

$$$u_{j} = \alpha p_{j} + \beta_1 x_{j}^{\mathrm{Feat}} + \beta_2 x_{j}^{\mathrm{Hiland}} + \beta_3 x_{j}^{\mathrm{Yoplait}} + \beta_4 x_{j}^{\mathrm{Dannon}} + \varepsilon_{j} \label{eq:mxlPrefExample}$$$

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 (parname_mu) and a standard deviation (parname_sigma).

To estimate the model, first load the logitr package:

library(logitr)

Estimate the model using the logitr() function:

mxl_pref <- logitr(
data       = yogurt,
choiceName = 'choice',
obsIDName  = 'obsID',
parNames   = c('price', 'feat', 'brand'),
randPars   = c(feat = 'n', brand = 'n'),
# You should run a multistart for MXL models since they are non-convex,
# but it can take a long time. Here I just use 5 starts for brevity:
options    = list(numMultiStarts = 5)
)
#> Running Multistart 1 of 5...
#> Running Multistart 2 of 5...
#> Running Multistart 3 of 5...
#> Running Multistart 4 of 5...
#> Running Multistart 5 of 5...
#> Done!

Print a summary of the results:

summary(mxl_pref)
#> =================================================
#> SUMMARY OF ALL MULTISTART RUNS:
#>
#>   run    logLik iterations status
#> 1   1 -2641.243         38      3
#> 2   2 -2641.501         34      3
#> 3   3 -2642.047         34      3
#> 4   4 -2641.501         32      3
#> 5   5 -2641.244         35      3
#> ---
#> Use statusCodes() to view the meaning of the status codes
#>
#> Below is the summary of run 1 of 5 multistart runs
#> (the run with the largest log-likelihood value)
#> =================================================
#> MODEL SUMMARY:
#>
#> Model Space:   Preference
#> Model Run:         1 of 5
#> Iterations:            38
#> Elapsed Time:   0h:0m:21s
#> Exit Status:            3
#> Weights Used?:      FALSE
#> robust?             FALSE
#>
#> Model Coefficients:
#>                     Estimate StdError    tStat   pVal signif
#> price              -0.438569 0.039058 -11.2286 0.0000    ***
#> feat_mu             0.382408 0.223551   1.7106 0.0873      .
#> brandhiland_mu     -4.146488 0.215550 -19.2367 0.0000    ***
#> brandweight_mu     -1.307327 0.346047  -3.7779 0.0002    ***
#> brandyoplait_mu     0.843400 0.125875   6.7003 0.0000    ***
#> feat_sigma          2.426518 0.506477   4.7910 0.0000    ***
#> brandhiland_sigma   0.022858 0.877946   0.0260 0.9792
#> brandweight_sigma   2.117790 0.652175   3.2473 0.0012     **
#> brandyoplait_sigma  0.298021 0.596881   0.4993 0.6176
#> ---
#> Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
#>
#> Model Fit Values:
#>
#> Log-Likelihood:         -2641.2434268
#> Null Log-Likelihood:    -3343.7419990
#> AIC:                     5300.4869000
#> BIC:                     5352.5808000
#> Number of Observations:  2412.0000000
#>
#> Summary of 10k Draws for Random Coefficients:
#>                             Min.    1st Qu.     Median       Mean    3rd Qu.
#> feat (normal)         -9.0487721 -1.2559026  0.3805539  0.3790109  2.0164518
#> brandhiland (normal)  -4.2340391 -4.1619137 -4.1464958 -4.1465097 -4.1310828
#> brandweight (normal)  -9.4569678 -2.7368457 -1.3090644 -1.3105949  0.1191186
#> brandyoplait (normal) -0.2933218  0.6419717  0.8428898  0.8426342  1.0438676
#>                            Max.
#> feat (normal)          9.144950
#> brandhiland (normal)  -4.065325
#> brandweight (normal)   6.092052
#> brandyoplait (normal)  1.855570

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, priceName = "price")
wtp_mxl_pref
#>                     Estimate StdError    tStat   pVal signif
#> lambda              0.438569 0.039062  11.2275 0.0000    ***
#> feat_mu             0.871945 0.525409   1.6596 0.0971      .
#> brandhiland_mu     -9.454589 0.674239 -14.0226 0.0000    ***
#> brandweight_mu     -2.980894 0.729784  -4.0846 0.0000    ***
#> brandyoplait_mu     1.923073 0.314092   6.1226 0.0000    ***
#> feat_sigma          5.532809 1.321726   4.1860 0.0000    ***
#> brandhiland_sigma   0.052118 2.029675   0.0257 0.9795
#> brandweight_sigma   4.828866 1.363160   3.5424 0.0004    ***
#> brandyoplait_sigma  0.679530 1.419701   0.4786 0.6322

# WTP space model

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

$$$u_{j} = \lambda ( \omega_1 x_{j}^{\mathrm{Feat}} + \omega_2 x_{j}^{\mathrm{Hiland}} + \omega_3 x_{j}^{\mathrm{Yoplait}} + \omega_4 x_{j}^{\mathrm{Dannon}} - p_{j}) + \varepsilon_{j} \label{eq:mnlWtpExample}$$$

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 (parname_mu) and a standard deviation (parname_sigma).

To estimate the model, first load the logitr package:

library(logitr)

Estimate the model using the logitr() function:

mxl_wtp <- logitr(
data       = yogurt,
choiceName = 'choice',
obsIDName  = 'obsID',
parNames   = c('feat', 'brand'),
priceName  = 'price',
randPars   = c(feat = 'n', brand = 'n'),
modelSpace = 'wtp',
options    = list(
# You should run a multistart for MXL models since they are non-convex,
# but it can take a long time. Here I just use 5 starts for brevity:
numMultiStarts = 5,
# 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 5...
#> NOTE: Using user-provided starting values for this run
#> Running Multistart 2 of 5...
#> Running Multistart 3 of 5...
#> Running Multistart 4 of 5...
#> Running Multistart 5 of 5...
#> Done!

Print a summary of the results:

summary(mxl_wtp)
#> =================================================
#> SUMMARY OF ALL MULTISTART RUNS:
#>
#>   run    logLik iterations status
#> 1   1 -2641.501         80      3
#> 2   2 -2796.938         78      3
#> 3   3 -2795.106         78      3
#> 4   4 -2792.687         68      3
#> 5   5 -2797.022         94      3
#> ---
#> Use statusCodes() to view the meaning of the status codes
#>
#> Below is the summary of run 1 of 5 multistart runs
#> (the run with the largest log-likelihood value)
#> =================================================
#> MODEL SUMMARY:
#>
#> Model Space:   Willingness-to-Pay
#> Model Run:                 1 of 5
#> Iterations:                    80
#> Elapsed Time:           0h:0m:47s
#> Exit Status:                    3
#> Weights Used?:              FALSE
#> robust?                     FALSE
#>
#> Model Coefficients:
#>                     Estimate StdError    tStat   pVal signif
#> lambda              0.431167 0.031208  13.8160 0.0000    ***
#> feat_mu             1.367602 0.528541   2.5875 0.0097     **
#> brandhiland_mu     -9.580413 0.602887 -15.8909 0.0000    ***
#> brandweight_mu     -2.995252 0.694212  -4.3146 0.0000    ***
#> brandyoplait_mu     2.036387 0.230559   8.8324 0.0000    ***
#> feat_sigma          5.800841 1.244972   4.6594 0.0000    ***
#> brandhiland_sigma   0.165878 1.680625   0.0987 0.9214
#> brandweight_sigma   4.758560 1.281229   3.7141 0.0002    ***
#> brandyoplait_sigma  0.072400 1.125689   0.0643 0.9487
#> ---
#> Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
#>
#> Model Fit Values:
#>
#> Log-Likelihood:         -2641.5012543
#> Null Log-Likelihood:    -3343.7419990
#> AIC:                     5301.0025000
#> BIC:                     5353.0964000
#> Number of Observations:  2412.0000000
#>
#> Summary of 10k Draws for Random Coefficients:
#>                             Min.   1st Qu.    Median      Mean    3rd Qu.
#> feat (normal)         -21.178607 -2.548949  1.363170  1.359481  5.2739523
#> brandhiland (normal)  -10.215773 -9.692355 -9.580466 -9.580567 -9.4686136
#> brandweight (normal)  -21.307055 -6.207303 -2.999156 -3.002595  0.2098943
#> brandyoplait (normal)   1.760235  1.987453  2.036263  2.036201  2.0850882
#>                            Max.
#> feat (normal)         22.315363
#> brandhiland (normal)  -8.991406
#> brandweight (normal)  13.630754
#> brandyoplait (normal)  2.282281

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

wtp_mxl_comparison <- wtpCompare(mxl_pref, mxl_wtp, priceName = 'price')
wtp_mxl_comparison
#>                            pref           wtp  difference
#> lambda                 0.438569  4.311665e-01 -0.00740250
#> feat_mu                0.871945  1.367602e+00  0.49565700
#> brandhiland_mu        -9.454589 -9.580413e+00 -0.12582426
#> brandweight_mu        -2.980894 -2.995252e+00 -0.01435846
#> brandyoplait_mu        1.923073  2.036387e+00  0.11331408
#> feat_sigma             5.532809  5.800841e+00  0.26803240
#> brandhiland_sigma      0.052118  1.658783e-01  0.11376028
#> brandweight_sigma      4.828866  4.758560e+00 -0.07030566
#> brandyoplait_sigma     0.679530  7.240032e-02 -0.60712968
#> logLik             -2641.243427 -2.641501e+03 -0.25782753

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.