1. 程式人生 > >嶺迴歸和lasso迴歸的r語言程式碼

嶺迴歸和lasso迴歸的r語言程式碼

setwd("C:/Users/IBM/Desktop/研一課程/2.2迴歸分析/迴歸作業")  #設定當前的工作目錄
shuju=read.table("shuju.txt",header=T)
shuju   #讀取資料


#使用lm.ridge來進行嶺迴歸,其好處是可通過GCV自動選擇調節係數,但運算結果與書上不同
library(MASS)    #lm.ridge在MASS包中
sx1<-scale(shuju$x1)  #先標準化資料
sx2<-scale(shuju$x2)
sx3<-scale(shuju$x3)
sx4<-scale(shuju$x4)
sx5<-scale(shuju$x5)
sx6<-scale(shuju$x6)
sx7<-scale(shuju$x7)
sy<-scale(shuju$y)
shuju.x <- cbind(sx1,sx2,sx3,sx4,sx5,sx6,sx7)
la<-seq(0,1,0.05)
shuju.ridge<-lm.ridge(sy~sx1+sx2+sx3+sx4+sx5+sx6+sx7-1,lambda=la) #標準化資料要-1沒截距項
coef(shuju.ridge)


#刪除x4
fly.ridge1<-lm.ridge(sy~sx1+sx2+sx3+sx5+sx6+sx7-1,lambda=seq(0,1,0.001))#嶺迴歸


#再刪除x5
fly.ridge2<-lm.ridge(sy~sx1+sx2+sx3+sx6+sx7-1,lambda=seq(0,1,0.001))#嶺迴歸




-------------------------------------------------------------------------------
setwd("C:/Users/IBM/Desktop/研一課程/2.2迴歸分析/迴歸作業")  #設定當前的工作目錄
shuju=read.table("shuju.txt",header=T)
shuju   #讀取資料


#使用glmnet函式求Lasso
library(glmnet)
sx1<-scale(shuju$x1)  #先標準化資料
sx2<-scale(shuju$x2)
sx3<-scale(shuju$x3)
sx4<-scale(shuju$x4)
sx5<-scale(shuju$x5)
sx6<-scale(shuju$x6)
sx7<-scale(shuju$x7)
sy<-scale(shuju$y)
shuju.x <- cbind(sx1,sx2,sx3,sx4,sx5,sx6,sx7)
shuju.lasso <- glmnet(shuju.x,sy,alpha=1,lambda=la)

coef(shuju.lasso)

附錄資料

yearsy x1x2x3x4x5 x6x7
1974172.9 11246681 105.910183 411011242 9
1975352.94 10335791 107.410414 399612693 6.5
1976447.67 13156607 114.413134 468916681 6
1977404.02 6127714 110.815033 687622131 4.75
1978409.51 27419911 99.417389 863631353 4.75
1979619.71 256331231 91.421715 1233943528 9.5
19801121.17 956842760 90.827075 1662370752 10
19811506.94 1059872651 86.331827 19937125989 16
19821105.79 462302105 125.335393 2478799468 10.5
1983933.03 371653030 107.438823 2511282478 10.5
19841008.54 487872810 106.646079 2441454936 8.5
19851567.56 758082649 115.747871 2297087135 6
19861960.06 1231283031 110.154372 24403129884 6.5
19872884.88 3714063644 105.865602 30531153044 5
19882556.72 1985693690 101.674917 37861215033 5.25