A choice simulator is an online app or an Excel workbook that allows users to specify different scenarios and get predictions. Choice simulators have many names: decision support systems, market simulators, preference simulators, desktop simulators, conjoint simulators, and choice model simulators.
This article outlines how to build one by hand. This is helpful for when you have respondent-level parameters from a model that wasn't created in Displayr.
Requirements
- A Displayr document.
- A data set that includes respondent-level parameters. In this example, we are using parameters from a latent class model, but there are many other types of data that could have been used (e.g., parameters from a GLM, draws from the posterior distribution, or beta draws from a maximum simulated likelihood model).
- This example uses estimated parameters of respondents from a discrete choice experiment of the market for eggs.
- The simulator we wish to create has 4 alternatives (columns), one fixed for each brand.
- You can work your way through this example here. This link will first take you to a login page in Displayr and then to a document that contains the data in the variable set called Individual-Level Parameter Means for Segments 26-Jun-17 9:01:57 AM.
Method
Prepare your data
Your respondent-level parameters will include a variable for each level per attribute. The easiest workflow to follow here is to combine each attribute's variables as a separate variable set with the label of the levels matching what you want to use in your simulator controls.
1. If your parameter variables are all combined as a single variable set in the Data Sources tree, select all of them and right-click > Split.
2. Now select the level variables for a particular attribute and right-click > Combine. For example, Weight is made up of 4 level variables: 55g, 60g, 65g, 70g:
3. Repeat step 2 for every other attribute set.
Create the controls
1. Click Anything in the toolbar > Page Design > Control > Combo Box (Drop-Down).
2. Under Control > Item list, replace the default items with the levels for the first attribute, separated by semi-colons. These must match exactly the labels you have entered in the related variable labels. For example, for Weight, this would be 55g; 60g; 65g; 70g. Where you have a numeric attribute, such as Price in this example, you enter the range of values that you wish the user to be able to choose from (e.g., 1.50; 2.00; 2.50; 3.00; 3.50; 4.00; 4.50; 5.00).
3. Under General > General > Name, set the name of the control to what you set the Label as for the corresponding variable set label with a full stop and number 1 affixed at the end. For example, Weight.1. This convention will save you time in later steps.
4. Repeat these steps for every attribute to be included in your simulator.
5. Select the combo boxes and right-click > Duplicate. This will create a copy of these controls for a second alternative or column.
6. Repeat step 5 for each remaining alternative.
7. Click the Textbox icon in the toolbar to add a label for each attribute on the left of the first column.
Calculate preference shares
1. Click the Calculation icon in the toolbar > Custom Code and draw the outline on your page under the first column of combo boxes.
2. Paste the below into the code editor:
# Computing the utility for each alternative
u1 = Weight[, Weight.1] + Organic[, Organic.1] + Charity[, Charity.1] + Quality[, Quality.1] + Uniformity[, Uniformity.1] + Feed[, Feed.1] + Price*as.numeric(gsub("$", "", Price.1))
u2 = Weight[, Weight.2] + Organic[, Organic.2] + Charity[, Charity.2] + Quality[, Quality.2] + Uniformity[, Uniformity.2] + Feed[, Feed.2] + Price*as.numeric(gsub("$", "", Price.2))
u3 = Weight[, Weight.3] + Organic[, Organic.3] + Charity[, Charity.3] + Quality[, Quality.3] + Uniformity[, Uniformity.3] + Feed[, Feed.1] + Price*as.numeric(gsub("$", "", Price.3))
u4 = Weight[, Weight.4] + Organic[, Organic.4] + Charity[, Charity.4] + Quality[, Quality.4] + Uniformity[, Uniformity.4] + Feed[, Feed.1] + Price*as.numeric(gsub("$", "", Price.4))
# Computing preference shares
utilities = as.matrix(cbind(u1, u2, u3, u4))
eutilities = exp(utilities)
shares = prop.table(eutilities, 1)
# Filtering the shares, if a filter is applied.
shares = shares[QFilter, ]
# Filtering the weight variable, if required.
weight = if (is.null(QPopulationWeight)) rep(1, length(u1)) else QPopulationWeight
weight = weight[QFilter]
# Computing shares for the total sample
shares = sweep(shares, 1, weight, "*")
shares = as.matrix(apply(shares, 2, sum))
shares = 100 * prop.table(shares, 2)[1]
A few key aspects of the code:
- The code is designed for four alternatives but can easily be modified to handle a different number of alternatives.
- We have been careful with the naming of the variable sets and controls to make this easier to read.
- The computing utility section of code at the start references the variable set Label for each attribute and filters it by the item label set in the corresponding combo box control. Note, if your variable set label includes spaces, you will need to wrap the label references here within backticks.
- The Price attribute includes some additional code to multiply it by the price level after removing the $ sign and converting it to numeric.
- The formulas for the utility of each alternative are expressed as simple mathematical expressions.
- The code is already setup to deal with filters and weights.
- The code works with other models.
3. Right-click > Duplicate this calculation and position the copy below the second column of combo boxes.
4. On the object inspector, click Data > R Code > Edit Code.
5. Modify the very last line of code, replacing [1]
with [2]
, which tells it to show the results of the second alternative.
6. Repeat steps 3 to 5 for alternatives 3 and 4.
7. Apply any filters or weight to your preference share calculations via Data > Filters & Weight in the object inspector. The simulator will automatically re-run with each change. This also applies to control and page filters in the published link. See, for example, How to Setup Filtering in a Dashboard.
8. You can customize the styling of the controls in the object inspector via Appearance > Appearance.
9. You can additionally add background images and logos or customize your page further. See Customizing Online Reports/Dashboards for details.
10. Click Share > Publish to web > Anyone with the link or Login and password required to share your simulator. See also How to Publish a Document as a Web Page (Dashboard).
An interactive version this choice simulator can be viewed here.
Next
How to Create an Online Choice Optimizer By Hand