## T-Square Chart for Bivariate Data

``` /****************************************************************/
/*          S A S   S A M P L E   L I B R A R Y                 */
/*                                                              */
/*    NAME: SHWTSQ2                                             */
/*   TITLE: T-Square Chart for Bivariate Data                   */
/* PRODUCT: QC                                                  */
/*  SYSTEM: ALL                                                 */
/*    KEYS: Shewhart Charts, T-Square Charts,                   */
/*   PROCS: SHEWHART PRINCOMP SCORE MEANS                       */
/*    DATA:                                                     */
/*                                                              */
/*     REF: Jackson, J. E. (1980).  Principal Components and    */
/*          Factor Analysis:  Part I--Principal Components,     */
/*          Journal of Quality Technology 12, 201-213.          */
/*                                                              */
/*   NOTES: The data in this example are based on the data      */
/*          in Tables 1 and 2 of Jackson (1980).  Control       */
/*          limits are computed from the first 15 observations, */
/*          and the control limits are then extended to the     */
/*          next 4 observations labeled A, B, C, D.             */
/*                                                              */
/*          In this example, the PRINCOMP and SCORE procedures  */
/*          are used to compute the principal components, and   */
/*          the DATA step is used to compute the T square       */
/*          statistic as a sum of squares of the principal      */
/*          components.  The results are passed to the SHEWHART */
/*          procedure for graphical display.                    */
/*                                                              */
/*          This example can be generalized in various ways.    */
/*          For instance, it is easily extended to problems     */
/*          in which the dimension is greater than two.         */
/*                                                              */
/*    MISC:                                                     */
/*                                                              */
/****************************************************************/

options ps=60 ls=100;

* Data set from which control limits are to be computed;
data original;
length i \$ 2 ;
input i x1 x2;
cards;
1   10.0  10.7
2   10.4   9.8
3    9.7  10.0
4    9.7  10.1
5   11.7  11.5
6   11.0  10.8
7    8.7   8.8
8    9.5   9.3
9   10.1   9.4
10   9.6   9.6
11  10.5  10.4
12   9.2   9.0
13  11.3  11.6
14  10.1   9.8
15   8.5   9.2
run;

data new;
length i \$ 2 ;
input i x1 x2;
cards;
A   12.3  12.5
B    7.0   7.3
C   11.0   9.0
D    7.3   9.1
run;

* Center training data and save number of observations;
proc means data=original noprint;
var x1 x2;
output out=xmeans mean=mx1 mx2 n=nx1;
data xmeans;
set xmeans;
keep mx1 mx2 nx1;
data original;
drop mx1 mx2;
set original;
if _n_=1 then set xmeans;
x1 = x1 - mx1;
x2 = x2 - mx2;
_subn_   = 1;
_limitn_ = 1;
run;

* Determine principal components;
proc princomp data=original std cov noprint out=prin
outstat=scores;
var x1 x2;
run;

title 'Scores For Chemical Data';
proc print data=scores;
run;

* Compute T-square statistic for training data;
data prin;
length period \$ 16;
set prin;
period  = 'Training Data';
tsquare = prin1*prin1 + prin2*prin2;
run;

* Center new observations with means of training data;
data new;
length period \$ 16;
drop mx1 mx2;
set new;
period = 'New Data';
if _n_=1 then set xmeans;
x1 = x1 - mx1;
x2 = x2 - mx2;
_subn_   = 1;
_limitn_ = 1;
run;

* Compute T-square statistics for new data;
proc score data=new score=scores out=newscore;
var x1 x2;
run;
data newscore;
set newscore;
tsquare = prin1*prin1 + prin2*prin2;
run;

* Combine T-square statistics for original and new data;
data combine (rename=(tsquare=_subx_));
set prin newscore;
length _var_ \$ 8 ;
p        = 2;
_var_    = 'tsquare';
_alpha_  = 0.05;
_lclx_   = 0.0 ;
_mean_   = p * ( nx1 - 1 ) / ( nx1 - p - 2 );
_uclx_   = ( p * ( nx1 - 1 ) / ( nx1 - p ) ) *
finv( 1.0 - _alpha_, p, nx1 - p );
run;

title 'Analysis of Data For Chemical Example';
proc print data=combine;
run;

* Display T-squared chart;
title f=none h=1  'T'
m=(+0,+1)   '2'
m=(+0,-1)   ' Chart For Chemical Example';
symbol1 v=dot c=white;
proc shewhart table=combine;
xchart tsquare * i ( period ) /
blockpos = 1
vaxis    = 0 to 25 by 5
xsymbol  = mu
cframe   = grey
cinfill  = red
novangle
nolegend ;
label _SUBX_ = 'T Square'
i      = 'Measurement Index';
run;

goptions reset=all;

```