Use the glmnet
package to fit a ridge regression model on the same data as in the previous part. Hint: read about the alpha
input to the glmnet
function in the documentation.
# install.packages("glmnet") # if necessary
library(glmnet)
set.seed(1)
n <- 100
p <- 1000
X <- matrix(rnorm(n*p), nrow = n)
beta <- rpois(p, lambda = 1)
y <- X %*% beta + rnorm(n)
model_ridge <- glmnet(X, y, intercept = FALSE, alpha = 0)
What does plotting the model object show?
plot(model_ridge, xvar = "lambda")