<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-covariates-to-improve-your-maxdiff-model/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://www.displayr.com/how-to-use-covariates-to-improve-your-maxdiff-model/?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>MaxDiff is a type of best-worst scaling. Respondents are asked to compare all choices in a given set and pick their best and worse (or most and least favorite). For an introduction, check out this great <a rel="nofollow" href="https://www.youtube.com/watch?v=L5hCFdkiPs0" target="_blank">webinar</a> by Tim Bock. In our post, we’ll discuss why you may want to include covariates in the first place and how they can be included in Hierarchical Bayes (HB) MaxDiff. Then we’ll use the approach to examine the qualities voters look for in a U.S. president.</p>
<p><img sizes="(max-width: 741px) 100vw, 741px" src="https://i1.wp.com/www.displayr.com/wp-content/uploads/2018/07/MaxDiffQuestion-300x211.png?w=450&ssl=1" alt="" class="wp-image-6096" data-recalc-dims="1"></p>
<h2>Why include respondent-specific covariates?</h2>
<p>Advances in computing means it is easy to include complex respondent-specific covariates in HB MaxDiff models. There are several reasons why we may want to do this in practice.</p>
<ol>
<li>A standard model which assumes each respondent’s part-worth is drawn from the same normal distribution may be too simplistic. Information drawn from additional covariates may improve the estimates of the part-worths. This is likely to be the case for surveys in which there were fewer questions and therefore less information.</li>
<li>Additionally, when respondents are segmented, we may be worried that the estimates for one segment are biased. Another concern is that HB may shrink the segment means overly close to each other. This is especially problematic if sample sizes vary greatly between segments.</li>
</ol>
<h2>How to include covariates in the model</h2>
<p>In the usual HB model, we model the part-worths for the ith respondent as β<sub>i</sub> ~ N(μ, ∑). Note that the mean and covariance parameters μ and ∑ do not depend on i and are the same for each respondent in the population. The simplest way to include respondent-specific covariates in the model is to modify μ to be dependent on the respondent’s covariates.</p>
<p>We do this by modifying the model for the part-worths to β<sub>i</sub> ~N(Θx<sub>i</sub>, ∑) where x<sub>i</sub> is a vector of known covariate values for the ith respondent and Θ is a matrix of unknown regression coefficients.  Each row of Θ is given a multivariate normal prior. The covariance matrix, ∑, is re-expressed into two parts: a correlation matrix and a vector of scales, and each part receives its own prior distribution.</p>
<h2>Fitting covariates in R and Displayr</h2>
<p>This model can be fit in R, Q and Displayr using the function <span>FitMaxDiff</span> in the <span>R</span> package <span>flipMaxDiff<span>. Download this package from <a rel="nofollow" href="https://github.com/Displayr/flipMaxDiff" target="_blank">GitHub</a>. The function fits the model using the No-U-Turn sampler implemented in <a rel="nofollow" href="http://mc-stan.org/" target="_blank"><span>stan</span></a> – the state-of-the-art software for fitting Bayesian models. The package allows us to quickly and efficiently estimate our model without having to worry about selecting the tuning parameters that are frequently a major hassle in Bayesian computation and machine learning. The package also provides a number of features for visualizing the results and diagnosing any issues with the model fit.</span></span></p>
<h2>Example in Displayr</h2>
<h3>The dataset</h3>
<p>Our data set asked 315 Americans ten questions about the attributes they look for in a U.S. president. Each question asked the respondents to pick their most and least important attributes from a set of five. The attributes were:</p>
<ul>
<li>Decent/ethical</li>
<li>Plain-speaking</li>
<li>Healthy</li>
<li>Successful in business</li>
<li>Good in a crisis</li>
<li>Experienced in government</li>
<li>Concerned for the welfare of minorities</li>
<li>Understands economics</li>
<li>Concerned about global warming</li>
<li>Concerned about poverty</li>
<li>Has served in the military</li>
<li>Multilingual</li>
<li>Entertaining</li>
<li>Male</li>
<li>From a traditional American background</li>
<li>Christian</li>
</ul>
<p>For more information, please see <a rel="nofollow" href="https://www.displayr.com/advanced-analysis-maxdiff/" target="_blank">this</a> earlier blog post, which analyzes the same data using HB, but does not consider covariates.</p>
<h3>Fitting your MaxDiff Model</h3>
<p>In Displayr and Q, we can fit a MaxDiff model by selecting <span>Marketing > MaxDiff > Hierarchical Bayes</span> from the menu.  See <a rel="nofollow" href="https://www.displayr.com/hb-maxdiff-displayr/" target="_blank">this</a> earlier blog post for a description of the HB controls/inputs and a demo using a different data set. Documentation specific to the Displayr GUI is on the <a rel="nofollow" href="https://wiki.q-researchsoftware.com/wiki/Marketing_-_MaxDiff_-_Hierarchical_Bayes" target="_blank">Q wiki</a>. To use the function in <span>R</span>, install the <span>devtools</span> package and then download <span>flipMaxDiff</span> using <span>devtools::install_github(“Displayr/flipMaxDiff”)</span>. Covariates can be included in the function <span>FitMaxDiff</span> inside <span>flipMaxDiff</span> via the parameters <span>cov.formula</span> and <span>cov.data</span>, which work just like the formula and data parameters in the <span>R</span> functions <span>lm</span> and <span>glm</span>.</p>
<p>We then included a single categorical predictor in the model – responses to the question of who they voted for in the 2016 election. The predictor had the following levels; voted for Clinton, voted for Trump, voted for another candidate, didn’t vote and don’t know or refused to answer.</p>
<p>We would expect this predictor to have a very strong correlation with the best and worse choices for each respondent. To compare the models with and without covariates in <span>Displayr</span>, first fit the model without covariates and then copy/paste the created <span>R</span> item.</p>
<p>To add the covariates, simply click <span>Properties > R CODE</span> in the Object Inspector on the right of the page and add a few lines of R code for the <span>cov.formula</span> and <span>cov.data</span> parameters. See the image carousel below.</p>
<div>

