



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
Introduction to Statistical Learning (James/Witten/Hastie/Tibshirani)
Typology: Exercises
1 / 6
This page cannot be seen from the preview
Don't miss anything!
set.seed (1) library (MASS) attach (Boston)
(a). Use the poly() function to fit a cubic polynomial regression to predict nox using dis. Report the regression output, and plot the resulting data and polynomial fits.
lm.fit = lm (nox ~ poly (dis, 3), data = Boston) summary (lm.fit)
dislim = range (dis) dis.grid = seq (from = dislim[1], to = dislim[2], by = 0.1) lm.pred = predict (lm.fit, list (dis = dis.grid)) plot (nox ~ dis, data = Boston, col = "darkgrey") lines (dis.grid, lm.pred, col = "red", lwd = 2)
dis
nox
(b). Plot the polynomial fits for a range of different polynomial degrees (say, from 1 to 10), and report the associated residual sum of squares.
all.rss = rep (NA, 10) for (i in 1:10) { lm.fit = lm (nox ~ poly (dis, i), data = Boston) all.rss[i] = sum (lm.fit$residuals^2) } all.rss
(c). Use the bs() function to fit a regression spline to predict nox using dis. Report the output for the fit using four degrees of freedom. How did you choose the knots? Plot the resulting fit.
library (splines) sp.fit = lm (nox ~ bs (dis, df = 4, knots = c (4, 7, 11)), data = Boston) summary (sp.fit)
(d). Now fit a regression spline for a range of degrees of freedom, and plot the resulting fits and report the resulting RSS. Describe the results obtained.
all.cv = rep (NA, 16) for (i in 3:16) { lm.fit = lm (nox ~ bs (dis, df = i), data = Boston) all.cv[i] = sum (lm.fit$residuals^2) } all.cv[- c (1, 2)]
lm.fit = lm (nox ~ bs (dis, df = 14), data = Boston) lm.pred = predict (lm.fit, list (dis = dis.grid)) plot (nox ~ dis, data = Boston, col = "darkgrey") lines (dis.grid, lm.pred, col = "red", lwd = 2) lm.fit = lm (nox ~ bs (dis, df = 5), data = Boston) lm.pred = predict (lm.fit, list (dis = dis.grid)) lines (dis.grid, lm.pred, col = "blue", lwd = 2)