Assignment 3#

Due Friday, November 4th at 11:59PM on Gradescope#

Download#

Question 1#

We perform best subset, forward stepwise, and backward stepwise selection on a single data set. For each approach, we obtain \(p+1\) models, containing \(0, 1, 2,\dots , p\) predictors. Explain your answers:

Part 1#

Which of the three models with k predictors has the smallest training RSS?

Part 2#

Which of the three models with k predictors has the smallest test RSS?

Part 3: True or False:#

  • The predictors in the k-variable model identified by forward stepwise are a subset of the predictors in the \((k+1)\)-variable model identified by forward stepwise selection.

  • The predictors in the k-variable model identified by backward stepwise are a subset of the predictors in the \((k+1)\) variable model identified by backward stepwise selection.

  • The predictors in the k-variable model identified by backward stepwise are a subset of the predictors in the \((k+1)\) variable model identified by forward stepwise selection.

  • The predictors in the k-variable model identified by forward stepwise are a subset of the predictors in the \((k+1)\)-variable model identified by backward stepwise selection.

  • The predictors in the k-variable model identified by best subset are a subset of the predictors in the \((k+1)\)-variable model identified by best subset selection.

Question 2#

Suppose we are interested in estimating the regression coefficients in linear regression with regularization. Particularly, the goal is to minimize

\[||Y - X \beta||_2^2 + \lambda \sum_{i = 1}^{p} J_{\tau}(|\beta_j|), \]

with respect to \(\beta,\) where \(Y\) is a vector of responses, \(X\) is our usual data matrix, and for some fixed \(\lambda > 0\), \(\tau > 0\), and \(J_{\tau}(|\beta_j|) = \min\{\frac{|\beta_j|}{\tau}, 1\}.\)

Part 1#

Intuitively, describe what this regularization \(J_{\tau}(|\beta_j|)\) is doing. Hint: Consider what happens to \(|\beta_j|\) that are smaller than \(\tau\) and greater than \(\tau\).

Part 2#

Describe what happens when \(\tau \downarrow 0\). That is, what happens when \(\tau \rightarrow 0\) from the right side of \(0\).

Part 3#

Instead, suppose we consider the LASSO, with the goal to minimize

\[||Y - X \beta||_2^2 + \lambda \sum_{i = 1}^{p} |\beta_j|.\]

In this case, explain what the \(\beta\) coefficient solution approaches when \(\lambda \rightarrow 0\) and when \(\lambda \rightarrow \infty\).

Part 4#

Explain what is the difference between the LASSO and Ridge Regression.

Question 3#

In this exercise, we will generate simulated data, and will then use this data to perform best subset selection.

Part 1#

Use the rnorm() function to generate a predictor \(X\) of length \(n = 200\), as well as a noise vector \(\varepsilon\) of length \(n = 200\).

Part 2#

Generate a response vector \(Y\) of length \(n = 200\) according to the model

\[Y = \beta_0 + \beta_1 X +\beta_2X^2 +\beta_3X^3 +\varepsilon,\]

where \(\beta_0, \beta_1, \beta_2\), and \(\beta_3\) are constants of your choice.

Part 3#

Use the regsubsets() function to perform best subset selection in order to choose the best model containing the predictors \(X,X^2, \dots , X^{9}\). What is the best model obtained according to RSS, \(C_p\), BIC, and adjusted \(R^2\)? Show some plots to provide evidence for your answer, and report the coefficients of the best model obtained. Note you will need to use the data.frame() function to create a single data set containing both \(X\) and \(Y\).

Part 4#

Repeat 3. using forward stepwise selection and also using backwards stepwise selection. How does your answer compare to the results in 3. ?

Part 5#

Now fit a lasso model to the simulated data, again using \(X,X^2, \dots , X^{9}\) as predictors. Use cross-validation to select the optimal value of \(\lambda\). Create plots of the cross-validation error as a function of \(\lambda\). Report the resulting coefficient estimates, and discuss the results obtained.

Part 6#

Now generate a response vector \(Y\) according to the model

\[Y = \beta_0 + \beta_7X^7 + \varepsilon,\]

and perform best subset selection and the lasso. Discuss the results obtained.

Question 4#

In this exercise, we will predict the number of applications received using the other variables in the College data set.

