How to do it...

To reduce dimensionality with PCA, follow these steps:

  1. Read the data:
> bh <- read.csv("BostonHousing.csv")
  1. View the correlation matrix to check whether some variables are highly correlated and whether PCA has the potential to yield some dimensionality reduction. As we are interested in reducing the dimensionality of the predictor variables, we leave out the outcome variable, MEDV:
> corr <- cor(bh[,-14]) > corrplot(corr, method="color")

Positive correlations are displayed in blue and negative correlations in red. Color intensity and the size of the circle are proportional to the correlation coefficients. Ignoring the main ...

Get R Data Analysis Cookbook - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.