This vignette demonstrates an example of how to use the logitr()
function with the weightsName
argument to estimate weighted logit models.
This example uses the cars_us data set from Helveston et al. (2015) containing 384 stated choice observations from US car buyers. Conjoint surveys were fielded in 2012 online in the US on Amazon Mechanical Turk and in person at the 2013 Pittsburgh Auto show. Participants were asked to select a vehicle from a set of three alternatives. Each participant answered 15 choice questions.
In the utility models described below, the data variables are represented as follows:
Symbol | Variable |
---|---|
\(p\) | The price in US dollars. |
\(x_{j}^{\mathrm{hev}}\) | Dummy variable for HEV vehicle type |
\(x_{j}^{\mathrm{phev10}}\) | Dummy variable for PHEV10 vehicle type |
\(x_{j}^{\mathrm{phev20}}\) | Dummy variable for PHEV20 vehicle type |
\(x_{j}^{\mathrm{phev40}}\) | Dummy variable for PHEV40 vehicle type |
\(x_{j}^{\mathrm{bev75}}\) | Dummy variable for BEV75 vehicle type |
\(x_{j}^{\mathrm{bev100}}\) | Dummy variable for BEV100 vehicle type |
\(x_{j}^{\mathrm{bev150}}\) | Dummy variable for BEV150 vehicle type |
\(x_{j}^{\mathrm{phevFastcharge}}\) | Dummy variable for if the PHEV has a fast charging capability |
\(x_{j}^{\mathrm{bevFastcharge}}\) | Dummy variable for if the BEV has a fast charging capability |
\(x_{j}^{\mathrm{opCost}}\) | The vehicle operating costs (cents / mile) |
\(x_{j}^{\mathrm{accelTime}}\) | The vehicle 0-60mph acceleration time |
\(x_{j}^{\mathrm{american}}\) | Dummy variable for an American brand |
\(x_{j}^{\mathrm{japanese}}\) | Dummy variable for a Japanese brand |
\(x_{j}^{\mathrm{chinese}}\) | Dummy variable for a Chinese brand |
\(x_{j}^{\mathrm{skorean}}\) | Dummy variable for a S. Korean brand |
In this example, we’ll estimate two versions of the following utility model in the WTP space: one without weights and one with weights. Notation is taken from Helveston et al. (2015):
\[\begin{equation} \begin{split} &u_{j} = \lambda (\\ &\omega_1 x_{j}^{\mathrm{hev}} + \omega_2 x_{j}^{\mathrm{phev10}} + \omega_3 x_{j}^{\mathrm{phev20}} + \omega_4 x_{j}^{\mathrm{phev40}} +\\ &\omega_5 x_{j}^{\mathrm{bev75}} + \omega_6 x_{j}^{\mathrm{bev100}} + \omega_7 x_{j}^{\mathrm{bev150}} +\\ &\omega_8 x_{j}^{\mathrm{phevFastcharge}} + \omega_9 x_{j}^{\mathrm{bevFastcharge}} + \omega_{10} x_{j}^{\mathrm{opCost}} + \omega_{11} x_{j}^{\mathrm{accelTime}} +\\ &\omega_{12} x_{j}^{\mathrm{american}} + \omega_{13} x_{j}^{\mathrm{japanese}} + \omega_{14} x_{j}^{\mathrm{chinese}} + \omega_{15} x_{j}^{\mathrm{skorean}} - p_{j}\\ &) +\varepsilon_{j} \end{split} \label{eq:mnlWtpCarsExample} \end{equation}\]
where all the \(\omega\) parameters have units of dollars and \(\lambda\) is the scale parameter.
To estimate the model, first load the logitr package:
library(logitr)
Estimate the unweighted model using the logitr()
function. In this example, I have set robust = TRUE
since it will also be TRUE
in the weighted model:
<- logitr(
mnl_wtp_unweighted data = cars_us,
choiceName = 'choice',
obsIDName = 'obsnum',
parNames = c(
'hev', 'phev10', 'phev20', 'phev40', 'bev75', 'bev100', 'bev150',
'american', 'japanese', 'chinese', 'skorean', 'phevFastcharge',
'bevFastcharge','opCost', 'accelTime'),
priceName = 'price',
modelSpace = 'wtp',
robust = TRUE,
options = list(
# Since WTP space models are non-convex, run a multistart:
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!
summary(mnl_wtp_unweighted)
#> =================================================
#> SUMMARY OF ALL MULTISTART RUNS:
#>
#> run logLik iterations status
#> 1 1 -4616.952 25 3
#> 2 2 -4616.952 43 3
#> 3 3 -5585.358 51 -1
#> 4 4 -4616.952 35 3
#> 5 5 -5573.557 60 -1
#> 6 6 -4616.952 40 3
#> 7 7 -4616.952 41 3
#> 8 8 -4616.952 39 3
#> 9 9 -4616.952 36 3
#> 10 10 -4616.952 42 3
#> ---
#> Use statusCodes() to view the meaning of the status codes
#>
#> Below is the summary of run 6 of 10 multistart runs
#> (the run with the largest log-likelihood value)
#> =================================================
#> MODEL SUMMARY:
#>
#> Model Space: Willingness-to-Pay
#> Model Run: 6 of 10
#> Iterations: 40
#> Elapsed Time: 0h:0m:0.91s
#> Exit Status: 3
#> Weights Used?: FALSE
#> robust? TRUE
#> Cluster Name: obsnum
#>
#> Model Coefficients:
#> Estimate StdError tStat pVal signif
#> lambda 0.073879 0.002193 33.6902 0.0000 ***
#> hev 0.807008 0.999059 0.8078 0.4193
#> phev10 1.165975 1.061498 1.0984 0.2721
#> phev20 1.647843 1.061744 1.5520 0.1207
#> phev40 2.579724 1.049927 2.4571 0.0140 *
#> bev75 -16.046077 1.254134 -12.7945 0.0000 ***
#> bev100 -13.002908 1.238847 -10.4960 0.0000 ***
#> bev150 -9.573261 1.164174 -8.2232 0.0000 ***
#> american 2.343446 0.797966 2.9368 0.0033 **
#> japanese -0.374954 0.799829 -0.4688 0.6392
#> chinese -10.269280 0.885952 -11.5912 0.0000 ***
#> skorean -6.031179 0.851428 -7.0836 0.0000 ***
#> phevFastcharge 2.879621 0.802881 3.5866 0.0003 ***
#> bevFastcharge 2.918935 0.918132 3.1792 0.0015 **
#> opCost -1.636029 0.068630 -23.8383 0.0000 ***
#> accelTime -1.697041 0.163809 -10.3599 0.0000 ***
#> ---
#> Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
#>
#> Model Fit Values:
#>
#> Log-Likelihood: -4616.9517802
#> Null Log-Likelihood: -6328.0067827
#> AIC: 9265.9036000
#> BIC: 9372.4426000
#> McFadden R2: 0.2703940
#> Adj McFadden R2: 0.2678655
#> Number of Observations: 5760.0000000
#> Number of Clusters 5760.0000000
To estimate the weighted model, simply add the weightsName
argument to the call to logitr()
, referring to the column of weights that will be used to weight each choice observation. In this example, the weights used in the weights
column range from 0.2 to 5:
summary(cars_us$weights)
#> Min. 1st Qu. Median Mean 3rd Qu. Max.
#> 0.2000 0.2000 0.2000 0.6891 0.2000 5.0000
<- logitr(
mnl_wtp_weighted data = cars_us,
choiceName = 'choice',
obsIDName = 'obsnum',
parNames = c(
'hev', 'phev10', 'phev20', 'phev40', 'bev75', 'bev100', 'bev150',
'american', 'japanese', 'chinese', 'skorean', 'phevFastcharge',
'bevFastcharge','opCost', 'accelTime'),
priceName = 'price',
modelSpace = 'wtp',
weightsName = 'weights', # This is the key argument for enabling weights
robust = TRUE,
options = list(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(mnl_wtp_weighted)
#> =================================================
#> SUMMARY OF ALL MULTISTART RUNS:
#>
#> run logLik iterations status
#> 1 1 -3425.630 22 3
#> 2 2 -3425.630 34 3
#> 3 3 -3425.630 28 3
#> 4 4 -3425.630 40 3
#> 5 5 -3425.630 34 3
#> 6 6 -3425.630 40 3
#> 7 7 -3425.630 30 3
#> 8 8 -3846.744 83 3
#> 9 9 -3425.630 41 3
#> 10 10 -3425.630 39 3
#> ---
#> Use statusCodes() to view the meaning of the status codes
#>
#> Below is the summary of run 6 of 10 multistart runs
#> (the run with the largest log-likelihood value)
#> =================================================
#> MODEL SUMMARY:
#>
#> Model Space: Willingness-to-Pay
#> Model Run: 6 of 10
#> Iterations: 40
#> Elapsed Time: 0h:0m:1s
#> Exit Status: 3
#> Weights Used?: TRUE
#> robust? TRUE
#> Cluster Name: obsnum
#>
#> Model Coefficients:
#> Estimate StdError tStat pVal signif
#> lambda 0.052284 0.004070 12.8470 0.0000 ***
#> hev -1.177279 2.913222 -0.4041 0.6861
#> phev10 0.026231 3.127895 0.0084 0.9933
#> phev20 1.693719 3.099590 0.5464 0.5848
#> phev40 2.648556 2.985045 0.8873 0.3750
#> bev75 -20.137161 3.667176 -5.4912 0.0000 ***
#> bev100 -19.495946 3.625485 -5.3775 0.0000 ***
#> bev150 -13.691707 3.492668 -3.9201 0.0001 ***
#> american 8.186357 2.405181 3.4036 0.0007 ***
#> japanese 0.934188 2.360255 0.3958 0.6923
#> chinese -19.009624 2.854346 -6.6599 0.0000 ***
#> skorean -9.509885 2.523337 -3.7688 0.0002 ***
#> phevFastcharge 3.945407 2.362398 1.6701 0.0950 .
#> bevFastcharge 3.343452 2.808596 1.1904 0.2339
#> opCost -1.597525 0.194853 -8.1986 0.0000 ***
#> accelTime -1.171596 0.483451 -2.4234 0.0154 *
#> ---
#> Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
#>
#> Model Fit Values:
#>
#> Log-Likelihood: -3425.6302906
#> Null Log-Likelihood: -4360.5909275
#> AIC: 6883.2606000
#> BIC: 6989.7997000
#> McFadden R2: 0.2144115
#> Adj McFadden R2: 0.2107422
#> Number of Observations: 5760.0000000
#> Number of Clusters 5760.0000000
Here is a comparison of the coefficients between the weighted and unweighted models. All of the significant coefficients have the same sign, but the magnitudes shift some based on the differential weighting of each individual choice in the weighted model:
<- data.frame(
coef_compare Unweighted = coef(mnl_wtp_unweighted),
Weighted = coef(mnl_wtp_weighted))
coef_compare#> Unweighted Weighted
#> lambda 0.07387874 0.05228363
#> hev 0.80700760 -1.17727879
#> phev10 1.16597453 0.02623100
#> phev20 1.64784312 1.69371875
#> phev40 2.57972420 2.64855605
#> bev75 -16.04607750 -20.13716130
#> bev100 -13.00290754 -19.49594564
#> bev150 -9.57326081 -13.69170698
#> american 2.34344600 8.18635666
#> japanese -0.37495425 0.93418825
#> chinese -10.26927958 -19.00962360
#> skorean -6.03117901 -9.50988548
#> phevFastcharge 2.87962065 3.94540741
#> bevFastcharge 2.91893495 3.34345154
#> opCost -1.63602882 -1.59752507
#> accelTime -1.69704075 -1.17159572
Here is a comparison of the log-likelihood for the weighted and unweighted models:
<- c(
logLik_compare "Unweighted" = mnl_wtp_unweighted$logLik,
"Weighted" = mnl_wtp_weighted$logLik)
logLik_compare#> Unweighted Weighted
#> -4616.952 -3425.630