Given the linear discriminant functions obtained in the DISCRIM procedure, compute the vector (dot) product of the observation with each of the functions. When you obtain quadratic discriminant functions, additional matrix multiplication is used. The group function yielding the largest result, or score, is the group into which the observation is classified. However, it is not necessary to compute this yourself unless you do not want to use SAS®. The example titled "Linear Discriminant Analysis of Remote-Sensing Data on Crops" in the DISCRIM documentation shows how to use the TESTDATA= option to classify additional observations. The general method is also discussed in "Saving and Using Calibration Information" in the Details section of the DISCRIM documentation.
Examples of scoring or classifying observations using both the linear and quadratic discriminant functions are given in this article. In general, the posterior probability of classification into a group is preferred over the score. The posterior probabilities are scaled from 0 to 1 and are an indication of the strength of the association. The scores are not scaled and have no interpretable meaning.
The following example shows how you can classify observations manually using the linear discriminant functions computed in PROC DISCRIM. It uses the Sashelp.Class data set, a small fictitious class of students including Sex, Age, Height, and Weight variables.
Save the discriminant function coefficients in a data set using the OUTSTAT= option. Save the resubstitution classification results in a data set using the OUT= option. By default, POOL=YES specifies the linear discriminant functions.
proc discrim data=sashelp.class pool=yes outstat=LinearFns out=Resub;
class sex;
var age height weight;
id name;
run;
The _NAME_=_LINEAR_ observations in the OUTSTAT= data contain the linear coefficients for each class level or group, and the _NAME_=_CONST_ observations contain the constant for each group.
Compute the score, dn, for each observation for each group in a similar fashion to computing simple linear regression estimates. Each observation is then classified into the group with the largest score. The scores are related to the distances from the observations to the group centroids but are different, partly, by a factor of -2. As a result, maximizing the score minimizes the distance. Also shown here is the computation of the posterior probabilities. The posterior probability for each observation for each group is the group's exponentiated score divided by the sum of the exponentiated scores.
data scores;
set Resub;
*scores for SEX=F;
d1 = -150.689331 + 0.020588*Age + 6.685123*Height - 1.153440*Weight;
*scores for SEX=M;
d2 = -152.077605 - 1.218233*Age + 6.843435*Height - 1.072826*Weight;
if d1>d2 then class='F';
else class='M';
denom = exp(d1) + exp(d2);
*posterior probabilities for SEX=F;
pprob_1 = exp(D1) / denom;
*posterior probabilities for SEX=M;
pprob_2 = exp(D2) / denom;
run;
Partial results are shown below.
Note that some of the manually computed scores are slightly different than the scores computed by PROC DISCRIM. This difference occurs because the coefficients are manually truncated in the code, so there is some loss of precision. In practice, you should use the coefficients saved to full precision.
We can extend this example to show the manual steps for classifying observations using quadratic discriminant functions. POOL=NO specifies quadratic functions:
proc discrim data=sashelp.class pool=no outstat=QuadFns out=Resub;
class sex;
var age height weight;
run;
Here is a portion of the classification results from the OUT=RESUB data set. The default variable names F and M are obtained from the class variable levels, and they contain the posterior probabilities for each observation in each group.
Here is a subset of the OUTSTAT=QuadFns data set showing the function coefficients:
To classify observations using quadratic discriminant functions, you need to perform matrix multiplication using the following notation:
t = subscript to distinguish the groups
n = number of quantitative variables
C = constant
L = n-by-1 vector of linear coefficients
Q = n-by-n matrix of quadratic coefficients
X = n-by-1 observation vector
To obtain the score for Group t on a new observation X using POOL=NO or POOL=TEST with a significant p-value, use this formula:
Score = Ct + ( Lt' * X ) + ( X' * Q * X )
The first observation in the Sashelp.Class data set contains the following values:
Sex=M
Age=14
Height=69.0
Weight=112.5
The following matrix algebra is used to compute the score for SEX=F for this observation:
By using new observations, you can classify new data using the derived discriminant functions. You can easily program the computation of the scores and posterior probabilities in PROC IML. The following program demonstrates using the same first observation for both sexes:
proc iml;
obs = {14 69 112.5};
constF=-258.61112256;
linearF = {25.37701248 7.63984087 -3.19183733};
quadraticF = {-1.66198725 -0.01546412 0.11345562,
-0.01546412 -0.09290696 0.02234662,
0.11345562 0.02234662 -0.01396247};
scoreF = constF + linearF*obs` + obs*quadraticF*obs`;
constM=-188.43286709;
linearM={-9.87402358 9.66586920 -1.06732151};
quadraticM={-0.77788269 0.25141848 -0.00649395,
0.25141848 -0.15519193 0.01575378,
-0.00649395 0.01575378 -0.00354423 };
scoreM = constM + linearM*obs` + obs*quadraticM*obs`;
* computing the posterior probabilities;
denom = exp(scoreF) + exp(ScoreM);
*posterior probability for SEX=F;
pprobF = exp(ScoreF) / denom;
*posterior probability for SEX=M;
pprobM = exp(ScoreM)/ denom;
print scoreF scoreM pprobF pprobM;
quit;
The manually computed posterior probabilities from PROC IML are below.
The manually computed posterior probabilities match those computed in PROC DISCRIM, and this observation is classified as Female. The coefficient values are hardcoded in all of the above examples for illustration purposes. In practice, you would use the variables saved in the PROC DISCRIM output data sets directly in PROC IML, or transfer them to another software package that performs matrix computations.