The formula for calculating PVE was given by:
\[\frac{\sum_{i=1}^{n}(\sum_{j=1}^{p}\phi_{jm}x_{ij})^2}{\sum_{j=1}^{p}\sum_{i=1}^{n}x_{ij}^2}\]We also saw that the PVE can be obtained using the sdev
output of the prcomp()
function.
On the USArrests
data (which comes with base R), calculate PVE in two ways :
Using the sdev
output of the prcomp()
function,
as was done in Principal Component Analysis 41.
Make sure to scale the data with the scale
argument. Store the PVE in pve1
.
Apply the equation given above.
That is, use the prcomp()
function to compute the principal component loadings.
Then, use those loadings in the equation to obtain the PVE.
loadings
.USArrests.sd
.USArrests.sd
and loadings
with the %*%
operator.
Store this in subtotal
.apply()
function on subtotal^2
,
where you apply the sum()
function to each column. Store the the outcome in nominator
.USArrests.sd
and sum all elements together.
Store the value in denominator
.nominator
by denominator
and store the result in pve2
.Assume that:
USArrests
dataset has been loaded and attached