22.2k views
0 votes
In order to consolidate your theoretical knowledge into technique and skills with practical and applicational value, you will use the glmnet() package in R to implement LASSO function to build linear and logistic models through LASSO over values of regularization parameter lambda.

Use one of the real-world example data sets from R (not previously used in the R practice assignment) or a dataset you have found, to build regularization models by using Lasso (least absolute shrinkage and selection operator) and extend Lasso model fitting to big data that cannot be loaded into memory. You will fit solution paths for linear or logistic regression models penalized by Lasso over a grid of values for the regularization parameter lambda. Use the resources in this module to guide your R code development.

User Elfan
by
3.9k points

1 Answer

5 votes

Answer:

Check the explanation

Step-by-step explanation:

Lasso: R example

To run Lasso Regression you can re-use the glmnet() function, but with the alpha parameter set to 1.

# Perform 10-fold cross-validation to select lambda --------------------------- lambdas_to_try <- 10^seq(-3, 5, length.out = 100) # Setting alpha = 1 implements lasso regression lasso_cv <- cv.glmnet(X, y, alpha = 1, lambda = lambdas_to_try, standardize = TRUE, nfolds = 10) # Plot cross-validation results plot(lasso_cv)

Best cross-validated lambda lambda_cv <- lasso_cv$lambda.min # Fit final model, get its sum of squared residuals and multiple R-squared model_cv <- glmnet(X, y, alpha = 1, lambda = lambda_cv, standardize = TRUE) y_hat_cv <- predict(model_cv, X) ssr_cv <- t(y - y_hat_cv) %*% (y - y_hat_cv) rsq_lasso_cv <- cor(y, y_hat_cv)^2 # See how increasing lambda shrinks the coefficients -------------------------- # Each line shows coefficients for one variables, for different lambdas. # The higher the lambda, the more the coefficients are shrinked towards zero. res <- glmnet(X, y, alpha = 1, lambda = lambdas_to_try, standardize = FALSE) plot(res, xvar = "lambda") legend("bottomright", lwd = 1, col = 1:6, legend = colnames(X), cex = .7)

Kindly check the Image below.

In order to consolidate your theoretical knowledge into technique and skills with-example-1
User Gekrish
by
4.0k points