<p><div><div><div><div data-show-faces="false" data-height="21" data-width="100" data-layout="button_count" data-send="true" data-href="https://www.r-bloggers.com/how-to-create-an-online-choice-simulator/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://www.displayr.com/choice-simulator/"> R – Displayr</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p><img sizes="(max-width: 300px) 100vw, 300px" src="https://i2.wp.com/www.displayr.com/wp-content/uploads/2017/07/ChoiceSimulator-300x173.png?resize=300%2C173&ssl=1" alt="Choice Simulator" class="webfeedsFeaturedVisual wp-post-image" data-recalc-dims="1">
<p><img sizes="(max-width: 780px) 100vw, 780px" src="https://i0.wp.com/www.displayr.com/wp-content/uploads/2017/07/ChoiceSimulator.png?w=450&ssl=1" alt="Choice model simulator" class="wp-image-2595" data-recalc-dims="1"></p>
<h1><span>What is a choice simulator?</span></h1>
<p>A <em>choice simulator </em>is an online app or an Excel workbook that allows users to specify different scenarios and get predictions. Here is an <a rel="nofollow" href="https://app.displayr.com/Dashboard?id=21043f64-45d0-47af-9797-cd4180805849" target="_blank">example of a choice simulator</a>.</p>
<p>Choice simulators have many names: decision support systems, market simulators, preference simulators, desktop simulators, conjoint simulators, and choice model simulators.</p>
<hr>
<h1><span>How to create a choice simulator</span></h1>
<p>In this post, I show how to create an online choice simulator, with the calculations done using R, and the simulator is hosted in Displayr.</p>
<hr>
<h2><span>Step 1: Import the model(s) results</span></h2>
<p>First of all, choice simulators are based on models. So, the first step in building a choice simulator is to obtain the model results that are to be used in the simulator. For example, here I use respondent-level 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, beta draws from a maximum simulated likelihood model).</p>
<p>If practical, it is usually a good idea to have model results at the case level (e.g., respondent level), as the resulting simulator can then be easily automatically weighted and/or filtered. If you have case level data, the model results should be imported into Displayr as a <a rel="nofollow" href="http://docs.displayr.com/wiki/Data_Set" target="_blank">Data Set</a>. See <a rel="nofollow" href="https://www.displayr.com/introduction-to-displayr-2-data/" target="_blank">Introduction to Displayr 2: Getting your data into Displayr</a> for an overview of ways of getting data into Displayr.</p>
<p>The table below shows estimated parameters of respondents from a discrete choice experiment of the market for eggs. You can work your way through the <a rel="nofollow" href="https://app.displayr.com/Try/Choice%20Utilities" target="_blank">choice simulator example used in this post here</a> (the link will first take you to a login page in Displayr and then to a document that contains the data in the <em>variable set </em>called <strong>Individual-Level Parameter Means for Segments 26-Jun-17 9:01:57 AM</strong>).</p>
<p><iframe width="450" src="https://embedded.azureedge.net/75668/225693/b183a60c-e383-43b3-b9c1-5e5fdea6dac1.html?v=28297-90b5b92269-989655037"></iframe>
</p>
<hr>
<h1><span>Step 2: Simplify calculations using variable sets</span></h1>
<p>Variable sets are a novel and very useful aspect of Displayr. Variable sets are related variables that are grouped. We can simplify the calculations of a choice simulator by using the variable sets, with one variable set for each attribute.</p>
<p><img sizes="(max-width: 224px) 100vw, 224px" src="https://i0.wp.com/www.displayr.com/wp-content/uploads/2017/06/TidiedAttributes.png?resize=224%2C417&ssl=1" alt="Variable sets in data tree" class="wp-image-2235" data-recalc-dims="1"></p>
<p>In this step, we group the variables for each attribute into separate variable sets, so that they appear as shown on the right. This is done as follows:</p>
<ol>
<li>If the variables are already grouped into a variable set, select the variable set, and select <strong>Data Manipulation ></strong> Split (Variables). In the dataset that I am using, all the variables I need for my calculation are already grouped into a single variable set called <strong>Individual-Level Parameter Means for Segments 26-Jun-17 9:01:57 AM</strong>, so I click on this and split it.</li>
<li>Next, select the first attribute’s variables. In my example, this is the four variables that start with <strong>Weight:</strong>, each of which represents the respondent-level parameters for different egg weights. (The first of these contains only 0s, as <em>dummy coding</em> was used.)<img sizes="(max-width: 272px) 100vw, 272px" src="https://i0.wp.com/www.displayr.com/wp-content/uploads/2017/06/WeightsSet.png?resize=272%2C237&ssl=1" alt="Variables in data tree for choice simulator" title="Variables in data tree for choice simulator" class="wp-image-2234" data-recalc-dims="1"></li>
<li>Then, go to <strong>Data Manipulation ></strong> Combine (Variables).</li>
<li>Next set the <strong>Label</strong> for the new variable set to something appropriate. For reasons that will become clearer below, it is preferable to set it to a single, short word. For example, <span>Weight</span>.</li>
<li>Set the <strong>Label </strong>field for each of the variables to whatever label you plan to show in the choice simulator. For example, if you want people to be able to choose an egg weight of 55g (about 2 ounces), set the <strong>Label </strong>to <span>55g.</span></li>
<li>Finally, repeat this process for all the attributes. If you have any numeric attributes, then leave these as a single variable, like <strong>Price</strong> in the example here.<br>
<hr>
</li>
</ol>
<h1><span>Step 3: Create the controls</span></h1>
<p><img sizes="(max-width: 553px) 100vw, 553px" src="https://i1.wp.com/www.displayr.com/wp-content/uploads/2017/06/ChoiceSimulatorBasic-1024x265.png?w=450&ssl=1" alt="Choice model simulation inputs" class="wp-image-2237" data-recalc-dims="1"></p>
<p>In my <a rel="nofollow" href="https://app.displayr.com/Dashboard?id=21043f64-45d0-47af-9797-cd4180805849" target="_blank">choice simulator</a>, I have separate columns of controls (i.e., combo boxes) for each of the brands. The fast way to do this is to first create them for the first alternative (column), and then copy and paste them:</p>
<ol>
<li><strong>Insert > Control (More)</strong>.</li>
<li>Type the levels, separated by semi-colons, into the <strong>Item list</strong>. These must match, exactly, to the labels that you have entered for the <strong>Labels </strong>for the first attribute in point 5 in the previous step. For example: <span>55g;</span> 60g; 65g; 70g. I recommend using copy and paste because if you make some typos they will be difficult to track down. Where you have a numeric attribute, such as Price in the example, you enter the range of values that you wish the user to be able to choose from (e.g., <strong><span>1.50</span></strong>; 2.00; 2.50; 3.00; 3.50; 4.00; 4.50; 5.00).</li>
<li>Select the <strong>Properties </strong>tab in the <a rel="nofollow" href="http://docs.displayr.com/wiki/Object_Inspector" target="_blank">Object Inspector</a> and set the <strong>Name </strong>of the control to whatever you set as the <strong>Label </strong>for the corresponding variable set with the number 1 affixed at the end. For example, <span>Weight.1 </span>(You can use any label, but following this convention will save you time later on.)</li>
<li>Click on the control and select the first level. For example, <strong>55g</strong>.</li>
<li>Repeat these steps until you have created controls for each of the attributes, each under each other, as shown above.</li>
<li>Select all the controls that you have created, and then select <strong>Home ></strong> <strong>Copy</strong> and <strong>Home > Paste</strong>, and move the new set of labels to the right of the previous labels. Repeat this for as many sets of alternatives as you wish to include. In my example, there are four alternatives.</li>
<li>Finally, add labels for the brands and attributes: <strong>Insert ></strong> TextBox (Text and Images).</li>
</ol>
<p>See also <a rel="nofollow" href="https://www.displayr.com/combo-box/" target="_blank">Adding a Combo Box to a Displayr Dashboard</a> for an intro to creating combo boxes.</p>
<hr>
<h1><span>Step 4: Calculate preference shares</span></h1>
<ol>
<li>Insert an <strong>R Output </strong>(<strong>Insert > R Output (Analysis)</strong>), setting it to <strong>Automatic </strong>with the appropriate code, and positioning it underneath the first column of combo boxes. Press the <strong>Calculate </strong>button, and it should calculate the share for the first alternative. If you paste the code below, and everything is setup properly, you will get a value of 25%.</li>
<li>Now, click on the R Output you just created, and copy-and-paste it. Position the new version immediately below the second column of combo boxes.</li>
<li>Modify the very last line of code, replacing <span>[1]</span> with <span>[2]</span>, which tells it to show the results of the second alternative.</li>
<li>Repeat steps 2 and 3 for alternatives 3 and 4.</li>
</ol>
<p>The code below can easily be modified for other models. A few key aspects of the code:</p>
<ul>
<li>It works with four alternatives and is readily modified to deal with different numbers of alternatives.</li>
<li>The formulas for the <em>utility </em>of each alternative are expressed as simple mathematical expressions. Because I was careful with the naming of the variable sets and the controls, they are easy to read. If you are using Displayr, you can hover over the various elements of the formula and you will get a preview of their data.</li>
<li>The code is already setup to deal with weights. Just click on the R Output that contains the formula and apply a weight (<strong>Home > Weight</strong>).</li>
<li>It is set up to automatically deal with any filters. More about this below.</li>
</ul>
<h5><span>R Code to paste:</span></h5>
<pre>
# 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(&quot;\\$&quot;, &quot;&quot;, 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(&quot;\\$&quot;, &quot;&quot;, 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(&quot;\\$&quot;, &quot;&quot;, 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(&quot;\\$&quot;, &quot;&quot;, 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, &quot;*&quot;)
shares = as.matrix(apply(shares, 2, sum))
shares = 100 * prop.table(shares, 2)[1]
</pre>
<hr>
<h1><span>Step 5: Make it pretty</span></h1>
<p>If you wish, you can make your choice simulator prettier. The R Outputs and the controls all have formatting options. In my example, I got our designer, Nat, to create the pretty background screen, which she did in Photoshop, and then added using <strong>Insert ></strong> <strong>Image</strong>.</p>
<hr>
<h1><span>Step 6: Add filters</span></h1>
<p>If you have stored the data as variable sets, you can quickly create filters. Note that the calculations will automatically update when the viewer selects the filters.</p>
<hr>
<h1><span>Step 7: Share</span></h1>
<p>To share the dashboard, go to the Export tab in the ribbon (at the top of the screen), and click on the black triangle under the <strong>Web Page </strong>button. Next, check the option for <strong>Hide page navigation on exported page</strong> and then click <strong>Export…</strong> and follow the prompts.</p>
<p>Note, the URL for the choice simulator I am using in this example is <a rel="nofollow" href="https://app.displayr.com/Dashboard?id=21043f64-45d0-47af-9797-cd4180805849" target="_blank">https://app.displayr.com/Dashboard?id=21043f64-45d0-47af-9797-cd4180805849</a>. This URL is <em>public.</em> You cannot guess or find this link by web-searching for security reasons. If, however, you give the URL to someone, then they can access the document. Alternatively, if you have an annual Displayr account, you can instead go into <strong>Settings </strong>for the document (the cog at the top-right of the screen) and press <strong>Disable Public URL.</strong> This will limit access to only people who are set up as users for your organization. You can set up people as users in the company’s <strong>Settings</strong>,<strong> </strong>accessible by clicking on the cog at the top-right of the screen. If you don’t see these settings, contact support@displayr.com to buy a license.</p>
<hr>
<h1><span>Worked example of a choice simulator</span></h1>
<p>You can see the <a rel="nofollow" href="https://app.displayr.com/Dashboard?id=21043f64-45d0-47af-9797-cd4180805849" target="_blank">choice simulator in View Mode </a><a rel="nofollow" href="https://app.displayr.com/Dashboard?id=21043f64-45d0-47af-9797-cd4180805849" target="_blank">here </a>(as an end-user will see it), or you can <a rel="nofollow" href="https://app.displayr.com/Try/Choice%20Simulator" target="_blank">create your own choice simulator here</a> (first log into Displayr and then edit or modify a copy of the document used to create this post).</p>
	<p>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="https://www.displayr.com/choice-simulator/"> R – Displayr</a></strong>.</div>
