Principal Component Analysis Action Set

Analyzing Crime Rates of US States Using the eig Action

This section contains PROC CAS code.

Note: Input data must be accessible in your CAS session, either as a CAS table or as a transient-scope table. A CAS table has a two-level name: the first level is your CAS engine libref, and the second level is the table name. You refer to this table in the CAS procedure by specifying only the second level. For more information about two-level names, see Chapter 2, Shared Concepts. A transient-scope table is called directly from the action and exists in memory for the duration of the action. For more information about accessing data, see SAS Viya: System Programming Guide. For more information about PROC CAS and programming in CASL, see SAS Cloud Analytic Services: CASL Programmer’s Guide and SAS Cloud Analytic Services: CASL Reference.

The following DATA step creates the mycas.Crime data table—which provides crime rates per 100,000 people in seven categories for each of the 50 US states in 1977—in your CAS session. This DATA step assumes that your CAS engine libref is named mycas, but you can substitute any appropriately defined CAS engine libref.

data mycas.Crime;
   input State $1-15 Murder Rape Robbery Assault
         Burglary Larceny Auto_Theft;
   datalines;
Alabama        14.2 25.2  96.8 278.3 1135.5 1881.9 280.7
Alaska         10.8 51.6  96.8 284.0 1331.7 3369.8 753.3
Arizona         9.5 34.2 138.2 312.3 2346.1 4467.4 439.5
Arkansas        8.8 27.6  83.2 203.4  972.6 1862.1 183.4
California     11.5 49.4 287.0 358.0 2139.4 3499.8 663.5
Colorado        6.3 42.0 170.7 292.9 1935.2 3903.2 477.1
Connecticut     4.2 16.8 129.5 131.8 1346.0 2620.7 593.2
Delaware        6.0 24.9 157.0 194.2 1682.6 3678.4 467.0
Florida        10.2 39.6 187.9 449.1 1859.9 3840.5 351.4
Georgia        11.7 31.1 140.5 256.5 1351.1 2170.2 297.9
Hawaii          7.2 25.5 128.0  64.1 1911.5 3920.4 489.4
Idaho           5.5 19.4  39.6 172.5 1050.8 2599.6 237.6
Illinois        9.9 21.8 211.3 209.0 1085.0 2828.5 528.6
Indiana         7.4 26.5 123.2 153.5 1086.2 2498.7 377.4
Iowa            2.3 10.6  41.2  89.8  812.5 2685.1 219.9
Kansas          6.6 22.0 100.7 180.5 1270.4 2739.3 244.3
Kentucky       10.1 19.1  81.1 123.3  872.2 1662.1 245.4
Louisiana      15.5 30.9 142.9 335.5 1165.5 2469.9 337.7
Maine           2.4 13.5  38.7 170.0 1253.1 2350.7 246.9
Maryland        8.0 34.8 292.1 358.9 1400.0 3177.7 428.5
Massachusetts   3.1 20.8 169.1 231.6 1532.2 2311.3 1140.1
Michigan        9.3 38.9 261.9 274.6 1522.7 3159.0 545.5
Minnesota       2.7 19.5  85.9  85.8 1134.7 2559.3 343.1
Mississippi    14.3 19.6  65.7 189.1  915.6 1239.9 144.4
Missouri        9.6 28.3 189.0 233.5 1318.3 2424.2 378.4
Montana         5.4 16.7  39.2 156.8  804.9 2773.2 309.2
Nebraska        3.9 18.1  64.7 112.7  760.0 2316.1 249.1
Nevada         15.8 49.1 323.1 355.0 2453.1 4212.6 559.2
New Hampshire   3.2 10.7  23.2  76.0 1041.7 2343.9 293.4
New Jersey      5.6 21.0 180.4 185.1 1435.8 2774.5 511.5
New Mexico      8.8 39.1 109.6 343.4 1418.7 3008.6 259.5
New York       10.7 29.4 472.6 319.1 1728.0 2782.0 745.8
North Carolina 10.6 17.0  61.3 318.3 1154.1 2037.8 192.1
North Dakota    0.9  9.0  13.3  43.8  446.1 1843.0 144.7
Ohio            7.8 27.3 190.5 181.1 1216.0 2696.8 400.4
Oklahoma        8.6 29.2  73.8 205.0 1288.2 2228.1 326.8
Oregon          4.9 39.9 124.1 286.9 1636.4 3506.1 388.9
Pennsylvania    5.6 19.0 130.3 128.0  877.5 1624.1 333.2
Rhode Island    3.6 10.5  86.5 201.0 1489.5 2844.1 791.4
South Carolina 11.9 33.0 105.9 485.3 1613.6 2342.4 245.1
South Dakota    2.0 13.5  17.9 155.7  570.5 1704.4 147.5
Tennessee      10.1 29.7 145.8 203.9 1259.7 1776.5 314.0
Texas          13.3 33.8 152.4 208.2 1603.1 2988.7 397.6
Utah            3.5 20.3  68.8 147.3 1171.6 3004.6 334.5
Vermont         1.4 15.9  30.8 101.2 1348.2 2201.0 265.2
Virginia        9.0 23.3  92.1 165.7  986.2 2521.2 226.7
Washington      4.3 39.6 106.2 224.8 1605.6 3386.9 360.3
West Virginia   6.0 13.2  42.2    .  597.4 1341.7 163.3
Wisconsin       2.8 12.9  52.2  63.7  846.9 2614.2 220.7
Wyoming          .  21.9  39.7 173.9  811.6 2772.2 282.0
;

