calculate AUC (GAM) in R [migrated]
- by ahmad
I used the following script to calculate AUC in R:
library(mgcv)
library(ROCR)
library(AUC)
data1=read.table("d:\\2005.txt", header=T)
GAM<-gam(tuna ~ s(chla)+s(sst)+s(ssha),family=binomial, data=data1)
gampred<- predict(GAM, type="response")
rp <- prediction(gampred, data1$tuna)
auc <- performance( rp, "auc")@y.values[[1]]
auc
roc <- performance( rp, "tpr", "fpr")
plot( roc )
But when I was running the script, the result is:
**rp <- prediction(gampred, data1$tuna)
Error in prediction(gampred, data1$tuna) :
Format of predictions is invalid.
>
> auc <- performance( rp, "auc")@y.values[[1]]
Error in performance(rp, "auc") : object 'rp' not found
> auc
function (x, min = 0, max = 1)
{
if (any(class(x) == "roc")) {
if (min != 0 || max != 1) {
x$fpr <- x$fpr[x$cutoffs >= min & x$cutoffs <= max]
x$tpr <- x$tpr[x$cutoffs >= min & x$cutoffs <= max]
}
ans <- 0
for (i in 2:length(x$fpr)) {
ans <- ans + 0.5 * abs(x$fpr[i] - x$fpr[i - 1]) *
(x$tpr[i] + x$tpr[i - 1])
}
}
else if (any(class(x) %in% c("accuracy", "sensitivity", "specificity"))) {
if (min != 0 || max != 1) {
x$cutoffs <- x$cutoffs[x$cutoffs >= min & x$cutoffs <=
max]
x$measure <- x$measure[x$cutoffs >= min & x$cutoffs <=
max]
}
ans <- 0
for (i in 2:(length(x$cutoffs))) {
ans <- ans + 0.5 * abs(x$cutoffs[i - 1] - x$cutoffs[i]) *
(x$measure[i] + x$measure[i - 1])
}
}
return(as.numeric(ans))
}
<bytecode: 0x03012f10>
<environment: namespace:AUC>
>
> roc <- performance( rp, "tpr", "fpr")
Error in performance(rp, "tpr", "fpr") : object 'rp' not found
> plot( roc )
Error in levels(labels) : argument "labels" is missing, with no default**
Can anybody help me to solve this problem?
Thank you in advance.