R Markdown

This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.

When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:

# Load required libraries
if (!require("C50")) install.packages("C50", dependencies = TRUE)
## Loading required package: C50
## Warning: package 'C50' was built under R version 4.4.3
if (!require("gmodels")) install.packages("gmodels", dependencies = TRUE)
## Loading required package: gmodels
## Warning: package 'gmodels' was built under R version 4.4.3
library(C50)
library(gmodels)

# Import the dataset
credit <- read.csv("C:/Users/yeu3178/Downloads/credit.csv", stringsAsFactors = TRUE)

# Preview structure and summary
str(credit)
## 'data.frame':    1000 obs. of  21 variables:
##  $ checking_balance    : Factor w/ 4 levels "< 0 DM","> 200 DM",..: 1 3 4 1 1 4 4 3 4 3 ...
##  $ months_loan_duration: int  6 48 12 42 24 36 24 36 12 30 ...
##  $ credit_history      : Factor w/ 5 levels "critical","delayed",..: 1 5 1 5 2 5 5 5 5 1 ...
##  $ purpose             : Factor w/ 10 levels "business","car (new)",..: 8 8 5 6 2 5 6 3 8 2 ...
##  $ amount              : int  1169 5951 2096 7882 4870 9055 2835 6948 3059 5234 ...
##  $ savings_balance     : Factor w/ 5 levels "< 100 DM","> 1000 DM",..: 5 1 1 1 1 5 4 1 2 1 ...
##  $ employment_length   : Factor w/ 5 levels "> 7 yrs","0 - 1 yrs",..: 1 3 4 4 3 3 1 3 4 5 ...
##  $ installment_rate    : int  4 2 2 2 3 2 3 2 2 4 ...
##  $ personal_status     : Factor w/ 4 levels "divorced male",..: 4 2 4 4 4 4 4 4 1 3 ...
##  $ other_debtors       : Factor w/ 3 levels "co-applicant",..: 3 3 3 2 3 3 3 3 3 3 ...
##  $ residence_history   : int  4 2 3 4 4 4 4 2 4 2 ...
##  $ property            : Factor w/ 4 levels "building society savings",..: 3 3 3 1 4 4 1 2 3 2 ...
##  $ age                 : int  67 22 49 45 53 35 53 35 61 28 ...
##  $ installment_plan    : Factor w/ 3 levels "bank","none",..: 2 2 2 2 2 2 2 2 2 2 ...
##  $ housing             : Factor w/ 3 levels "for free","own",..: 2 2 2 1 1 1 2 3 2 2 ...
##  $ existing_credits    : int  2 1 1 1 2 1 1 1 1 2 ...
##  $ default             : int  1 2 1 1 2 1 1 1 1 2 ...
##  $ dependents          : int  1 1 2 2 2 2 1 1 1 1 ...
##  $ telephone           : Factor w/ 2 levels "none","yes": 2 1 1 1 1 2 1 2 1 1 ...
##  $ foreign_worker      : Factor w/ 2 levels "no","yes": 2 2 2 2 2 2 2 2 2 2 ...
##  $ job                 : Factor w/ 4 levels "mangement self-employed",..: 2 2 4 2 2 4 2 1 4 1 ...
summary(credit)
##    checking_balance months_loan_duration                credit_history
##  < 0 DM    :274     Min.   : 4.0         critical              :293   
##  > 200 DM  : 63     1st Qu.:12.0         delayed               : 88   
##  1 - 200 DM:269     Median :18.0         fully repaid          : 40   
##  unknown   :394     Mean   :20.9         fully repaid this bank: 49   
##                     3rd Qu.:24.0         repaid                :530   
##                     Max.   :72.0                                      
##                                                                       
##        purpose        amount           savings_balance  employment_length
##  radio/tv  :280   Min.   :  250   < 100 DM     :603    > 7 yrs   :253    
##  car (new) :234   1st Qu.: 1366   > 1000 DM    : 48    0 - 1 yrs :172    
##  furniture :181   Median : 2320   101 - 500 DM :103    1 - 4 yrs :339    
##  car (used):103   Mean   : 3271   501 - 1000 DM: 63    4 - 7 yrs :174    
##  business  : 97   3rd Qu.: 3972   unknown      :183    unemployed: 62    
##  education : 50   Max.   :18424                                          
##  (Other)   : 55                                                          
##  installment_rate      personal_status      other_debtors residence_history
##  Min.   :1.000    divorced male: 50    co-applicant: 41   Min.   :1.000    
##  1st Qu.:2.000    female       :310    guarantor   : 52   1st Qu.:2.000    
##  Median :3.000    married male : 92    none        :907   Median :3.000    
##  Mean   :2.973    single male  :548                       Mean   :2.845    
##  3rd Qu.:4.000                                            3rd Qu.:4.000    
##  Max.   :4.000                                            Max.   :4.000    
##                                                                            
##                      property        age        installment_plan     housing   
##  building society savings:232   Min.   :19.00   bank  :139       for free:108  
##  other                   :332   1st Qu.:27.00   none  :814       own     :713  
##  real estate             :282   Median :33.00   stores: 47       rent    :179  
##  unknown/none            :154   Mean   :35.55                                  
##                                 3rd Qu.:42.00                                  
##                                 Max.   :75.00                                  
##                                                                                
##  existing_credits    default      dependents    telephone  foreign_worker
##  Min.   :1.000    Min.   :1.0   Min.   :1.000   none:596   no : 37       
##  1st Qu.:1.000    1st Qu.:1.0   1st Qu.:1.000   yes :404   yes:963       
##  Median :1.000    Median :1.0   Median :1.000                            
##  Mean   :1.407    Mean   :1.3   Mean   :1.155                            
##  3rd Qu.:2.000    3rd Qu.:2.0   3rd Qu.:1.000                            
##  Max.   :4.000    Max.   :2.0   Max.   :2.000                            
##                                                                          
##                       job     
##  mangement self-employed:148  
##  skilled employee       :630  
##  unemployed non-resident: 22  
##  unskilled resident     :200  
##                               
##                               
## 
# Set seed for reproducibility
set.seed(12345)

