【问题标题】:Aggregated logistic lasso regression in glmnetglmnet中的聚合逻辑套索回归
【发布时间】:2020-11-05 00:20:30
【问题描述】:

glm() 中,可以使用以下语法对bernoulli [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 包中对这种类型的聚合二项式数据进行建模?如果有,语法是什么?

【问题讨论】:

    标签: r logistic-regression glmnet lasso-regression


    【解决方案1】:

    是的,你可以这样做

    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 接受至少两个协变量

    在你的帖子中使用 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
    

    【讨论】:

      猜你喜欢
      • 2018-07-19
      • 2017-10-06
      • 2015-07-11
      • 2018-11-25
      • 2015-09-06
      • 2019-12-24
      • 1970-01-01
      • 2014-03-12
      • 2020-11-12
      相关资源
      最近更新 更多