<p><div><div><div><div data-width="100" data-show-faces="false" data-send="true" data-layout="button_count" data-height="21" data-href="http://www.r-bloggers.com/principal-components-regression-pt-2-y-aware-methods/"></div></div><div><a data-url="http://www.r-bloggers.com/principal-components-regression-pt-2-y-aware-methods/" data-counturl="http://www.r-bloggers.com/principal-components-regression-pt-2-y-aware-methods/" data-count="horizontal" data-text="Principal Components Regression, Pt. 2: Y-Aware Methods" class="twitter-share-button" data-via="rbloggers" href="https://twitter.com/share"></a></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://www.win-vector.com/blog/2016/05/pcr_part2_yaware/"> R – Win-Vector Blog</a></strong>, and kindly contributed to <a rel="nofollow" href="http://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>In our <a target="_blank" href="http://www.win-vector.com/blog/2016/05/pcr_part1_xonly">previous note</a>, we discussed some problems that can arise when using standard principal components analysis (specifically, principal components regression) to model the relationship between independent (<em>x</em>) and dependent (<em>y</em>) variables. In this note, we present some dimensionality reduction techniques that alleviate some of those problems, in particular what we call <em>Y-Aware Principal Components Analysis</em>, or <em>Y-Aware PCA</em>. We will use our variable treatment package <a target="_blank" href="https://github.com/WinVector/vtreat"><code>vtreat</code></a> in the examples we show in this note, but you can easily implement the approach independently of <code>vtreat</code>.</p>
<p><span></span></p>
<div>
<h2>What is <em>Y</em>-Aware PCA?</h2>
<p>As with other geometric algorithms, principal components analysis is sensitive to the units of the data. In standard (&quot;<em>x</em>-only&quot;) PCA, we often attempt to alleviate this problem by rescaling the <em>x</em> variables to their &quot;natural units&quot;: that is, we rescale <em>x</em> by its own standard deviation. By individually rescaling each <em>x</em> variable to its &quot;natural unit,&quot; we hope (but cannot guarantee) that all the data as a group will be in some &quot;natural metric space,&quot; and that the structure we hope to discover in the data will manifest itself in this coordinate system. As we saw in the previous note, if the structure that we hope to discover is the relationship between <em>x</em> and <em>y</em>, we have even less guarantee that we are in the correct space, since the decomposition of the data was done without knowledge of <em>y</em>.</p>
<p><em>Y</em>-aware PCA is simply PCA with a different scaling: we rescale the <em>x</em> data to be in <em>y</em>-units. That is, we want scaled variables <em>x’</em> such that a unit change in <em>x’</em> corresponds to a unit change in <em>y</em>. Under this rescaling, all the independent variables are in <em>the same</em> units, which are indeed the natural units for the problem at hand: characterizing their effect on <em>y</em>. (We also center the transformed variables <em>x’</em> to be zero mean, as is done with standard centering and scaling).</p>
<p>It’s easy to determine the scaling for a variable <em>x</em> by fitting a linear regression model between <em>x</em> and <em>y</em>:</p>
<p><center><code>
<p><span>y = m * x + b</span></p>
<p></p>
<p>The coefficient <em>m</em> is the slope of the best fit line, so a unit change in <em>x</em> corresponds (on average) to a change of <em>m</em> units in <em>y</em>. If we rescale (and recenter) <em>x</em> as</p>
<p><center><code>
<p><span>x' := m * x - mean(m * x)</span></p>
<p></p>
<p>then <em>x’</em> is in <em>y</em> units. This <em>y</em>-aware scaling is both complementary to variable pruning and powerful enough to perform well on its own.</p>
<p>In <code>vtreat</code>, the treatment plan created by <code>designTreatmentsN()</code> will store the information needed for <em>y</em>-aware scaling, so that if you then <code>prepare</code> your data with the flag <code>scale=TRUE</code>, the resulting treated frame will be scaled appropriately.</p>

