Homework 3 in DSCI445: Statistical Machine Learning @ CSU
Explain how \(k\)-fold cross-validation is implements.
What are the advantages and disadvantages of \(k\)-fold cross-validation relative to
In Ch. 4, we used logistic regression to predict the probability of default
using income
and balance
on the Default
data set. We will now estimate the test error of this logistic regression model using the validation set approach.
Fit a logistic regression model that uses income
and balance
to predict default
.
Using the validation set approach, estimate the test error of this model. In order to do this, you must perform the following steps:
default
status for each individual in the validation set.Repeat the process in b. using 3 different splits of the observations into training and validation sets. Comment on the results obtained.
Now consider logistic regression model that predicts the probability of default using income
, balance
, and a dummy variable for student
. Estimate the test error for this model using the validation set approach. Comment on whether or not including student
leads to a reduction in the test error rate.
The cv.glm()
function can be used to compute the LOOCV test error rate estimate. Alternatively, one could compute those quanitities using glm()
, predict.glm()
, and a for
loop. You will take the second approach in the following problem.
Fit a logistic regression model that predicts Direction
using Lag1
and Lag2
using the Weekly
data set.
Fit a logistic regression model that predicts Direction
using Lag1
and Lag2
using the Weekly
data set using all but the first observation.
Use the model from b. to predict the direction of the first observation. Was this observation correctly classified?
Write a for loop from \(i = 1\) to \(i = n\) where \(n\) is the number of observations in the data set that performs each of the following steps:
Fit a logistic regression model that predicts Direction
using Lag1
and Lag2
using the Weekly
data set using all but the \(i\)th observation.
Predict the direction of the \(ith\) observation.
Determine whether or not an error was made in predicting the direction for the \(i\)th observation. If an error was made, indicate this as a \(1\) and if not, a \(0\).
Take the average of the \(n\) numbers obtained in d. iii. to obtain the LOOCV estimate for the test error. Comment on the results.
We will now perform cross-validation on a simulated data set.
Generate a simulated data set as follows ``` x <- rnorm(100) y <- x - 2*x^2 + rnorm(100)
df <- data.frame(x = x, y = y) ```
In this data set, what is \(n\) and \(p\)? Write out the model used to generate the data in equation form.
Create a scatterplot of \(X\) against \(Y\). Commend on what you see.
Compute the LOOCV errors that result from fitting the following four models using least squares:
Repeat c. using a random seed of \(200\) (set.seed(200)
) and report your results. Are your results the dame as what you got in c.? Why or why not?
Which of the models in c. had the smallest LOOCV error? Is this what you expected? Explain your answer.
Comment on the statistical significance of the coefficient estimates that results from fitting each of the models in c. using least suares. Do these results agree with your conclusions drawn based on the cross-validation results?