带套索修改的Lars算法

时间:2018-04-28 12:57:08

标签: r regression linear-regression least-squares

我正在尝试使用套索修改来实现lars算法。

enter image description here

在第3点。我被卡住了,我想编程,但我真的不明白。

我已经做过第1点和第2点,这是代码:

#1. Standardize the predictors to have mean zero and unit norm.
set.seed(19875)  
n <- 10  
p <- 5  
real_p <- 5  
x <- matrix(rnorm(n*p), nrow=n, ncol=p)
x <- x-matrix(apply(x,2,mean),ncol=ncol(x),nrow=nrow(x),byrow=T)
x <- x/matrix(apply(x,2,sd),ncol=ncol(x),nrow=nrow(x),byrow=T)
y <- apply(x[,1:real_p], 1, sum) + rnorm(n)

#Start with the residual r = y − y ¯, β1,β2,... ,βp = 0
r=y-mean(y)
beta=matrix(0, ncol=ncol(x), nrow=1)
#2. Find the predictor xj most correlated with r.
co= t(x)%*%r
j= (1:ncol(x))[abs(co)==max(abs(co))][1]

#3. Move βj from 0 towards its least-squares coefficient xj,ri, until some
#other competitor xk has as much correlation with the current residual
#as does xj.

我非常感谢任何澄清。

0 个答案:

没有答案