The following PROC CAS statements use the eig action in the pca action set to request a principal component analysis of the data. The ODS TRACE command displays the full paths of the output tables in the SAS log.

ods trace on;
proc cas;
   action pca.eig /
      table='Crime';
run;

The default output from this analysis is presented in Output 17.1.1 through Output 17.1.4.

Output 17.1.1 displays the "Model Information," "Number of Variables," "Number of Observations," and "Simple Statistics" tables.

The "Model Information" table identifies the data source and shows that the eigenvalue decomposition method is used to extract principal components.

The "Number of Variables" table indicates that there are seven variables to be analyzed and seven principal components to be computed. By default, if you omit the inputs parameter, all numeric variables defined in the table parameter, except the ones listed in the groupBy subparameter in the table parameter and other parameters, are used in the analysis.

The "Number of Observations" table shows that of the 50 observations in the input data, only 48 observations are used in the analysis, because some observations have incomplete data.

The "Simple Statistics" table displays the mean and standard deviation of the analysis variables.

Output 17.1.1: Model Information and Simple Statistics

Results from pca.eig

Model Information
Data SourceCRIME
Component Extraction MethodEigenvalue Decomposition

Number of Variables7
Number of Principal Components7

Number of Observations Read50
Number of Observations Used48

Simple Statistics
VariableMeanStandard
Deviation
Murder7.516673.93059
Rape26.0750010.81304
Robbery127.5562588.49374
Assault214.58750100.64360
Burglary1316.37917423.31261
Larceny2696.88542714.75023
Auto_Theft383.97917194.37033


Output 17.1.2 displays the "Correlation Matrix" table. By default, the eig action requests that principal components be computed from the correlation matrix, so the total variance is equal to the number of variables, 7.

Output 17.1.2: Correlation Matrix Table

Correlation Matrix
VariableMurderRapeRobberyAssaultBurglaryLarcenyAuto_Theft
Murder1.00000.60000.47680.64850.37780.09250.0555
Rape0.60001.00000.58170.73160.70380.60090.3282
Robbery0.47680.58171.00000.54520.62000.43710.5787
Assault0.64850.73160.54521.00000.60820.37910.2520
Burglary0.37780.70380.62000.60821.00000.79320.5390
Larceny0.09250.60090.43710.37910.79321.00000.4246
Auto_Theft0.05550.32820.57870.25200.53900.42461.0000


Output 17.1.3 displays the "Eigenvalues" table. The first principal component accounts for about 57.8% of the total variance, the second principal component accounts for about 18.1%, and the third principal component accounts for about 10.7%. Note that the eigenvalues sum to the total variance.

The eigenvalues indicate that two or three components provide a good summary of the data: two components account for 76% of the total variance, and three components account for 87%. Subsequent components account for less than 5% each.

Output 17.1.3: Eigenvalues Table

Eigenvalues of the Correlation Matrix
 EigenvalueDifferenceProportionCumulative
14.0458242.7817950.57800.5780
21.2640300.5165290.18060.7586
30.7475000.4211750.10680.8653
40.3263250.0611190.04660.9120
50.2652070.0368430.03790.9498
60.2283640.1056130.03260.9825
70.122750 0.01751.0000


Output 17.1.4 displays the "Eigenvectors" table. From the eigenvectors matrix, you can represent the first principal component, Prin1, as a linear combination of the original variables:

StartLayout 1st Row 1st Column sans-serif upper P sans-serif r sans-serif i sans-serif n Baseline sans-serif 1 2nd Column equals negative 0.30289 times left-parenthesis sans-serif upper M sans-serif u sans-serif r sans-serif d sans-serif e sans-serif r right-parenthesis 2nd Row 1st Column Blank 2nd Column negative 0.43410 times left-parenthesis sans-serif upper R sans-serif a sans-serif p sans-serif e right-parenthesis 3rd Row 1st Column Blank 2nd Column negative 0.39705 times left-parenthesis sans-serif upper R sans-serif o sans-serif b sans-serif b sans-serif e sans-serif r sans-serif y right-parenthesis 4th Row 1st Column Blank 2nd Column period 5th Row 1st Column Blank 2nd Column period 6th Row 1st Column Blank 2nd Column period 7th Row 1st Column Blank 2nd Column negative 0.28834 times left-parenthesis sans-serif upper A sans-serif u sans-serif t sans-serif o sans-serif bar sans-serif upper T sans-serif h sans-serif e sans-serif f sans-serif t right-parenthesis EndLayout

