--- title: "Arbres de décision et SVM" author: "Jean-Michel Marin" date: "octobre 2016" output: pdf_document toc: true numbersections: true --- Nous allons traiter les données pima. ```{r} library(MASS) data("Pima.te") data("Pima.tr") ``` # Algorithme CART ## Construction avec la fonction rpart de la bibliothèque rpart ```{r,fig.width=4,fig.height=4} library(rpart) model.cart <- rpart(type ~ .,data=Pima.tr) library(rpart.plot) rpart.plot(model.cart) ``` ## Elagage ```{r,fig.width=4,fig.height=4} printcp(model.cart) library(caret) cv.cart <- train(type~.,data=Pima.tr,method="rpart", metric="Accuracy",trControl=trainControl(method="repeatedcv", repeats=50,number=10),tuneGrid=data.frame(cp=seq(0.01,0.2,length=30))) plot(cv.cart) model.cart <- prune(model.cart,cp=as.numeric(cv.cart$best)) rpart.plot(model.cart) ``` ## Prédiction ```{r} pred.cart <- predict(model.cart,Pima.te,type="class") mean(Pima.te$type!=pred.cart) ``` # Fôrets aléatoires ## Construction avec la fonction randomForest de la bibliothèque randomForest ```{r, fig.width=4, fig.height=4, cache=TRUE} library(randomForest) model.rf <- randomForest(type~.,data=Pima.tr,ntree=500) plot(model.rf) varImpPlot(model.rf) ``` ## Prédiction ```{r, cache=TRUE} pred.rf <- predict(model.rf,Pima.te,type="class") mean(Pima.te$type!=pred.rf) ``` # SVM ## Construction avec la fonction svm de la bibliothéque e1071 ```{r, eval=TRUE} library(e1071) calibration <- tune.svm(type~.,data=Pima.tr,gamma=seq(0.001,1,by=0.01)) model.svm <- svm(type~.,data=Pima.tr,gamma=calibration$best.parameters) ``` ## Prédiction ```{r, eval=TRUE} pred.svm <- predict(model.svm,Pima.te) mean(Pima.te$type!=pred.svm) ```