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 58 59 60 61 62 63 |
#------------------ # Data Preparation #------------------ #Read datasets #Download the data from http://datascience.esy.es/wp-content/uploads/2018/03/CreditData-1.zip 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) #----- # LDA #----- library(caret) library(AUC) library(MASS) #remove all records with missing values train <- na.omit(train) test <- na.omit(test) #train model.LDA <- lda(DEFAULT~., data=train) model.LDA #variables importance model.LDA$scaling #test pc <- predict(model.LDA, test) summary(pc$class) xtab <- table(pc$class, test$DEFAULT) caret::confusionMatrix(xtab, positive = "Y") #lift chart pb <- NULL pb <- pc$posterior pb <- as.data.frame(pb) pred.LDA <- data.frame(test$DEFAULT, pb$Y) colnames(pred.LDA) <- c("target","score") lift.LDA <- lift(target ~ score, data = pred.LDA, cuts=10, class="Y") xyplot(lift.LDA, main="LDA - Lift Chart", type=c("l","g"), lwd=2 , scales=list(x=list(alternating=FALSE,tick.number = 10) ,y=list(alternating=FALSE,tick.number = 10))) #roc chart labels <- as.factor(ifelse(pred.LDA$target=="Y", 1, 0)) predictions <- pred.LDA$score auc(roc(predictions, labels), min = 0, max = 1) plot(roc(predictions, labels), min=0, max=1, type="l", main="LDA - ROC Chart") |
Ejercicio en R LDA
Mapa -> Predecir el Futuro (inferir) -> Modelizacion -> Clasificacion -> Analisis de Discriminacion Lineal -> Ejercicio en R