Current location - Education and Training Encyclopedia - Graduation thesis - Realization of Lasso regression analysis based on r language
Realization of Lasso regression analysis based on r language
Realization of Lasso regression analysis based on r language

Main steps:

Save data in csv format, separated by commas.

In R, data is read and then converted into matrix form.

Load the lars package and install it first.

Call lars function

Determine the number of steps with the minimum Cp value

Determine the selected variables and calculate the regression coefficient.

The specific code is as follows:

It should be noted that:

1, the method of data reading, file.choose () is used here. The advantage of this is that a window will pop up for you to select the file you want to load, which saves the trouble of entering the path.

2. Data should be converted into matrix form.

3.(la) You can see the R plane, which is 0.66 here, slightly lower.

4. What do you think of this painting? In the summary result, step 1 is the smallest Cp. In the figure, we can see the junction of step 1 and horizontal axis 0.0, and only the variable 1 is non-zero. So nongyangungun was chosen.

Ps: R language has only learned data input and some simple processing, but not the graphic visualization part. After writing the paper, study this part carefully ~ ~ and put up a flag here.