由于我有二进制响应的数据,但事件很少,我想通过拟合bgeva模型而不是gam模型来改进其预测。为了证明和比较它的预测准确性并将其与我尝试过的其他模型进行比较,我需要计算 AUC 并绘制 ROC 曲线。
问题是我的代码,它适用于glmand gam,不适用于bgevaobject. 确切地说,该函数的使用predict()打印了 Error:
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)
Run Code Online (Sandbox Code Playgroud)
小智 1
#你可以计算一下
pred <-as.numeric(predict(out$gam.fit, type="response", newdata=dataSim))
Run Code Online (Sandbox Code Playgroud)
#你的例子
> auc
[1] 0.7840645
Run Code Online (Sandbox Code Playgroud)