主要内容

部分最小二乘回归和主要成分回归

This example shows how to apply partial least squares regression (PLSR) and principal components regression (PCR), and explores the effectiveness of the two methods. PLSR and PCR are both methods to model a response variable when there are a large number of predictor variables, and those predictors are highly correlated or even collinear. Both methods construct new predictor variables, known as components, as linear combinations of the original predictor variables, but they construct those components in different ways. PCR creates components to explain the observed variability in the predictor variables, without considering the response variable at all. On the other hand, PLSR does take the response variable into account, and therefore often leads to models that are able to fit the response variable with fewer components. Whether or not that ultimately translates into a more parsimonious model, in terms of its practical use, depends on the context.

加载数据

Load a data set comprising spectral intensities of 60 samples of gasoline at 401 wavelengths, and their octane ratings. These data are described in Kalivas, John H., "Two Data Sets of Near Infrared Spectra," Chemometrics and Intelligent Laboratory Systems, v.37 (1997) pp.255-259.

加载spectrawhos尼尔辛烷
Name Size Bytes Class Attributes NIR 60x401 192480 double octane 60x1 480 double
[虚拟,h] =排序(辛烷);oldorder = get(gcf,“ DefaultaxesColorOrder”);设置(GCF,“ DefaultaxesColorOrder”,喷气式飞机(60));plot3(repmat(1:401,60,1)',repmat(octane(h),1,401)',nir(h,:)');设置(GCF,“ DefaultaxesColorOrder”,Oldorder);Xlabel('Wavelength Index');ylabel('辛烷值');axis('紧的');网格

图包含一个坐标轴对象。轴对象包含60个类型行的对象。

将数据与两个组件拟合

Use theplsregress拟合具有十个PLS组件和一个响应的PLSR模型的功能。

x = nir;y =辛烷值;[n,p] = size(x);[x loadings,yloadings,Xscores,yscores,betapls10,plspctvar] = plsRegress(。。。x,y,10);

十个组件可能比充分拟合数据所需的十个组件,但是该拟合的诊断可用于选择更少组件的简单模型。例如,选择组件数量的一种快速方法是绘制响应变量中解释的方差百分比作为组件数量的函数。

情节(1:10,暨(100*plspctvar(2,:)),,'-bo');Xlabel(“ PLS组件的数量”);ylabel('Percent Variance Explained in Y');

图包含一个坐标轴对象。轴对象包含一个类型行的对象。

实际上,在选择组件数量时,可能建议更多的护理。例如,交叉验证是一种广泛使用的方法,将在本示例稍后进行说明。目前,以上图表明,具有两个组件的PLSR解释了观察到的大多数差异y。计算两个组件模型的拟合响应值。

[x loadings,yloadings,Xscores,yscores,betapls] = plsRegress(x,y,2);yfitpls = [hons(n,1)x]*betapls;

接下来,拟合具有两个主要组件的PCR模型。第一步是对X, using thePCA功能,并保留两个主要组件。然后,PCR只是这两个组件上响应变量的线性回归。当变量具有非常不同的可变性时,首先通过其标准偏差将每个变量归一化是有意义的,但是在这里没有进行。

[PCALOADINGS,PCASCORE,PCAVAR] = PCA(X,'Economy',错误的);betapcr =回归(y-mean(y),pcascores(:,1:2));

为了使PCR结果更易于根据原始光谱数据解释,请转换为原始未列入变量的回归系数。

betapcr = pcaloadings(:,1:2)*betapcr;betapcr = [平均(y) - 平均值(x)*betapcr;betapcr];yfitpcr = [一个(n,1)x]*betapcr;

Plot fitted vs. observed response for the PLSR and PCR fits.

情节(y,yfitpls,'bo',y,yfitpcr,'r^');Xlabel(“观察到的反应”);ylabel(“合适的响应”);传奇({“ PLSR 2个组件”“带2个组件的PCR”},,。。。'地点','NW');

图包含一个坐标轴对象。轴对象包含2个类型行的对象。These objects represent PLSR with 2 Components, PCR with 2 Components.

从某种意义上说,以上图中的比较不是一个公平的 - 通过查看两个组件PLSR模型预测响应的效果,选择了组件(两个)的数量(两个),并且没有理由为什么PCR模型应仅限于相同数量的组件。但是,对于相同数量的组件,PLSR在适应方面做得更好y。实际上,查看上面图中拟合值的水平散射,具有两个组件的PCR几乎不比使用恒定模型更好。来自两个回归的R平方值证实了这一点。

tss = sum((y-mean(y))。^2);rss_pls = sum((y-yfitpls)。^2);rsquaredpls = 1 -rss_pls/tss
rsquaredpls = 0.9466
rss_pcr = sum((y-yfitpcr)。^2);rsquaredpcr = 1 -rss_pcr/tss
rsquaredpcr = 0.1962

在两种情况下,比较两个模型的预测能力的另一种方法是将响应变量与两个预测变量绘制。

plot3(xscores(::,1),xscores(:,2),y-mean(y),,'bo');传奇('plsr');网格; view(-30,30);

图包含一个坐标轴对象。轴对象包含一个类型行的对象。This object represents PLSR.

It's a little hard to see without being able to interactively rotate the figure, but the PLSR plot above shows points closely scattered about a plane. On the other hand, the PCR plot below shows a cloud of points with little indication of a linear relationship.

plot3(PCASCORE(:,1),PCASCORE(:,2),Y-均值(Y),,,'r^');传奇('PCR');网格; view(-30,30);

图包含一个坐标轴对象。轴对象包含一个类型行的对象。该对象代表PCR。

Notice that while the two PLS components are much better predictors of the observedy,下图表明,它们解释了观察到的差异的差异较小Xthan the first two principal components used in the PCR.

plot(1:10,100*cumsum(PLSPctVar(1,:)),'B-O',1:10,。。。100*cumsum(pcavar(1:10))/sum(pcavar(1:10)),'r-^');Xlabel(“主要组件数量”);ylabel(“ x中解释的百分比差异”);传奇({'plsr''PCR'},,'地点','SE');

图包含一个坐标轴对象。轴对象包含2个类型行的对象。这些对象代表plsr,pcr。

PCR曲线统一的事实表明,为什么使用两个组件的PCR相对于PLSR而做得如此差,这一事实在拟合时做得如此差。y。PCR constructs components to best explainX,结果,前两个组件忽略了数据中重要的信息,这些信息对于拟合观察到的信息很重要y

配合更多组件

As more components are added in PCR, it will necessarily do a better job of fitting the original datay,仅仅是因为在某个时候,大多数重要的预测信息X主要组成部分将存在。例如,下图表明,使用十个组件时,两种方法的残差差异要比两个组件的差异要小得多。

yfitPLS10 = [ones(n,1) X]*betaPLS10; betaPCR10 = regress(y-mean(y), PCAScores(:,1:10)); betaPCR10 = PCALoadings(:,1:10)*betaPCR10; betaPCR10 = [mean(y) - mean(X)*betaPCR10; betaPCR10]; yfitPCR10 = [ones(n,1) X]*betaPCR10; plot(y,yfitPLS10,'bo',y,yfitpcr10,'r^');Xlabel(“观察到的反应”);ylabel(“合适的响应”);传奇({'PLSR with 10 components''PCR with 10 Components'},,。。。'地点','NW');

图包含一个坐标轴对象。轴对象包含2个类型行的对象。这些对象代表具有10个组件的PLSR,具有10个组件的PCR。

两种模型都合适yfairly accurately, although PLSR still makes a slightly more accurate fit. However, ten components is still an arbitrarily-chosen number for either model.

Choosing the Number of Components with Cross-Validation

在预测预测变量的未来观察结果时,选择组件数量以最小化预期误差通常很有用。简单地使用大量组件将在适应当前观察到的数据方面做得很好,但这是导致过度拟合的策略。将当前数据拟合得太好了,导致无法很好地推广到其他数据的模型,并对预期误差进行了过于愉快的估计。

交叉验证是一种更统计上的合理方法,用于选择PLSR或PCR中的组件数量。它通过不重用相同的数据既适合模型又估算预测错误来避免过度拟合数据。因此,预测误差的估计不会在向下偏差。

plsregresshas an option to estimate the mean squared prediction error (MSEP) by cross-validation, in this case using 10-fold C-V.

[XL,YL,XS,YS,BETA,PCTVAR,PLSMSEP] = PLSREGRESS(x,y,10,10,'CV',10);

对于PCR,crossvalcombined with a simple function to compute the sum of squared errors for PCR, can estimate the MSEP, again using 10-fold cross-validation.

pcrmsep = sum(crossVal(@pcrsse,x,y,'kfold',10),1) / n;

PLSR的MSEP曲线表明两个或三个组件的作用尽可能出色。另一方面,PCR需要四个组件才能获得相同的预测准确性。

情节(0:10,plsmsep(2,:),,,'B-O',0:10,pcrmsep,'r-^');Xlabel(“组件数量”);ylabel('Estimated Mean Squared Prediction Error');传奇({'plsr''PCR'},,'地点','ne');

图包含一个坐标轴对象。轴对象包含2个类型行的对象。这些对象代表plsr,pcr。

实际上,PCR中的第二个成分increases模型的预测误差表明该组件中包含的预测变量的组合与y。同样,这是因为PCR构造了组件来解释变化X, 不是y

模型简约

因此,如果PCR需要四个组件才能获得与具有三个组件的PLSR相同的预测准确性,那么PLSR模型是否会更加简单?这取决于您考虑的模型的哪个方面。

The PLS weights are the linear combinations of the original variables that define the PLS components, i.e., they describe how strongly each component in the PLSR depends on the original variables, and in what direction.

[XL,YL,XS,YS,BETA,PCTVAR,MSE,STATS] = PLSREGRESS(X,Y,3);情节(1:401,stats.w,“- - -”);Xlabel('多变的');ylabel('PLS Weight');传奇({'第一组件''第二组件''3rd Component'},,。。。'地点','NW');

图包含一个坐标轴对象。轴对象包含3个类型行的对象。这些对象代表第一组件,第二组件,第三个组件。

同样,PCA载荷描述了PCR中每个组件的强大程度取决于原始变量。

plot(1:401,PCALoadings(:,1:4),“- - -”);Xlabel('多变的');ylabel(“ PCA加载”);传奇({'第一组件''第二组件''3rd Component'。。。“第四组件”},,'地点','NW');

图包含一个坐标轴对象。轴对象包含4个类型行的对象。这些对象代表第一组件,第二组件,第三个组件,第四组件。

对于PLSR或PCR,可以通过检查哪些变量最大的变量来给出每个组件的物理有意义的解释。例如,使用这些光谱数据,可以用汽油中存在的化合物来解释强度峰,然后观察到特定成分的权重挑选出少数这些化合物。从这个角度来看,较少的组件更简单地解释,并且由于PLSR通常需要更少的组件来充分预测响应,因此它会导致更简约的模型。

另一方面,PLSR和PCR均导致每个原始预测变量的回归系数,以及截距。从这个意义上讲,这两个模型都取决于所有预测因子,这两个模型都不是更简约的,因为无论使用多少个组件。更具体地说,对于这些数据,两个模型都需要401个光谱强度值才能进行预测。

However, the ultimate goal may to reduce the original set of variables to a smaller subset still able to predict the response accurately. For example, it may be possible to use the PLS weights or the PCA loadings to select only those variables that contribute most to each component. As shown earlier, some components from a PCR model fit may serve primarily to describe the variation in the predictor variables, and may include large weights for variables that are not strongly correlated with the response. Thus, PCR can lead to retaining variables that are unnecessary for prediction.

对于此示例中使用的数据,PLSR和PCR用于准确预测所需的组件数量的差异不是很好,并且PLS权重和PCA负载似乎挑选了相同的变量。对于其他数据,这可能不是正确的。

也可以看看

|

相关话题