#LDA
library(readxl)
## Warning: package 'readxl' was built under R version 3.6.1
data <- read_excel("C:/Users/Administrator/Downloads/Data Bank.xlsx") #importdata
View(data)
summary(data)
##    COLLATERAL      CHARACTER         CAPACITY       CONDITION   
##  Min.   :1.250   Min.   :-8.248   Min.   :1.000   Min.   :1.65  
##  1st Qu.:2.500   1st Qu.: 4.668   1st Qu.:2.976   1st Qu.:1.65  
##  Median :3.750   Median : 5.201   Median :3.277   Median :1.65  
##  Mean   :3.537   Mean   : 4.974   Mean   :3.278   Mean   :1.77  
##  3rd Qu.:3.750   3rd Qu.: 5.676   3rd Qu.:3.583   3rd Qu.:1.65  
##  Max.   :7.000   Max.   : 6.400   Max.   :4.786   Max.   :5.36  
##     CAPITAL          kode KOL     
##  Min.   : 4.000   Min.   :0.0000  
##  1st Qu.: 5.000   1st Qu.:0.0000  
##  Median : 8.000   Median :1.0000  
##  Mean   : 8.745   Mean   :0.6147  
##  3rd Qu.:10.000   3rd Qu.:1.0000  
##  Max.   :20.000   Max.   :1.0000
data = as.data.frame(data)
n      = dim(data)[1]
Y = as.matrix(data[,6])
#discriminantanalysis
library(lda)
library(MASS)
## Warning: package 'MASS' was built under R version 3.6.2
library(caret)
## Warning: package 'caret' was built under R version 3.6.2
## Loading required package: lattice
## Loading required package: ggplot2
## Warning: package 'ggplot2' was built under R version 3.6.2
model=lda( data[,6] ~ ., data[,1:5])
model
## Call:
## lda(data[, 6] ~ ., data = data[, 1:5])
## 
## Prior probabilities of groups:
##         0         1 
## 0.3852814 0.6147186 
## 
## Group means:
##   COLLATERAL CHARACTER CAPACITY CONDITION  CAPITAL
## 0   4.148989  4.740983 3.141124  1.931292 8.353933
## 1   3.153944  5.120715 3.364276  1.668680 8.989437
## 
## Coefficients of linear discriminants:
##                     LD1
## COLLATERAL -0.660192460
## CHARACTER   0.029602165
## CAPACITY    1.285572537
## CONDITION  -0.961248155
## CAPITAL    -0.003063417
fit <- lda(data[,6] ~ ., data[,1:5], CV=TRUE)
compare = confusionMatrix(table(data[,6],fit$class))
compare
## Confusion Matrix and Statistics
## 
##    
##       0   1
##   0 190 166
##   1  40 528
##                                           
##                Accuracy : 0.7771          
##                  95% CI : (0.7488, 0.8035)
##     No Information Rate : 0.7511          
##     P-Value [Acc > NIR] : 0.0357          
##                                           
##                   Kappa : 0.496           
##                                           
##  Mcnemar's Test P-Value : <2e-16          
##                                           
##             Sensitivity : 0.8261          
##             Specificity : 0.7608          
##          Pos Pred Value : 0.5337          
##          Neg Pred Value : 0.9296          
##              Prevalence : 0.2489          
##          Detection Rate : 0.2056          
##    Detection Prevalence : 0.3853          
##       Balanced Accuracy : 0.7934          
##                                           
##        'Positive' Class : 0               
## 
#CART
library(rpart)
library(rpart.plot)
## Warning: package 'rpart.plot' was built under R version 3.6.3
model1 = rpart(data[,6] ~ ., data[,1:5], method = "class")
# Plot the trees
rpart.plot(model1)

fit = predict(model1, data[,1:5], type = 'class')
compare2 = confusionMatrix(table(data[,6],fit))
compare2
## Confusion Matrix and Statistics
## 
##    fit
##       0   1
##   0 246 110
##   1  22 546
##                                           
##                Accuracy : 0.8571          
##                  95% CI : (0.8329, 0.8791)
##     No Information Rate : 0.71            
##     P-Value [Acc > NIR] : < 2.2e-16       
##                                           
##                   Kappa : 0.6838          
##                                           
##  Mcnemar's Test P-Value : 3.665e-14       
##                                           
##             Sensitivity : 0.9179          
##             Specificity : 0.8323          
##          Pos Pred Value : 0.6910          
##          Neg Pred Value : 0.9613          
##              Prevalence : 0.2900          
##          Detection Rate : 0.2662          
##    Detection Prevalence : 0.3853          
##       Balanced Accuracy : 0.8751          
##                                           
##        'Positive' Class : 0               
##