# Randomize the order of records
credit_rand <- credit[order(runif(1000)), ]

# Compare summaries to check similarity
summary(credit)
##    checking_balance months_loan_duration                credit_history
##  < 0 DM    :274     Min.   : 4.0         critical              :293   
##  > 200 DM  : 63     1st Qu.:12.0         delayed               : 88   
##  1 - 200 DM:269     Median :18.0         fully repaid          : 40   
##  unknown   :394     Mean   :20.9         fully repaid this bank: 49   
##                     3rd Qu.:24.0         repaid                :530   
##                     Max.   :72.0                                      
##                                                                       
##        purpose        amount           savings_balance  employment_length
##  radio/tv  :280   Min.   :  250   < 100 DM     :603    > 7 yrs   :253    
##  car (new) :234   1st Qu.: 1366   > 1000 DM    : 48    0 - 1 yrs :172    
##  furniture :181   Median : 2320   101 - 500 DM :103    1 - 4 yrs :339    
##  car (used):103   Mean   : 3271   501 - 1000 DM: 63    4 - 7 yrs :174    
##  business  : 97   3rd Qu.: 3972   unknown      :183    unemployed: 62    
##  education : 50   Max.   :18424                                          
##  (Other)   : 55                                                          
##  installment_rate      personal_status      other_debtors residence_history
##  Min.   :1.000    divorced male: 50    co-applicant: 41   Min.   :1.000    
##  1st Qu.:2.000    female       :310    guarantor   : 52   1st Qu.:2.000    
##  Median :3.000    married male : 92    none        :907   Median :3.000    
##  Mean   :2.973    single male  :548                       Mean   :2.845    
##  3rd Qu.:4.000                                            3rd Qu.:4.000    
##  Max.   :4.000                                            Max.   :4.000    
##                                                                            
##                      property        age        installment_plan     housing   
##  building society savings:232   Min.   :19.00   bank  :139       for free:108  
##  other                   :332   1st Qu.:27.00   none  :814       own     :713  
##  real estate             :282   Median :33.00   stores: 47       rent    :179  
##  unknown/none            :154   Mean   :35.55                                  
##                                 3rd Qu.:42.00                                  
##                                 Max.   :75.00                                  
##                                                                                
##  existing_credits    default      dependents    telephone  foreign_worker
##  Min.   :1.000    Min.   :1.0   Min.   :1.000   none:596   no : 37       
##  1st Qu.:1.000    1st Qu.:1.0   1st Qu.:1.000   yes :404   yes:963       
##  Median :1.000    Median :1.0   Median :1.000                            
##  Mean   :1.407    Mean   :1.3   Mean   :1.155                            
##  3rd Qu.:2.000    3rd Qu.:2.0   3rd Qu.:1.000                            
##  Max.   :4.000    Max.   :2.0   Max.   :2.000                            
##                                                                          
##                       job     
##  mangement self-employed:148  
##  skilled employee       :630  
##  unemployed non-resident: 22  
##  unskilled resident     :200  
##                               
##                               
## 
summary(credit_rand)
##    checking_balance months_loan_duration                credit_history
##  < 0 DM    :274     Min.   : 4.0         critical              :293   
##  > 200 DM  : 63     1st Qu.:12.0         delayed               : 88   
##  1 - 200 DM:269     Median :18.0         fully repaid          : 40   
##  unknown   :394     Mean   :20.9         fully repaid this bank: 49   
##                     3rd Qu.:24.0         repaid                :530   
##                     Max.   :72.0                                      
##                                                                       
##        purpose        amount           savings_balance  employment_length
##  radio/tv  :280   Min.   :  250   < 100 DM     :603    > 7 yrs   :253    
##  car (new) :234   1st Qu.: 1366   > 1000 DM    : 48    0 - 1 yrs :172    
##  furniture :181   Median : 2320   101 - 500 DM :103    1 - 4 yrs :339    
##  car (used):103   Mean   : 3271   501 - 1000 DM: 63    4 - 7 yrs :174    
##  business  : 97   3rd Qu.: 3972   unknown      :183    unemployed: 62    
##  education : 50   Max.   :18424                                          
##  (Other)   : 55                                                          
##  installment_rate      personal_status      other_debtors residence_history
##  Min.   :1.000    divorced male: 50    co-applicant: 41   Min.   :1.000    
##  1st Qu.:2.000    female       :310    guarantor   : 52   1st Qu.:2.000    
##  Median :3.000    married male : 92    none        :907   Median :3.000    
##  Mean   :2.973    single male  :548                       Mean   :2.845    
##  3rd Qu.:4.000                                            3rd Qu.:4.000    
##  Max.   :4.000                                            Max.   :4.000    
##                                                                            
##                      property        age        installment_plan     housing   
##  building society savings:232   Min.   :19.00   bank  :139       for free:108  
##  other                   :332   1st Qu.:27.00   none  :814       own     :713  
##  real estate             :282   Median :33.00   stores: 47       rent    :179  
##  unknown/none            :154   Mean   :35.55                                  
##                                 3rd Qu.:42.00                                  
##                                 Max.   :75.00                                  
##                                                                                
##  existing_credits    default      dependents    telephone  foreign_worker
##  Min.   :1.000    Min.   :1.0   Min.   :1.000   none:596   no : 37       
##  1st Qu.:1.000    1st Qu.:1.0   1st Qu.:1.000   yes :404   yes:963       
##  Median :1.000    Median :1.0   Median :1.000                            
##  Mean   :1.407    Mean   :1.3   Mean   :1.155                            
##  3rd Qu.:2.000    3rd Qu.:2.0   3rd Qu.:1.000                            
##  Max.   :4.000    Max.   :2.0   Max.   :2.000                            
##                                                                          
##                       job     
##  mangement self-employed:148  
##  skilled employee       :630  
##  unemployed non-resident: 22  
##  unskilled resident     :200  
##                               
##                               
## 
# Check record order
head(credit$amount, 10)
##  [1] 1169 5951 2096 7882 4870 9055 2835 6948 3059 5234
head(credit_rand$amount, 10)
##  [1] 1199 2576 1103 4020 1501 1568 4281  918 2629 1845
# 900 training, 100 testing
credit_train <- credit_rand[1:900, ]
credit_test <- credit_rand[901:1000, ]

