jhelvy / logitr

Fast estimation of multinomial (MNL) and mixed logit (MXL) models in R with "Preference" space or "Willingness-to-pay" (WTP) space utility parameterizations in R
https://jhelvy.github.io/logitr/
Other
42 stars 15 forks source link

Predicted probability labeling #13

Closed shannonpileggi closed 3 years ago

shannonpileggi commented 3 years ago

Hi John! Thanks for this awesome package! I was reviewing Predicting Choice Probabilities from Estimated Models and I noticed that the predicted probabilities don't carry forward the brand assignment:

probs_mnl_pref
#>   obsID  prob_mean   prob_low  prob_high
#> 1    13 0.43684871 0.41564967 0.45746384
#> 2    13 0.03313009 0.02630457 0.04172679
#> 3    13 0.19155738 0.17621753 0.20760195
#> 4    13 0.33846382 0.31891924 0.35883234
#> 5    42 0.60763975 0.57328271 0.64024329
#> 6    42 0.02602079 0.01832816 0.03662558
#> 7    42 0.17803594 0.16244937 0.19462645
#> 8    42 0.18830353 0.16858802 0.20970439

is it safe to assume that those are in the same order of the input new data here?

alts
#>     obsID price feat   brand
#> 49     13   8.1    0  dannon
#> 50     13   5.0    0  hiland
#> 51     13   8.6    0  weight
#> 52     13  10.8    0 yoplait
#> 165    42   6.3    0  dannon
#> 166    42   6.1    1  hiland
#> 167    42   7.9    0  weight
#> 168    42  11.5    0 yoplait

And can the predictProbs function be modified to include brand as a column in the predicted probabilities data frame? This could help protect users against misspecification.

Thank you again for all of your work on this!

jhelvy commented 3 years ago

Hmm, good points. The row order is preserved, so yes the predicted probabilities align with the original alts data frame. But you're right that a user could very easily mix this up. I think all I need is to add an altIDName argument that identifies the alternative, similar to how the obsIDName identifies the choice observation. I'll make this update.

In the specific yogurt example, there's actually an alt variable that IDs the alternative, though you could just as well use brand.

jhelvy commented 3 years ago

Okay, I bumped the version to 0.2.1. If you install from Github you should now be able to include a altIDName argument to both prediction functions.

shannonpileggi commented 3 years ago

Wow, thanks for addressing this so quickly!