我在下面复制了一个玩具示例,其中响应变量具有三个可能的类。我正在尝试创建一个 ROC,但不知道当有三个类时如何处理它。任何帮助将不胜感激。谢谢
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
对于两个类的情况,我用来执行以下操作,但它对三个类不再有效。
yhat <- pred[,2]
y = test[, -1]
plot.roc(y, yhat)
