This vignette demonstrates how to use the prefeR
package on a real dataset. The mtcars
dataset provides us such an opportunity.
knitr::kable(head(mtcars, 5), caption = "1974 Motor Trends Car Data")
If we wanted to give a user a list of their top five most preferred cars from the mtcars
dataset, there are three approaches we could take:
Option #1 quickly becomes an enormous burden on the user as the number of alternatives increases. Option #2 is difficult for the user to do and replicate. What exactly does it mean if the weight assigned to horsepower is double the weight assigned to fuel efficiency?
Option #3 is enabled by the preference elicitation package. To begin, we create a preference elicitation object and give it our data:
library(prefeR) p <- prefEl(data = mtcars) p
Now we can add in our Bayesian priors for the weights. Although it is difficult to determine weights exactly, usually one has some ballpark estimate for what they should be, and often one knows with certainty the sign of the weights: all else equal, everyone would prefer a more fuel efficient car. The prefeR
package contains three built-in priors:
Normal(mu, sigma)
provides a one-dimensional Normal prior with mean mu and standard deviation sigma. This prior is useful if you have a good guess for what the weight should be, and an understanding of how much you expect to differ from that guess. Exp(mu)
provides a one dimensional Exponential prior with mean mu (not rate!). This prior is particularly useful if you deterministically know the sign of the weight, and have a guess for the value of the weight. The mean may be negative.Flat()
yields a completely agnostic, flat prior. We can now add in our priors for our mtcars
attributes.
p$priors <- c(Exp(1), # MPG Normal(), # Number of cylinders (Normal() = Normal(0, 1)) Normal(), # displacement Exp(2), # horsepower Normal(), # real axle ratio Normal(), # weight Exp(-3), # quarter mile time Normal(), # Engine type Normal(), # transmission type Normal(), # number of gears Normal() # number of carburetors )
Now, we can add in our user's preferences:
p$addPref("Pontiac Firebird" %>% "Fiat 128") # prefer a cool sports car p$addPref("Mazda RX4 Wag" %<% "Mazda RX4") # prefer not to have the station wagon p$addPref("Merc 280" %=% "Merc 280C") # indifferent about C-option
p
Now, we can infer what our attribute weights should be:
p$infer()
And we can get our top five cars:
p$rank()[1:5]
Finally, we can figure out what query we should answer next:
p$suggest()
Any scripts or data that you put into this service are public.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.