前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布

R lasso

作者头像
用户1359560
发布2018-08-27 11:34:50
5230
发布2018-08-27 11:34:50
举报
文章被收录于专栏:生信小驿站生信小驿站
代码语言:javascript
复制
library(HDeconometrics)
data("BRinf")
data=embed(BRinf,2)
y=data[,1]; x=data[,-c(1:ncol(BRinf))]

## == Break the data into in-sample and out-of-sample
y.in=y[1:100]; y.out=y[-c(1:100)]
x.in=x[1:100,]; x.out=x[-c(1:100),]

## == LASSO == ##
lasso=ic.glmnet(x.in,y.in,crit = "bic")
plot(lasso$glmnet,"lambda",ylim=c(-2,2))


plot(lasso)

The first plot above shows the variables going to zero as we increase the penalty in the objective function of the LASSO. The Second plot shows the BIC curve and the selected model.

代码语言:javascript
复制
# # # Now we can calculate the forecast:

## == Forecasting == ##
pred.lasso=predict(lasso,newdata=x.out)
plot(y.out, type="l")
lines(pred.lasso, col=2)
代码语言:javascript
复制
## = adaLASSO = ##
tau=1
first.step.coef=coef(lasso)[-1]
penalty.factor=abs(first.step.coef+1/sqrt(nrow(x)))^(-tau)
adalasso=ic.glmnet(x.in,y.in,crit="bic",penalty.factor=penalty.factor)
pred.adalasso=predict(adalasso,newdata=x.out)

plot(y.out, type="l")
lines(pred.lasso, col=2)
lines(pred.adalasso, col=4)
代码语言:javascript
复制
## = comparing the errors = ##
c(LASSO=sqrt(mean((y.out-pred.lasso)^2)), 
  adaLASSO=sqrt(mean((y.out-pred.adalasso)^2)))

LASSO

adaLASSO

0.1810612

0.1678397

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2018.02.13 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档