Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

ISLR Chapter 7 Moving Beyond Linearity Lab Manual, Exercises of Statistics

Introduction to Statistical Learning (James/Witten/Hastie/Tibshirani)

Typology: Exercises

2020/2021

Uploaded on 05/26/2021

ekani
ekani 🇺🇸

4.7

(26)

265 documents

1 / 6

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Chapter 7 - Non-linear Models
Lab Solution
1 Problem 9
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)
##
## Call:
## lm(formula = nox ~ poly(dis, 3), data = Boston)
##
## Residuals:
## Min 1Q Median 3Q Max
## -0.121130 -0.040619 -0.009738 0.023385 0.194904
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 0.554695 0.002759 201.021 < 2e-16 ***
## poly(dis, 3)1 -2.003096 0.062071 -32.271 < 2e-16 ***
## poly(dis, 3)2 0.856330 0.062071 13.796 < 2e-16 ***
## poly(dis, 3)3 -0.318049 0.062071 -5.124 4.27e-07 ***
## ---
## Signif. codes: 0 '***'0.001 '**'0.01 '*'0.05 '.'0.1 ' ' 1
##
## Residual standard error: 0.06207 on 502 degrees of freedom
## Multiple R-squared: 0.7148,Adjusted R-squared: 0.7131
## F-statistic: 419.3 on 3 and 502 DF, p-value: < 2.2e-16
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)
1
pf3
pf4
pf5

Partial preview of the text

Download ISLR Chapter 7 Moving Beyond Linearity Lab Manual and more Exercises Statistics in PDF only on Docsity!

Chapter 7 - Non-linear Models

Lab Solution

1 Problem 9

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)

Call:

lm(formula = nox ~ poly(dis, 3), data = Boston)

Residuals:

Min 1Q Median 3Q Max

-0.121130 -0.040619 -0.009738 0.023385 0.

Coefficients:

Estimate Std. Error t value Pr(>|t|)

(Intercept) 0.554695 0.002759 201.021 < 2e-16 ***

poly(dis, 3)1 -2.003096 0.062071 -32.271 < 2e-16 ***

poly(dis, 3)2 0.856330 0.062071 13.796 < 2e-16 ***

poly(dis, 3)3 -0.318049 0.062071 -5.124 4.27e-07 ***

---

Signif. codes: 0 '' 0.001 '' 0.01 '' 0.05 '.' 0.1 ' ' 1

Residual standard error: 0.06207 on 502 degrees of freedom

Multiple R-squared: 0.7148,Adjusted R-squared: 0.

F-statistic: 419.3 on 3 and 502 DF, p-value: < 2.2e-

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

## [1] 2.768563 2.035262 1.934107 1.932981 1.915290 1.878257 1.849484 1.835630 1.

## [10] 1.

(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)

Call:

dis

nox

(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)]

## [1] 1.934107 1.922775 1.840173 1.833966 1.829884 1.816995 1.825653 1.792535 1.

## [10] 1.788999 1.782350 1.781838 1.782798 1.

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)