2017-04-24 92 views
1

我一直试图在R中创建一个随机化套索函数,但它似乎没有产生与Python sklearn随机化套索函数相同的结果。我在这里应用相同的理念,但无法理解其中的差异。该代码被修改基于该代码:randomized lasso function in R.R中的随机化套索

这里是代码和样本数据:

# generate synthetic data 
set.seed(100) 
size = 750 
x = matrix(runif(14*size),ncol=14) 
y = 10 * sin(pi*X[,1]*X[,2]) + 20*(X[,3]-0.5)**2 + 10*X[,4] + 5*X[,5] + runif(1,0,1) 
nbootstrap = 200 
nsteps = 20 
alpha = 0.2 

dimx <- dim(x) 
n <- dimx[1] 
p <- dimx[2] 
halfsize <- as.integer(n/2) 
freq <- matrix(0,1,p) 

for (i in seq(nbootstrap)) { 

    # Randomly reweight each variable 
    xs <- t(t(x)*runif(p,alpha,1)) 

    # Ramdomly split the sample in two sets 
    perm <- sample(dimx[1]) 
    i1 <- perm[1:halfsize] 
    i2 <- perm[(halfsize+1):n] 

    # run the randomized lasso on each sample and check which variables are selected 
    cv_lasso <- lars::cv.lars(xs[i1,],y[i1],plot.it=FALSE, mode = 'step') 
    idx <- which.max(cv_lasso$cv - cv_lasso$cv.error <= min(cv_lasso$cv)) 
    coef.lasso <- coef(lars::lars(xs[i1,],y[i1]))[idx,] 
    freq <- freq + abs(sign(coef.lasso)) 

    cv_lasso <- lars::cv.lars(xs[i2,],y[i2],plot.it=FALSE, mode = 'step') 
    idx <- which.max(cv_lasso$cv - cv_lasso$cv.error <= min(cv_lasso$cv)) 
    coef.lasso <- coef(lars::lars(xs[i1,],y[i1]))[idx,] 
    freq <- freq + abs(sign(coef.lasso)) 
    print(freq) 
} 

# normalize frequence in [0,1] 
freq <- freq/(2*nbootstrap) 

结果应当与类似于在此表(稳定性)所示的结果stability in python.然而,这方法和第一个超链接参考中显示的原始R代码没有找到相关特征X11至X14。不确定哪个部分在我的R代码中不能正常工作。

+0

当然。将阅读未来问题的指导原则。 –

回答

3

首先感谢您发布此问题。我很喜欢学习稳定性选择,同时查看代码和参考。其次,当你看到这个答案时,你可能会踢自己。我认为你的代码是有效的,但你忘了创建“Friedamn#1回归数据集”的强相关特征。您的第二个链接的Python代码如下:

#"Friedamn #1” regression problem 
Y = (10 * np.sin(np.pi*X[:,0]*X[:,1]) + 20*(X[:,2] - .5)**2 + 
    10*X[:,3] + 5*X[:,4] + np.random.normal(0,1)) 
#Add 3 additional correlated variables (correlated with X1-X3) 
X[:,10:] = X[:,:4] + np.random.normal(0, .025, (size,4)) 

您的代码不包含第二步。因此,除了第一对特征之外,所有特征都是噪声,并且被正确排除在稳定性选择算法之外。

+0

太好了。你拯救了我的一天。我现在踢自己。 –