Part 1#

Split the data set into a training set and a test set.

Part 2#

Fit a linear model using least squares on the training set, and report the test error obtained.

Part 3#

Fit a ridge regression model on the training set, with \(\lambda\) chosen by cross-validation. Report the test error obtained.

Part 4#

Fit a lasso model on the training set, with \(\lambda\) chosen by cross-validation. Report the test error obtained, along with the number of non-zero coefficient estimates.

Part 5#

Fit a PCR model on the training set, with M chosen by cross-validation. Report the test error obtained, along with the value of M selected by cross-validation.

Part 6#

Comment on the results obtained. How accurately can we predict the number of college applications received? Is there much difference among the test errors resulting from these five approaches?

Question 5#

Suppose that a curve \(\hat{g}\) is computed to smoothly fit a set of \(n\) points using the following formula:

\[ \hat{g} = \mathrm{argmin}_g \left( \sum_{i=1}^n (y_i - g(x_i)^2) + \lambda \int H(g^{(m)}(x)) \; dx \right) \]

where \(g^{(m)}\) represents the \(m\)th derivative of \(g\) (and \(g^{(0)} = g\)) for some pre-determined \(\delta\) and \(H\) is the Huber loss defined here. The Huber loss is a loss function typically used in robust regression that is less sensitive to outliers in data than the usual squared error loss.

Provide example sketches of \(\hat{g}\) in each of the following scenarios.

  1. \(\lambda = \infty, m = 0\).

  2. \(\lambda = \infty, m = 1\).

  3. \(\lambda = \infty, m = 2\).

  4. \(\lambda = \infty, m = 3\).

  5. \(\lambda = 0, m = 3\).

Question 6#

The Wage data 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 and wage. Use non-linear fitting techniques with regularization in order to fit flexible models to the data. You should use \(k\)-fold cross validation to select a reasonable \(\lambda\) as your penalized regression parameter. Create plots of the results obtained, and write a summary of your findings.

Question 7#

This question relates to the College data set.

Part 1#

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 backward stepwise selection on the training set in order to identify a satisfactory model that uses just a subset of the predictors.

Part 2#

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.

Part 3#

Evaluate the model obtained on the test set, and explain the results obtained.

Part 4#

For which variables, if any, is there evidence of a non-linear relationship with the response?

Question 8#

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 the coefficient estimates stop changing (convergence).

We now try this out on a toy example.

Part 1#

Use the following code to generate a response \(Y\) and two predictors \(X_1\) and \(X_2\), with \(n = 100\).

set.seed(2022)
n = 100
x1 = rnorm(n)
x2 = rnorm(n)
y = 13 + 4 * x1 - 7 * x2 + rnorm(n)

Part 2#

Initialize \(\hat{\alpha}_1\) to the value 0.

alpha1 = 0

Part 3#

Keeping \(\hat{\alpha}_1\) fixed, fit the model

\[Y - \hat{\alpha}_1 X_1 = \alpha_0 + \alpha_2 X_2 + \epsilon.\]

You can do this as follows:

a = y - alpha1 * x1
alpha2 = lm(a ~ x2)$coef[2]

Part 4#

Keeping \(\hat{\alpha}_2\) fixed, fit the model

\[Y - \hat{\alpha}_2 X_2 = \alpha_0 + \alpha_1 X_1 + \epsilon.\]

You can do this as follows:

a = y - alpha2 * x2
alpha1 = lm(a ~ x1)$coef[2]

Part 5#

Write a for loop to repeat 3. and 4. 1,000 times. Report the estimates of \(\hat{\alpha}_0\), \(\hat{\alpha}_1\), and \(\hat{\alpha}_2\) at each iteration of the for loop. Create a plot in which each of these values is displayed, with \(\hat{\alpha}_0\), \(\hat{\alpha}_1\), and \(\hat{\alpha}_2\) each shown in a different color.

Part 6#

Compare your answer in 5. to the results of simply performing multiple linear regression to predict \(Y\) using \(X_1\) and \(X_2\). Use the abline() function to overlay those multiple linear regression coefficient estimates on the plot obtained in 5.

Part 7#

On this data set, how many backfitting iterations were required in order to obtain a “good” approximation to the multiple regression coefficient estimates?