简体   繁体   中英

R: How to compute AUC and ROC curve for ´bgeva´ objekt/model?

Since I have data with binary response, but rare events, I would like to improve its forecast by fitting a bgeva model instead of a gam model. To prove and compare it´s prediction accuracy and compare it to other models that I tried, I need to calculate AUC and plot a ROC curve.

The problem is that my code, which works with glm and gam , does not work with bgeva object. Precisely, the use of the function predict() prints the Error: no applicable method for 'predict' applied to an object of class "bgeva" and my friend Google did not find any solution for me.

Here is one simple Example from bgeva() package and the code that I used to calculate the AUC and plot the ROC curve for glm and gam objects:

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)

#You can to calculate

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

#your example

> auc
[1] 0.7840645

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM