R - Principal Component Analysis

Card Puncher Data Processing

About

Data Mining - Principal Component (Analysis|Regression) (PCA|PCR) in R.

Function

prcomp 

Package stats

Steps

Perform the analysis

pcaData = prcomp(USArrests, scale=TRUE)

where:

  • scale=TRUE will standardize the variables in order to take into account the different unity.

Review the principal component data

  • The data
pcaData
Standard deviations:
[1] 1.5748783 0.9948694 0.5971291 0.4164494

Rotation:
                PC1        PC2        PC3         PC4
Murder   -0.5358995  0.4181809 -0.3412327  0.64922780
Assault  -0.5831836  0.1879856 -0.2681484 -0.74340748
UrbanPop -0.2781909 -0.8728062 -0.3780158  0.13387773
Rapee    -0.5434321 -0.1673186  0.8177779  0.08902432

  • The variables associated (names)
names(pcaData)
[1] "sdev"     "rotation" "center"   "scale"    "x"

  • Unclass will show the data :)
unclass(pcaData)
$sdev
[1] 1.5748783 0.9948694 0.5971291 0.4164494

$rotation
                PC1        PC2        PC3         PC4
Murder   -0.5358995  0.4181809 -0.3412327  0.64922780
Assault  -0.5831836  0.1879856 -0.2681484 -0.74340748
UrbanPop -0.2781909 -0.8728062 -0.3780158  0.13387773
Rapee    -0.5434321 -0.1673186  0.8177779  0.08902432

$center
  Murder  Assault UrbanPop     Rapee 
   7.788  170.760   65.540   21.232 

$scale
   Murder   Assault  UrbanPop      Rapee
 4.355510 83.337661 14.474763  9.366385 

$x
                       PC1         PC2         PC3          PC4
Alabama        -0.97566045  1.12200121 -0.43980366  0.154696581
Alaska         -1.93053788  1.06242692  2.01950027 -0.434175454
Arizona        -1.74544285 -0.73845954  0.05423025 -0.826264240
Arkansas        0.13999894  1.10854226  0.11342217 -0.180973554
California     -2.49861285 -1.52742672  0.59254100 -0.338559240
Colorado       -1.49934074 -0.97762966  1.08400162  0.001450164
Connecticut     1.34499236 -1.07798362 -0.63679250 -0.117278736
Delaware       -0.04722981 -0.32208890 -0.71141032 -0.873113315
Florida        -2.98275967  0.03883425 -0.57103206 -0.095317042
......................................

Plot the data

biplot(pcaData, scale=0)

Biplot Pca R







Share this page:
Follow us:
Task Runner