Principal Component Analysis (PCA) in R

Principal component analysis (PCA) is a statistical procedure that uses an orthogonal transformation to convert a set of observations of possibly correlated variables into a set of values of linearly uncorrelated variables called principal components. This transformation is defined in such a way that the first principal component has the largest possible variance (that is, accounts for as much of the variability in the data as possible), and each succeeding component in turn has the highest variance possible under the constraint that it is orthogonal to (i.e., uncorrelated with) the preceding components.

PCA is a widely used statistical technique for dimensionality reduction, feature extraction, and visualization of high-dimensional data. It can be used to reduce the number of variables in a dataset without losing too much information. PCA can also be used to identify the most important features in a dataset.

Linear Transformation

PCA is a linear transformation, which means that it does not change the original data in any way. It simply transforms the data into a new set of variables that are uncorrelated with each other. This can be useful for visualization, as it can make it easier to see the relationships between the different variables in the dataset.

Here are some of the key steps involved in PCA:

  1. Standardize the data. This means that the mean of each variable is 0 and the standard deviation of each variable is 1.
  2. Calculate the covariance matrix. This is a matrix that shows the correlation between each pair of variables.
  3. Find the eigenvalues and eigenvectors of the covariance matrix. The eigenvalues are the variances of the principal components, and the eigenvectors are the directions of the principal components.
  4. Retain the principal components with the largest eigenvalues. These are the principal components that explain the most variance in the data.
  5. Transform the data into the new set of principal components.

Principal Component Analysis (PCA) in R

PCA can be performed in R using the prcomp() function. The prcomp() function takes two arguments:

  1. data: A data frame that contains the data.
  2. scale: A logical value that specifies whether to standardize the data before performing PCA.

For example, the following code performs PCA on the Iris dataset:

iris <- datasets::iris pca <- prcomp(iris, scale = TRUE)

The output of the prcomp() function is an object that contains the results of the PCA. You can use the summary() function to view the results of the PCA:

#Output: Importance of components: PC1 PC2 PC3 PC4 Standard deviation 3.3472352 1.7497053 0.7689693 0.4374231 Proportion of variance 0.9246486 0.0549224 0.0124492 0.0079798 Cumulative proportion 0.9246486 0.9795710 0.9920202 0.9999999

The summary of the PCA shows that the first principal component, PC1, explains 92.47% of the variance in the data. The second principal component, PC2, explains 5.49% of the variance in the data. The third and fourth principal components explain less than 1% of the variance in the data.


By performing PCA, you can reduce the dimensionality of the data while retaining most of the important information. This can simplify subsequent analyses and visualization, making it easier to identify patterns and relationships in the data.