<div>
<h2>An Example of <em>Y</em>-Aware PCA</h2>
<p>First, let’s build our example. We will use the same data set as our earlier <a target="_blank" href="http://www.win-vector.com/blog/2016/05/pcr_part1_xonly">&quot;X only&quot; discussion</a>.</p>
<p>In this data set, there are two (unobservable) processes: one that produces the output <code>yA</code> and one that produces the output <code>yB</code>.We only observe the mixture of the two: <code>y =  yA + yB + eps</code>, where <code>eps</code> is a noise term. Think of <code>y</code> as measuring some notion of success and the <code>x</code> variables as noisy estimates of two different factors that can each drive success.</p>
<p>We’ll set things up so that the first five variables (x.01, x.02, x.03, x.04, x.05) have all the signal. The odd numbered variables correspond to one process (<code>yB</code>) and the even numbered variables correspond to the other (<code>yA</code>). Then, to simulate the difficulties of real world modeling, we’ll add lots of pure noise variables (<code>noise*</code>). The noise variables are unrelated to our <em>y</em> of interest — but are related to other &quot;y-style&quot; processes that we are not interested in. We do this because in real applications, there is no reason to believe that unhelpful variables have limited variation or are uncorrelated with each other, though things would certainly be easier if we could so assume. As we showed in the <a target="_blank" href="http://www.win-vector.com/blog/2016/05/pcr_part1_xonly">previous note</a>, this correlation undesirably out-competed the <em>y</em> induced correlation among signaling variables when using standard PCA.</p>
<p>All the variables are also deliberately mis-scaled to model some of the difficulties of working with under-curated real world data.</p>
<p>Let’s start with our train and test data.</p>
<pre><code># make data
set.seed(23525)
dTrain &lt;- mkData(1000)
dTest &lt;- mkData(1000)</code></pre>
<p>Let’s look at our outcome <em>y</em> and a few of our variables.</p>
<pre><code>summary(dTrain[, c(&quot;y&quot;, &quot;x.01&quot;, &quot;x.02&quot;, &quot;noise1.01&quot;, &quot;noise1.02&quot;)])</code></pre>
<pre><code>##        y                 x.01               x.02        
##  Min.   :-5.08978   Min.   :-4.94531   Min.   :-9.9796  
##  1st Qu.:-1.01488   1st Qu.:-0.97409   1st Qu.:-1.8235  
##  Median : 0.08223   Median : 0.04962   Median : 0.2025  
##  Mean   : 0.08504   Mean   : 0.02968   Mean   : 0.1406  
##  3rd Qu.: 1.17766   3rd Qu.: 0.93307   3rd Qu.: 1.9949  
##  Max.   : 5.84932   Max.   : 4.25777   Max.   :10.0261  
##    noise1.01          noise1.02       
##  Min.   :-30.5661   Min.   :-30.4412  
##  1st Qu.: -5.6814   1st Qu.: -6.4069  
##  Median :  0.5278   Median :  0.3031  
##  Mean   :  0.1754   Mean   :  0.4145  
##  3rd Qu.:  5.9238   3rd Qu.:  6.8142  
##  Max.   : 26.4111   Max.   : 31.8405</code></pre>
<p>Next, we’ll design a treatment plan for the frame, and examine the variable significances, as estimated by <code>vtreat</code>.</p>
<pre><code># design treatment plan
treatmentsN &lt;- designTreatmentsN(dTrain,setdiff(colnames(dTrain),'y'),'y',
                                 verbose=FALSE)

scoreFrame = treatmentsN$scoreFrame
scoreFrame$vartype = ifelse(grepl(&quot;noise&quot;, scoreFrame$varName), &quot;noise&quot;, &quot;signal&quot;)