<div data-version="5.2.6">
<ul>	
<li data-param10="" data-transition="fade" data-index="rs-12" data-easein="default" data-saveperformance="off" data-easeout="default" data-fsslotamount="7" data-rotate="0" data-hideafterloop="0" data-masterspeed="300" data-hideslideonmobile="off" data-fsmasterspeed="1500" data-param1="" data-param3="" data-param2="" data-param5="" data-param4="" data-description data-param7="" data-param6="" data-param9="" data-param8="" data-thumb="" data-slotamount="default" data-title="Slide" data-fstransition="fade">
<br>
<img src="https://i1.wp.com/www.displayr.com/wp-content/plugins/revslider/admin/assets/images/dummy.png?w=450&ssl=1" data-bgparallax="off" alt="" data-bgposition="center top" data-bgrepeat="no-repeat" data-bgfit="contain" title="ChoiceModelWithCovAndWithout1" data-lazyload="https://www.displayr.com/wp-content/plugins/revslider/admin/assets/images/transparent.png" data-no-retina data-recalc-dims="1"><br>

<p>	</p>
<div data-transform_idle="o:1;" data-height="['auto']" data-x="8" data-y="bottom" data-width="['auto']" data-start="500.000305176" data-voffset="26" data-transform_in="opacity:0;s:300;e:Power2.easeInOut;" data-splitout="none" data-responsive_offset="on" data-transform_out="opacity:0;s:300;" data-splitin="none">Respondent parameter estimates and summary statistics for fitting<br>
a choice </div>
<p>	</p>
<div data-transform_idle="o:1;" data-height="['none','none','none','none']" data-x="47" data-y="2" data-width="['none','none','none','none']" data-start="500.000305176" data-transform_in="opacity:0;s:300;e:Power2.easeInOut;" data-responsive_offset="on" data-transform_out="opacity:0;s:300;"><img src="https://i1.wp.com/www.displayr.com/wp-content/plugins/revslider/admin/assets/images/dummy.png?w=450&ssl=1" data-hh="449px" data-ww="619px" alt="" data-lazyload="https://www.displayr.com/wp-content/uploads/2018/06/ChoiceModelWithCovAndWithout1.png" data-no-retina data-recalc-dims="1"> </div>
<p>	</p>
<div data-transform_idle="o:1;" data-height="['auto']" data-x="101" data-y="473" data-width="['auto']" data-start="500.000305176" data-transform_in="opacity:0;s:300;e:Power2.easeInOut;" data-splitout="none" data-responsive_offset="on" data-transform_out="opacity:0;s:300;" data-splitin="none">model with a covariate to the presidents data set. </div>
</li>
<p>	</p>
<li data-hideslideonmobile="off" data-param10="" data-transition="fade" data-param1="" data-index="rs-13" data-param3="" data-param2="" data-easein="default" data-saveperformance="off" data-param5="" data-easeout="default" data-param4="" data-description data-param7="" data-param6="" data-param9="" data-rotate="0" data-param8="" data-thumb="" data-slotamount="default" data-title="Slide" data-hideafterloop="0" data-masterspeed="300">
<br>
<img src="https://i1.wp.com/www.displayr.com/wp-content/plugins/revslider/admin/assets/images/dummy.png?w=450&ssl=1" data-bgparallax="off" alt="" data-bgposition="center center" data-bgrepeat="no-repeat" data-bgfit="contain" title="ChoiceModelWithCovAndWithout2" data-lazyload="https://www.displayr.com/wp-content/plugins/revslider/admin/assets/images/transparent.png" data-no-retina data-recalc-dims="1"><br>

