【发布时间】:2015-11-24 14:09:16
【问题描述】:
由于我的数据具有二元响应,但很少发生事件,我想通过拟合 bgeva 模型而不是 gam 模型来改进其预测。为了证明和比较它的预测准确性并将其与我尝试过的其他模型进行比较,我需要计算 AUC 并绘制 ROC 曲线。
问题是我的代码适用于glm 和gam,不适用于bgeva 对象。准确地说,使用函数predict() 会打印错误:
no applicable method for 'predict' applied to an object of class "bgeva"
我的朋友 Google 没有为我找到任何解决方案。
这是来自bgeva() 包的一个简单示例以及我用来计算AUC 并绘制glm 和gam 对象的ROC 曲线的代码:
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)
【问题讨论】:
-
通过
str(out),我意识到bgeva对象中的模型没有拟合值。也许有人知道如何手动计算它们?然后我就不再需要predict()函数了。