library(rpart) attach(olives) n<-length(olives[,1]) fold <- 15 chunk <- trunc(n/fold) nn <- chunk*fold runs <- 20 Area <- as.factor(Area) CV <- matrix(0, runs, 1) for(i in 1:runs) { for(j in 1:fold) { test <- (1:chunk)*fold-fold+j train <- (1:nn)[-test] rpt <- rpart(Area ~ palmitic + palmitoleic + stearic + oleic + linoleic + linolenic + arachidic + eicosenoic, subset=train, cp=0.000001, minsplit=i, minbucket=i) conf <- as.matrix(table(predict(rpt, newdata=olives[test,4:11], type="class") , Area[test])) CV[i] <- CV[i] + sum(conf - diag(diag(conf), 9,9)) } } plot(CV, xlab="Minbucket", pch="o") lines(lowess(CV ~ (1:runs)))