Question: Q 1 This problem is regarding writing your own code for choosing the regularization parameter via cross validation for a lasso estimator. a . Simulate
Q This problem is regarding writing your own code for choosing the regularization parameter via cross
validation for a lasso estimator.
a Simulate a data set as follows
set.seed
;;
matrix
rep
rnorm
In this synthetic dataset there are observations, regression coefficients, of which only are nonzero.
The objective of the following steps is to recover the vector from the data in and
b Define a grid dots, of numbers between and this grid shall serve as potential values for
the regularizer Hint: you may want to use the function seq with the argument length.out
c Use the function glmnet to obtain lasso estimates for each value in the grid that you define in part
b Using the function coef extract the estimated coefficient vector when ie the
value in the grid
d For each value of in the grid of part b compute the mean squared error on the entire dataset.
Use a for loop for this purpose This will provide a vector of values of mse, one for each value
of Plot vs mse. What do you observe?
e Using a for loop. compute the cross vlaidation error, for each value of in the grid under a
fold cross validation setup. you will need to repeatedly divide the data into testing and training, this
will require another for loop
f Compile all your code into a custom function with input arguments and a grid This function
should output the following results, i the best fit model with a kfold cross validation, ii the vector
of cross validation errors one for each value of iii the grid used for cross validation, iv the
value of lambda at which the best fit model is obtained.
g Finally, use the function you make in Part f with then extract the vector of cross validation
errors say CVV and the grid that is used. Make a plot of L vs CVV
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