# Check proportion of default in both sets
prop.table(table(credit_train$default))
## 
##         1         2 
## 0.7022222 0.2977778
prop.table(table(credit_test$default))
## 
##    1    2 
## 0.68 0.32
credit_train$default <- as.factor(credit_train$default)
credit_test$default  <- as.factor(credit_test$default)


# Build model
credit_model <- C5.0(credit_train[-17], credit_train$default)

# Model summary
summary(credit_model)
## 
## Call:
## C5.0.default(x = credit_train[-17], y = credit_train$default)
## 
## 
## C5.0 [Release 2.07 GPL Edition]      Thu Apr 10 14:11:37 2025
## -------------------------------
## 
## Class specified by attribute `outcome'
## 
## Read 900 cases (21 attributes) from undefined.data
## 
## Decision tree:
## 
## checking_balance = unknown: 1 (358/44)
## checking_balance in {< 0 DM,> 200 DM,1 - 200 DM}:
## :...foreign_worker = no:
##     :...installment_plan in {none,stores}: 1 (17/1)
##     :   installment_plan = bank:
##     :   :...residence_history <= 3: 2 (2)
##     :       residence_history > 3: 1 (2)
##     foreign_worker = yes:
##     :...credit_history in {fully repaid,fully repaid this bank}: 2 (61/20)
##         credit_history in {critical,delayed,repaid}:
##         :...months_loan_duration <= 11: 1 (76/13)
##             months_loan_duration > 11:
##             :...savings_balance = > 1000 DM: 1 (13)
##                 savings_balance in {< 100 DM,101 - 500 DM,501 - 1000 DM,
##                 :                   unknown}:
##                 :...checking_balance = > 200 DM:
##                     :...dependents > 1: 2 (3)
##                     :   dependents <= 1:
##                     :   :...credit_history in {delayed,repaid}: 1 (23/3)
##                     :       credit_history = critical:
##                     :       :...amount <= 2337: 2 (3)
##                     :           amount > 2337: 1 (6)
##                     checking_balance = < 0 DM:
##                     :...other_debtors = guarantor:
##                     :   :...credit_history = critical: 2 (1)
##                     :   :   credit_history in {delayed,repaid}: 1 (11/1)
##                     :   other_debtors in {co-applicant,none}:
##                     :   :...job = mangement self-employed: 1 (26/6)
##                     :       job in {skilled employee,unemployed non-resident,
##                     :       :       unskilled resident}:
##                     :       :...purpose in {domestic appliances,others,
##                     :           :           radio/tv,repairs,
##                     :           :           retraining}: 2 (33/10)
##                     :           purpose = business:
##                     :           :...job = skilled employee: 2 (3)
##                     :           :   job in {unemployed non-resident,
##                     :           :           unskilled resident}: 1 (3)
##                     :           purpose = education: [S1]
##                     :           purpose = car (new): [S2]
##                     :           purpose = car (used):
##                     :           :...amount > 6229: 2 (5)
##                     :           :   amount <= 6229: [S3]
##                     :           purpose = furniture:
##                     :           :...months_loan_duration > 27: 2 (9/1)
##                     :               months_loan_duration <= 27: [S4]
##                     checking_balance = 1 - 200 DM:
##                     :...savings_balance = unknown: 1 (34/6)
##                         savings_balance in {< 100 DM,101 - 500 DM,
##                         :                   501 - 1000 DM}:
##                         :...months_loan_duration > 45: 2 (11/1)
##                             months_loan_duration <= 45:
##                             :...installment_plan = stores:
##                                 :...age <= 35: 2 (4)
##                                 :   age > 35: 1 (2)
##                                 installment_plan = bank:
##                                 :...residence_history <= 1: 1 (3)
##                                 :   residence_history > 1:
##                                 :   :...existing_credits <= 1: 2 (5)
##                                 :       existing_credits > 1:
##                                 :       :...installment_rate > 2: 2 (3)
##                                 :           installment_rate <= 2: [S5]
##                                 installment_plan = none:
##                                 :...other_debtors = co-applicant: 2 (3/1)
##                                     other_debtors = guarantor: 1 (7/1)
##                                     other_debtors = none:
##                                     :...employment_length = 4 - 7 yrs:
##                                         :...age <= 41: 1 (16)
##                                         :   age > 41: 2 (3/1)
##                                         employment_length in {> 7 yrs,
##                                         :                     0 - 1 yrs,
##                                         :                     1 - 4 yrs,
##                                         :                     unemployed}:
##                                         :...amount > 7980: 2 (7)
##                                             amount <= 7980:
##                                             :...amount > 4746: 1 (10)
##                                                 amount <= 4746: [S6]
## 
## SubTree [S1]
## 
## savings_balance in {< 100 DM,101 - 500 DM,501 - 1000 DM}: 2 (6)
## savings_balance = unknown: 1 (2)
## 
## SubTree [S2]
## 
## savings_balance = 101 - 500 DM: 1 (1)
## savings_balance in {501 - 1000 DM,unknown}: 2 (4)
## savings_balance = < 100 DM:
## :...personal_status in {divorced male,female,single male}: 2 (29/6)
##     personal_status = married male: 1 (2)
## 
## SubTree [S3]
## 
## job in {skilled employee,unemployed non-resident}: 1 (8/1)
## job = unskilled resident: 2 (1)
## 
## SubTree [S4]
## 
## employment_length in {> 7 yrs,4 - 7 yrs}: 1 (7/1)
## employment_length = unemployed: 2 (2)
## employment_length = 0 - 1 yrs:
## :...job in {skilled employee,unemployed non-resident}: 1 (4)
## :   job = unskilled resident: 2 (1)
## employment_length = 1 - 4 yrs:
## :...property in {building society savings,unknown/none}: 1 (5)
##     property in {other,real estate}:
##     :...residence_history <= 2: 1 (4/1)
##         residence_history > 2: 2 (5)
## 
## SubTree [S5]
## 
## other_debtors = co-applicant: 2 (1)
## other_debtors in {guarantor,none}: 1 (3)
## 
## SubTree [S6]
## 
## housing = for free: 1 (2)
## housing = rent:
## :...credit_history = critical: 1 (1)
## :   credit_history in {delayed,repaid}: 2 (10/2)
## housing = own:
## :...savings_balance = 101 - 500 DM: 1 (6)
##     savings_balance in {< 100 DM,501 - 1000 DM}:
##     :...residence_history <= 1: 1 (8/1)
##         residence_history > 1:
##         :...installment_rate <= 1: 1 (2)
##             installment_rate > 1:
##             :...employment_length in {> 7 yrs,unemployed}: 1 (13/6)
##                 employment_length in {0 - 1 yrs,1 - 4 yrs}: 2 (10)
## 
## 
## Evaluation on training data (900 cases):
## 
##      Decision Tree   
##    ----------------  
##    Size      Errors  
## 
##      57  127(14.1%)   <<
## 
## 
##     (a)   (b)    <-classified as
##    ----  ----
##     590    42    (a): class 1
##      85   183    (b): class 2
## 
## 
##  Attribute usage:
## 
##  100.00% checking_balance
##   60.22% foreign_worker
##   57.89% credit_history
##   51.11% months_loan_duration
##   42.67% savings_balance
##   30.44% other_debtors
##   17.78% job
##   15.56% installment_plan
##   14.89% purpose
##   12.89% employment_length
##   10.22% amount
##    6.78% residence_history
##    5.78% housing
##    3.89% dependents
##    3.56% installment_rate
##    3.44% personal_status
##    2.78% age
##    1.56% property
##    1.33% existing_credits
## 
## 
## Time: 0.0 secs
# Predict on test set
credit_pred <- predict(credit_model, credit_test[-17])

