我发现R函数
glmnet::glmnet()
在不同的lambda
序列中,对于相同的值会产生不同的结果: # Generate a small dataset at random.
set.seed(123)
X = matrix(runif(12), ncol = 3)
y = runif(nrow(X))
lda = sort(c((runif(1) + 1) * 0.001, 0.001, 0.001 * (1 - runif(1))), decreasing = T)
theModel = glmnet::glmnet(X, y, lambda = lda, intercept = T, standardize = T)
coef(theModel)[, 2] # Show the coefficients corresponding to lambda = 0.001
# (Intercept) V1 V2 V3
# 0.8794007 1.6709897 -0.9141978 -1.5334346
lda = sort(c((runif(1) + 1) * 0.001, 0.001, 0.001 * (1 - runif(1))), decreasing = T)
theModel = glmnet::glmnet(X, y, lambda = lda, intercept = T, standardize = T)
coef(theModel)[, 2] # Show the coefficients corresponding to lambda = 0.001
# (Intercept) V1 V2 V3
# 0.8793756 1.6708981 -0.9141037 -1.5333721
为什么会发生这种情况,如何避免它?
谢谢!
set.seed(...)
以提高再现性?(这并非绝对必要,因为该现象似乎很稳健...) - Ben Bolker