r language for LASSO, Ridge ridge regression model implementation and Elastic Net

Original link: http://tecdat.cn/?p=3795

 

Introduction

Glmnet is a likelihood to the maximum penalty by fitting generalized linear model packet. Regularization path is calculated for a lasso or elastic net penalty regularization parameter λ value at the grid. The algorithm is fast, it can be utilized in the sparsity input matrix x. It is consistent with the linear, logical and polynomials, Poisson and Cox regression model. It can be made from a variety of fitting the model prediction. It is also suitable for multi-response linear regression.

glmnet cyclic coordinate descent algorithm, optimizing an objective function which continuously on each parameter and other parameters fixed, and the cycle is repeated until convergence. The package also take advantage of the powerful rules to effectively limit the active set. Due to efficient updates and technologies, such as hot-start the initiative and set of convergence, our algorithm can calculate very quickly the solution path.

The code may be input sparse matrix format, and the coverage factor constraints. Glmnet its core is a set of Fortran subroutines that enable the implementation of very fast.

The package also includes a method for the prediction and drawing and perform the functions of the K-fold cross validation.

First, we load glmnet package:

library(glmnet)

## Loading required package: Matrix## Loaded glmnet 1.9-9

 

The default model used in the package is a Gaussian linear model or the "least squares", we will demonstrate in this section. We load a set of data created in advance for instructions. Users can load their own data, you can also use the data stored in the work area.

load("QuickStartExample.RData")

This command loads the input matrix x and y from the response vector R stored data files.

We use the most basic call to adapt the model glmnet.

fit=glmnet(x,y)

 

"Fit" is an object class, glmnet it contains all relevant information model fit for further use. We do not encourage users to directly extract components. Rather, the object, such as a variety of methods plot, print, coef and predict, so that we can perform these tasks more elegant.

We can show the coefficient by executing the plot function:

MSE on the test set

We see lasso (alpha = 1) here to do the best. We also see a range of different use of lambda and alpha.

Coefficient upper and lower limits

These models are enhanced range of features recently added. Suppose we want to fit our model, but is limited to the coefficient greater than -0.7 and less than 0.5. This is easily achieved by upper.limits and lower.limits parameters:

Punishment factor

This parameter allows the user to apply a separate penalty factor for each coefficient. The default value of each parameter 1, but other values ​​can be specified. In particular, any variable penalty.factor equal to zero will not be punished! Let [mathematical processing error] vĴ represent [mathematical processing errors] penalty factor Ĵ variables. Fine period becomes [mathematical processing errors] Please note that the penalty factor is added to readjust nvars internally.

When people have a preference for a priori knowledge or variable, which is very useful. In many cases, some variables may be very important, so always wanted to keep these variables, this can be achieved by setting the appropriate penalty factor is set to 0:

 

 

If you have any questions, please leave a comment below. 

 

 

Big Data tribe  - Chinese professional third-party data service providers to provide customized one-stop data mining and statistical analysis consultancy services

Statistical analysis and data mining consulting services: y0.cn/teradat (Consulting Services, please contact the official website customer service )

Click here to send me a messageQQ:3025393450

 

QQ exchange group: 186 388 004 

[Service] Scene  

Research; the company outsourcing; online and offline one training; data reptile collection; academic research; report writing; market research.

[Tribe] big data to provide customized one-stop data mining and statistical analysis consultancy

 

Welcome attention to micro-channel public number for more information about data dry!
 
 

Welcome to elective our R language data analysis will be mining will know the course!

Guess you like

Origin www.cnblogs.com/tecdat/p/12040907.html