We now perform principal components analysis using the prcomp()
function,
which is one of several functions in R that perform PCA.
pr.out <- prcomp(USArrests, ...)
In the previous exercise, we manually investigated the values we need to scale the data.
The R function however has scaling as an embedded option.
By default, the prcomp()
function centers the variables to have mean zero.
We also want to scale the variables, so that they have standard deviation of one.
Use the appropriate argument in the function to achieve this.
(You might want to consult the help file of this function.
Remember that you can access the help file by running ?functionname
).
Store your result in pr.out
.
The output from prcomp()
contains a number of useful quantities.
> names(pr.out)
[1] "sdev" "rotation" "center" "scale"
[5] "x"
The center
and scale
components correspond to the means and standard
deviations of the variables that were used for scaling prior to implementing
PCA.
Store the center
and scale
components of pr.out
with in pr.center
and pr.scale
respectively.
You should notice that you get the same results as in the previous section.