<p>	</p>
<div data-transform_idle="o:1;" data-height="['auto']" data-x="6" data-y="446" data-width="['auto']" data-start="500.000305176" data-transform_in="opacity:0;s:300;e:Power2.easeInOut;" data-splitout="none" data-responsive_offset="on" data-transform_out="opacity:0;s:300;" data-splitin="none">Respondent parameter estimates and summary statistics for fitting a choice </div>
<p>	</p>
<div data-transform_idle="o:1;" data-height="['auto']" data-x="73" data-y="475" data-width="['auto']" data-start="500.000305176" data-transform_in="opacity:0;s:300;e:Power2.easeInOut;" data-splitout="none" data-responsive_offset="on" data-transform_out="opacity:0;s:300;" data-splitin="none">model with a categorical covariate to the presidents data set. </div>
<p>	</p>
<div data-transform_idle="o:1;" data-height="['none','none','none','none']" data-x="75" data-y="-2" data-width="['none','none','none','none']" data-start="500.000305176" data-transform_in="opacity:0;s:300;e:Power2.easeInOut;" data-responsive_offset="on" data-transform_out="opacity:0;s:300;"><img src="https://i1.wp.com/www.displayr.com/wp-content/plugins/revslider/admin/assets/images/dummy.png?w=450&ssl=1" data-hh="445px" data-ww="560px" alt="" data-lazyload="https://www.displayr.com/wp-content/uploads/2018/06/ChoiceModelWithCovAndWithout2.png" data-no-retina data-recalc-dims="1"> </div>
</li>
<p>	</p>
<li data-hideslideonmobile="off" data-param10="" data-transition="fade" data-param1="" data-index="rs-14" data-param3="" data-param2="" data-easein="default" data-saveperformance="off" data-param5="" data-easeout="default" data-param4="" data-description data-param7="" data-param6="" data-param9="" data-rotate="0" data-param8="" data-thumb="" data-slotamount="default" data-title="Slide" data-hideafterloop="0" data-masterspeed="300">
<br>
<img src="https://i1.wp.com/www.displayr.com/wp-content/plugins/revslider/admin/assets/images/dummy.png?w=450&ssl=1" data-bgparallax="off" alt="" data-bgposition="center center" data-bgrepeat="no-repeat" data-bgfit="contain" title="ChoiceModelWithCovAndWithout3a" data-lazyload="https://www.displayr.com/wp-content/plugins/revslider/admin/assets/images/transparent.png" data-no-retina data-recalc-dims="1"><br>

