glmnet 中的聚合逻辑套索回归

Aggregated logistic lasso regression in glmnet

glm() 中,可以使用以下语法通过逻辑回归对伯努利 [0,1] 结果进行建模。

glm(bin ~ x, df, family = "binomial")

不过,您也可以执行聚合二项式回归,其中每个观察值代表一定数量的伯努利试验中的目标事件计数。例如看下面的数据:

set.seed(1)
n <- 50
cov <- 10
x <- c(rep(0,n/2), rep(1, n/2))
p <- 0.4 + 0.2*x
y <- rbinom(n, cov, p)

对于这些类型的数据,您在 glm()

中使用的语法略有不同
mod <- glm(cbind(y, cov-y) ~ x, family="binomial")
mod

# output

# Call:  glm(formula = cbind(y, cov - y) ~ x, family = "binomial")
# 
# Coefficients:
#   (Intercept)            x  
# -0.3064       0.6786  
# 
# Degrees of Freedom: 49 Total (i.e. Null);  48 Residual
# Null Deviance:        53.72 
# Residual Deviance: 39.54  AIC: 178

我想知道是否可以在 glmnet 包中对这种类型的聚合二项式数据建模?如果是这样,语法是什么?

是的,您可以按以下方式进行

set.seed(1)
n <- 50
cov <- 10
x <- c(rep(0,n/2), rep(1, n/2))
x = cbind(x, xx = c(rep(0.5,20), rep(0.7, 20), rep(1,10)))
p <- 0.4 + 0.2*x
y <- rbinom(n, cov, p)

我在这里添加了另一个名为 xx 的协变量,因为 glmnet 接受至少两个协变量

在 post

中的 glm 中
mod <- glm(cbind(y, cov-y) ~ x, family="binomial")
mod

# output
# Call:  glm(formula = cbind(y, cov - y) ~ x, family = "binomial")

# Coefficients:
# (Intercept)           xx          xxx  
# 0.04366      0.86126     -0.64862  

# Degrees of Freedom: 49 Total (i.e. Null);  47 Residual
# Null Deviance:        53.72 
# Residual Deviance: 38.82  AIC: 179.3

在 glmnet 中,没有正则化 (lambda=0) 以重现与 glm 中类似的结果

library(glmnet)
fit = glmnet(x, cbind(cov-y,y), family="binomial", lambda=0)
coef(fit)
# output
# 3 x 1 sparse Matrix of class "dgCMatrix"
#                     s0
# (Intercept)  0.04352689
# x            0.86111234
# xx          -0.64831806