Elastic Net Regression in R Programming Last Updated : 28 Jul, 2020 Summarize Comments Improve Suggest changes Share Like Article Like Report Elastic Net regression is a classification algorithm that overcomes the limitations of the lasso(least absolute shrinkage and selection operator) method which uses a penalty function in its L1 regularization. Elastic Net regression is a hybrid approach that blends both penalizations of the L2 and L1 regularization of lasso and ridge methods. It finds an estimator in a two-stage procedure i.e first for each fixed λ2 it finds the ridge regression coefficients and then does a lasso regression type shrinkage which does a double amount of shrinkage which eventually leads to increased bias and poor predictions. Rescaling the coefficients of the naive version of the elastic net by multiplying the estimated coefficients by (1 + λ2) is done to improve the prediction performance. Elastic Net regression is used in: Metric learning Portfolio optimization Cancer prognosis Elastic Net regression always aims at minimizing the following loss function: Elastic Net also allows us to tune the alpha parameter where alpha = 0 corresponds to Ridge regression and alpha = 1 to Lasso regression. Similarly, when alpha = 0, the penalty function reduces to the L1(ridge) regularization, and when alpha = 1, the penalty function reduces to L2(lasso) regularization. Therefore, we can choose an alpha value between 0 and 1 to optimize the Elastic Net and this will shrink some coefficients and set some to 0 for sparse selection. In Elastic Net regression, the lambda hyper-parameter is mostly and heavily dependent on the alpha hyper-parameter. Now let's implement elastic net regression in R programming. Implementation in R The Dataset mtcars(motor trend car road test) comprises fuel consumption, performance and 10 aspects of automobile design for 32 automobiles. It comes pre-installed with dplyr package in R. R # Installing the package install.packages("dplyr") # Loading package library(dplyr) # Summary of dataset in package summary(mtcars) Output: Performing Elastic Net Regression on Dataset Using the Elastic Net regression algorithm on the dataset by training the model using features or variables in the dataset. R # Installing Packages install.packages("dplyr") install.packages("glmnet") install.packages("ggplot2") install.packages("caret") # X and Y datasets X <- mtcars %>% select(disp) %>% scale(center = TRUE, scale = FALSE) %>% as.matrix() Y <- mtcars %>% select(-disp) %>% as.matrix() # Model Building : Elastic Net Regression control <- trainControl(method = "repeatedcv", number = 5, repeats = 5, search = "random", verboseIter = TRUE) # Training ELastic Net Regression model elastic_model <- train(disp ~ ., data = cbind(X, Y), method = "glmnet", preProcess = c("center", "scale"), tuneLength = 25, trControl = control) elastic_model # Model Prediction x_hat_pre <- predict(elastic_model, Y) x_hat_pre # Multiple R-squared rsq <- cor(X, x_hat_pre)^2 rsq # Plot plot(elastic_model, main = "Elastic Net Regression") Output: Training of Elastic Net Regression model: The Elastic Net regression model is trained to find the optimum alpha and lambda values. Model elastic_model: The Elastic Net regression model uses the alpha value as 0.6242021 and lambda value as 1.801398. RMSE was used to select the optimal model using the smallest value. Model Prediction: The model is predicted using the Y dataset and values are shown. Multiple R-Squared: The multiple R-Squared values of disp is 0.9514679. Plot: The mixing percentage is plotted with RMSE scores with different values of the regularization parameter. So, Elastic Net regression applications are used in many sectors of industry and with full capacity. Comment More infoAdvertise with us D dhruv5819 Follow Improve Article Tags : R Language R Machine-Learning R Data-science Similar Reads R Tutorial | Learn R Programming Language R is an interpreted programming language widely used for statistical computing, data analysis and visualization. R language is open-source with large community support. R provides structured approach to data manipulation, along with decent libraries and packages like Dplyr, Ggplot2, shiny, Janitor a 4 min read R Programming Language - Introduction R is a programming language and software environment that has become the first choice for statistical computing and data analysis. Developed in the early 1990s by Ross Ihaka and Robert Gentleman, R was built to simplify complex data manipulation and create clear, customizable visualizations. Over ti 4 min read R-Data Frames R Programming Language is an open-source programming language that is widely used as a statistical software and data analysis tool. Data Frames in R Language are generic data objects of R that are used to store tabular data. Data frames can also be interpreted as matrices where each column of a matr 6 min read Read contents of a CSV File in R Programming - read.csv() Function read.csv() function in R Language is used to read "comma separated value" files. It imports data in the form of a data frame. The read.csv() function also accepts a number of optional arguments that we can use to modify the import procedure. we can choose to treat the first row as column names, sele 3 min read R-Data Types Data types in R define the kind of values that variables can hold. Choosing the right data type helps optimize memory usage and computation. Unlike some languages, R does not require explicit data type declarations while variables can change their type dynamically during execution.R Programming lang 5 min read Data Visualization in R Data visualization is the practice of representing data through visual elements like graphs, charts, and maps. It helps in understanding large datasets more easily, making it possible to identify patterns and trends that support better decision-making. R is a language designed for statistical analys 5 min read R-Matrices R-matrix is a two-dimensional arrangement of data in rows and columns. In a matrix, rows are the ones that run horizontally and columns are the ones that run vertically. In R programming, matrices are two-dimensional, homogeneous data structures. These are some examples of matrices:R - MatricesCreat 10 min read apply(), lapply(), sapply(), and tapply() in R In this article, we will learn about the apply(), lapply(), sapply(), and tapply() functions in the R Programming Language. The apply() collection is a part of R essential package. This family of functions helps us to apply a certain function to a certain data frame, list, or vector and return the r 4 min read R-Operators Operators are the symbols directing the compiler to perform various kinds of operations between the operands. Operators simulate the various mathematical, logical, and decision operations performed on a set of Complex Numbers, Integers, and Numericals as input operands. R supports majorly four kinds 5 min read Functions in R Programming A function accepts input arguments and produces the output by executing valid R commands that are inside the function. Functions are useful when we want to perform a certain task multiple times.In R Programming Language when we are creating a function the function name and the file in which we are c 5 min read Like