<p>	</p>
<div data-transform_idle="o:1;" data-height="['auto']" data-x="3" data-y="476" data-width="['auto']" data-start="500.000305176" data-transform_in="opacity:0;s:300;e:Power2.easeInOut;" data-splitout="none" data-responsive_offset="on" data-transform_out="opacity:0;s:300;" data-splitin="none">GUI inputs for fitting a choice model with the presidents data set in Displayr. </div>
<p>	</p>
<div data-transform_idle="o:1;" data-height="['none','none','none','none']" data-x="71" data-y="3" data-width="['none','none','none','none']" data-start="500.000305176" data-transform_in="opacity:0;s:300;e:Power2.easeInOut;" data-responsive_offset="on" data-transform_out="opacity:0;s:300;"><img src="https://i1.wp.com/www.displayr.com/wp-content/plugins/revslider/admin/assets/images/dummy.png?w=450&ssl=1" data-hh="471" data-ww="543" alt="" data-lazyload="https://www.displayr.com/wp-content/uploads/2018/06/ChoiceModelWithCovAndWithout3a.png" data-no-retina data-recalc-dims="1"> </div>
</li>
<p>	</p>
<li data-hideslideonmobile="off" data-param10="" data-transition="fade" data-param1="" data-index="rs-15" data-param3="" data-param2="" data-easein="default" data-saveperformance="off" data-param5="" data-easeout="default" data-param4="" data-description data-param7="" data-param6="" data-param9="" data-rotate="0" data-param8="" data-thumb="" data-slotamount="default" data-title="Slide" data-hideafterloop="0" data-masterspeed="300">
<br>
<img src="https://i1.wp.com/www.displayr.com/wp-content/plugins/revslider/admin/assets/images/dummy.png?w=450&ssl=1" data-bgparallax="off" alt="" data-bgposition="center center" data-bgrepeat="no-repeat" data-bgfit="contain" title="ChoiceModelWithCovAndWithout3b2" data-lazyload="https://www.displayr.com/wp-content/plugins/revslider/admin/assets/images/transparent.png" data-no-retina data-recalc-dims="1"><br>

<p>	</p>
<div data-transform_idle="o:1;" data-height="['auto']" data-x="67" data-y="477" data-width="['auto']" data-start="500.000305176" data-transform_in="opacity:0;s:300;e:Power2.easeInOut;" data-splitout="none" data-responsive_offset="on" data-transform_out="opacity:0;s:300;" data-splitin="none">R code for fitting the choice model with fixed covariates in Displayr. </div>
<p>	</p>
<div data-transform_idle="o:1;" data-height="['none','none','none','none']" data-x="98" data-y="1" data-width="['none','none','none','none']" data-start="500.000305176" data-transform_in="opacity:0;s:300;e:Power2.easeInOut;" data-responsive_offset="on" data-transform_out="opacity:0;s:300;"><img src="https://i1.wp.com/www.displayr.com/wp-content/plugins/revslider/admin/assets/images/dummy.png?w=450&ssl=1" data-hh="475px" data-ww="531px" alt="" data-lazyload="https://www.displayr.com/wp-content/uploads/2018/06/ChoiceModelWithCovAndWithout3b2.png" data-no-retina data-recalc-dims="1"> </div>
</li>
</ul>
<p></p>
<div></div>
</div>
<p><br>
<br>

</div>
<p></p>
<h3>Checking convergence</h3>
<p>We fit the models using 1000 iterations and eight Markov chains. When conducting a HB analysis, it is vital to check that the algorithm used has both converged to and adequately sampled from the posterior distribution. Using the HB diagnostics available in Displayr (see <a rel="nofollow" href="https://www.displayr.com/convergence-hb-maxdiff/" target="_blank">this post</a> for a detailed overview), there appeared to be no issues with convergence for this data. We then assessed the performance of our models by leaving out one or more respondent questions and seeing how well we could predict their choice using the estimated model.</p>
<h3>Results</h3>
<p>If we only hold out one question for prediction and use the other nine questions to fit the models, the effect of the categorical predictor is small. The model with the categorical predictor takes longer to run for the same number of iterations due to the increased number of parameters. Both models have only a modest improvement in out-of-sample prediction accuracy (from 67.0% to 67.3%). We did not gain much from running the predictor because we could already draw substantial information from the nine MaxDiff questions.</p>
<p>Including fixed covariates becomes much more advantageous when you have less MaxDiff questions – like in the extreme example of only having two questions to fit the models. We see a larger improvement in out-of-sample prediction accuracy (from 54.5% to 55.0%). We also see a much higher effective sample size per second (from 5.04 to 13.37 for the mean parameters). This means that the algorithm is able to sample much more efficiently with the covariate included. Even more importantly, this saves us time as we don’t need to use as many iterations to obtain our desired number of effective samples.</p>
<p><strong>To see how this was done in Displayr, head to this dashboard “<a rel="nofollow" href="https://app.displayr.com/Try/HB%20Choice%20Model%20With%20Fixed%20Covariates" target="_blank">HB Choice Model with Fixed Covariates</a>” to view the complete results! Ready to include your own covariates for analysis? <a rel="nofollow" href="https://app.displayr.com/SignUp?plan_id=0" target="_blank">Start a free Displayr trial</a>. </strong></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/how-to-use-covariates-to-improve-your-maxdiff-model/?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 alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/KII0ztu-2WY" height="1"></a></div>