PLS Procedure Examples and Analysis
PLS Procedure Examples and Analysis
Table of Contents
Introduction
The following example, from Umetrics (1995), demonstrates the use of partial
least squares in drug discovery. New drugs are developed from chemicals that
are biologically active. Testing a compound for biological activity is an expensive
procedure, so it would be useful to be able to predict biological activity from other
cheaper chemical measurements. In fact, computational chemistry makes it possible
to calculate certain chemical measurements without even making the compound.
These measurements include size, lipophilicity, and polarity at various sites on the
molecule. The SAS statements to create a SAS data set named PENTA containing
these data are given in Appendix 1.
You would like to study the relationship between these measurements and the activity
of the compound, represented by the logarithm of the relative Bradykinin activating
activity (log RAI). Notice that these data consist of many predictors relative to
the number of observations. Partial least squares is especially appropriate in this
situation as a useful tool for finding a few underlying predictors that account for
most of the variation in the response. Typically, the model is fit for part of the data
(the training set), and the quality of the fit is judged by how well it predicts the other
part of the data (the prediction set). For this example, the first fifteen observations
serve as the training set and the rest constitute the test set (refer to Ufkes et al. 1978,
1982).
4 ❒ ❒ ❒ Examples Using the PLS Procedure
/*********************************************************/
/ Select the first 15 observations for the training set /
/ from the original data set, PENTA. /
/*********************************************************/
/*********************************************************/
/ Set Parameters for Macros /
/*********************************************************/
/*********************************************************/
/ Fit the PLS model /
/*********************************************************/
The PLS procedure displays a table, shown in Output 1.1, showing how much
First PLS Model ❒ ❒ ❒ 5
Number of
Latent Model Effects Dependent Variables
Variables Current Total Current Total
----------------------------------------------------------
1 16.9014 16.9014 89.6399 89.6399
2 12.7721 29.6735 7.8368 97.4767
The procedure also produces two data sets: the EST1 data set containing information
on the model fit, and the OUTPLS data set containing predictions, residuals, scores,
and other information.
From Output 1.1, note that 97% of the response variation is already explained, but
only 29% of the predictor variation is explained.
The PLS model has the form
X = TP
0 + E ; and
Y = 0
UQ + F
where X and Y are the matrices of predictors and responses. The matrices on the
right-hand side of this model are defined by
T = X-scores U = Y-scores
P = X-loadings Q = Y-loadings
E = X-residuals F = Y-residuals
Partial least squares algorithms choose successive orthogonal factors that maximize
the covariance between each X-score and the corresponding Y-score. For a good PLS
model, the first few factors show a high correlation between the X- and Y-scores.
The correlation usually decreases from one factor to the next. You can plot the
X-scores T versus the corresponding Y-scores U using the following macro call.
%plot_scr(outpls);
The plots for these data appear in Figures 2 and 3. The numbers on the plot represent
the observation number in the PENTAPEP data set, which appears in Appendix 1.
For this example, the figures show high correlation between X- and Y-scores for the
first component but somewhat looser correlation for the second component.
You can also plot the X-scores against each other to look for irregularities in the data.
You should look out for patterns or clearly grouped observations. If you see a curved
pattern, for example, you may want to add a quadratic term. Two or more groupings
of observations indicate that it might be better to analyze the groups separately. The
following macro call produces plots of scores for consecutive PLS components for
as many components as desired, up to the number of components fit.
6 ❒ ❒ ❒ Examples Using the PLS Procedure
%plotxscr(outpls,max_lv=2);
The plot of the first and second X-scores is shown in Figure 4. This plot appears to
show most of the observations close together, with a few being more spread out with
larger positive X-scores for component 2. Observation 13 stands out the most and
has been the most extreme on all three plots so far. This run may be influential in the
PLS analysis, and thus you should check to make sure it is reliable. There are not
any distinct grouping patterns.
Plots of the weights give the directions toward which each PLS factor projects. They
show which predictors are most represented in each factor. Those predictors with
small weights are less important than those with large weights in absolute value.
The X-weights W represent the correlation between the X-variables and the Y-scores
First PLS Model ❒ ❒ ❒ 7
U. The Y-loadings Q represent the correlation between the Y-variables and the
X-scores T . The X-loadings P represent the directions of the lines u = b0t in the
X-space. The X-loadings and X-weights are usually very similar to each other.
You can produce these plots with the following macro calls.
/*********************************************************/
/ Compute the X-weights for each PLS component /
/*********************************************************/
%get_wts(est1,dsxwts=xwts);
/*********************************************************/
/ Plot X-weights w1 and w2 for the two components /
/*********************************************************/
%plot_wt(xwts,max_lv=2);
/*********************************************************/
/ Compute X-loadings p1-p2 for the two components /
/*********************************************************/
%getxload(est1,dsxload=xloads);
/*********************************************************/
/ Plot X-loadings p1 and p2 for the two components /
/*********************************************************/
%pltxload(xloads,max_lv=2);
The plot of the X-weights is shown in Figure 5. The plot of the X-loadings, which is
similar, is not shown.
The weights plot shows a cluster of X-variables that are weighted at nearly zero for
8 ❒ ❒ ❒ Examples Using the PLS Procedure
both components. These variables add little to the model fit and removing them may
improve the model’s predictive capability.
Residual plots and normal quantile plots help in detecting outliers that might be
harming the fit; these plots also help in detecting nonnormality, autocorrelations,
and heteroscedasticity, all of which can cause various problems in constructing
confidence and tolerance bounds for predictions. The ideal residual plot looks like
a rectangular point cloud with a majority of the points falling in the vertical middle
third of the plot. In an ideal normal plot, the points fall on a straight line. You can
produce the plot of residuals versus predicted values with the %res_plot macro
and the normal quantile plot of the residuals with the %nor_plot macro for each
response variable.
%res_plot(outpls);
%nor_plot(outpls);
%get_bpls(est1,dsout=bpls);
%get_vip(est1,dsvip=vip_data);
data eval;
merge bpls vip_data;
run;
1 S1 -0.13831 0.61108
2 L1 0.05720 0.31822
3 P1 -0.19064 0.75127
4 S2 0.12383 0.50482
5 L2 0.05909 0.27123
6 P2 0.09361 0.35927
7 S3 -0.28415 1.57775
8 L3 0.47131 2.43480
9 P3 0.26613 1.13222
10 S4 -0.09145 1.22255
11 L4 0.12265 1.17994
12 P4 -0.04878 0.88380
13 S5 0.03320 0.21288
14 L5 0.03320 0.21288
15 P5 -0.03320 0.21288
For this data set, the variables L1, L2, P2, P4, S5, L5, and P5 have small absolute
coefficients and small VIP, so they are dropped from the analysis.
Looking back at the loadings plot, you can see that these variables tend to be the
ones near zero for both PLS components.
/*********************************************************/
/ Refit the PLS model with 7 X-variables deleted /
/*********************************************************/
/*********************************************************/
/ Plot a normal quantile plot of the residuals /
/ (for comparison to the original fit). /
/*********************************************************/
%nor_plot(outpls1b);
Reduced Model Analysis ❒ ❒ ❒ 11
/*********************************************************/
/ Plot the X-scores vs. Y-scores for each component. /
/*********************************************************/
%plot_scr(outpls1b);
The printed output from the PLS procedure appears in Output 1.3, the normal quantile
plot appears in Figure 8 and the plot of the second X and Y-scores against each other
appears in Figure 9.
Output 1.3. Amount of Training Set Variation Explained (Reduced Model)
Number of
Latent Model Effects Dependent Variables
Variables Current Total Current Total
----------------------------------------------------------
1 28.5163 28.5163 90.7983 90.7983
2 19.1750 47.6913 5.9785 96.7769
When the model is fit with the remaining eight predictors, the R-squared values for
X improve to 47% for two PLS components. See Output 1.3.
So if you drop predictors that appear to be the least related to Y, you find that the
PLS factors are better represented by the data in the remaining X-space. Note that the
normal quantile plot (Figure 8) is closer to being linear than previously (Figure 7).
You can also see in Figure 9 that the correlation between the X- and Y-scores for the
second component appears stronger.
Another way to check for outliers in the model is to look at the Euclidean distance
from each point to the PLS model in both X and Y. No point should be dramatically
12 ❒ ❒ ❒ Examples Using the PLS Procedure
farther from the model than the rest. If there is a group of points that are all farther
from the model than the rest, it may be that they have something in common and
should be analyzed separately. The following statements compute and plot these
distances to the model, which Umetrics (1995) call DModX and DModY.
%get_dmod(outpls1b,dsdmod=distmd1b,qresname=qres,id=n);
Figure 10: Distances from the X-variables to the Model (Training Set)
Figure 11: Distances from the Y-variables to the Model (Training Set)
/*********************************************************/
/ Refit the model with missing values at the points /
/ to be predicted. /
/*********************************************************/
run;
/*********************************************************/
/ Put the predicted values and actual observations in /
/ the same data set. /
/*********************************************************/
/*********************************************************/
/ Calculate the residuals at the points in the test set. /
/*********************************************************/
/*********************************************************/
/ Compare the test set and training set residuals. /
/*********************************************************/
%res_plot(predict);
Figure 12 displays the plot. You can also print out the predictions in the PREDICT
data set, but these are not displayed here.
Figure 12: Residuals for all Observations Based on Model for Training Set
In Figure 12, the residuals for observations 16-30 calculated based on predictions
from observations 1-15 appear to have a slight systematic pattern.
Observations 27 and 29 stand out the most, and in general it appears that the
Conclusion ❒ ❒ ❒ 15
model is slightly underpredicting the Y-activity when it predicts low activity and
overpredicting it when it predicts high activity.
To see if the new observations are representative of the model for X, you can call the
%get_dmod macro again and plot the distances.
%get_dmod(outpls2,dsdmod=distmod2,qresname=qres,id=n);
Figure 13: Distances from the X-variables to the Model (All Data)
In Figure 13, the distances of observations 16-30 to the PLS model for the predictors
are much larger on average than the distances for the first 15 observations.
This indicates that the X-values for the first 15 are not as representative of the second
15 as you would like and it helps explain the problems in prediction.
Conclusion
In this example, partial least squares provided an effective method for predicting
the chemical activity of a penta-peptide by taking only eight total measurements
of size, lipophilicity, or polarity. Two underlying factors based on these quantities
accounted for almost all of the variation in the response and provided a good model
for predicting responses in the prediction set.
16 ❒ ❒ ❒ Examples Using the PLS Procedure
Number of
Latent Model Effects Dependent Variables
Variables Current Total Current Total
----------------------------------------------------------
1 97.4607 97.4607 41.9155 41.9155
2 2.1830 99.6436 24.2435 66.1590
The cross-validation results in Output 2.1 show that the procedure selected a model
with two PLS components (latent variables) because that is the simplest model with
First Model Fit ❒ ❒ ❒ 19
a PRESS statistic that is insignificantly different from the absolute minimum PRESS
value. Output 2.2 shows that the PLS model explains more than 99% of the variation
in predictors and about 66% of the variation in responses. If you had not used the
CVTEST option, the procedure would have fit a model with seven PLS components
instead of two.
To check the quality of the model, you can check to see if the X-scores and respective
Y-scores are highly correlated using the following command.
%plot_scr(outpls);
The plots appear in Figures 14 and 15. Recall that the numbers on the plots refer to
the observation numbers in the MSWKAL data set, given in Appendix 1.
From these plots, you can see that the X- and Y-scores are highly correlated for the
first two PLS components, indicating a good model. To check for irregularities in the
predictors, such as outliers or distinct groupings, you can plot the X-scores against
each other using the following statements.
%plotxscr(outpls);
The plot of the first and second X-scores is shown in Figure 16. The plot of X-scores
shows no irregularities.
To see which predictors are most dominant in each factor, you can plot the weights
and loadings across the range of predictors. Since the predictors are frequencies, it
makes sense to plot the weights and loadings across frequencies rather than against
each other. You can use the following statements to generate these plots.
/*********************************************************/
/ Compute the X-Weights for each PLS component /
/*********************************************************/
%get_wts(est1,dsxwts=xwts);
/*********************************************************/
/ Plot the X-weights vs. the frequency on the same axes /
/*********************************************************/
%pltwtfrq(xwts,plotyvar=w,plotxvar=n,max_lv=&lv,
label=Weight);
First Model Fit ❒ ❒ ❒ 21
/*********************************************************/
/ Compute X-loadings p1-p2 for the two components /
/*********************************************************/
%getxload(est1,dsxload=xloads);
/*********************************************************/
/ Plot the X-loadings for each component vs. frequency /
/*********************************************************/
%pltwtfrq(xloads,plotyvar=p,plotxvar=n,max_lv=&lv,
label=Loading);
Figure 17 displays the weight plot across frequencies. The loadings plot looks very
similar.
The plot shows a fairly constant weight across frequencies for the first PLS
component, revealing that the integral of the spectrogram is the most important
predictor. For the second component, the weights increase as the frequency
increases. The second component is a smoothed contrast between frequencies below
and above 9 or so.
The X-loadings give the combination of predictors that comprise each PLS com-
ponent. In the same way, you can examine the Y-loadings to see how each
PLS component represents the responses. The following statements compute the
Y-loadings and then plot them for each PLS component.
/*********************************************************/
/ Compute Y-loadings q1-q2 for the two components /
/*********************************************************/
%getyload(est1,dsyload=yloads);
22 ❒ ❒ ❒ Examples Using the PLS Procedure
/*********************************************************/
/ Plot the Y-loadings vs. the PLS components /
/*********************************************************/
%plt_y_lv(est1);
The plots show that the first component is based mainly on LS, with some emphasis
on the other two responses. The second component emphasizes DT and, to a lesser
extent, LS.
To see which frequencies are important, you can look at the B(PLS) regression
coefficient matrix and at the Variable Importance for the Projection (VIP). Since the
predictors are ordered, it makes sense to plot VIP and B(PLS) against them. It also
may help visually to standardize the regression coefficients. You can produce these
plots with the following statements.
%get_bpls(est1,dsout=bpls);
%plt_bpls(bpls);
/*********************************************************/
/ Standardize the PLS regression coefficients /
/*********************************************************/
/*********************************************************/
/ Plot the standardized PLS regression coefficients /
/*********************************************************/
%plt_bpls(bpls);
/*********************************************************/
/ Get VIP and plot it across frequencies /
/*********************************************************/
%get_vip(est1,dsvip=vip_data);
%plot_vip(vip_data);
The standardized coefficient and VIP plots appear in Figures 19 and 20.
When you standardize to take into account location and scale differences in the
responses, the resulting coefficient plot (Figure 19) shows very interesting relation-
ships. The predictions for standardized LS and HA are essentially the same linear
combination of predictors while the prediction for standardized DT is close to the
negative of that linear combination.
The VIP plot shows that all frequencies are important, as the VIP is uniformly larger
than 0.8.
Figure 20: Variable Importance for the Projection for each Frequency
/*********************************************************/
/ Refit the model with missing values at the points /
/ to be predicted. /
/*********************************************************/
/*********************************************************/
/ Put the predicted values and actual observations in /
/ the same data set. /
/*********************************************************/
/*********************************************************/
/ Calculate the residuals at the points in the test set. /
/*********************************************************/
/*********************************************************/
/ Compare the test set and training set residuals. /
/*********************************************************/
%res_plot(predict);
The residual plots appear in Figures 21, 22, and 23. The printed output is omitted.
You can see from the residual plot that the model predicts observation 17 very well,
but it predicts observation 18 very poorly. Observation 18 could be an outlier, or it
could be that observation 18 is just far from the other observations in terms of X.
Note also that for all observations, modeling of DT is less successful than it is for
the other two responses. However, if you add more PLS components, it does not
to help model DT significantly better, and it makes the prediction of observation 18
even worse.
To discern why the model doesn’t fit observation 18 well, you can calculate the
distance between the observation and the model for the predictors. The following
statements calculate and plot these distances for each observation.
%get_dmod(outpls2,dsdmod=d_mod,qresname=qres,id=n);
26 ❒ ❒ ❒ Examples Using the PLS Procedure
Conclusion
This example demonstrates that partial least squares enables you to calibrate an
instrument to estimate concentrations of chemical compounds based on the spectro-
graph readings that the sample produces. For this example, you can estimate the
amounts of LS, HA, and DT based on linear combinations of spectrograph readings
at the 27 frequencies, provided the readings are reasonably close to the model for the
original 16 observations.
28 ❒ ❒ ❒ Examples Using the PLS Procedure
%let resname=res;
%let xscrname=xscr;
%let yscrname=yscr;
%let num_y=3;
%let num_x=30;
/****************************************************/
/ Fit the PLS model to observations 15-32 /
/****************************************************/
Number of
Latent Model Effects Dependent Variables
Variables Current Total Current Total
----------------------------------------------------------
1 81.1648 81.1648 48.3383 48.3383
2 16.8119 97.9768 32.5471 80.8854
3 1.7639 99.7406 11.4465 92.3320
4 0.1951 99.9357 3.8334 96.1654
5 0.0276 99.9633 1.6857 97.8510
6 0.0132 99.9765 0.7245 98.5755
First Model Fit ❒ ❒ ❒ 31
You can see from the output that PROC PLS selected a model with six PLS
components (latent variables) that explain nearly all of the variation in both predictors
and responses. Actually, the first three components capture most of the variation, so
it would be good to keep this in mind when doing the analysis.
To check for possible improvements in the model, you can use the following
statements to examine plots of Y-scores versus the corresponding X-scores.
%plot_scr(outpls);
The plots for the first three PLS components appear in Figures 25, 26 and 27. Recall
that the numbers on the plot represent observation numbers in the data set.
In Figures 25-27, notice the interesting patterns formed by the scores. Recall that
observations 15-27 all have the same total concentration and observations 28-32
32 ❒ ❒ ❒ Examples Using the PLS Procedure
also have the same total concentration. Each group forms a distinctive pattern due
to the fact that within each group tyrosine gradually increases while the tryptophan
concentration gradually decreases from one observation to the next.
You can see from the score plots that the first three components have considerably
higher correlated X- and Y-scores, as the R-square table suggested earlier. The score
plot for the third component hints at curvature. You can test for curvature by taking
the third X- and Y-scores from the PLS OUTPUT data set and fitting a regression of
the Y-score on the X-score with a quadratic term in X.
The output from the GLM procedure (not shown) reveals that there is a statistically
significant quadratic relationship, but incorporating this into the model changes very
little; thus, quadratic terms in the frequencies are not added to the model.
You can plot as many pairs of consecutive X-scores against each other as you would
like by calling the %plotxscr macro and specifying the MAX LV parameter to be
the last PLS component to be included in a plot. For example, if MAX LV=3, the
macro generates plots for X-score 2 versus X-score 1 and X-score 3 versus X-score
2.
%plotxscr(outpls,max_lv=3);
The plots are shown in Figures 28 and 29. The pattern between X-scores 1 and 2
again shows the two groups based on the total concentration and the pattern due to the
increasing proportion of tyrosine (TYR) in the mix. You might consider analyzing
the two groups separately, but this would further limit the applicability of the model
to differing amounts of total concentration.
First Model Fit ❒ ❒ ❒ 33
X-scores 2 and 3 form an interesting pattern in Figure 29, but observation 20 appears
to deviate from it. This indicates it might be worthwhile to check observation 20 for
accuracy. To study the source of the patterns in the score plots, you can plot the
residuals versus the predicted values and a normal quantile plot of the residuals using
the following macro calls.
%res_plot(outpls);
%nor_plot(outpls);
The three plots of the residuals versus predicted values appear in Figures 30, 31, and
32, while the three normal quantile plots appear in Figures 33, 34, and 35.
The plot of residuals versus predicted values for the first response (TOT LOG) looks
granular, but this happens because there are only two values for TOT LOG.
34 ❒ ❒ ❒ Examples Using the PLS Procedure
The residual versus predicted value plots for TYR LOG and TRY LOG in Figures 31
and 32 show that the residuals may be heteroscedastic. In this case, it appears that
there is less variability in TYR LOG and TRY LOG for higher relative concentrations
of TYR and TRY, respectively. Also, the variability seems to decrease when the
total concentration increases.
In the normal quantile plot for TOT LOG, observations 15, 20, 29 and 32 do not fit
the pattern of the rest of the observations. Observations 15 and 20 do not fit well in
the normal plot for the TYR LOG residuals either. In observation 15, the amino acid
is pure tryptophan, so it is not surprising that the residual for tyrosine is nonnormal.
The normal plot for the TRY LOG residuals looks fine.
Since the normal plots indicate possible outliers for several observations, it might
be useful to look at the distance of each observation from the model. The following
statements produce the appropriate plots.
First Model Fit ❒ ❒ ❒ 35
%get_dmod(outpls,dsdmod=d_mod,qresname=qres,id=n);
The plots appear in Figures 36 and 37. In the figures, no observation stands out from
the others in terms of distance from the model in either X or Y.
36 ❒ ❒ ❒ Examples Using the PLS Procedure
When the score plots reveal irregularities, the loadings plots are especially useful for
diagnosing problems. First, you can plot the weights and loadings for the predictors.
Because the predictors are ordered by frequency, it makes sense to plot the weights
and loadings versus frequency for each PLS component. You can do this using the
following statements.
/*********************************************************/
/ Compute the X-Weights for each PLS component /
/*********************************************************/
%get_wts(est1,dsxwts=xwts);
First Model Fit ❒ ❒ ❒ 37
/*********************************************************/
/ Plot the X-weights vs. the frequency on the same axes /
/*********************************************************/
%pltwtfrq(xwts,plotyvar=w,plotxvar=n,max_lv=&lv,
label=Weight);
/*********************************************************/
/ Compute X-loadings p1-p6 for the six components /
/*********************************************************/
%getxload(est1,dsxload=xloads);
38 ❒ ❒ ❒ Examples Using the PLS Procedure
/*********************************************************/
/ Plot the X-loadings for each component vs. frequency /
/*********************************************************/
%pltwtfrq(xloads,plotyvar=p,plotxvar=n,max_lv=&lv,
label=Loading);
The plot of the X-loadings versus the frequency appears in Figure 38. The X-weights
plot is very similar.
The loadings plot shows that the PLS model gives somewhat larger importance to
the lower frequencies. However, it does give nonzero weight to all frequencies.
Note from the figure that the loading curves are much bumpier for components 4-6
than for components 1-3. This raises the possibility that components 4-6 are just
modeling noise. Recall that the R-square table showed much smaller improvements
to the fit for components 4-6.
This plot may seem somewhat cluttered, especially in black and white. If you want
to see the plot of loadings for only the first three PLS factors, you can reinvoke the
%pltwtfrq macro with MAX LV=3.
The X-loadings plot appears to indicate that the lower frequencies are the most
important for the model. To further study how the frequencies contribute to the
model, you can plot the PLS coefficients and the VIP using the following statements.
%get_bpls(est1,dsout=bpls);
First Model Fit ❒ ❒ ❒ 39
/*********************************************************/
/ Standardize the PLS regression coefficients /
/*********************************************************/
/**********************************************************
/ Plot the standardized PLS regression coefficients /
**********************************************************/
%plt_bpls(bpls);
/**********************************************************
/ Get VIP and plot it across frequencies /
**********************************************************/
%get_vip(est1,dsvip=vip_data);
%plot_vip(vip_data);
Figures 39 and 40 show that the first ten frequencies have the most impact on the
model, while the highest frequencies have slightly more impact than the middle
frequencies. The coefficients for each of the three responses form a fairly bumpy
curve, indicating again that partial least squares regression may be attempting to
40 ❒ ❒ ❒ Examples Using the PLS Procedure
Figure 40: Variable Importance for the Projection for each Frequency
model noise.
The R-square table, the X-weights plot, and the PLS coefficients plot have all given
evidence that the model is overfit, which means it fits the observations used in
modeling well but will predict new observations poorly. To check this, you can use
the model to predict observations 1-14 and 33.
/*********************************************************/
/ Refit the model with missing values at the points /
/ to be predicted. /
/*********************************************************/
run;
/*********************************************************/
/ Put the predicted values and actual observations in /
/ the same data set. /
/*********************************************************/
/*********************************************************/
/ Calculate the residuals at the points in the test set. /
/*********************************************************/
/*********************************************************/
/ Compare the test set and training set residuals. /
/*********************************************************/
%res_plot(predict);
The residual plots for the three responses for all observations based on the model for
observations 15-32 appear in Figures 41, 42, and 43.
The residual plots for the second and third responses, TYR LOG and TRY LOG, show
much more variability in predicting the new observations than in predicting those
observations used in modeling. This indicates that the model for observations 15-32
may not apply to observations 1-14 and 33. The distances of the new observations to
the PLS model for the predictors illuminate this further, seen in the plots produced
by the following statements.
%get_dmod(outpls2,dsdmod=d_mod,qresname=qres,id=n);
The plot shows that the new observations are much farther from the model than the
first set of observations. So now the question is, how can you improve this model?
Recall that the improvement in the R-square for the responses tailed off considerably
after the third PLS component, even though cross-validation recommended a six-term
model. Also recall the evidence from the weights and loadings plots, as well as the
regression coefficients, which indicated that components 4-6 may be modeling noise.
Thus, a natural approach would be to fit a PLS model with three components.
Second PLS Model ❒ ❒ ❒ 43
The output is the same as the first three components in Output 3.2
44 ❒ ❒ ❒ Examples Using the PLS Procedure
You can study the X-loadings for each PLS component across frequencies by again
plotting the loadings with the following commands.
%getxload(est3,dsxload=xloads3);
%pltwtfrq(xloads3,plotyvar=p,plotxvar=n,max_lv=&lv,
label=Loading);
The loadings plot appears in Figure 45. Note that the first PLS component appears
to contrast frequencies 1-7 with the remaining ones. The second component appears
to represent the weighted average of the first 10 frequencies. The third component
appears to be a contrast between frequencies 1-5 and 6-10 or so.
You can judge the impact of simplifying the model on the PLS coefficients by
looking at the coefficient plot. The following statements plot these coefficients and
also plot the Variable Importance for the Projection (VIP) for the new model.
/*********************************************************/
/ Get B(PLS), the matrix of regression coefficients /
/*********************************************************/
%get_bpls(est3,dsout=bpls3);
/*********************************************************/
/ Standardize the PLS regression coefficients /
/*********************************************************/
run;
/*********************************************************/
/ Plot the standardized PLS regression coefficients /
/*********************************************************/
%plt_bpls(bpls3);
/*********************************************************/
/ Get VIP and plot it across frequencies /
/*********************************************************/
%get_vip(est3,dsvip=vipdata3);
%plot_vip(vipdata3);
The coefficient and VIP plots appear in Figures 46 and 47, respectively.
Notice the dramatic difference in the coefficient plot by comparing it to the one
generated in the six-component model. It is much smoother than before. The VIP
plot shows that the new model still emphasizes the lower frequencies but uses all
frequencies, since VIP is larger than the 0.8 cutoff of Wold (1994).
To see how well this three-term model predicts new observations, you can redo the
predictions and plot the residuals.
/**********************************************************/
/ Redo predictions again /
/**********************************************************/
%res_plot(predict2);
The three residual plots for all observations based on the three-factor PLS model for
observations 15-32 appear in Figures 48, 49, and 50.
The residual plot for TOT LOG looks about the same, but those for TYR LOG and
TRY LOG show that this model does a much better job of predicting observations
1-14 and 33, which include observations with smaller total concentration than those
in the set used for the model fit.
On the TYR LOG (response 2) residual plot, observations 1 and 8 are more outlying
than the rest. However, it is interesting that these are the observations that contain no
Conclusion ❒ ❒ ❒ 47
Figure 48: Second Residual Plot of TOT LOG for all Observations
Figure 49: Second Residual Plot of TYR LOG for all Observations
tyrosine, the quantity you are trying to predict. The same problem shows up on the
plot for TRY LOG (response 3). Here, new observations 7, 14, and 33 have outlying
residuals, but note that they are the observations with no tryptophan. Observation 27
comes from the set used for the model, but it also contains no tryptophan.
Conclusion
This example demonstrates that although cross-validation helps in selecting the
number of PLS components, you should not use it blindly. The model recommended
by the cross-validation test overfit the data and failed to predict new observations
well. However, the R-square table, the X-weight plot, and the coefficient plot all
were useful in diagnosing overfitting as a possible problem.
The model with three PLS components predicted new observations well, even though
48 ❒ ❒ ❒ Examples Using the PLS Procedure
Figure 50: Second Residual Plot of TRY LOG for all Observations
the new samples (except observation 33) had much lower total concentration. The
only exceptions were the cases where there was no tyrosine or no tryptophan. In
those cases it predicted a nonzero quantity for the given amino acid. Overall, the
three-term model does very well.
References ❒ ❒ ❒ 49
References
Lindberg, W., Persson, J-A., and Wold, S. (1983), ‘‘Partial Least-Squares Method
for Spectrofluorimetric Analysis of Mixtures of Humic Acid and Ligninsulfonate’’
Analytical Chemistry 55, 643–648.
McAvoy, T. J., Wang, N. S., Naidu, S., Bhat, N., Gunter, J., and Simmons, M. (1989),
‘‘Interpreting Biosensor Data via Backpropagation,’’ International Joint Conference
on Neural Networks, 1, 227–233.
Shao, J. (1993), ‘‘Linear Model Selection by Cross-Validation,’’ Journal of the
American Statistical Association, 88, 486–494.
Tobias, R. (1995), ‘‘An Introduction to Partial Least Squares Regression,’’ in
Proceedings of the Twentieth Annual SAS Users Group International Conference,
Cary, NC: SAS Institute Inc., 1250–1257.
Ufkes, J. G. R., Visser, B. J., Heuver, G., and Van Der Meer, C. (1978), ‘‘Structure-
Activity Relationships of Bradykinin-Potentiating Peptides,’’ European Journal of
Pharmacology, 50, 119.
Ufkes, J. G. R., Visser, B. J., Heuver, G., Wynne, H. J., and Van Der Meer, C. (1982),
‘‘Further Studies on the Structure-Activity Relationships of Bradykinin-Potentiating
Peptides,’’ European Journal of Pharmacology, 79, 155.
Umetrics, Inc. (1995), Multivariate Analysis (3-day course), Winchester, MA.
Wold, S. (1994), ‘‘PLS for Multivariate Linear Modeling,’’ QSAR: Chemometric
Metods in Molecular Design. Methods and Principles in Medicinal Chemistry. (Ed.
H. van de Waterbeemd), Weinheim, Germany: Verlag- Chemie.
50 ❒ ❒ ❒ Examples Using the PLS Procedure
data penta;
input n obsnam $ S1 L1 P1 S2 L2 P2 S3 L3 P3 S4 L4 P4
S5 L5 P5 log_RAI @;
datalines;
1 VESSK -2.6931 -2.5271 -1.2871 3.0777 0.3891 -0.0701
1.9607 -1.6324 0.5746 1.9607 -1.6324 0.5746 2.8369
1.4092 -3.1398 0
2 VESAK -2.6931 -2.5271 -1.2871 3.0777 0.3891 -0.0701
1.9607 -1.6324 0.5746 0.0744 -1.7333 0.0902 2.8369
1.4092 -3.1398 0.28
3 VEASK -2.6931 -2.5271 -1.2871 3.0777 0.3891 -0.0701
0.0744 -1.7333 0.0902 1.9607 -1.6324 0.5746 2.8369
1.4092 -3.1398 0.2
4 VEAAK -2.6931 -2.5271 -1.2871 3.0777 0.3891 -0.0701
0.0744 -1.7333 0.0902 0.0744 -1.7333 0.0902 2.8369
1.4092 -3.1398 0.51
5 VKAAK -2.6931 -2.5271 -1.2871 2.8369 1.4092 -3.1398
0.0744 -1.7333 0.0902 0.0744 -1.7333 0.0902 2.8369
1.4092 -3.1398 0.11
6 VEWAK -2.6931 -2.5271 -1.2871 3.0777 0.3891 -0.0701
-4.7548 3.6521 0.8524 0.0744 -1.7333 0.0902 2.8369
1.4092 -3.1398 2.73
7 VEAAP -2.6931 -2.5271 -1.2871 3.0777 0.3891 -0.0701
0.0744 -1.7333 0.0902 0.0744 -1.7333 0.0902 -1.2201
0.8829 2.2253 0.18
8 VEHAK -2.6931 -2.5271 -1.2871 3.0777 0.3891 -0.0701
2.4064 1.7438 1.1057 0.0744 -1.7333 0.0902 2.8369
1.4092 -3.1398 1.53
9 VAAAK -2.6931 -2.5271 -1.2871 0.0744 -1.7333 0.0902
0.0744 -1.7333 0.0902 0.0744 -1.7333 0.0902 2.8369
1.4092 -3.1398 -0.1
10 GEAAK 2.2261 -5.3648 0.3049 3.0777 0.3891 -0.0701
0.0744 -1.7333 0.0902 0.0744 -1.7333 0.0902 2.8369
1.4092 -3.1398 -0.52
11 LEAAK -4.1921 -1.0285 -0.9801 3.0777 0.3891 -0.0701
0.0744 -1.7333 0.0902 0.0744 -1.7333 0.0902 2.8369
1.4092 -3.1398 0.4
12 FEAAK -4.9217 1.2977 0.4473 3.0777 0.3891 -0.0701
0.0744 -1.7333 0.0902 0.0744 -1.7333 0.0902 2.8369
1.4092 -3.1398 0.3
13 VEGGK -2.6931 -2.5271 -1.2871 3.0777 0.3891 -0.0701
2.2261 -5.3648 0.3049 2.2261 -5.3648 0.3049 2.8369
1.4092 -3.1398 -1
14 VEFAK -2.6931 -2.5271 -1.2871 3.0777 0.3891 -0.0701
-4.9217 1.2977 0.4473 0.0744 -1.7333 0.0902 2.8369
1.4092 -3.1398 1.57
15 VELAK -2.6931 -2.5271 -1.2871 3.0777 0.3891 -0.0701
Appendix 1: Data Sets ❒ ❒ ❒ 51
data mswkal;
input n OBSNAM $ v1-v27 LS HA DT @;
datalines;
1 EM1 2766 2610 3306 3630 3600 3438 3213 3051 2907
2844 2796 2787 2760 2754 2670 2520 2310 2100 1917 1755
1602 1467 1353 1260 1167 1101 1017 3.011 0 0
2 EM2 1492 1419 1369 1158 958 887 905 929 920
887 800 710 617 535 451 368 296 241 190 157
128 106 89 70 65 56 50 0 0.4005 0
3 EM3 2450 2379 2400 2055 1689 1355 1109 908 750
673 644 640 630 618 571 512 440 368 305 247
196 156 120 98 80 61 50 0 0 90.63
4 EM4 2751 2883 3492 3570 3282 2937 2634 2370 2187
2070 2007 1974 1950 1890 1824 1680 1527 1350 1206 1080
984 888 810 732 669 630 582 1.482 0.158 40
5 EM5 2652 2691 3225 3285 3033 2784 2520 2340 2235
2148 2094 2049 2007 1917 1800 1650 1464 1299 1140 1020
909 810 726 657 594 549 507 1.116 0.4104 30.45
6 EM6 3993 4722 6147 6720 6531 5970 5382 4842 4470
4200 4077 4008 3948 3864 3663 3390 3090 2787 2481 2241
2028 1830 1680 1533 1440 1314 1227 3.397 0.3032 50.82
7 EM7 4032 4350 5430 5763 5490 4974 4452 3990 3690
3474 3357 3300 3213 3147 3000 2772 2490 2220 1980 1779
1599 1440 1320 1200 1119 1032 957 2.428 0.2981 70.59
8 EM8 4530 5190 6910 7580 7510 6930 6150 5490 4990
4670 4490 4370 4300 4210 4000 3770 3420 3060 2760 2490
2230 2060 1860 1700 1590 1490 1380 4.024 0.1153 89.39
9 EM9 4077 4410 5460 5857 5607 5097 4605 4170 3864
3708 3588 3537 3480 3330 3192 2910 2610 2325 2064 1830
1638 1476 1350 1236 1122 1044 963 2.275 0.504 81.75
10 EM10 3450 3432 3969 4020 3678 3237 2814 2487 2205
2061 2001 1965 1947 1890 1776 1635 1452 1278 1128 981
867 753 663 600 552 507 468 0.9588 0.145 101.1
11 EM11 4989 5301 6807 7425 7155 6525 5784 5166 4695
4380 4197 4131 4077 3972 3777 3531 3168 2835 2517 2244
2004 1809 1620 1470 1359 1266 1167 3.19 0.253 120
12 EM12 5340 5790 7590 8390 8310 7670 6890 6190 5700
5380 5200 5110 5040 4900 4700 4390 3970 3540 3170 2810
2490 2240 2060 1870 1700 1590 1470 4.132 0.5691 117.7
13 EM13 3162 3477 4365 4650 4470 4107 3717 3432 3228
3093 3009 2964 2916 2838 2694 2490 2253 2013 1788 1599
1431 1305 1194 1077 990 927 855 2.16 0.436 27.59
14 EM14 4380 4695 6018 6510 6342 5760 5151 4596 4200
3948 3807 3720 3672 3567 3438 3171 2880 2571 2280 2046
1857 1680 1548 1413 1314 1200 1119 3.094 0.2471 61.71
15 EM15 4587 4200 5040 5289 4965 4449 3939 3507 3174
2970 2850 2814 2748 2670 2529 2328 2088 1851 1641 1431
1284 1134 1020 918 840 756 714 1.604 0.2856 108.8
16 EM16 4017 4725 6090 6570 6354 5895 5346 4911 4611
4422 4314 4287 4224 4110 3915 3600 3240 2913 2598 2325
2088 1917 1734 1587 1452 1356 1257 3.162 0.7012 60
17 EM17 3933 4518 5637 6006 5721 5187 4641 4149 3789
Appendix 1: Data Sets ❒ ❒ ❒ 53
3579 3447 3381 3327 3234 3078 2832 2571 2274 2040 1818
1629 1470 1350 1245 1134 1050 987 2.5 0.3 80
18 EM25 2904 2997 3255 3150 2922 2778 2700 2646 2571
2487 2370 2250 2127 2052 1713 1419 1200 984 795 648
525 426 351 291 240 204 162 30 40 150
;
54 ❒ ❒ ❒ Examples Using the PLS Procedure
data fluor5;
input n OBSNAM $ f1-f30 TOT_LOG TYR_LOG TRY_LOG @;
datalines;
1 43trp6 -5.91450 -5.91821 -6.90776 -5.42843 -4.11659
-5.10275 -4.65963 -4.35053 -4.02295 -3.84905 -3.63439
-3.63439 -3.57199 -3.51325 -3.63439 -3.57199 -3.77226
-3.77226 -3.84436 -3.93223 -4.01738 -4.02295 -4.11659
-4.22673 -4.49184 -4.65963 -4.85491 -5.42843 -5.10275
-5.42843 -6 -8 -6
2 59mix6 -5.90345 -5.90345 -5.90345 -5.08159 -4.21313
-5.08321 -4.83836 -4.63873 -4.47414 -4.21313 -4.00085
-4.09835 -4.00085 -4.00085 -3.90704 -4.00085 -4.09835
-4.09835 -4.20639 -4.09835 -4.21313 -4.21313 -4.33514
-4.47414 -4.63873 -4.83836 -4.83710 -5.08482 -5.41037
-5.41037 -6 -7 -6.04576
3 51mix6 -5.90712 -5.90712 -5.41485 -4.84343 -4.21313
-4.84343 -4.84343 -4.48295 -4.34281 -4.00633 -4.00633
-3.91202 -3.83044 -3.83044 -3.75502 -3.91202 -4.00633
-4.00085 -4.21313 -4.21313 -4.33514 -4.48295 -4.48295
-4.64183 -4.84089 -5.08806 -5.08806 -5.41485 -5.41485
-5.41485 -6 -6.60206 -6.12494
4 49mix6 -5.41936 -5.09130 -5.09130 -4.64808 -4.00633
-4.84597 -4.64808 -4.48295 -4.34281 -4.21991 -4.21991
-4.21991 -4.11047 -4.11047 -4.11047 -4.21991 -4.21991
-4.34281 -4.48295 -4.48295 -4.65017 -4.65017 -4.84597
-4.84597 -5.09293 -5.09130 -5.41936 -5.41710 -5.41710
-5.90712 -6 -6.30103 -6.30103
5 53mix6 -5.08321 -4.83710 -4.83710 -4.47414 -3.82585
-4.47414 -4.63873 -4.83836 -4.83710 -4.63873 -4.63873
-4.64080 -4.64080 -4.63873 -4.63873 -4.83710 -4.83836
-4.83836 -5.08321 -5.08159 -5.08321 -5.41037 -5.41037
-5.40813 -5.40813 -5.89980 -5.41037 -5.90345 -5.89980
-6.90776 -6 -6.12494 -6.60206
6 57mix6 -5.08159 -4.83584 -4.63873 -4.47414 -3.82585
-4.63563 -4.63769 -4.63769 -4.83710 -5.08159 -5.08159
-5.40813 -5.08159 -5.07999 -5.40813 -5.40813 -5.40813
-5.40813 -5.40813 -5.40813 -5.40813 -5.89980 -5.89980
-5.89980 -5.89980 -5.89980 -5.89980 -5.89980 -6.90776
-6.90776 -6 -6.04576 -7
7 41tyro6 -5.10440 -4.66174 -4.66174 -4.35831 -3.70501
-4.50081 -4.66174 -4.85877 -5.10440 -5.43071 -5.43299
-5.91821 -5.91821 -5.91821 -5.43071 -5.91821 -5.91821
-5.91821 -5.91821 -5.91821 -5.91821 -5.91821 -5.91821
-6.90776 -5.91821 -5.91821 -6.90776 -6.90776 -5.91821
-5.91821 -6 -6 -8
8 28trp5 -5.93698 -5.93698 -5.93698 -4.52636 -3.54391
-3.17009 -2.57308 -2.11470 -1.79234 -1.56446 -1.40039
-1.30383 -1.24355 -1.21322 -1.23953 -1.29182 -1.37255
-1.45333 -1.57070 -1.69679 -1.80060 -1.87269 -2.00828
-2.19804 -2.46864 -2.70605 -2.98975 -3.20893 -3.38434
-3.60087 -5 -8 -5
9 37mix5 -5.10935 -4.86524 -4.50081 -4.02856 -3.31870
Appendix 1: Data Sets ❒ ❒ ❒ 55
Appendix 2
Macros
/****************************************************************/
/* */
/* NOTE: These macros work with releases 6.12 and up. */
/* For more information, send e-mail to Bruce */
/* Elsheimer at sasbze@[Link] or Randy Tobias */
/* at sasrdt@[Link]. */
/* */
/****************************************************************/
/*-------------------------------------------------------------------
DISCLAIMER:
-------------------------------------------------------------------*/
%macro res_plot(ds);
/************************************************************
/ Plots Y-residuals vs. predicted values for each PLS /
/ component. /
/ Variable: /
/ DS - The input data set: Must at least /
/ contain variables for observation /
/ numbers, predicted values and residuals /
/ and should not contain missing values. /
************************************************************/
%end;
%mend;
%macro nor_plot(ds);
/************************************************************
/ Plots Y-residuals vs. Normal quantiles for each PLS /
/ component. /
/ Variable: /
/ DS - The input data set: Must at least /
/ contain variables for observation /
/ numbers, predicted values and residuals /
/ and should not contain missing values. /
************************************************************/
/***********************************************************
/ Calculate the expected values under normality for each /
/ residual. /
***********************************************************/
60 ❒ ❒ ❒ Examples Using the PLS Procedure
%end;
%mend;
%macro plot_scr(ds,
max_lv=&lv);
/************************************************************
/ Plots the Y-scores vs. the corresponding X-scores for /
/ each PLS component. /
/ Variables: /
/ DS - The data set containing the scores and /
/ observation numbers. /
/ MAX_LV - Number of the last PLS component to have /
/ its scores plotted. /
************************************************************/
%end;
%mend;
%macro plotxscr(ds,
max_lv=&lv);
/************************************************************
/ Plots X-scores for a given number of PLS components /
/ vs. those of the preceding PLS component. /
/ Variables: /
/ DS - The data set containing the X-scores and /
/ observation numbers. /
/ MAX_LV - Number of the last PLS component to have /
/ its scores plotted. /
************************************************************/
%let j=%eval(&i+1);
%end;
%mend;
%macro get_wts(dsoutmod,
dsxwts=xwts);
/***********************************************************
/ Gets X-weights w from OUTMODEL data set: /
/ 1. Gets appropriate section of OUTMODEL data set. /
/ 2. Outputs this data set as DSXWTS1 (will be used /
/ in VIP calculation.) /
/ 3. Transposes the data set so the w’s are the /
/ variables. /
/ 4. Renames the columns to w1 - wA, where A is the /
/ number of PLS components LV in the final model. /
/ Variables: /
/ DSOUTMOD - Name of the OUTMODEL data set generated /
/ by proc PLS. /
/ DSXWTS - Name of the data set containing the /
/ X-weights as variables that is output /
/ by this macro. /
***********************************************************/
%end;
%mend;
Macros ❒ ❒ ❒ 63
%macro plot_wt(ds,
max_lv=&lv);
/************************************************************
/ Plots X-weights for a given number of PLS components /
/ vs. those of the preceding PLS component. /
/ Variables: /
/ DS - Name of the data set containing the /
/ weights as variables w1-wA, where A=LV, /
/ the number of PLS components, and a /
/ character variable _NAME_ containing the /
/ X-variable names. /
/ MAX_LV - Number of the last PLS component to have /
/ its weights plotted. /
************************************************************/
/***********************************************************
/ Determine the largest label to be put on plot /
***********************************************************/
%let name_len=1;
/***********************************************************
/ Plot X-weights for each PLS component /
***********************************************************/
%let j=%eval(&i+1);
run;
%end;
%mend;
%macro pltwtfrq(ds,
plotyvar=W,
plotxvar=f,
max_lv=&lv,
label=Weight);
/************************************************************
/ Plots X-Weights or X-Loadings versus the frequency for /
/ spectrometric calibration data sets. /
/ Variables: /
/ DS - Data set containing the weights/loadings /
/ as variables with each observation /
/ representing the weights for a particular /
/ X-variable, which in this case is a /
/ frequency. /
/ PLOTYVAR - The name (excluding the component number) /
/ of the weight/loading variables. For /
/ example, PLOTYVAR=w if the variables to /
/ be plotted are w1, w2, w3,... /
/ PLOTXVAR - The variable name of the frequency /
/ variable. /
/ MAX_LV - Number of PLS components to be plotted /
/ LABEL - The label for the vertical axis in the /
/ plot. /
************************************************************/
%let plotvars=%str( );
%mend;
Macros ❒ ❒ ❒ 65
%macro getxload(dsoutmod,
dsxload=xloads);
/***********************************************************
/ Gets X-loadings p from OUTMODEL data set: /
/ 1. Gets appropriate section of OUTMODEL data set. /
/ 2. Transposes it so the p’s are column vectors. /
/ 3. Renames the columns to p1 - pA, where A is the /
/ number of PLS components in the final model. /
/ Variables: /
/ DSOUTMOD - Name of the OUTMODEL data set produced /
/ by proc PLS. /
/ DSXLOAD - Name of the data set to contain the /
/ X-loadings as variables. /
***********************************************************/
%end;
%mend;
66 ❒ ❒ ❒ Examples Using the PLS Procedure
%macro pltxload(ds,
max_lv=&lv);
/************************************************************
/ Plots X-loadings for a given number of PLS components /
/ vs. those of the preceding PLS component. /
/ Variables: /
/ DS - Name of the data set containing the /
/ loadings as variables p1-pA, where A=LV, /
/ the number of PLS components, and a /
/ character variable _NAME_ containing the /
/ X-variable names. /
/ MAX_LV - Number of the last PLS component to have /
/ its loadings plotted. /
************************************************************/
/***********************************************************
/ Determine the largest label to be put on plot /
***********************************************************/
%let name_len=1;
/***********************************************************
/ Plot X-loadings for each PLS component /
***********************************************************/
%let j=%eval(&i+1);
run;
%end;
%mend;
%macro getyload(dsoutmod,
dsyload=yloads);
/***********************************************************
/ Gets Y-loadings q from OUTMODEL data set: /
/ 1. Gets appropriate section of OUTMODEL data set. /
/ 2. Transposes it so the q’s are column vectors. /
/ 3. Renames the columns to q1 - qA, where A is the /
/ number of latent variables in the final model. /
/ Variables: /
/ DSOUTMOD - Name of the OUTMODEL data set produced /
/ by proc PLS. /
/ DSYLOAD - Name of the data set to contain the /
/ Y-loadings as variables. /
***********************************************************/
%end;
%mend;
%macro plt_y_lv(dsoutmod);
/***********************************************************
/ Plots Y-loadings for each Y-variable versus the PLS /
/ component. /
/ Variable: /
/ DSOUTMOD - The OUTMODEL data set from proc PLS. /
***********************************************************/
major=(number=5) minor=none;
axis2 label=(’PLS Component’) order=(1 to &lv by 1) minor=none;
%mend;
%macro pltyload(ds,
max_lv=&lv);
/************************************************************
/ Plots Y-loadings for a given number of PLS components /
/ vs. those of the preceding PLS component. /
/ Variables: /
/ DS - Name of the data set containing the /
/ loadings as variables q1-qA, where A=LV, /
/ the number of PLS components, and a /
/ character variable _NAME_ containing the /
/ Y-variable names. /
/ MAX_LV - Number of the last PLS component to have /
/ its loadings plotted. /
************************************************************/
/***********************************************************
/ Determine the largest label to be put on plot /
***********************************************************/
%let name_len=1;
/***********************************************************
/ Plot Y-loadings for each PLS component /
***********************************************************/
%let j=%eval(&i+1);
%end;
%mend;
%macro get_bpls(dsoutmod,
dsout=bpls);
/************************************************************
/ Gets B(PLS), the matrix of PLS regression coefficients /
/ of Y on X. For each Y, the values represent the /
/ importance of each X-variable in the modeling of the /
/ corresponding Y-variable. /
/ Variables: /
/ DSOUTMOD - Name of the OUTMODEL data set produced /
/ by proc PLS. /
/ DSOUT - Name of the data set to contain the /
/ regression coefficients, with the /
/ variables representing columns in /
/ B(PLS), and one variable naming the /
/ X-variable for each row of B(PLS). /
************************************************************/
proc iml;
use est_wb;
read all var {&xvars} into w_prime;
read all var {_Y_} into b;
use est_pq;
read all var {&xvars} into p_prime;
read all var {&yvars} into q_prime;
W=w_prime‘;
P=p_prime‘;
Q=q_prime‘;
B_PLS = W*inv(P‘*W)*diag(b)*Q‘;
b_col=(’B1’:"B&num_y");
x_var={&xvars};
create &dsout from B_PLS[colname=b_col rowname=x_var];
append from B_PLS[rowname=x_var];
quit;
%mend;
70 ❒ ❒ ❒ Examples Using the PLS Procedure
%macro plt_bpls(ds);
/***********************************************************
/ Plot the PLS predictor (regression) coefficients in /
/ B(PLS) vs. the frequency, for each response variable. /
/ Variables: /
/ DS - Data set containing the columns of /
/ B(PLS) as variables, as well as a /
/ variable for the frequency. /
***********************************************************/
%let plotvars=%str( );
%mend;
%macro get_vip(dsoutmod,
dsvip=vip_data);
/************************************************************
/ Calculate VIP: Variable Importance for the Projection. /
/ This represents the importance of each X-variable in /
/ the PLS modeling of both the X- and Y-variables. /
/ Variables: /
/ DSOUTMOD - Name of the OUTMODEL data set produced /
/ by proc PLS. /
/ DSVIP - Name of the data set to contain the /
/ variable named ’VIP’ and the names of /
/ X-variables. /
************************************************************/
run;
proc iml;
use y_rsq;
read all var {_Y_} into rsq_y;
read all var {&xvars} into w_prime;
A=nrow(rsq_y);
K=ncol(w_prime);
W=w_prime‘;
Wnorm=W#(1/sqrt(W[##,]));
part_rsq=rsq_y-(0//rsq_y[1:(A-1),]);
tot_rsq=rsq_y[A,];
vip_sq=((Wnorm##2)*part_rsq)#(K/tot_rsq);
VIP=sqrt(vip_sq);
x_var={&xvars};
create &dsvip from VIP[colname=’VIP’ rowname=x_var];
append from VIP[rowname=x_var];
quit;
%mend;
%macro plot_vip(ds);
/************************************************************
/ Plot the VIP: Variable Importance for the Projection. /
/ Variables: /
/ DS - Data set containing the frequencies /
/ the VIP for each frequency. /
************************************************************/
%mend;
72 ❒ ❒ ❒ Examples Using the PLS Procedure
%macro get_dmod(dsoutput,
dsdmod=dmod,
qresname=qres,
id=n);
/************************************************************
/ Calculate the distance from each data point to the model /
/ in both the X-space (DMODX) and in the Y-space (DMODY). /
/ Variables: /
/ DSOUTPUT - OUTPUT data set from proc PLS. /
/ DSDMOD - Data set to contain the distances to /
/ the model. /
/ QRESNAME - Suffix of variable names for XQRES and /
/ YQRES assigned by the user in the /
/ proc PLS OUTPUT statement. /
/ ID - Observation identification variable /
/ in input data set. /
************************************************************/
proc iml;
use &dsoutput;
read all var {x&qresname} into xqres;
read all var {y&qresname} into yqres;
read all var{&id} into id;
dmodx=sqrt(xqres/&xqres_mn);
do i=1 to nrow(xqres);
if yqres[i]=. then
dmodx[i]=dmodx[i]/sqrt(&num_trn/(&num_trn-&lv-1));
end;
dmody=sqrt(yqres*(&num_trn/(&num_trn-&lv-1)));
dmodboth=id||dmodx||dmody;
col={&ID DMODX DMODY};
create &dsdmod from dmodboth[colname=col];
append from dmodboth;
quit;
%mend;