library(rpart) TrainC<-read.table("trainC.dat") names(TrainC)<-c("x1","x2","y") xp <- seq(min(TrainC$x1), max(TrainC$x1), length = 50); np <- length(xp) yp <- seq(min(TrainC$x2), max(TrainC$x2), length = 50) pt <- expand.grid(x1 = xp, x2 = yp) par(mfcol=c(2,1)) Z.rp <- rpart(y ~ x1 + x2, data=TrainC) Z.rp.t <- predict(Z.rp, pt ) zp.rp <- Z.rp.t[,1]-Z.rp.t[,2] plot(TrainC[, 1], TrainC[, 2], xlab = "x1", ylab = "x2", col=codes(TrainC$y)+1) title("RP TrainC") contour(xp, yp, matrix(zp.rp, np), add = T, levels = 0, labex = 0) TestC <- read.table("ExamC.dat") names(TestC)<-c("x1","x2","y") N <- length(TestC[,1]) xp <- seq(min(TestC$x1), max(TestC$x1), length = 50); np <- length(xp) yp <- seq(min(TestC$x2), max(TestC$x2), length = 50) pt <- expand.grid(x1 = xp, x2 = yp) yhat <- predict(Z.rp, TestC[,-3] ) yThat <- rep('d', length(yhat[,1])) for(i in 1:length(yhat[,1])) { if( yhat[i,1] > yhat[i,2] ) yThat[i]="a" else yThat[i]="b" } plot(TestC[, 1], TestC[, 2], xlab = "x1", ylab = "x2", col=codes(TestC$y)+1) title("RP ExamC") T.rp.t <- predict(Z.rp, pt ) zp.rp <- T.rp.t[,1]-T.rp.t[,2] contour(xp, yp, matrix(zp.rp, np), add = T, levels = 0, labex = 0)