<hr>
<a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="https://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a rel="nofollow" href="https://www.r-project.org/" title="The R Project for Statistical Computing">R</a> news and <a rel="nofollow" href="https://www.r-bloggers.com/search/tutorial" title="R tutorials">tutorials</a> on topics such as: <a rel="nofollow" href="https://www.r-bloggers.com/search/data%20science" title="Data science">Data science</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data" title="Big Data">Big Data, <a rel="nofollow" href="https://www.r-users.com/" title="R jobs">R jobs</a>, visualization (<a rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2" title="ggplot and ggplot2 tutorials">ggplot2</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/boxplot" title="Boxplots using lattice and ggplot2 tutorials">Boxplots</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/map" title="Maps and gis">maps</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/animation" title="Animation in R">animation</a>), programming (<a rel="nofollow" href="https://www.r-bloggers.com/search/RStudio" title="RStudio IDE for R">RStudio</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/sweave" title="Sweave and literate programming">Sweave</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX" title="LaTeX in R">LaTeX</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/SQL" title="SQL and databases">SQL</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/eclipse" title="Eclipse IDE for R">Eclipse</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/git" title="git and github, Version Control System">git</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/hadoop" title="Large data in R using Hadoop">hadoop</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping" title="Web Scraping of google, facebook, yahoo, twitter and more using R">Web Scraping</a>) statistics (<a rel="nofollow" href="https://www.r-bloggers.com/search/regression" title="Regressions and ANOVA analysis tutorials">regression</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/PCA" title="principal component analysis tutorial">PCA</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/time+series" title="Time series">time series</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/trading" title="finance trading">trading</a>) and more...
<img width="1" alt="" src="http://feeds.feedburner.com/~r/RBloggers/~4/Gx7zkZQy55M" height="1"></a></div>