当存在权重时,glmnet 如何标准化变量?
How does glmnet standardize variables when weights are present?
glmnet 允许用户通过 weights
参数输入观察权重向量。 glmnet 还标准化(默认情况下)预测变量具有零均值和单位方差。我的问题是:当提供 weights
时,glmnet 是否使用每列的加权平均值(和标准差)或未加权平均值(和标准差)对预测变量进行标准化?
在 Link
中描述了 glmnet
的标准化
在 post 中,您可以看到计算标准化的 glmnet
来源的 Fortran 代码片段。 ("Proof" 段,第二个项目符号)。
我不熟悉 Fortran,但对我来说它看起来非常像实际上使用 weighted mean 和 sd.
编辑:来自 glmnet 插图:
"weights
is for the observation weights. Default is 1 for each
observation. (Note: glmnet rescales the weights to sum to N, the
sample size.)"
Fortran 代码中的 w
是重新缩放的权重,这似乎与 加权平均标准化.
一致
就其价值而言,与接受的答案一致,glmnet
中的权重是抽样权重,而不是逆方差权重。例如,如果您的观测值比 unique 观测值多得多,您可以压缩数据集并获得相同的系数估计值:
n <- 50
m <- 5
y_norm <- rnorm(n)
y_bool <- rbinom(n,1,.5)
x <- matrix(rnorm(n*m),n)
w <- rpois(n,3) + 1 # weights
w_indx <- rep(1:n,times=w) # weights index
m1 = glmnet(x, y_norm, weights = w)
m2 = glmnet(x[w_indx,] ,y_norm[w_indx])
all.equal(coef(m1,s=.1),
coef(m2,s=.1))
>>> TRUE
M1 = glmnet(x,y_bool,weights = w,family = "binomial")
M2 = glmnet(x[w_indx,],y_bool[w_indx],family = "binomial")
all.equal(coef(M1,s=.1),
coef(M2,s=.1))
>>> TRUE
当然,在 cv.glmnet 中使用权重时需要多加注意,因为聚合记录的权重应该使用多项式分布分布在多个折叠中...
glmnet 允许用户通过 weights
参数输入观察权重向量。 glmnet 还标准化(默认情况下)预测变量具有零均值和单位方差。我的问题是:当提供 weights
时,glmnet 是否使用每列的加权平均值(和标准差)或未加权平均值(和标准差)对预测变量进行标准化?
在 Link
中描述了glmnet
的标准化
在 post 中,您可以看到计算标准化的 glmnet
来源的 Fortran 代码片段。 ("Proof" 段,第二个项目符号)。
我不熟悉 Fortran,但对我来说它看起来非常像实际上使用 weighted mean 和 sd.
编辑:来自 glmnet 插图:
"
weights
is for the observation weights. Default is 1 for each observation. (Note: glmnet rescales the weights to sum to N, the sample size.)"
Fortran 代码中的 w
是重新缩放的权重,这似乎与 加权平均标准化.
就其价值而言,与接受的答案一致,glmnet
中的权重是抽样权重,而不是逆方差权重。例如,如果您的观测值比 unique 观测值多得多,您可以压缩数据集并获得相同的系数估计值:
n <- 50
m <- 5
y_norm <- rnorm(n)
y_bool <- rbinom(n,1,.5)
x <- matrix(rnorm(n*m),n)
w <- rpois(n,3) + 1 # weights
w_indx <- rep(1:n,times=w) # weights index
m1 = glmnet(x, y_norm, weights = w)
m2 = glmnet(x[w_indx,] ,y_norm[w_indx])
all.equal(coef(m1,s=.1),
coef(m2,s=.1))
>>> TRUE
M1 = glmnet(x,y_bool,weights = w,family = "binomial")
M2 = glmnet(x[w_indx,],y_bool[w_indx],family = "binomial")
all.equal(coef(M1,s=.1),
coef(M2,s=.1))
>>> TRUE
当然,在 cv.glmnet 中使用权重时需要多加注意,因为聚合记录的权重应该使用多项式分布分布在多个折叠中...