# 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).

Estimate the model using the logitr() function:

library("logitr")

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, outcome = "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       -2641.501         35           3
#> 3       -2641.502         40           3
#> 4       -2641.372         28           3
#> 5       -2641.502         30           3
#> 6       -2641.247         33           3
#> 7       -2641.371         33           3
#> 8       -2642.047         36           3
#> 9       -2642.047         37           3
#> 10      -2641.509         42           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:3s
#> 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
#> 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.038853  11.2829 < 2.2e-16 ***
#> feat_mu             0.872085   0.525874   1.6584 0.0972459 .
#> brandhiland_mu     -9.458547   0.676359 -13.9845 < 2.2e-16 ***
#> brandweight_mu     -2.979435   0.728443  -4.0901 4.311e-05 ***
#> brandyoplait_mu     1.924308   0.315272   6.1036 1.037e-09 ***
#> feat_sigma          5.535958   1.321408   4.1894 2.796e-05 ***
#> brandhiland_sigma   0.052431   2.047527   0.0256 0.9795708
#> brandweight_sigma   4.824817   1.360543   3.5462 0.0003908 ***
#> brandyoplait_sigma  0.673590   1.420062   0.4743 0.6352585
#> ---
#> 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).

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, outcome = "choice", obsID = "obsID", pars = c("feat", #> "brand"), price = "price", randPars = c(feat = "n", brand = "n"), #> modelSpace = "wtp", startVals = wtp_mxl_pref$Estimate, 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       -2670.953         95           3
#> 2       -2655.712         60           3
#> 3       -2650.150         65           3
#> 4       -2652.244         66           3
#> 5       -2641.270        130           3
#> 6       -2654.827         73           3
#> 7       -2641.565        140           3
#> 8       -2641.243         96           3
#> 9       -2641.501         76           3
#> 10      -2794.810        100           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:                8 of 10
#> Iterations:                    96
#> Elapsed Time:           0h:0m:11s
#> Algorithm:         NLOPT_LD_LBFGS
#> Weights Used?:              FALSE
#> Robust?                     FALSE
#>
#> Model Coefficients:
#>                     Estimate Std. Error  z-value  Pr(>|z|)
#> lambda              0.437942   0.038126  11.4867 < 2.2e-16 ***
#> feat_mu             0.875891   0.519568   1.6858 0.0918333 .
#> brandhiland_mu     -9.461111   0.643429 -14.7042 < 2.2e-16 ***
#> brandweight_mu     -2.992878   0.727389  -4.1146 3.879e-05 ***
#> brandyoplait_mu     1.932394   0.307156   6.2912 3.149e-10 ***
#> feat_sigma          5.545651   1.205094   4.6018 4.188e-06 ***
#> brandhiland_sigma   0.024748   2.003269   0.0124 0.9901433
#> brandweight_sigma   4.843353   1.354013   3.5770 0.0003475 ***
#> brandyoplait_sigma  0.637025   1.319627   0.4827 0.6292870
#> ---
#> Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
#>
#> Log-Likelihood:         -2641.2426752
#> Null Log-Likelihood:    -3343.7419990
#> AIC:                     5300.4853505
#> BIC:                     5352.5793000
#> Number of Observations:  2412.0000000
#>
#> Summary of 10k Draws for Random Coefficients:
#>                             Min.   1st Qu.     Median       Mean    3rd Qu.
#> feat (normal)         -20.678465 -2.868363  0.8716532  0.8681266  4.6103927
#> brandhiland (normal)   -9.555902 -9.477812 -9.4611185 -9.4611335 -9.4444307
#> brandweight (normal)  -21.630978 -6.262164 -2.9968509 -3.0003512  0.2693811
#> brandyoplait (normal)  -0.497371  1.501837  1.9313030  1.9307567  2.3608971
#>                            Max.
#> feat (normal)         20.902118
#> brandhiland (normal)  -9.373234
#> brandweight (normal)  13.929386
#> brandyoplait (normal)  4.095926

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.43794218 -0.00043646
#> feat_mu                0.87208545     0.87589063  0.00380518
#> brandhiland_mu        -9.45854712    -9.46111055 -0.00256344
#> brandweight_mu        -2.97943538    -2.99287802 -0.01344264
#> brandyoplait_mu        1.92430825     1.93239361  0.00808536
#> feat_sigma             5.53595796     5.54565076  0.00969279
#> brandhiland_sigma      0.05243094     0.02474799 -0.02768294
#> brandweight_sigma      4.82481684     4.84335292  0.01853608
#> brandyoplait_sigma     0.67359004     0.63702459 -0.03656545
#> logLik             -2641.24333902 -2641.24267524  0.00066378

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.