Question: In this exercise we will generate simulated data and use it to fit a lasso model. Use the code from class, ISLR text, or a
In this exercise we will generate simulated data and use it to fit a lasso model. Use
the code from class, ISLR text, or a LLM to help with completing each task.
marks
a Use the rnorm function to generate a predictor X of length n as well
as a noise vector of length n Hint: make the standard deviation
much smaller for generating the noise vector Just paste your code, not all
the data values in your answer.
b Generate a response vector Y of length n according to the model
Y beta beta X beta Xbeta X
where beta beta beta and beta are constants of your choice. Just paste your code in
your response, not all the generated data.
c Create a data frame with the response variable Y and the predictors
X X X X Run the following code, replacing Y and data with your
response variable and data frame name and paste the output as your answer
to this part.
libraryglmnet
x model.matrixY ~ data data
lasso.model cvglmnetx Y alpha
plotlassomodel
bestlambda lasso.model$lambda.min
lasso.coef predictlassomodel, s bestlambda, type "coefficients"
# Print the best lambda and coefficients
printpasteBest lambda:", bestlambda
printLasso Coefficients:"
printlassocoef
d Explain what the code in part c is doing. You will need to lookup what
the cvglmnet function does and research crossvalidation This can be
completed with a few sentences. How close are the coefficients to the actual
coefficients you chose in b
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
