5

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

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

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

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)
4

1 回答 1

1

#你可以计算

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

#你的例子

> auc
[1] 0.7840645
于 2020-12-18T01:03:51.307 回答