WEEK 4 HOMEWORK – SAMPLE SOLUTIONS
IMPORTANT NOTE
These homework solutions show multiple approaches and some optional extensions for most of
the questions in the assignment. You don’t need to submit all this in your a
...
WEEK 4 HOMEWORK – SAMPLE SOLUTIONS
IMPORTANT NOTE
These homework solutions show multiple approaches and some optional extensions for most of
the questions in the assignment. You don’t need to submit all this in your assignments; they’re
included here just to help you learn more – because remember, the main goal of the homework
assignments, and of the entire course, is to help you learn as much as you can, and develop
your analytics skills as much as possible!
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)!)
Here’s one possible solution. Please note that a good solution doesn’t have to try all of the possibilities
in the code; they’re shown to help you learn, but they’re not necessary.
The file solution 9.1.R shows how to calculate the principal components, run a regression, and find the
resulting coefficients in terms of the original variables.
The model using the first four principal components is the following:
Crime ~ 1667 – 16.9M + 21.3So + 12.8Ed + 21.4Po1 + 23.1Po2 – 347LF – 8.3MF + 1.0Pop +
1.5NW – 1510U1 + 1.7U2 + 0.040Wealth – 6.9Ineq + 144.9Prob – 0.9Time
Its R2 is just 0.309, much lower than the 0.803 found by the regression model found in the previous
homework. But, remember that cross-validation showed a lot of overfitting in the previous homework.
The R code shows the results using the first k principal components, for k=1..15:
[Show More]