Week 6 Homework
Question 9.1
Using the same crime data set uscrime.txt as in Question 8.2, apply Principal Component
Analysis and then create a regression model using the first few principal components. Specify
your
...
Week 6 Homework
Question 9.1
Using the same crime data set uscrime.txt as in Question 8.2, apply Principal Component
Analysis and then create a regression model using the first few principal components. Specify
your new model in terms of the original variables (not the principal components), and compare
its quality to that of your solution to Question 8.2. You can use the R function prcomp for
PCA. (Note that to first scale the data, you can include scale. = TRUE to scale as part of the
PCA function. Don’t forget that, to make a prediction for the new city, you’ll need to unscale
the coefficients (i.e., do the scaling calculation in reverse)!)
Loading libraries that we will be using in this homework
if (!require(knitr)) install.packages(’knitr’)
if (!require(GGally)) install.packages(’GGally’)
if (!require(rgl)) install.packages(’rgl’)
library(knitr)
library(car)
library(GGally)
library(ggplot2)
library(rgl)
rm(list = ls(all.names = TRUE)) #will clear all objects includes hidden objects.
graphics.off()
shell("cls")
gc() #free up memrory and report the memory usage.
## | used (Mb) gc trigger (Mb) max used (Mb)
## Ncells 945805 50.6
## Vcells 1551563 11.9 | 1954260 104.4 1176755 62.9
8388608 64.0 2278040 17.4First I am loading US Crime data into a variable called usCrime, I am also declaring another
variable called newCityValueToPredict, in this I am storing the values which we are being
asked to use for prediction
usCrime <- read.table(’data 9.1/uscrime.txt’,header=TRUE)
usCrimeMinusResponse <- usCrime[, 1:15]
newCityValueToPredict <- data.frame(M=14.0,
So=0,
Ed=10.0,
Po1=12.0,
Po2=15.5,
1
LF=0.640,
M.F = 94.0,
Pop=150,
NW=1.1,U1=0.120,
U2=3.6,
Wealth=3200,
Ineq=20.1,
Prob=0.01,
Time=39.0)
Let’s look at the correlation between predictors, highly correlated predictors doesn’t contribute to model
performance, rather these can be detrimental
ggcorr(usCrimeMinusResponse, label=TRUE, label_size=3, name=’correlation’)
correlation
###### there seems to have correlations exists between some predictors
Let’s now apply Principal component analysis (PCA) on our entire dataset, except the response column,
naively, and see what we get
# Naively apply principal components analysis to raw data and plot
pcaNaive <- princomp(usCrimeMinusResponse)
plot(pcaNaive)
[Show More]