How to Conduct Principal Component Analysis in R

How to Conduct Principal Component Analysis in R

Principal component analysis (PCA) is a method that helps make large datasets easier to understand. It cuts down the number of variables and keeps the important information. PCA transforms original data into new variables called principal components. These components highlight patterns and relationships in the data.

Let’s have a look at how to implement PCA in R.

Installing Necessary Packages

First, install the required packages. You can use the stats package for PCA. It is already included in R. You can use ggplot2 and factoextra to visualize the PCA results.

# Install packages if not already installed
install.packages("ggplot2")
install.packages("factoextra")

# Load necessary libraries
library(ggplot2)
library(factoextra)

Loading Your Data

You can load any dataset for PCA. Here, we will use the built-in iris dataset.

# Load the iris dataset
data(iris)

# View the first few rows of this dataset
head(iris)

dataset

The iris dataset has five columns. Four are numeric: sepal length, sepal width, petal length, and petal width. The fifth column is called species. It shows the type of iris flower for each row.

Data Preprocessing

Before PCA, you should remove the species column because PCA focuses on numerical data. The species column is categorical and can interfere with the analysis. You should also implement scaling and centering. Scaling makes sure all variables have the same weight. Centering makes the mean of each variable zero.

# Remove the species column for PCA
iris_data <- iris[, -5]

# Scale and center the data
scaled_data <- scale(iris_data)

# View the first few rows of the scaled data
head(scaled_data)

preprocessing

Conducting Principal Component Analysis

Use the prcomp() function to perform PCA. This function finds the principal components. It also shows how much each component explains the variation in the dataset.

The summary() function shows the standard deviations of the principal components, the proportion of variance explained by each, and the cumulative variance. Standard deviations tell you how much variance each component explains. Proportion of variance shows the percentage of total variance captured by each component. Cumulative proportion indicates the running total of variance explained.

# Perform PCA
pca_result <- prcomp(scaled_data, center = TRUE, scale. = TRUE)

# Check PCA results
summary(pca_result)

result

Visualize the Principal Components

Visualizing the principal components is important for understanding the results of PCA. It shows how the data is spread out. It also shows patterns that are hard to notice in the original data.

Scree Plot

This plot shows the variance explained by each principal component. It helps you determine how many components to keep. Look for an “elbow” in the plot where the variance starts to level off.

# Perform PCA on the scaled data
pca_result <- prcomp(scaled_data, center = TRUE, scale. = TRUE)

# Scree plot
fviz_eig(pca_result, addlabels = TRUE)

scree_plot

Biplot

A biplot combines scores and loadings in one visualization. It shows how each observation relates to the principal components. The arrows represent the variables. This indicates their contribution to the components.

# Biplot of PCA results
fviz_pca_biplot(pca_result, 
                 geom.ind = "point",    
                 geom.var = "arrow",    
                 col.ind = iris$Species, 
                 label = "var",         
                 repel = TRUE,          
                 legend.title = "Species") +
  labs(title = "Biplot of PCA Results")

biplot

Scatter Plot

You can create a scatter plot of the first two principal components. Color the points by the species of iris flowers. This allows you to see how well the components separate the groups.

# Create a data frame with PCA results
pca_df <- as.data.frame(pca_result$x)
pca_df$Species <- iris$Species

# Scatter plot of the first two principal components
ggplot(pca_df, aes(x = PC1, y = PC2, color = Species)) +
  geom_point(size = 3) +
  labs(title = "Scatter Plot of PCA results", x = "Principal Component 1", y = "Principal Component 2") +
  theme_minimal()

scatter_plot

Conclusion

Principal Component Analysis (PCA) helps understand large datasets. This lets you identify patterns in your data and makes it easier to see the results. Use PCA to improve your data analysis and get better insights.

Posted in R

2 Replies to “How to Conduct Principal Component Analysis in R”

  1. Hi!
    I hope this isn’t a stupid question, but when you run the prcomp() why do you include center = TRUE and .scale = TRUE if you already scaled the data before running the pca?

    I am trying to do a pca for my data. I scaled using the robust scaling method, x – median(x)/IQR(x). If I set center = TRUE and .scale = TRUE will that scale the data again?

    1. Hi Sophie…When applying the code as given in the tutorial…did it alter it from what you expected?

Leave a Reply

Your email address will not be published. Required fields are marked *