/* Filename: PCAExample.sas */ /* Purpose: IML code that demonstrates PCA */ /* Last Update: FDN 4.23.02 */ proc iml; CALL GSTART ; x= {2 1, 3 4, 5 0, 7 6, 9 2}; *create the data matrix; print x; call gstart; run GXYPLOT(X[,1],X[,2]); *plot the data ; call gclose; xbar= repeat(x[+,]/nrow(x),nrow(x),1); *get the means of the cols (variables); print xbar; y= x-xbar; *Center the data; print y; call gstart; run GXYPLOT(Y[,1],Y[,2]); *plot the centered data ; call gclose; sscp= y`*y; *get the syum of s1qured deviations and sum of cross products; cov= sscp/(nrow(x)-1); *divide by degrees of freedom for the covariance matrix; print cov; *note the variances on the diagonal and the covariances on the off diagnoal; call eigen(l,u,cov); *compute the eigenvalues (l) and vectors (u); print l; *Eigenvalues -- the variance of the scores of the the ojects on the PC's; print u; *the eigenvectors -- the weights fo the linear combinations; f= y*u; *compute the PC's -- these are the linear combinations ; *note the use of the centered data; * f[1,1] = y[1,1]u[1,1] + y[1,2]u[1,2], etc.; print f; *print the PC's; call gstart; run GXYPLOT(f[,1],f[,2]); *plot the PC's -- check out the rotation! ; call gclose; angles= (180/arcos(-1))*(arcos(u)); *convert the cosines to radians to decimal degrees please! print angles; s =(f`*f)/(nrow(f)-1); *now compute the variances and Covariances of the PC's; print s; *Note the values -- some look familiar -- why the 0's?; print