1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
#------------------ # Data Preparation #------------------ #Read datasets #Download the data from <a href="http://datascience.esy.es/wp-content/uploads/2018/03/CreditData-1.zip">http://www.saedsayad.com/datasets/CreditData.zip</a> train <- read.csv("Credit_train.csv") test <- read.csv("Credit_test.csv") #Rows and Cols dim(train) dim(test) #Columns name colnames(train) colnames(test) #Show head(train) head(test) #--------------- # Decision tree #--------------- library(caret) library(rpart) library(rpart.plot) library(AUC) #train model.Dtree <- rpart(DEFAULT~., data = train, method="class") prp(model.Dtree) #lift chart pb <- NULL pb <- predict(model.Dtree, test) pb <- as.data.frame(pb) pred.Dtree <- data.frame(test$DEFAULT, pb$Y) colnames(pred.Dtree) <- c("target","score") lift.Dtree <- lift(target ~ score, data = pred.Dtree, cuts=10, class="Y") xyplot(lift.Dtree, main="Decision Tree - Lift Chart", type=c("l","g"), lwd=2 , scales=list(x=list(alternating=FALSE,tick.number = 10) ,y=list(alternating=FALSE,tick.number = 10))) #confusion matrix pc <- NULL pc <- ifelse(pb$N > pb$Y, "N", "Y") summary(as.data.frame(pc)) xtab <- table(pc, test$DEFAULT) caret::confusionMatrix(xtab, positive = "Y") #roc chart labels <- as.factor(ifelse(pred.Dtree$target=="Y", 1, 0)) predictions <- pred.Dtree$score auc(roc(predictions, labels), min = 0, max = 1) plot(roc(predictions, labels), min=0, max=1, type="l", main="Decision Tree - ROC Chart") |
Ejercicio en R Arbol
Mapa -> Predecir el Futuro (inferir) -> Modelizacion -> Clasificacion -> Arbol de Decisiones -> Ejercicio en R