Applied
-
In this exercise, you will further analyze the
Wagedata set considered throughout this chapter.-
(a) Perform polynomial regression to predict
wageusingage. Use cross-validation to select the optimal degree d for the polynomial. What degree was chosen, and how does this compare to the results of hypothesis testing using ANOVA? Make a plot of the resulting polynomial fit to the data. -
(b) Fit a step function to predict
wageusingage, and perform crossvalidation to choose the optimal number of cuts. Make a plot of the fit obtained.
-
-
The
Wagedata set contains a number of other features not explored in this chapter, such as marital status (maritl), job class (jobclass), and others. Explore the relationships between some of these other predictors andwage, and use non-linear fitting techniques in order to fit flexible models to the data. Create plots of the results obtained, and write a summary of your findings. -
Fit some of the non-linear models investigated in this chapter to the
Autodata set. Is there evidence for non-linear relationships in this data set? Create some informative plots to justify your answer. -
This question uses the variables
dis(the weighted mean of distances to five Boston employment centers) andnox(nitrogen oxides concentration in parts per 10 million) from theBostondata. We will treatdisas the predictor andnoxas the response.-
(a) Use the
poly()function from theISLP.modelsmodule to fit a cubic polynomial regression to predictnoxusingdis. Report the regression output, and plot the resulting data and polynomial fits. -
(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.
-
(c) Perform cross-validation or another approach to select the optimal degree for the polynomial, and explain your results.
-
(d) Use the
bs()function from theISLP.modelsmodule to fit a regression spline to predictnoxusingdis. Report the output for the fit using four degrees of freedom. How did you choose the knots? Plot the resulting fit. -
(e) 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.
-
(f) Perform cross-validation or another approach in order to select the best degrees of freedom for a regression spline on this data. Describe your results.
-
328 7. Moving Beyond Linearity
- This question relates to the
Collegedata set.
-
(a) Split the data into a training set and a test set. Using out-of-state tuition as the response and the other variables as the predictors, perform forward stepwise selection on the training set in order to identify a satisfactory model that uses just a subset of the predictors.
-
(b) Fit a GAM on the training data, using out-of-state tuition as the response and the features selected in the previous step as the predictors. Plot the results, and explain your findings.
-
(c) Evaluate the model obtained on the test set, and explain the results obtained.
-
(d) For which variables, if any, is there evidence of a non-linear relationship with the response?
- In Section 7.7, it was mentioned that GAMs are generally fit using a backfitting approach. The idea behind backfitting is actually quite simple. We will now explore backfitting in the context of multiple linear regression.
Suppose that we would like to perform multiple linear regression, but we do not have software to do so. Instead, we only have software to perform simple linear regression. Therefore, we take the following iterative approach: we repeatedly hold all but one coefficient estimate fixed at its current value, and update only that coefficient estimate using a simple linear regression. The process is continued until convergence —that is, until the coefficient estimates stop changing.
We now try this out on a toy example.
-
(a) Generate a response Y and two predictors X 1 and X 2, with n = 100.
-
(b) Write a function
simple_reg()that takes two argumentsoutcomeandfeature, fits a simple linear regression model with this outcome and feature, and returns the estimated intercept and slope. -
(c) Initialize
beta1to take on a value of your choice. It does not matter what value you choose. -
(d) Keeping
beta1fixed, use your functionsimple_reg()to fit the model:
Store the resulting values as beta0 and beta2 .
- (e) Keeping
beta2fixed, fit the model
Store the result as beta0 and beta1 (overwriting their previous values).
- (f) Write a for loop to repeat (c) and (d) 1,000 times. Report the estimates of
beta0,beta1, andbeta2at each iteration of the for loop. Create a plot in which each of these values is displayed, withbeta0,beta1, andbeta2.
7.9 Exercises 329
-
(g) Compare your answer in (e) to the results of simply performing multiple linear regression to predict Y using X 1 and X 2. Use
axline()method to overlay those multiple linear regression coefficient estimates on the plot obtained in (e). -
(h) On this data set, how many backfitting iterations were required in order to obtain a “good” approximation to the multiple regression coefficient estimates?
- This problem is a continuation of the previous exercise. In a toy example with p = 100, show that one can approximate the multiple linear regression coefficient estimates by repeatedly performing simple linear regression in a backfitting procedure. How many backfitting iterations are required in order to obtain a “good” approximation to the multiple regression coefficient estimates? Create a plot to justify your answer.