【问题标题】:Weighted logistic regression in RR中的加权逻辑回归
【发布时间】:2018-04-28 16:09:17
【问题描述】:

给定成功比例的样本数据加上样本量和自变量,我正在尝试在 R 中进行逻辑回归。

下面的代码做了我想要的,似乎给出了合理的结果,但看起来不像是一个明智的方法;实际上它使数据集的大小增加了一倍

datf <- data.frame(prop  = c(0.125, 0,  0.667, 1,  0.9),
                   cases = c(8,     1,  3,     3,  10),
                   x     = c(11,    12, 15,    16, 18))

datf2         <- rbind(datf,datf)
datf2$success <- rep(c(1, 0), each=nrow(datf))
datf2$cases   <- round(datf2$cases*ifelse(datf2$success,datf2$prop,1-datf2$prop))
fit2          <- glm(success ~ x, weight=cases, data=datf2, family="binomial")

datf$proppredicted    <- 1 / (1 + exp(-predict(fit2, datf)))
plot(datf$x, datf$proppredicted, type="l", col="red", ylim=c(0,1))
points(datf$x, datf$prop, cex=sqrt(datf$cases))

生成类似 的图表

这看起来相当明智。

但我不喜欢使用datf2 作为通过复制数据来区分成功和失败的方法。这样的事情有必要吗?

作为一个较小的问题,是否有更简洁的方法来计算预测比例?

【问题讨论】:

  • Close voters:这是一个关于如何使用glm的问题,而不是关于模型的统计质量。
  • 权重应该是试验次数,而不是成功次数。
  • @Slouei weight=cases 是成功的次数(当success==1)和不成功的次数(当success==0)所以总共是所有的试验

标签: r logistic-regression


【解决方案1】:

不需要像那样构建人工数据; glm 可以根据给定的数据集拟合您的模型。

> glm(prop ~ x, family=binomial, data=datf, weights=cases)

Call:  glm(formula = prop ~ x, family = binomial, data = datf, weights = cases)

Coefficients:
(Intercept)            x  
    -9.3533       0.6714  

Degrees of Freedom: 4 Total (i.e. Null);  3 Residual
Null Deviance:      17.3 
Residual Deviance: 2.043    AIC: 11.43

您将收到有关“非整数#successes”的警告,但那是因为glm 很傻。与您构建的数据集上的模型进行比较:

> fit2

Call:  glm(formula = success ~ x, family = "binomial", data = datf2, 
    weights = cases)

Coefficients:
(Intercept)            x  
    -9.3532       0.6713  

Degrees of Freedom: 7 Total (i.e. Null);  6 Residual
Null Deviance:      33.65 
Residual Deviance: 18.39    AIC: 22.39

回归系数(因此预测值)基本相等。但是,您的残余偏差和 AIC 是可疑的,因为您创建了人工数据点。

【讨论】:

  • 这很有帮助 - 我一直担心 Warning message: In eval(family$initialize) : non-integer #successes in a binomial glm! 并试图通过四舍五入来避免它。至于残余偏差和 AIC,我怀疑这两种方法都可能具有误导性:如果我有原始的 25 个观察值并使用 glm 未加权,我会得到非常不同的数字
猜你喜欢
  • 2011-11-22
  • 2012-10-04
  • 2018-01-26
  • 2018-02-12
  • 2014-06-20
  • 2021-11-13
  • 1970-01-01
  • 2014-06-26
  • 2019-08-20
相关资源
最近更新 更多