Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

r

roc

auc

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)
like image 958
Peky84 Avatar asked Nov 10 '22 06:11

Peky84


1 Answers

#You can to calculate

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

#your example

> auc
[1] 0.7840645
like image 75
Felipe Barletta Avatar answered Nov 15 '22 07:11

Felipe Barletta