【问题标题】:R: How to compute AUC and ROC curve for ´bgeva´ objekt/model?R:如何计算“bgeva”对象/模型的 AUC 和 ROC 曲线?
【发布时间】:2015-11-24 14:09:16
【问题描述】:

由于我的数据具有二元响应,但很少发生事件,我想通过拟合 bgeva 模型而不是 gam 模型来改进其预测。为了证明和比较它的预测准确性并将其与我尝试过的其他模型进行比较,我需要计算 AUC 并绘制 ROC 曲线。

问题是我的代码适用于glmgam,不适用于bgeva 对象。准确地说,使用函数predict() 会打印错误: no applicable method for 'predict' applied to an object of class "bgeva" 我的朋友 Google 没有为我找到任何解决方案。

这是来自bgeva() 包的一个简单示例以及我用来计算AUC 并绘制glmgam 对象的ROC 曲线的代码:

library(bgeva)

set.seed(0)
n <- 1500
x1 <- round(runif(n))
x2 <- runif(n)
x3 <- runif(n)
f1 <- function(x) (cos(pi*2*x)) + sin(pi*x)
f2 <- function(x) (x+exp(-30*(x-0.5)^2))
y <- as.integer(rlogis(n, location = -6 + 2*x1 + f1(x2) + f2(x3), scale  = 1) > 0)
dataSim <- data.frame(y,x1,x2,x3)

################
# bgeva model: #
################
out <- bgeva(y ~ x1 + s(x2) + s(x3))

# AUC for bgeva (does not work)##################################
library(ROCR)
pred <-as.numeric(predict(out, type="response", newdata=dataSim))
rp <- prediction(pred, dataSim$y) 
auc <- performance( rp, "auc")@y.values[[1]]
auc

################
# gam model:   #
################
library(mgcv)

out_gam <- gam(y ~ x1 + s(x2) + s(x3), family=binomial(link=logit))

# AUC and ROC for gam (the same code, works with gam) ############
 pred_gam <-as.numeric(predict(out_gam, type="response"))
 rp_gam <- prediction(pred_gam, dataSim$y)

 auc_gam <- performance( rp_gam, "auc")@y.values[[1]]
 auc_gam

 roc_gam <- performance( rp_gam, "tpr", "fpr")
 plot(roc_gam)

【问题讨论】:

  • 通过str(out),我意识到bgeva 对象中的模型没有拟合值。也许有人知道如何手动计算它们?然后我就不再需要predict() 函数了。

标签: r roc auc


【解决方案1】:

#你可以计算

pred <-as.numeric(predict(out$gam.fit, type="response", newdata=dataSim))

#你的例子

> auc
[1] 0.7840645

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-10-08
    • 2021-03-12
    • 2012-04-15
    • 2012-10-10
    • 2019-09-29
    • 1970-01-01
    • 2019-04-11
    相关资源
    最近更新 更多