I have a toy example reproduced below in which the response variable has three possible classes. I am trying to create an ROC but not sure how to deal with it when there are three classes. Any help will be appreciated. Thanks
library(ipred)
control = rpart.control(maxdepth = 20, minsplit = 20, cp = 0.01, maxsurrogate=2, surrogatestyle = 0, xval=25)
n <- 500; p <- 10
f <- function(x,a,b,d) return( a*(x-b)^2+d )
x1 <- runif(n/2,0,4)
y1 <- f(x1,-1,2,1.7)+runif(n/2,-1,1)
x2 <- runif(n/2,2,6)
y2 <- f(x2,1,4,-1.7)+runif(n/2,-1,1)
y <- c(rep(-1,floor(n/3)),rep(0,ceiling(n/3)), rep(1,ceiling(n/3)))
dat <- data.frame(y=factor(y),x1=c(x1,x2),x2=c(y1,y2), matrix(rnorm(n*(p-2)),ncol=(p-2)))
names(dat)<-c("y",paste("x",1:p,sep=""))
dat
plot(dat$x1,dat$x2,pch=c(1:2)[y], col=c(1,8)[y],
xlab=names(dat)[2],ylab=names(dat)[3])
indtrain<-sample(1:n,300,replace=FALSE)
train<-dat[indtrain,]; dim(train)
test<-dat[setdiff(1:n,indtrain),]; dim(test)
test
mod <- bagging(y~., data=train, control=control, coob=TRUE, nbagg=25, keepX = TRUE)
mod
pred<-predict(mod, newdata=test[,-1],type="prob", aggregation= "average"); pred
For two class case, I use to do the following but it is no longer valid for three classes.
yhat <- pred[,2]
y = test[, -1]
plot.roc(y, yhat)