1

在下面的代码中,我希望两个打印语句打印相同的结果,因为我明确地将参数传递s给两个预测函数。

library(glmnet)

set.seed(1)
x=rnorm(100)
eps=rnorm(100)

y = 1 + x + x^2 + x^3 + eps

xmat=model.matrix(y~poly(x,10,raw=T),data=data.frame(x=x))

grid=10^seq(10,-2,length=100)

lasso.mod = glmnet(xmat,y, alpha=1,lambda=grid)
lasso.coef=predict(lasso.mod,type="coefficients",s=0.01495444)[1:10,]
print(lasso.coef)

lasso.mod = glmnet(xmat,y, alpha=1,lambda=5)
lasso.coef=predict(lasso.mod,type="coefficients",s=0.01495444)[1:10,]
print(lasso.coef)

但是,结果非常不同,我想了解原因。

          (Intercept)           (Intercept) poly(x, 10, raw = T)1 
         1.1329454011          0.0000000000          1.3081576745 
poly(x, 10, raw = T)2 poly(x, 10, raw = T)3 poly(x, 10, raw = T)4 
         0.6887020751          0.6576599481          0.0336098492 
poly(x, 10, raw = T)5 poly(x, 10, raw = T)6 poly(x, 10, raw = T)7 
         0.0566899437          0.0002744787          0.0006870169 
poly(x, 10, raw = T)8 
         0.0001053833 
          (Intercept)           (Intercept) poly(x, 10, raw = T)1 
             2.092266              0.000000              0.000000 
poly(x, 10, raw = T)2 poly(x, 10, raw = T)3 poly(x, 10, raw = T)4 
             0.000000              0.000000              0.000000 
poly(x, 10, raw = T)5 poly(x, 10, raw = T)6 poly(x, 10, raw = T)7 
             0.000000              0.000000              0.000000 
poly(x, 10, raw = T)8 
             0.000000 

我做了一个实验,我改成lasso.mod = glmnet(xmat,y, alpha=1,lambda=5)lasso.mod = glmnet(xmat,y, alpha=1,lambda=0.015)结果更接近了。

似乎 predict 函数依赖于grid传递给训练函数的 ,但文档似乎表明s参数 onpredict应该覆盖它。是否存在依赖关系,如果是,它是什么以及如何解决它来处理任意系数s

更新 1:我在glmnet.

      Do not
      supply a single value for 'lambda' (for predictions after CV
      use 'predict()' instead).  Supply instead a decreasing
      sequence of 'lambda' values. 'glmnet' relies on its warms
      starts for speed, and its often faster to fit a whole path
      than compute a single fit.

警告仅指性能而不是准确性/稳定性,但我尝试改变不同的递减顺序grid,结果predict仍然不同。

4

1 回答 1

1

The issue seems to be related to the fact that the s value you use does not belong to grid. Refer to predict.glmnet help for the use of exact. One way around it is to use the exact same s value you want to use in predict when building models using glmnet.

于 2013-11-29T20:31:25.393 回答