# Evaluate predictions
CrossTable(credit_test$default, credit_pred,
           prop.chisq = FALSE, prop.c = FALSE, prop.r = FALSE,
           dnn = c("Actual Default", "Predicted Default"))
## 
##  
##    Cell Contents
## |-------------------------|
## |                       N |
## |         N / Table Total |
## |-------------------------|
## 
##  
## Total Observations in Table:  100 
## 
##  
##                | Predicted Default 
## Actual Default |         1 |         2 | Row Total | 
## ---------------|-----------|-----------|-----------|
##              1 |        54 |        14 |        68 | 
##                |     0.540 |     0.140 |           | 
## ---------------|-----------|-----------|-----------|
##              2 |        11 |        21 |        32 | 
##                |     0.110 |     0.210 |           | 
## ---------------|-----------|-----------|-----------|
##   Column Total |        65 |        35 |       100 | 
## ---------------|-----------|-----------|-----------|
## 
## 
# Train boosted model with 10 trials
credit_boost10 <- C5.0(credit_train[-17], credit_train$default, trials = 10)

# Predict using boosted model
credit_boost_pred10 <- predict(credit_boost10, credit_test[-17])

# Evaluate boosted predictions
CrossTable(credit_test$default, credit_boost_pred10,
           prop.chisq = FALSE, prop.c = FALSE, prop.r = FALSE,
           dnn = c("Actual Default", "Predicted Default"))