dotplot_identity(scoreFrame, &quot;varName&quot;, &quot;sig&quot;, &quot;vartype&quot;) + 
  coord_flip()  + ggtitle(&quot;vtreat variable significance estimates&quot;)+ 
  scale_color_manual(values = c(&quot;noise&quot; = &quot;#d95f02&quot;, &quot;signal&quot; = &quot;#1b9e77&quot;)) </code></pre>
<p><img title="" alt="" src="http://i2.wp.com/www.win-vector.com/dfiles/YAwarePCA_files/figure-html/design1prep-1.png?w=660" data-recalc-dims="1"></p>
<p>Note that the noise variables typically have large significance values, denoting statistical insignificance. Usually we recommend doing some significance pruning on variables before moving on — see <a target="_blank" href="http://www.win-vector.com/blog/2014/02/bad-bayes-an-example-of-why-you-need-hold-out-testing/">here</a> for possible consequences of not pruning an over-abundance of variables, and <a target="_blank" href="http://www.win-vector.com/blog/2015/08/how-do-you-know-if-your-data-has-signal/">here</a> for a discussion of one way to prune, based on significance. For this example, however, we will attempt dimensionality reduction without pruning.</p>
</div>
<div>
<h2><em>Y</em>-Aware PCA</h2>
<div>
<h3>Prepare the frame with <em>y</em>-aware scaling</h3>
<p>Now let’s prepare the treated frame, with scaling turned on. We will deliberately turn off variable pruning by setting <code>pruneSig = 1</code>. In real applications, you would want to set <code>pruneSig</code> to a value less than one to prune insignificant variables. However, here we turn off variable pruning to show that you can recover some of pruning’s benefits via scaling effects, because the scaled noise variables should not have a major effect in the principal components analysis. Pruning by significance is in fact a good additional precaution complementary to scaling by effects.</p>
<pre><code># prepare the treated frames, with y-aware scaling
examplePruneSig = 1.0 
dTrainNTreatedYScaled &lt;- prepare(treatmentsN,dTrain,pruneSig=examplePruneSig,scale=TRUE)
dTestNTreatedYScaled &lt;- prepare(treatmentsN,dTest,pruneSig=examplePruneSig,scale=TRUE)

# get the variable ranges
ranges = vapply(dTrainNTreatedYScaled, FUN=function(col) c(min(col), max(col)), numeric(2))
rownames(ranges) = c(&quot;vmin&quot;, &quot;vmax&quot;) 
rframe = as.data.frame(t(ranges))  # make ymin/ymax the columns
rframe$varName = rownames(rframe)
varnames = setdiff(rownames(rframe), &quot;y&quot;)
rframe = rframe[varnames,]
rframe$vartype = ifelse(grepl(&quot;noise&quot;, rframe$varName), &quot;noise&quot;, &quot;signal&quot;)

# show a few columns
summary(dTrainNTreatedYScaled[, c(&quot;y&quot;, &quot;x.01_clean&quot;, &quot;x.02_clean&quot;, &quot;noise1.02_clean&quot;, &quot;noise1.02_clean&quot;)])</code></pre>
<pre><code>##        y              x.01_clean         x.02_clean      
##  Min.   :-5.08978   Min.   :-2.65396   Min.   :-2.51975  
##  1st Qu.:-1.01488   1st Qu.:-0.53547   1st Qu.:-0.48904  
##  Median : 0.08223   Median : 0.01063   Median : 0.01539  
##  Mean   : 0.08504   Mean   : 0.00000   Mean   : 0.00000  
##  3rd Qu.: 1.17766   3rd Qu.: 0.48192   3rd Qu.: 0.46167  
##  Max.   : 5.84932   Max.   : 2.25552   Max.   : 2.46128  
##  noise1.02_clean      noise1.02_clean.1   
##  Min.   :-0.0917910   Min.   :-0.0917910  
##  1st Qu.:-0.0186927   1st Qu.:-0.0186927  
##  Median : 0.0003253   Median : 0.0003253  
##  Mean   : 0.0000000   Mean   : 0.0000000  
##  3rd Qu.: 0.0199244   3rd Qu.: 0.0199244  
##  Max.   : 0.0901253   Max.   : 0.0901253</code></pre>
<pre><code>barbell_plot(rframe, &quot;varName&quot;, &quot;vmin&quot;, &quot;vmax&quot;, &quot;vartype&quot;) +
  coord_flip() + ggtitle(&quot;y-scaled variables: ranges&quot;) + 
  scale_color_manual(values = c(&quot;noise&quot; = &quot;#d95f02&quot;, &quot;signal&quot; = &quot;#1b9e77&quot;))</code></pre>
<p><img title="" alt="" src="http://i0.wp.com/www.win-vector.com/dfiles/YAwarePCA_files/figure-html/workscaled1-1.png?w=660" data-recalc-dims="1"></p>
<p>Notice that after the <em>y</em>-aware rescaling, the signal carrying variables have larger ranges than the noise variables.</p>
</div>
<div>
<h3>The Principal Components Analysis</h3>
<p>Now we do the principal components analysis. In this case it is critical that the <code>scale</code> parameter in <code>prcomp</code> is set to <code>FALSE</code> so that it does not undo our own scaling. Notice the magnitudes of the singular values fall off quickly after the first two to five values.</p>
<pre><code>vars &lt;- setdiff(colnames(dTrainNTreatedYScaled),'y')
# prcomp defaults to scale. = FALSE, but we already scaled/centered in vtreat- which we don't want to lose.
dmTrain &lt;- as.matrix(dTrainNTreatedYScaled[,vars])
dmTest &lt;- as.matrix(dTestNTreatedYScaled[,vars])
princ &lt;- prcomp(dmTrain, center = FALSE, scale. = FALSE)
dotplot_identity(frame = data.frame(pc=1:length(princ$sdev), 
                            magnitude=princ$sdev), 
                 xvar=&quot;pc&quot;,yvar=&quot;magnitude&quot;) +
  ggtitle(&quot;Y-Scaled variables: Magnitudes of singular values&quot;)</code></pre>
<p><img title="" alt="" src="http://i0.wp.com/www.win-vector.com/dfiles/YAwarePCA_files/figure-html/scaledpca-1.png?w=660" data-recalc-dims="1"></p>
<p>When we look at the variable loadings of the first five principal components, we see that we recover the even/odd loadings of the original signal variables. <code>PC1</code> has the odd variables, and <code>PC2</code> has the even variables. These two principal components carry most of the signal. The next three principal components complete the basis for the five original signal variables. The noise variables have very small loadings, compared to the signal variables.</p>
<pre><code>proj &lt;- extractProjection(2,princ)
rot5 &lt;- extractProjection(5,princ)
rotf = as.data.frame(rot5)
rotf$varName = rownames(rotf)
rotflong = gather(rotf, &quot;PC&quot;, &quot;loading&quot;, starts_with(&quot;PC&quot;))
rotflong$vartype = ifelse(grepl(&quot;noise&quot;, rotflong$varName), &quot;noise&quot;, &quot;signal&quot;)

dotplot_identity(rotflong, &quot;varName&quot;, &quot;loading&quot;, &quot;vartype&quot;) + 
  facet_wrap(~PC,nrow=1) + coord_flip() + 
  ggtitle(&quot;Y-Scaled Variable loadings, first five principal components&quot;) + 
  scale_color_manual(values = c(&quot;noise&quot; = &quot;#d95f02&quot;, &quot;signal&quot; = &quot;#1b9e77&quot;))</code></pre>
<p><img title="" alt="" src="http://i1.wp.com/www.win-vector.com/dfiles/YAwarePCA_files/figure-html/scaledvarload-1.png?w=660" data-recalc-dims="1"></p>
<p>Let’s look at the projection of the data onto its first two principal components, using color to code the <em>y</em> value. Notice that y increases both as we move up and as we move right. We have recovered two features that correlate with an increase in y. In fact, <code>PC1</code> corresponds to the odd signal variables, which correspond to process <em>yB</em>, and <code>PC2</code> corresponds to the even signal variables, which correspond to process <em>yA</em>.</p>
<pre><code># apply projection
projectedTrain &lt;- as.data.frame(dmTrain %*% proj,
                      stringsAsFactors = FALSE)
# plot data sorted by principal components
projectedTrain$y &lt;- dTrainNTreatedYScaled$y
ScatterHistN(projectedTrain,'PC1','PC2','y',
               &quot;Y-Scaled Training Data projected to first two principal components&quot;)</code></pre>
<p><img title="" alt="" src="http://i2.wp.com/www.win-vector.com/dfiles/YAwarePCA_files/figure-html/scaledplottrain-1.png?w=660" data-recalc-dims="1"></p>
<p>Now let’s fit a linear regression model to the first two principal components.</p>
<pre><code>model &lt;- lm(y~PC1+PC2,data=projectedTrain)
summary(model)</code></pre>
<pre><code>## 
## Call:
## lm(formula = y ~ PC1 + PC2, data = projectedTrain)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -3.3470 -0.7919  0.0172  0.7955  3.9588 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept)  0.08504    0.03912   2.174     0.03 *  
## PC1          0.78611    0.04092  19.212   &lt;2e-16 ***
## PC2          1.03243    0.04469  23.101   &lt;2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 1.237 on 997 degrees of freedom
## Multiple R-squared:  0.4752, Adjusted R-squared:  0.4742 
## F-statistic: 451.4 on 2 and 997 DF,  p-value: &lt; 2.2e-16</code></pre>
<pre><code>projectedTrain$estimate &lt;- predict(model,newdata=projectedTrain)
trainrsq = rsq(projectedTrain$estimate,projectedTrain$y)

ScatterHist(projectedTrain,'estimate','y','Recovered model versus truth (y aware PCA train)',
            smoothmethod='identity',annot_size=3)</code></pre>
<p><img title="" alt="" src="http://i1.wp.com/www.win-vector.com/dfiles/YAwarePCA_files/figure-html/quant1-1.png?w=660" data-recalc-dims="1"></p>
<p>This model, with only two variables, explains 47.52% of the variation in <em>y</em>. This is comparable to the variance explained by the model fit to twenty principal components using <em>x</em>-only PCA (as well as a model fit to all the original variables) in the previous note.</p>
<p>Let’s see how the model does on hold-out data.</p>
<pre><code># apply projection
projectedTest &lt;- as.data.frame(dmTest %*% proj,
                      stringsAsFactors = FALSE)
# plot data sorted by principal components
projectedTest$y &lt;- dTestNTreatedYScaled$y
ScatterHistN(projectedTest,'PC1','PC2','y',
               &quot;Y-Scaled Test Data projected to first two principal components&quot;)</code></pre>
<p><img title="" alt="" src="http://i0.wp.com/www.win-vector.com/dfiles/YAwarePCA_files/figure-html/scaledplotest-1.png?w=660" data-recalc-dims="1"></p>
<pre><code>projectedTest$estimate &lt;- predict(model,newdata=projectedTest)
testrsq = rsq(projectedTest$estimate,projectedTest$y)
testrsq</code></pre>
<pre><code>## [1] 0.5063724</code></pre>
<pre><code>ScatterHist(projectedTest,'estimate','y','Recovered model versus truth (y aware PCA test)',
            smoothmethod='identity',annot_size=3)</code></pre>
<p><img title="" alt="" src="http://i1.wp.com/www.win-vector.com/dfiles/YAwarePCA_files/figure-html/quant1test-1.png?w=660" data-recalc-dims="1"></p>
<p>We see that this two-variable model captures about 50.64% of the variance in <em>y</em> on hold-out — again, comparable to the hold-out performance of the model fit to twenty principal components using <em>x</em>-only PCA. These two principal components also do a <em>much</em> better job of capturing the internal structure of the data — that is, the relationship of the signaling variables to the <code>yA</code> and <code>yB</code> processes — than the first two principal components of the <em>x</em>-only PCA.</p>
</div>
</div>
<div>
<h2>Is this the same as <code>caret::preProcess</code>?</h2>
<p>In this note, we used <code>vtreat</code>, a data.frame processor/conditioner that prepares real-world data for predictive modeling in a statistically sound manner, followed by principal components regression. One could instead use <code>caret</code>. The <code>caret</code> package, as described in the documentation, &quot;is a set of functions that attempt to streamline the process for creating predictive models.&quot;</p>
<p><code>caret::preProcess</code> is designed to implement a number of sophisticated <em>x</em> alone transformations, groupings, prunings, and repairs (see <a target="_blank" href="http://topepo.github.io/caret/preprocess.html#all">caret/preprocess.html#all</a>, which demonstrates &quot;the function on all the columns except the last, which is the outcome&quot; on the <em>schedulingData</em> dataset). So <code>caret::preProcess</code> is a super-version of the PCA step.</p>
<p>We could use it as follows either alone or <em>before</em> vtreat design/prepare as a initial pre-processor. Using it alone is similar to PCA for this data set, as our example doesn’t have some of the additional problems <code>caret::preProcess</code> is designed to help with.</p>
<pre><code>library('caret')
origVars &lt;- setdiff(colnames(dTrain),'y')
# can try variations such adding/removing non-linear steps such as &quot;YeoJohnson&quot;
prep &lt;- preProcess(dTrain[,origVars],
                     method = c(&quot;center&quot;, &quot;scale&quot;, &quot;pca&quot;))
prepared &lt;- predict(prep,newdata=dTrain[,origVars])
newVars &lt;- colnames(prepared)
prepared$y &lt;- dTrain$y
print(length(newVars))</code></pre>
<pre><code>## [1] 44</code></pre>
<pre><code>modelB &lt;- lm(paste('y',paste(newVars,collapse=' + '),sep=' ~ '),data=prepared)
print(summary(modelB)$r.squared)</code></pre>
<pre><code>## [1] 0.5004569</code></pre>
<pre><code>print(summary(modelB)$adj.r.squared)</code></pre>
<pre><code>## [1] 0.4774413</code></pre>
<pre><code>preparedTest &lt;- predict(prep,newdata=dTest[,origVars])
testRsqC &lt;- rsq(predict(modelB,newdata=preparedTest),dTest$y)
testRsqC</code></pre>
<pre><code>## [1] 0.4824284</code></pre>
<p>The 44 <code>caret</code>-chosen PCA variables are designed to capture 95% of the in-sample explainable variation of the variables. The linear regression model fit to the selected variables explains about 50.05% of the <em>y</em> variance on training and 48.24% of the <em>y</em> variance on test. This <em>is</em> quite good, comparable to our previous results. However, note that <code>caret</code> picked more than the twenty principal components that we picked visually in the previous note, and needed far more variables than we needed with <em>y</em>-aware PCA.</p>
<p>Because <code>caret::preProcess</code> is <em>x</em>-only processing, the first few variables capture much less of the <em>y</em> variation. So we can’t model <em>y</em> without using a lot of the derived variables. To show this, let’s try fitting a model using only five of <code>caret</code>‘s PCA variables.</p>
<pre><code>model5 &lt;- lm(paste('y',paste(newVars[1:5],collapse=' + '),sep=' ~ '),data=prepared)
print(summary(model5)$r.squared)</code></pre>
<pre><code>## [1] 0.1352</code></pre>
<pre><code>print(summary(model5)$adj.r.squared)</code></pre>
<pre><code>## [1] 0.1308499</code></pre>
<p>The first 5 variables only capture about 13.52% of the in-sample variance; without being informed about <em>y</em>, we can’t know which variation to preserve and which we can ignore. We certainly haven’t captured the two subprocesses that drive <em>y</em> in an inspectable manner.</p>
</div>
<div>
<h2>Other <em>Y</em>-aware Approaches to Dimensionality Reduction</h2>
<p>If your goal is regression, there are other workable <em>y</em>-aware dimension reducing procedures, such as L2-regularized regression or partial least squares. Both methods are also related to principal components analysis (see Hastie, <em>etal</em> 2009).</p>
<p>Bair, <em>etal</em> proposed a variant of principal components regression that they call <em>Supervised PCR</em>. In supervised PCR, <em>as described in their 2006 paper</em>, a univariate linear regression model is fit to each variable (after scaling and centering), and any variable whose coefficient (what we called <em>m</em> above) has a magnitude less than some threshold <span>(theta)</span> is pruned. PCR is then done on the remaining variables. Conceptually, this is similar to the significance pruning that <code>vtreat</code> offers, except that the pruning criterion is &quot;effects-based&quot; (that is, it’s based on the magnitude of a parameter, or the strength of an effect) rather than probability-based, such as pruning on significance.</p>
<p>One issue with an effects-based pruning criterion is that the appropriate pruning threshold varies from problem to problem, and not necessarily in an obvious way. Bair, <em>etal</em> find an appropriate threshold via cross-validation. Probability-based thresholds are in some sense more generalizable from problem to problem, since the score is always in probability units — the same units for all problems. A simple variation of supervised PCR might prune on the <em>significance</em> of the coefficient <em>m</em>, as determined by its t-statistic. This would be essentially equivalent to significance pruning of the variables via <code>vtreat</code> before standard PCR.</p>
<p>Note that <a target="_blank" href="http://www.win-vector.com/blog/2015/08/how-do-you-know-if-your-data-has-signal/"><code>vtreat</code> uses the significance of the one-variable model fits, not coefficient significance</a> to estimate variable significance. When both the dependent and independent variables are numeric, the model significance and the coefficient significance are identical (see Weisberg, <em>Applied Linear Regression</em>). In more general modeling situations where either the outcome is categorical or the original input variable is categorical with many degrees of freedom, they are not the same, and, in our opinion, using the model significance is preferable.</p>
<p>In general modeling situations where you are not specifically interested in the structure of the feature space, as described by the principal components, then we recommend significance pruning of the variables. As a rule of thumb, we suggest setting your significance pruning threshold based on the rate at which you can tolerate bad variables slipping into the model. For example, setting the pruning threshold at <span>(p=0.05)</span> would let pure noise variables in at the rate of about 1 in 20 in expectation. So a good upper bound on the pruning threshold might be <em>1/nvar</em>, where <em>nvar</em> is the number of variables. We discuss this issue briefly <a target="_blank" href="http://winvector.github.io/vtreathtml/vtreatSignificance.html">here</a> in the <code>vtreat</code> documentation.</p>
<p><code>vtreat</code> does not supply any joint variable dimension reduction as we feel dimension reduction is a modeling task. <code>vtreat</code> is intended to limit itself to only necessary &quot;prior to modeling&quot; processing and includes significance pruning reductions because <a target="_blank" href="http://www.win-vector.com/blog/2014/02/bad-bayes-an-example-of-why-you-need-hold-out-testing/">such pruning can be necessary prior to modeling</a>.</p>
</div>
<div>
<h2>Conclusion</h2>
<p>In our experience, there are two camps of analysts: those who never use principal components regression and those who use it far too often. While principal components analysis is a useful data conditioning method, it is sensitive to distances and geometry. Therefore it is only to be trusted when the variables are curated, pruned, and in appropriate units. Principal components regression should not be used blindly; it requires proper domain aware scaling, initial variable pruning, and posterior component pruning. If the goal is regression many of the purported benefits of principal components regression can be achieved through regularization.</p>
<p>The general principals are widely applicable, and often re-discovered and re-formulated in useful ways (such as <a target="_blank" href="https://en.wikipedia.org/wiki/Autoencoder">autoencoders</a>).</p>
<p>In our next note, we will look at some ways to pick the appropriate number of principal components procedurally.</p>
<div>
<h3>References</h3>
<ul>
<li>
<p>Bair, Eric, Trevor Hastie, Debashis Paul and Robert Tibshirani, &quot;Prediction by Supervised Principal Components&quot;, <em>Journal of the American Statistical Association</em>, Vol. 101, No. 473 (March 2006), pp. 119-137.</p>
</li>
<li>
<p>Hastie, Trevor, Robert Tibshirani, and Jerome Friedman, <em>The Elements of Statistical Learning</em>, 2nd Edition, 2009.</p>
</li>
<li>
<p>Weisberg, Sanford, <em>Applied Linear Regression</em>, Third Edition, Wiley, 2005.</p>
</li>
</ul>
</div>
<p><div><div><div><div data-width="55" data-show-faces="false" data-send="true" data-layout="box_count" data-height="62" data-href="http://www.r-bloggers.com/principal-components-regression-pt-2-y-aware-methods/"></div></div><div><a data-url="http://www.r-bloggers.com/principal-components-regression-pt-2-y-aware-methods/" data-counturl="http://www.r-bloggers.com/principal-components-regression-pt-2-y-aware-methods/" data-count="vertical" data-text="Principal Components Regression, Pt. 2: Y-Aware Methods" class="twitter-share-button" data-via="rbloggers" href="https://twitter.com/share"></a></div><div></div></div><div></div></div>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="http://www.win-vector.com/blog/2016/05/pcr_part2_yaware/"> R – Win-Vector Blog</a></strong>.</div>
<hr>
<a rel="nofollow" href="http://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="http://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a title="The R Project for Statistical Computing" rel="nofollow" href="http://www.r-project.org/">R</a> news and <a title="R tutorials" rel="nofollow" href="http://www.r-bloggers.com/?s=tutorial">tutorials</a> on topics such as: <a title="Data science" rel="nofollow" href="http://www.r-bloggers.com/?s=data%20science">Data science</a>, <a title="Big Data" rel="nofollow" href="http://www.r-bloggers.com/?s=Big%20Data">Big Data, <a title="R jobs" rel="nofollow" href="http://www.r-users.com/">R jobs</a>, visualization (<a title="ggplot and ggplot2 tutorials" rel="nofollow" href="http://www.r-bloggers.com/?s=ggplot2">ggplot2</a>, <a title="Boxplots using lattice and ggplot2 tutorials" rel="nofollow" href="http://www.r-bloggers.com/?s=boxplot">Boxplots</a>, <a title="Maps and gis" rel="nofollow" href="http://www.r-bloggers.com/?s=map">maps</a>, <a title="Animation in R" rel="nofollow" href="http://www.r-bloggers.com/?s=animation">animation</a>), programming (<a title="RStudio IDE for R" rel="nofollow" href="http://www.r-bloggers.com/?s=RStudio">RStudio</a>, <a title="Sweave and literate programming" rel="nofollow" href="http://www.r-bloggers.com/?s=sweave">Sweave</a>, <a title="LaTeX in R" rel="nofollow" href="http://www.r-bloggers.com/?s=LaTeX">LaTeX</a>, <a title="SQL and databases" rel="nofollow" href="http://www.r-bloggers.com/?s=SQL">SQL</a>, <a title="Eclipse IDE for R" rel="nofollow" href="http://www.r-bloggers.com/?s=eclipse">Eclipse</a>, <a title="git and github, Version Control System" rel="nofollow" href="http://www.r-bloggers.com/?s=git">git</a>, <a title="Large data in R using Hadoop" rel="nofollow" href="http://www.r-bloggers.com/?s=hadoop">hadoop</a>, <a title="Web Scraping of google, facebook, yahoo, twitter and more using R" rel="nofollow" href="http://www.r-bloggers.com/?s=Web+Scraping">Web Scraping</a>) statistics (<a title="Regressions and ANOVA analysis tutorials" rel="nofollow" href="http://www.r-bloggers.com/?s=regression">regression</a>, <a title="principal component analysis tutorial" rel="nofollow" href="http://www.r-bloggers.com/?s=PCA">PCA</a>, <a title="Time series" rel="nofollow" href="http://www.r-bloggers.com/?s=time+series">time series</a>, <a title="finance trading" rel="nofollow" href="http://www.r-bloggers.com/?s=trading">trading</a>) and more...
<img height="1" alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/CJ8IO8x4Rm8"></a></div></div></code></center></code></center></div>