问题描述
glmnet
程序包使用从最大lambda_max
缩放到的LASSO
调整参数lambda
范围,在该范围内未选择任何预测变量.我想找出glmnet
如何计算此lambda_max
值.例如,在一个琐碎的数据集中:
The glmnet
package uses a range of LASSO
tuning parameters lambda
scaled from the maximal lambda_max
under which no predictors are selected. I want to find out how glmnet
computes this lambda_max
value. For example, in a trivial dataset:
set.seed(1)
library("glmnet")
x <- matrix(rnorm(100*20),100,20)
y <- rnorm(100)
fitGLM <- glmnet(x,y)
max(fitGLM$lambda)
# 0.1975946
包插图( http://www.jstatsoft.org/v33/i01/paper )在第2.5节中描述了该值的计算方式如下:
The package vignette (http://www.jstatsoft.org/v33/i01/paper) describes in section 2.5 that it computes this value as follows:
sx <- as.matrix(scale(x))
sy <- as.vector(scale(y))
max(abs(colSums(sx*sy)))/100
# 0.1865232
显然接近,但值不相同.那么,是什么原因导致这种差异呢?在一个相关的问题中,如何计算lambda_max
进行逻辑回归?
Which clearly is close but not the same value. So, what causes this difference? And in a related question, how could I compute lambda_max
for a logistic regression?
推荐答案
要获得相同的结果,您需要使用带有n
而不是n-1
分母的标准偏差来标准化变量.
To get the same result you need to standardize the variables using a standard deviation with n
instead of n-1
denominator.
mysd <- function(y) sqrt(sum((y-mean(y))^2)/length(y))
sx <- scale(x,scale=apply(x, 2, mysd))
sx <- as.matrix(sx, ncol=20, nrow=100)
sy <- as.vector(scale(y, scale=mysd(y)))
max(abs(colSums(sx*sy)))/100
## [1] 0.1758808
fitGLM <- glmnet(sx,sy)
max(fitGLM$lambda)
## [1] 0.1758808
这篇关于glmnet如何计算最大lambda值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!