<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-use-hierarchical-bayes-for-choice-modeling-in-r/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://www.displayr.com/hb-choice-model-r/?utm_medium=Feed&utm_source=Syndication"> R – Displayr</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>There are multiple add-on packages available in <span>R</span> to fit choice models in a Bayesian framework.  These include <span>bayesm</span>, <span>choiceModelR</span>, and <span>flipChoice</span>. In this article, I fill focus on the use of <span>flipChoice</span>, which relies on the state-of-the-art probabilistic programming language <a rel="nofollow" href="http://mc-stan.org/" target="_blank"><span>stan</span></a> to fit the choice model.</p>
<p><img sizes="(max-width: 1321px) 100vw, 1321px" src="https://i2.wp.com/www.displayr.com/wp-content/uploads/2018/07/stan_both-300x109.png?w=450&ssl=1" alt="" class="wp-image-5972" data-recalc-dims="1"></p>
<h2>Getting Started</h2>
<p>I assume you are already familiar with choice models so I’ll be focusing solely on their estimation using <span>flipChoice</span>. You can install <span>flipChoice</span> from GitHub using the <span>devtools</span> package but you may need to first install <span>devtools</span> using the command <span>install.packages(“devtools”)</span> in <span>R</span>. You can then install <span>flipChoice</span> using <span>devtools::install_github(“Displayr/flipChoice”)</span>. The main function that handles fitting of choice models in the package is <span>FitChoiceModel</span>.</p>
<p><span>FitChoiceModel</span> offers great flexibility in the types of inputs it accepts.  Data can be in any of five different formats, including <span>Sawtooth CHO</span> file format, <span>Q</span> experiment question format, a <span>ChoiceModelDesign</span> output from <span>flipChoice::ChoiceModelDesign</span>, <span>Sawtooth <span>Dual</span></span> file format, and <span>JMP</span> file format.</p>
<h2>Inputs/Function Arguments</h2>
<p>I’ll key you in on some of the arguments/parameters of the <span>FitChoiceModel</span> function you should be aware of to fit your own choice model.</p>
<ul>
<li><span>design</span> – use this argument if you have a <span>ChoiceModelDesign</span> output from <span>flipChoice</span></li>
<li><span>choices</span> – for use with design, a data.frame with the choices made by each respondent (one column per question)</li>
<li><span>questions</span> – for use with design, a data.frame with the IDs of the tasks presented to each respondent (with one column per question and one row for each respondent)</li>
<li><span>data</span> – use if you have a data.frame in <a rel="nofollow" href="https://wiki.q-researchsoftware.com/wiki/Category:Experiments" target="_blank">Q experiment question</a> format (example below)</li>
<li><span>file</span> – A text string giving the path to a <span>CHO</span> file</li>
<li><span>respondent.ids</span> – for use with cho.file, a vector of respondent IDs</li>
<li><span>file</span> – A text string giving the path (possibly a URL) to a <span>Sawtooth</span> or <span>JMP</span> design file</li>
<li><span>levels.file</span> – A text string giving the path to a <span>JMP</span> or <span>CHO</span></li>
<li><span>iterations</span>, <span>hb.chains</span>, <span>hb.max.tree.depth</span>, <span>hb.adapt.delta</span> – parameters controlling the <span>stan</span> Monte Carlo algorithm. The function tries to provide sensible defaults for these, but they should be adjusted if there are any warnings about sampling problems or the diagnostic functions (mentioned below) indicate any convergence issues.</li>
<li><span>classes</span> – For multi-class HB, the number of latent classes. The default is to only have one class.</li>
<li><span>tasks.left.out</span> – If you wish to perform cross validation to check the predictive accuracy of your model, this specifies the number of classes to leave out.</li>
<li><span>cov.formula</span>, <span>cov.data</span> – can be used to specific respondent specific covariates (see <a rel="nofollow" href="https://www.displayr.com/include-fixed-covariates-mean-parameters-hb-based-cbc-analysis/" target="_blank">here</a>)</li>
<li><span>hb.prior.mean</span>, <span>hb.prior.sd</span> – Optional vectors for specifying prior values for the mean parameters</li>
</ul>
<p>Additional parameters are discussed in the documentation; type <span>?flipChoice::FitChoiceModel</span> at the <span>R</span> prompt to view it. The function also accepts arguments which will be passed on to <span>rstan::sampling</span> and <span>rstan::stan</span>.</p>
<h2><img sizes="(max-width: 625px) 100vw, 625px" src="https://i0.wp.com/www.displayr.com/wp-content/uploads/2019/06/doors-1587329_1280.jpg?w=450&ssl=1" alt="Multiple door choice" class="wp-image-5933" data-recalc-dims="1"></h2>
<h2>Example data</h2>
<p>I’ll walk you through the rest of this method using data from a discrete choice experiment. This experiment investigated consumer preferences when purchasing a 12-pack of chicken eggs from a supermarket. Each of the 380 respondents were asked eight questions requiring them to choose between three alternative 12-packs of eggs.</p>
<p>The alternatives varied in their levels of seven attributes:</p>
<ul>
<li>egg weight (55g, 60g, 65g, or 70g)</li>
<li>egg quality (caged, barn, or free range)</li>
<li>price ($2, $3, $4, $5, or $6)</li>
<li>chicken feed used (not stated, grain and fish, or vegetables only)</li>
<li>egg uniformity (all eggs the same or some different)</li>
<li>if the eggs were organic (not stated or antibiotic and hormone free)</li>
<li>whether any proceeds from the sale went to charity (not stated or 10% donated to RSPCA).</li>
</ul>
<p>I’ve provided an example question below:</p>
<div class="wp-caption"><img sizes="(max-width: 662px) 100vw, 662px" src="https://i2.wp.com/www.displayr.com/wp-content/uploads/2018/06/EggsQuestion-300x191.png?w=450&ssl=1" alt="Egg Question" class="wp-image-5338" data-recalc-dims="1">
<p class="wp-caption-text">Example question from egg preference discrete choice experiment.</p>
</div>
<h2>Fitting your Choice Model</h2>
<p>The eggs data is included in the package in multiple formats, so you can experiment and try out the different input formats accepted by <span>FitChoiceModel</span>.</p>
<p>To fit a choice model to the eggs data in Cho file format, we can use the following commands:</p>
<p><span>library(flipChoice)</span><br>
<span>cho.file &lt;-system.file(“testdata”, “Training.cho”, package = “flipChoice”)</span><br>
<span>attribute.lvls.file &lt;-system.file(“testdata”, “Attribute_labels_-_Training.xlsx”, package = “flipChoice”)</span><br>
<span>data(cho.ids, package = “flipChoice”)  # vector of respondent IDs</span><br>
<span>fit &lt;-FitChoiceModel(cho.file = cho.file,</span><br>
<span>attribute.levels.file = attribute.lvls.file,</span><br>
<span>respondent.ids = respondent.ids,</span><br>
<span>hb.iterations = 500, hb.chains = 2)</span></p>
<p>The function will issue a warning if it detects any issues with sampling but we should also check for convergence by examining the effect sample sizes and Rhat values for the mean and standard deviation parameters. These are included in the output of summary statistics using <span>ExtractParameterStats(fit)</span>. We can examine trace plots for the same parameters using <span>TracePlots(fit)</span>. Estimates of the posterior medians and 95% confidence bands for these parameters can be plotted using the function <span>PlotPosteriorIntervals</span>. My colleague, Justin, has discussed the diagnostics in more detail (including images) for a very similar <a rel="nofollow" href="https://www.displayr.com/convergence-hb-maxdiff" target="_blank">model (MaxDiff) </a>here.</p>
<p><b>We hope this post has helped you use Hierarchical Bayes to create your choice model. But if you’re feeling a bit confused and overwhelmed, it’s OK! Feel free to check out our guide on how to use <a rel="nofollow" href="https://www.displayr.com/how-to-hierarchical-bayes-choice-model-displayr/" target="_blank">Hierarchical Bayes to create a choice model</a> in Displayr, it’s easier, we promise. </b></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/hb-choice-model-r/?utm_medium=Feed&utm_source=Syndication"> 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/qgXlkXGRQqg" height="1"></a></div>