Since I have data with a binary answer, but with rare events, I would like to improve its forecast by setting a model bgevainstead of a model gam. In order to prove and compare its prediction accuracy and compare it with other models that I tried, I need to calculate the AUC and plot the ROC curve.
The problem is that my code that works with glmand gamdoes not work with the object bgeva. Exactly, using the function predict()prints an 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 the package bgeva()and the code that I used to calculate the AUC, and plot the ROC curve for the objects glmand 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)
out <- bgeva(y ~ x1 + s(x2) + s(x3))
library(ROCR)
pred <-as.numeric(predict(out, type="response", newdata=dataSim))
rp <- prediction(pred, dataSim$y)
auc <- performance( rp, "auc")@y.values[[1]]
auc
library(mgcv)
out_gam <- gam(y ~ x1 + s(x2) + s(x3), family=binomial(link=logit))
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)
Pek84 source
share