岭回归和lasso回归的r语言代码

本篇博文为转载,转载自 li603060971的博客
http://blog.csdn.net/li603060971/article/details/49508279
原文内容:
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(shujux1)  #先标准化数据  
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(shujux1)  #先标准化数据  
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)

附录数据

years y x1 x2 x3
x4 x5 x6 x7
1974 172.9 11246 681 105.9 10183 4110 11242 9
1975 352.94 10335 791 107.4 10414 3996 12693 6.5
1976 447.67 13156 607 114.4 13134 4689 16681 6
1977 404.02 6127 714 110.8 15033 6876 22131 4.75
1978 409.51 27419 911 99.4 17389 8636 31353 4.75
1979 619.71 25633 1231 91.4 21715 12339 43528 9.5
1980 1121.17 95684 2760 90.8 27075 16623 70752 10
1981 1506.94 105987 2651 86.3 31827 19937 125989 16
1982 1105.79 46230 2105 125.3 35393 24787 99468 10.5
1983 933.03 37165 3030 107.4 38823 25112 82478 10.5
1984 1008.54 48787 2810 106.6 46079 24414 54936 8.5
1985 1567.56 75808 2649 115.7 47871 22970 87135 6
1986 1960.06 123128 3031 110.1 54372 24403 129884 6.5
1987 2884.88 371406 3644 105.8 65602 30531 153044 5
1988 2556.72 198569 3690 101.6 74917 37861 215033 5.25

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值