{"word_count": 734, "character_count": 6478}
In order to illustrate the problem of chosing a classification model consider some simulated data,&#62; n = 500&#62; set.seed(1)&#62; X = rnorm(n)&#62; ma = 10-(X+1.5)^2*2&#62; mb = -10+(X-1.5)^2*2&#62; M = cbind(ma,mb)&#62; set.seed(1)&#62; Z = sample(1:2,size=n,replace=TRUE)&#62; Y = ma*(Z==1)+mb*(Z==2)+rnorm(n)*5&#62; df = data.frame(Z=as.factor(Z),X,Y)A first strategy is to split the dataset in two parts, a training dataset, and a testing dataset.&#62; df1 = training = df[1:300,]&#62; df2 = testing  = df[301:500,]The Holdout Method: Training and Testing DatasetsThe two datasets can be visualised below, with the training dataset on top, and the testing dataset below&#62; plot(df1$X,df1$Y,pch=19,col=c(rgb(1,0,0,.4),+ rgb(0,0,1,.4))[df1$Z])We can consider a simple classification tree.&#62; library(rpart)&#62; fit = rpart(Z~X+Y,data=df1)&#62; pred = function(x,y) predict(fit,newdata=data.frame(X=x,Y=y))[,1]To visualize it, use&#62; vx=seq(-3,3,length=101)&#62; vy=seq(-25,25,length=101)&#62; z=matrix(NA,length(vx),length(vy))&#62; for(i in 1:length(vx)){+   for(j in 1:length(vy))+  {z[i,j]=pred(vx[i],vy[j])}}and&#62; image(vx,vy,z,axes=FALSE,xlab="",ylab="")&#62; points(df1$X,df1$Y,pch=19,col=c(rgb(1,0,0,.4),+ rgb(0,0,1,.4))[df1$Z])We have on top the prediction on the training dataset, then below the prediction on the testing dataset. And finally on the bottom, the ROC curve for the testing dataset (black curve) as well as the training dataset (grey curve)&#62; Y1=as.numeric(df1$Z)-1&#62; Y2=as.numeric(df2$Z)-1&#62; library(ROCR)&#62; S1 = predict(fit,newdata=df1)[,1]&#62; S2 = predict(fit,newdata=df2)[,1]&#62; pred &#60;- prediction( S2, Y2 ) &#62; perf &#60;- performance( pred, "tpr", "fpr" ) &#62; plot( perf ) &#62; pred &#60;- prediction( S1, Y1 ) &#62; perf &#60;- performance( pred, "tpr", "fpr" ) &#62; plot( perf ,add=TRUE,col="grey")From that tree, it is natural to get a prediction using a random forest&#62; library(randomForest)&#62; fit=randomForest(Z~X+Y,data=df1)To get a prediction, here, use&#62; pred=function(x,y) + predict(fit,newdata=data.frame(X=x,Y=y),+ type="prob")[,2]Not that the fit is perfect on the training sample.  Another popoular model might be the logistic regression&#62; fit=glm(Z~X+Y,data=df1,family=binomial)&#62; pred=function(x,y) + predict(fit,newdata=data.frame(X=x,Y=y),+ type="response")The later is very close to the linear discriminent analysis&#62; library(MASS)&#62; fit=lda(Z~X+Y,data=df1,family=binomial)&#62; pred=function(x,y) + predict(fit,newdata=+ data.frame(X=x,Y=y))$posterior[,2]or a quadratic discriminent analysis&#62; fit=qda(Z~X+Y,data=df1,family=binomial)&#62; pred=function(x,y) + predict(fit,newdata=+ data.frame(X=x,Y=y))$posterior[,2]To try very different models, consider a k-nearest neighbor(here with 9 neighbors)&#62; library(caret)&#62; fit=knn3(Z~X+Y,data=df1,k=9)&#62; pred=function(x,y)  + predict(fit,newdata=data.frame(X=x,Y=y))[,2]some logistic regression with bivarriate splines&#62; library(mgcv)&#62; fit=gam(Z~s(X,Y),data=df1,family=binomial)&#62; pred=function(x,y) + predict(fit,newdata=data.frame(X=x,Y=y),+ type="response")or some gradient boosting algorithm&#62; library(dismo)&#62; df1$Z01 = 1*(df1$Z=="2")&#62; fit=gbm.step(data=df1, gbm.x = 2:3, gbm.y = 4,+     family = "bernoulli", tree.complexity = 5,+     learning.rate = 0.01, bag.fraction = 0.5)&#62; pred = function(x,y) + predict(fit,newdata=data.frame(X=x,Y=y),+ type="response",n.trees=400)Nevertheless, the use of this holdout method to create some training and testing datasets can be difficult. It can be done only when we have a lot of observations. And even so, we can still be unluckly when creating the testing sample (simply because of bad luck). An alternative is to use cross validation.Using Cross-ValidationHere, we consider one-leave-out cross-validation (but v-fold cross validation can also be consider, to get results faster).Consider here a classification tree. To derive the cross validation ROC curve, for instance, use&#62; FIT=list()&#62; for(i in 1:n) + FIT[[i]] = rpart(Z~X+Y,data=df[-i,])&#62; predict_i = function(i) + predict(FIT[[i]],newdata=df[i,])[,2] &#62; S = Vectorize(predict_i)(1:n)Here, I store the n models, where each time, one observation is removed, and then I get a prediction for the removed observation. And we can generate the ROC curve&#62; Y = as.numeric(df$Z)-1&#62; library(ROCR)&#62; pred = prediction( S, Y )&#62; perf = performance( pred, "tpr", "fpr" )&#62; plot( perf )Of course, one can easily get a code that runs faster, but that one is working on that small dataset.If we consider a random forest, we get&#62; FIT=list()&#62; for(i in 1:n) + FIT[[i]] = randomForest(Z~X+Y,data=df[-i,])&#62; predict_i = function(i) + predict(FIT[[i]],newdata=df[i,],+ type="prob")[,2] &#62; S = Vectorize(predict_i)(1:n)One can consider a logistic regression&#62; FIT=list()&#62; for(i in 1:n) + FIT[[i]] = glm(Z~X+Y,data=df[-i,],+ family=binomial)&#62; predict_i = function(i) + predict(FIT[[i]],newdata=df[i,],+ type="response") &#62; S = Vectorize(predict_i)(1:n)or a linear discriminent analysis&#62; FIT=list()&#62; for(i in 1:n) + FIT[[i]] = lda(Z~X+Y,data=df[-i,],+ family=binomial)&#62; predict_i = function(i) + predict(FIT[[i]],newdata=df[i,])$posterior[,2] &#62; S = Vectorize(predict_i)(1:n)while a quadratic discriminent analysis yields&#62; FIT=list()&#62; for(i in 1:n) + FIT[[i]] =  qda(Z~X+Y,data=df[-i,],+ family=binomial)&#62; predict_i = function(i) + predict(FIT[[i]],newdata=df[i,])$posterior[,2] &#62; S = Vectorize(predict_i)(1:n)Again, if we consider another type of model, such as a k-nearest neighbor (with k=5) we get&#62; FIT=list()&#62; for(i in 1:n) + FIT[[i]] = knn3(Z~X+Y,data=df[-i,],k=5)&#62; predict_i = function(i) + predict(FIT[[i]],newdata=df[i,])[,2] &#62; S = Vectorize(predict_i)(1:n)or a k nearest neighbor with k=9or k=21One can also consider a logistic regression with bivariate splinesor, last but not least, some gradient boosting model. On that one, we might have trouble storing 500 output from the gradient boosting function (the output is large). A faster code might be&#62; VS = rep(NA,n)&#62; for(i in 1:n){+  FIT = gbm.step(data=df[-i,], +  gbm.x = 2:3, gbm.y = 4, family = "bernoulli", +  tree.complexity = 5, learning.rate = 0.01,+  bag.fraction = 0.5)+  VS[i] = predict(FIT,newdata=df[i,],+  n.trees=400) + }Actually, it is now possible to compare all those models, with all ROC curves on a single graph