## 
##  
##    Cell Contents
## |-------------------------|
## |                       N |
## |         N / Table Total |
## |-------------------------|
## 
##  
## Total Observations in Table:  100 
## 
##  
##                | Predicted Default 
## Actual Default |         1 |         2 | Row Total | 
## ---------------|-----------|-----------|-----------|
##              1 |        63 |         5 |        68 | 
##                |     0.630 |     0.050 |           | 
## ---------------|-----------|-----------|-----------|
##              2 |        16 |        16 |        32 | 
##                |     0.160 |     0.160 |           | 
## ---------------|-----------|-----------|-----------|
##   Column Total |        79 |        21 |       100 | 
## ---------------|-----------|-----------|-----------|
## 
## 
# Define cost matrix
matrix_dimensions <- list(c("no", "yes"), c("no", "yes"))
names(matrix_dimensions) <- c("predicted", "actual")

error_cost <- matrix(c(0, 1, 4, 0), nrow = 2)

# Train cost-sensitive model
credit_cost <- C5.0(credit_train[-17], credit_train$default, costs = error_cost)
## Warning: no dimnames were given for the cost matrix; the factor levels will be
## used
# Predict with cost-sensitive model
credit_cost_pred <- predict(credit_cost, credit_test[-17])

# Evaluate cost-sensitive predictions
CrossTable(credit_test$default, credit_cost_pred,
           prop.chisq = FALSE, prop.c = FALSE, prop.r = FALSE,
           dnn = c("Actual Default", "Predicted Default"))
## 
##  
##    Cell Contents
## |-------------------------|
## |                       N |
## |         N / Table Total |
## |-------------------------|
## 
##  
## Total Observations in Table:  100 
## 
##  
##                | Predicted Default 
## Actual Default |         1 |         2 | Row Total | 
## ---------------|-----------|-----------|-----------|
##              1 |        38 |        30 |        68 | 
##                |     0.380 |     0.300 |           | 
## ---------------|-----------|-----------|-----------|
##              2 |         5 |        27 |        32 | 
##                |     0.050 |     0.270 |           | 
## ---------------|-----------|-----------|-----------|
##   Column Total |        43 |        57 |       100 | 
## ---------------|-----------|-----------|-----------|
## 
## 

Including Plots

You can also embed plots, for example:

Note that the echo = FALSE parameter was added to the code chunk to prevent printing of the R code that generated the plot.