Similarly, the second principal component, Prin2, is

StartLayout 1st Row 1st Column sans-serif upper P sans-serif r sans-serif i sans-serif n Baseline sans-serif 2 2nd Column equals 0.61893 times left-parenthesis sans-serif upper M sans-serif u sans-serif r sans-serif d sans-serif e sans-serif r right-parenthesis 2nd Row 1st Column Blank 2nd Column plus 0.17053 times left-parenthesis sans-serif upper R sans-serif a sans-serif p sans-serif e right-parenthesis 3rd Row 1st Column Blank 2nd Column negative 0.04713 times left-parenthesis sans-serif upper R sans-serif o sans-serif b sans-serif b sans-serif e sans-serif r sans-serif y right-parenthesis 4th Row 1st Column Blank 2nd Column period 5th Row 1st Column Blank 2nd Column period 6th Row 1st Column Blank 2nd Column period 7th Row 1st Column Blank 2nd Column negative 0.50400 times left-parenthesis sans-serif upper A sans-serif u sans-serif t sans-serif o sans-serif bar sans-serif upper T sans-serif h sans-serif e sans-serif f sans-serif t right-parenthesis EndLayout

where the variables are standardized.

Output 17.1.4: Eigenvectors Table

Eigenvectors
VariablePrin1Prin2Prin3Prin4Prin5Prin6Prin7
Murder-0.302890.618930.17353-0.23308-0.548960.26371-0.26428
Rape-0.434100.17053-0.235390.06540-0.18075-0.782320.27946
Robbery-0.39705-0.047130.49208-0.574700.50808-0.094520.02497
Assault-0.396220.35142-0.053430.617430.515250.17395-0.19921
Burglary-0.44164-0.20861-0.22454-0.02750-0.112730.523400.65085
Larceny-0.35634-0.40570-0.53681-0.23231-0.021720.04085-0.60346
Auto_Theft-0.28834-0.504000.575240.41853-0.35939-0.06024-0.15487


The first component is a measure of the overall crime rate, because the first eigenvector shows approximately equal loadings on all variables. The second eigenvector has high negative loadings on the variables Auto_Theft and Larceny and high positive loadings on the variables Murder and Assault. There is also a small negative loading on the variable Burglary and a small positive loading on the variable Rape. This component seems to measure the preponderance of property crime compared to violent crime. The interpretation of the third component is not obvious.

Analyzing Crime Rates of US States Using the eig Action

This section contains Lua code for the analysis in the CASL version of this example, which contains details about the results.

Note: In order to run this code, the data that are described in the CASL version need to be accessible to the CAS server. One way to do this is to convert the Crime data to the comma-separated-value (CSV) file Crime.csv and then use the following code to load the CSV file into CAS:

s:loadtable{casLib="casuser", path="Crime.csv"}

For more information about coding in Lua, see Getting Started with SAS Viya for Lua and SAS Viya: System Programming Guide.

The following code loads the pca action set and then uses the eig action to perform a principal component analysis of the data:

s:loadactionset{actionset='pca'}
r=s:eig{
   table='Crime'}

The following commands display the tables that are produced by this action call:

print(r.ModelInfo)
print(r.NVars)
print(r.NObs)
print(r.SimpleStatistics)
print(r.Corr)
print(r.Eigenvalues)
print(r.Eigenvectors)
print(r.Timing)

The following command displays all the tables that are produced by this action call along with their full paths:

r

For more information about the results of this analysis, see the CASL version of this example.

Analyzing Crime Rates of US States Using the eig Action

This section contains Python code for the analysis in the CASL version of this example, which contains details about the results.

Note: In order to run this code, the data that are described in the CASL version need to be accessible to the CAS server. One way to do this is to convert the Crime data to the comma-separated-value (CSV) file Crime.csv and then use the following code to load the CSV file into CAS:

s.upload_file('Crime.csv')

For more information about coding in Python, see Getting Started with SAS Viya for Python and SAS Viya: System Programming Guide.

The following code loads the pca action set and then uses the eig action to perform a principal component analysis of the data:

s.loadactionset(actionset='pca')
r=s.eig(
   table='Crime')

The following commands display the tables that are produced by this action call:

print(r.ModelInfo)
print(r.NVars)
print(r.NObs)
print(r.SimpleStatistics)
print(r.Corr)
print(r.Eigenvalues)
print(r.Eigenvectors)
print(r.Timing)

The following command displays all the tables that are produced by this action call along with their full paths:

print(r)

For more information about the results of this analysis, see the CASL version of this example.

Analyzing Crime Rates of US States Using the eig Action

This example is not available for the R programming language.

Last updated: September 13, 2022