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.

Questions

On the USArrests data (which comes with base R), calculate PVE in two ways :

  1. 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.

  2. 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.

    1. Store the loadings of the PCA model you obtained in the first questions in loadings.
    2. Scale the data with \(\mu = 0\) and \(\sigma = 1\) and store the new data in USArrests.sd.
    3. To calculate the numerator \(\sum_{i=1}^{n}(\sum_{j=1}^{p}\phi_{jm}x_{ij})^2\):
      1. First, calculate \(\sum_{j=1}^{p}\phi_{jm}x_{ij}\) by calculating the matrix multiplication of the scaled matrix USArrests.sd and loadings with the %*% operator. Store this in subtotal.
      2. Second, calculate \(\sum_{i=1}^{n}(\sum_{j=1}^{p}\phi_{jm}x_{ij})^2\) by summing up each squared column by using the apply() function on subtotal^2, where you apply the sum() function to each column. Store the the outcome in nominator.
    4. To calculate the denominator \(\sum_{j=1}^{p}\sum_{i=1}^{n}x_{ij}^2\), take square of each element of USArrests.sd and sum all elements together. Store the value in denominator.
    5. Finally, divide nominator by denominator and store the result in pve2.

Assume that: