<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>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 how to use an alternative method, <em>Partial Least Squares, </em>in R. 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>
<hr>
<h1><span>Partial Least Squares</span></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 Principal Component Analysis the dimension reduction procedure ignores the outcome variable. However PLS aims to choose new mapped variables that maximally explain the outcome variable.</p>
<p>To get started I’ll import some data into R and examine it with the following few lines of code:</p>
<pre>
cola.url = &quot;http://wiki.q-researchsoftware.com/images/d/db/Stacked_colas.csv&quot;
colas = read.csv(cola.url)
str(colas)
</pre>
<p>The output below show 37 variables. I am going to predict <span>pref,</span> the strength of a respondent’s preference for a brand on a scale from 1 to 5.  To do this I’ll use the 34 binary predictor variables that indicate whether the person perceives the brand to have a particular characteristic.</p>
<p><img sizes="(max-width: 758px) 100vw, 758px" src="https://i2.wp.com/www.displayr.com/wp-content/uploads/2017/05/cola_data.png?w=450&ssl=1" alt="" class="wp-image-1606" data-recalc-dims="1"></p>
<hr>
<h1><span>Using Partial Least Squares in R</span></h1>
<p>The next step is to remove unwanted variables and then build a model.  Cross validation is used to find the optimal number of retained dimensions. Then the model is rebuilt with this optimal number of dimensions. This is all contained in the R code below.</p>
<pre>
colas = subset(colas, select = -c(URLID, brand))

library(pls)
pls.model = plsr(pref ~ ., data = colas, validation = &quot;CV&quot;)

# 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 = colas, ncomp = best.dims)
</pre>
<hr>
<h1><span>Producing the Output</span></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
coefficients = sort(coefficients[, 1 , 1])
barplot(tail(coefficients, 5))
</pre>
<p>The regression coefficients are normalized so their absolute sum is 100 and the result is sorted.</p>
<p>The results below show that Reliable and Fun are positive predictors of preference.  You could run the code <span>barplot(head(coefficients, 5))</span> to see that at the other end of the scale Unconventional and Sleepy are negative predictors.</p>
<p><img sizes="(max-width: 714px) 100vw, 714px" src="https://i0.wp.com/www.displayr.com/wp-content/uploads/2017/05/cola_barplot.png?w=450&ssl=1" alt="" class="wp-image-1607" data-recalc-dims="1"></p>
<p> </p>
<hr>
<p><span>TRY IT OUT</span><br>
Displayr is a data science platform providing analytics, visualization and the full power of R. You can <a rel="nofollow" href="https://app.displayr.com/Try/Partial%20Least%20Squares%20in%20R" target="_blank">perform this analysis</a> for yourself in Displayr.</p>
<hr>
	<img width="1" alt="" src="http://feeds.feedburner.com/~r/RBloggers/~4/aQ6bt22307Y" height="1">