{"word_count": 280, "character_count": 3363}
Consider here the dataset used in a previous post, about visualising a classification (with more than 2 features),&#62; MYOCARDE=read.table(+ "http://freakonometrics.free.fr/saporta.csv",+ header=TRUE,sep=";")The default classification tree is&#62; arbre = rpart(factor(PRONO)~.,data=MYOCARDE)&#62; rpart.plot(arbre,type=4,extra=6)We can change the options here, such as the minimum number of observations, per node&#62; arbre = rpart(factor(PRONO)~.,data=MYOCARDE,+       control=rpart.control(minsplit=10))&#62; rpart.plot(arbre,type=4,extra=6)or&#62; arbre = rpart(factor(PRONO)~.,data=MYOCARDE,+        control=rpart.control(minsplit=5))&#62; rpart.plot(arbre,type=4,extra=6)To visualize that classification, use the following code (to get a projection on the first two components)&#62; library(FactoMineR) # ACP (sur les var continues)&#62; X = MYOCARDE[,1:7]&#62; acp = PCA(X,ncp=ncol(X))&#62; M = acp$var$coord&#62; Minv = solve(M)&#62; m = apply(X,2,mean)&#62; s = apply(X,2,sd)&#62; &#62; arbre = rpart(factor(PRONO)~.,data=MYOCARDE)&#62; pred2=function(d1,d2,Mat,tree){+   z=Mat %*% c(d1,d2,rep(0,ncol(X)-2))+   newd=data.frame(t(z*s+m))+   names(newd)=names(X)+   predict(tree,newdata=newd,+           type="prob")[2] }&#62; p=function(d1,d2) pred2(d1,d2,Minv,arbre)&#62; Outer &#60;- function(x,y,fun) {+   mat &#60;- matrix(NA, length(x), length(y))+   for (i in seq_along(x)) {+     for (j in seq_along(y)) +       mat[i,j]=fun(x[i],y[j])}+   return(mat)}&#62; xgrid=seq(-5,5,length=251)&#62; ygrid=seq(-5,5,length=251)&#62; zgrid=Outer(xgrid,ygrid,p)&#62; bluereds=c(+   rgb(1,0,0,(10:0)/25),rgb(0,0,1,(0:10)/25))&#62; acp2=PCA(MYOCARDE,quali.sup=8,graph=TRUE)&#62; plot(acp2, habillage = 8,col.hab=c("red","blue"))&#62; image(xgrid,ygrid,zgrid,add=TRUE,col=bluereds)&#62; contour(xgrid,ygrid,zgrid,add=TRUE,levels=.5)It is also possible to consider the case where&#62; arbre = rpart(factor(PRONO)~.,data=MYOCARDE,+        control=rpart.control(minsplit=5))Finaly, one can also grow more trees, obtained by sampling. This is the idea of bagging: we boostrap our observations, we grow some trees, and then, we aggregate the predicted values. On the grid&#62; xgrid=seq(-5,5,length=201)&#62; ygrid=seq(-5,5,length=201)the code is the following,&#62; Z = matrix(0,201,201)&#62; for(i in 1:200){+ indice = sample(1:nrow(MYOCARDE),+          size=nrow(MYOCARDE),+          replace=TRUE)+ ECHANTILLON=MYOCARDE[indice,]+ arbre_b = rpart(factor(PRONO)~.,+   data=ECHANTILLON)+ p2 = function(d1,d2) pred2(d1,d2, Minv,arbre_b)+ zgrid2_b = Outer(xgrid,ygrid,p2)+ Z = Z+zgrid2_b }&#62; Zgrid = Z/200To visualize it, use&#62; plot(acp2, habillage = 8,+ col.hab=c("red","blue"))&#62; image(xgrid,ygrid,Zgrid,add=TRUE,+ col=bluereds)&#62; contour(xgrid,ygrid,Zgrid,add=TRUE,+ levels=.5,lwd=3)Last, but not least, it is possible to use some random forrest algorithm. The method combines Breiman's bagging idea (mentioned previously) and the random selection of features.&#62; library(randomForest)&#62; foret = randomForest(factor(PRONO)~.,+          data=MYOCARDE)&#62; pF=function(d1,d2) pred2(d1,d2,Minv,foret)&#62; zgridF=Outer(xgrid,ygrid,pF) &#62; acp2=PCA(MYOCARDE,quali.sup=8,graph=TRUE)&#62; plot(acp2, habillage = 8,col.hab=c("red","blue"))&#62; image(xgrid,ygrid,Zgrid,add=TRUE,+ col=bluereds)&#62; contour(xgrid,ygrid,zgridF,+ add=TRUE,levels=.5,lwd=3)