<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/using-partial-least-squares-to-conduct-relative-importance-analysis-in-displayr/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://www.displayr.com/using-partial-least-squares-conduct-relative-importance-analysis-displayr/"> 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://i1.wp.com/www.displayr.com/wp-content/uploads/2017/04/arrows-780-480-300x195.png?resize=300%2C195&ssl=1" alt="Partial Least Squares can be used find drivers of consumer preference" class="webfeedsFeaturedVisual wp-post-image" data-recalc-dims="1">
<p><img sizes="(max-width: 740px) 100vw, 740px" src="https://i0.wp.com/www.displayr.com/wp-content/uploads/2017/04/arrows-780-480.png?w=450" alt="" class="wp-image-1581" data-recalc-dims="1"></p>
<p>Partial Least Squares (PLS) is a popular method for <em>relative importance analysis</em> in fields where the data typically includes more predictors than observations.<em> </em>Relative importance analysis is a general term applied to any technique used for estimating the importance of predictor variables in a regression model. The output is a set of scores which enable the predictor variables to be ranked based upon how strongly each influences the outcome variable.</p>
<p>There are a number of different approaches to calculating relative importance analysis including <em>Relative Weights</em> and <em>Shapley Regression</em> as described <a rel="nofollow" href="https://www.displayr.com/reasons-to-use-relative-weights/" target="_blank">here</a> and <a rel="nofollow" href="https://www.displayr.com/shapley-vs-relative-weights/" target="_blank">here</a>. In this blog post I briefly describe an alternative method – Partial Least Squares. Because it effectively compresses the data before regression, PLS is particularly useful when the number of predictor variables is more than the number of observations.</p>
<h1>Partial Least Squares</h1>
<p>PLS is a dimension reduction technique with some similarity to <em>principal component analysis</em>. The predictor variables are mapped to a smaller set of variables and within that smaller space we perform a regression against the outcome variable.  In contrast to principal component analysis where the dimension reduction ignores the outcome variable, the PLS procedure aims to choose new mapped variables that maximally explain the outcome variable.</p>
<h1>Loading the example data</h1>
<p>First I’ll add some data with <strong>Insert > Data Set > URL</strong> and paste in this link:</p>
<p><span>http://wiki.q-researchsoftware.com/images/6/69/Stacked_Cola_Brand_Associations.sav</span></p>
<p>Dragging Brand preference onto the page from the <span><strong>Data</strong> </span>tree on the left table produces a table showing the breakdown of the respondents by category. This includes a <em>Don’t Know</em> category that doesn’t fit in the ordered scale from Love to Hate<em>.  </em>To remove Don’t Know I click on top of Brand preference in the <span><strong>Data</strong> </span>tree on the left and then click on <strong>Value Attributes</strong>. Changing <strong>Missing Values </strong>for the Don’t Know category to <strong>Exclude from analyses</strong> produces the table below.</p>
<p><iframe width="346.42" src="https://embedded.azureedge.net/75668/209079/391ed3a5-0d4d-40a9-9493-36a33dabb0b0.html?" height="232.99"></iframe></p>
<h1>Creating the PLS model</h1>
<p>Partial least squares is easy to run with a few lines of code. Select <strong>Insert ></strong> <strong>R Output </strong>and enter the following snippet of code into the <span><strong>R CODE</strong></span> box:</p>
<pre>
dat = data.frame(pref, Q5r0, Q5r1, Q5r2, Q5r3, Q5r4, Q5r5, Q5r6, Q5r7, Q5r8, 
                  Q5r9, Q5r10, Q5r11, Q5r12, Q5r13, Q5r14, Q5r15, Q5r16, Q5r17,
                  Q5r18, Q5r19, Q5r20, Q5r21, Q5r22, Q5r23, Q5r24, Q5r25, Q5r26,
                  Q5r27, Q5r29, Q5r28, Q5r30, Q5r31, Q5r32, Q5r33)

library(pls)
library(flipFormat)
library(flipTransformations)

dat = AsNumeric(ProcessQVariables(dat), binary = FALSE, remove.first = FALSE)
pls.model = plsr(pref ~ ., data = dat, validation = &quot;CV&quot;)
</pre>
<p>The first line selects <span>pref</span> as the outcome variable (strength of preference for a brand) and then adds 34 predictor variables, each indicating whether the respondent perceives the brand to have a particular characteristic. These variables can be dragged across from the <span><strong>Data</strong></span> tree on the left.</p>
<p>Next, the 3 libraries containing useful functions are loaded. The package <span>pls</span> contains the function to estimate the PLS model, and our own publicly-available packages, <span>flipFormat</span> and <span>flipTransformations</span> are included for function to help us transform and tidy the data. Since the R <span>pls </span>package requires inputs to be numerical I convert the variables from categorical.</p>
<p>In the final line above the <span>plsr</span> function does the work and creates <span>pls.model</span>.</p>
<h1>Automatically Selecting the Dimensions</h1>
<p>The following few lines recreate the model having found the optimal number of dimensions,</p>
<pre>
# Find the number of dimensions with lowest cross validation error
cv = RMSEP(pls.model)
best.dims = which.min(cv$val[estimate = &quot;adjCV&quot;, , ]) - 1
# Rerun the model
pls.model = plsr(pref ~ ., data = dat, ncomp = best.dims)
</pre>
<h1>Producing the Output</h1>
<p>Finally, we extract the useful information and format the output,</p>
<pre>
coefficients = coef(pls.model)
sum.coef = sum(sapply(coefficients, abs))
coefficients = coefficients * 100 / sum.coef
names(coefficients) = TidyLabels(Labels(dat)[-1])
coefficients = sort(coefficients, decreasing = TRUE)
</pre>
<p>The regression coefficients are normalized so their absolute sum is 100. The labels are added and the result is sorted.</p>
<p>The results below show that Reliable and Fun are positive predictors of preference, Unconventional and Sleepy are negative predictors and Tough has little relevance.</p>
<p><iframe src="https://embedded.azureedge.net/75668/209079/e0cdb11d-1a71-4e24-b988-dbfa26c87f9c.html?" width="381" height="513"></iframe></p>
<hr>
<p><span>TRY IT OUT</span><br>
You can <a rel="nofollow" href="https://app.displayr.com/Try/Partial%20Least%20Squares" target="_blank">perform this analysis</a> for yourself in Displayr.</p>
<hr>
	<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/using-partial-least-squares-conduct-relative-importance-analysis-displayr/"> 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/nkDlDdHYZrA" height="1"></a></div>