O'Reilly logo

R: Recipes for Analysis, Visualization and Machine Learning by Chiu Yu-Wei, Atmajitsinh Gohil, Shanthi Viswanathan, Viswa Viswanathan

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Reducing dimensionality with principal component analysis

The stats package offers the prcomp function to perform PCA. This recipe shows you how to perform PCA using these capabilities.

Getting ready

If you have not already done so, download the data files for this chapter and ensure that the BostonHousing.csv file is in your R working directory. We want to predict MEDV based on the remaining 13 predictor variables. We will use PCA to reduce the dimensionality.

How to do it...

To reduce dimensionality with PCA, follow the steps:

  1. Read the data:
    > bh <- read.csv("BostonHousing.csv")
  2. View the correlation matrix to check whether some variables are highly correlated and whether PCA has the potential to yield some dimensionality reduction. Since we are interested ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required