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.
# # # Now we can calculate the forecast:
## == Forecasting == ##
pred.lasso=predict(lasso,newdata=x.out)
plot(y.out, type="l")
lines(pred.lasso, col=2)
## = 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)
## = 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 |
网友评论