对使用 R 的 glm 中的 "weights" 参数的逻辑回归执行 ROC 曲线

Perform ROC curve for logistic regression that uses the "weights" argument in R's glm

我目前 运行 逻辑回归需要在 glm 函数中使用 "whights" 参数,如下所示:

model <-glm(cr ~kw_url+row_number+domn*plform+score100,family=binomial,weights=weights,data=glm_data)

head(glm_data[cr>0 & cr <1]) 
   kw_url  plform row_number   domn score    cr weights score100
1:  other Desktop          0 ***  0.25 0.007407407     135       25
2:  other Desktop          0 d***  0.24 0.011494253      87       24
3:  other  Mobile          0 ***  0.14 0.001414427     707       14
4:  other  Mobile          1 ***  0.43 0.013888889     144       43
5:  other  Mobile          2 ***  0.38 0.027027027      37       38
6:  other  Mobile          1 ***  0.48 0.014285714      70       48

head(glm_data[cr>0 & cr <1,.(cr)]) #Dependant variable is a fraction!, not 0 or 1
            cr
1: 0.007407407
2: 0.011494253
3: 0.001414427
4: 0.013888889
5: 0.027027027
6: 0.014285714

我通常使用 pROCROCR 库来执行 ROC 曲线,尽管它们要求回归的因变量为 0 或 1,但不一小部分。

由于这个问题,我收到以下错误:

library(ROCR)
> p <- predict(bayes_model, newdata=glm_data, type="response")
> pr <- prediction(p, glm_data$cr)
Error in prediction(p, glm_data$cr) : 
  Number of classes is not equal to 2.
ROCR currently supports only evaluation of binary classification tasks

所以我的问题是:是否有一些 R 包可以生成 ROC 曲线,并支持 R 的 glm 加权数据函数?

那就试试这个吧。它不是一个包,但应该得到 ROC。 prob 是逻辑回归的概率。如果这仍然太多,那么就拿一个样本吧。

d <- data.frame(cr = c(1/212, 1/142, 1/15*2, 10/16, 10/3), 
                weight = c(212, 142, 15, 16, 3), 
                prob = c(1/200, 1/100, 1/35, 1/2, .7))


d$N <- (1 + d$cr) * d$weight
d$y <- d$cr * d$weight
o <- order(d$prob)
d <- d[o,]

N <- sum(d$y)
TOT <- sum(d$N)

x.plot <- cumsum(d$y) / N
y.plot <- cumsum(d$N) / (TOT - N)


plot(x.plot, y.plot, type = 'b')