library(readr)
creditS <- read_csv("C:/GGTUAN/DREAMS/Yankee/TSU/MSc_TSU/Spring_2024/CS-583 Data Minning/creditS.csv")
## Rows: 1001 Columns: 21
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (13): checking_balance, credit_history, purpose, savings_balance, employ...
## dbl  (8): months_loan_duration, amount, installment_rate, residence_history,...
## 
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
View(creditS)

#I(color=green) <- [(1/3)* log2(1/3) + (2/3)*log2((2/3))]

summary(creditS)
##  checking_balance   months_loan_duration credit_history       purpose         
##  Length:1001        Min.   : 4.0         Length:1001        Length:1001       
##  Class :character   1st Qu.:12.0         Class :character   Class :character  
##  Mode  :character   Median :18.0         Mode  :character   Mode  :character  
##                     Mean   :20.9                                              
##                     3rd Qu.:24.0                                              
##                     Max.   :72.0                                              
##                     NA's   :1                                                 
##      amount      savings_balance    employment_length  installment_rate
##  Min.   :  250   Length:1001        Length:1001        Min.   :1.000   
##  1st Qu.: 1366   Class :character   Class :character   1st Qu.:2.000   
##  Median : 2320   Mode  :character   Mode  :character   Median :3.000   
##  Mean   : 3271                                         Mean   :2.973   
##  3rd Qu.: 3972                                         3rd Qu.:4.000   
##  Max.   :18424                                         Max.   :4.000   
##  NA's   :1                                             NA's   :1       
##  personal_status    other_debtors      residence_history   property        
##  Length:1001        Length:1001        Min.   :1.000     Length:1001       
##  Class :character   Class :character   1st Qu.:2.000     Class :character  
##  Mode  :character   Mode  :character   Median :3.000     Mode  :character  
##                                        Mean   :2.845                       
##                                        3rd Qu.:4.000                       
##                                        Max.   :4.000                       
##                                        NA's   :1                           
##       age        installment_plan     housing          existing_credits
##  Min.   :19.00   Length:1001        Length:1001        Min.   :1.000   
##  1st Qu.:27.00   Class :character   Class :character   1st Qu.:1.000   
##  Median :33.00   Mode  :character   Mode  :character   Median :1.000   
##  Mean   :35.55                                         Mean   :1.407   
##  3rd Qu.:42.00                                         3rd Qu.:2.000   
##  Max.   :75.00                                         Max.   :4.000   
##  NA's   :1                                             NA's   :1       
##     default      dependents     telephone         foreign_worker    
##  Min.   :1.0   Min.   :1.000   Length:1001        Length:1001       
##  1st Qu.:1.0   1st Qu.:1.000   Class :character   Class :character  
##  Median :1.0   Median :1.000   Mode  :character   Mode  :character  
##  Mean   :1.3   Mean   :1.155                                        
##  3rd Qu.:2.0   3rd Qu.:1.000                                        
##  Max.   :2.0   Max.   :2.000                                        
##  NA's   :1     NA's   :1                                            
##      job           
##  Length:1001       
##  Class :character  
##  Mode  :character  
##                    
##                    
##                    
## 
dim(creditS)
## [1] 1001   21
str(creditS)
## spc_tbl_ [1,001 × 21] (S3: spec_tbl_df/tbl_df/tbl/data.frame)
##  $ checking_balance    : chr [1:1001] "unknown" "1 - 200 DM" "< 0 DM" "< 0 DM" ...
##  $ months_loan_duration: num [1:1001] 6 9 6 12 6 6 6 12 15 12 ...
##  $ credit_history      : chr [1:1001] "critical" "repaid" "critical" "fully repaid this bank" ...
##  $ purpose             : chr [1:1001] "car (new)" "car (new)" "radio/tv" "retraining" ...
##  $ amount              : num [1:1001] 250 276 338 339 343 362 368 385 392 409 ...
##  $ savings_balance     : chr [1:1001] "> 1000 DM" "< 100 DM" "501 - 1000 DM" "< 100 DM" ...
##  $ employment_length   : chr [1:1001] "1 - 4 yrs" "1 - 4 yrs" "> 7 yrs" "> 7 yrs" ...
##  $ installment_rate    : num [1:1001] 2 4 4 4 4 4 4 4 4 3 ...
##  $ personal_status     : chr [1:1001] "female" "married male" "single male" "married male" ...
##  $ other_debtors       : chr [1:1001] "none" "none" "none" "none" ...
##  $ residence_history   : num [1:1001] 2 4 4 1 1 4 4 3 4 3 ...
##  $ property            : chr [1:1001] "real estate" "real estate" "other" "other" ...
##  $ age                 : num [1:1001] 41 22 52 45 27 52 38 58 23 42 ...
##  $ installment_plan    : chr [1:1001] "bank" "none" "none" "bank" ...
##  $ housing             : chr [1:1001] "own" "rent" "own" "own" ...
##  $ existing_credits    : num [1:1001] 2 1 2 1 1 2 1 4 1 2 ...
##  $ default             : num [1:1001] 1 1 1 1 1 1 1 1 1 1 ...
##  $ dependents          : num [1:1001] 1 1 1 1 1 1 1 1 1 1 ...
##  $ telephone           : chr [1:1001] "none" "none" "none" "none" ...
##  $ foreign_worker      : chr [1:1001] "yes" "yes" "yes" "yes" ...
##  $ job                 : chr [1:1001] "unskilled resident" "unskilled resident" "skilled employee" "unskilled resident" ...
##  - attr(*, "spec")=
##   .. cols(
##   ..   checking_balance = col_character(),
##   ..   months_loan_duration = col_double(),
##   ..   credit_history = col_character(),
##   ..   purpose = col_character(),
##   ..   amount = col_double(),
##   ..   savings_balance = col_character(),
##   ..   employment_length = col_character(),
##   ..   installment_rate = col_double(),
##   ..   personal_status = col_character(),
##   ..   other_debtors = col_character(),
##   ..   residence_history = col_double(),
##   ..   property = col_character(),
##   ..   age = col_double(),
##   ..   installment_plan = col_character(),
##   ..   housing = col_character(),
##   ..   existing_credits = col_double(),
##   ..   default = col_double(),
##   ..   dependents = col_double(),
##   ..   telephone = col_character(),
##   ..   foreign_worker = col_character(),
##   ..   job = col_character()
##   .. )
##  - attr(*, "problems")=<externalptr>
credits2 <- creditS[-1001,]

credits2
## # A tibble: 1,000 × 21
##    checking_balance months_loan_duration credit_history         purpose   amount
##    <chr>                           <dbl> <chr>                  <chr>      <dbl>
##  1 unknown                             6 critical               car (new)    250
##  2 1 - 200 DM                          9 repaid                 car (new)    276
##  3 < 0 DM                              6 critical               radio/tv     338
##  4 < 0 DM                             12 fully repaid this bank retraini…    339
##  5 < 0 DM                              6 repaid                 domestic…    343
##  6 1 - 200 DM                          6 critical               car (new)    362
##  7 unknown                             6 repaid                 radio/tv     368
##  8 < 0 DM                             12 critical               radio/tv     385
##  9 1 - 200 DM                         15 repaid                 education    392
## 10 unknown                            12 fully repaid this bank radio/tv     409
## # ℹ 990 more rows
## # ℹ 16 more variables: savings_balance <chr>, employment_length <chr>,
## #   installment_rate <dbl>, personal_status <chr>, other_debtors <chr>,
## #   residence_history <dbl>, property <chr>, age <dbl>, installment_plan <chr>,
## #   housing <chr>, existing_credits <dbl>, default <dbl>, dependents <dbl>,
## #   telephone <chr>, foreign_worker <chr>, job <chr>
str(credits2)
## tibble [1,000 × 21] (S3: tbl_df/tbl/data.frame)
##  $ checking_balance    : chr [1:1000] "unknown" "1 - 200 DM" "< 0 DM" "< 0 DM" ...
##  $ months_loan_duration: num [1:1000] 6 9 6 12 6 6 6 12 15 12 ...
##  $ credit_history      : chr [1:1000] "critical" "repaid" "critical" "fully repaid this bank" ...
##  $ purpose             : chr [1:1000] "car (new)" "car (new)" "radio/tv" "retraining" ...
##  $ amount              : num [1:1000] 250 276 338 339 343 362 368 385 392 409 ...
##  $ savings_balance     : chr [1:1000] "> 1000 DM" "< 100 DM" "501 - 1000 DM" "< 100 DM" ...
##  $ employment_length   : chr [1:1000] "1 - 4 yrs" "1 - 4 yrs" "> 7 yrs" "> 7 yrs" ...
##  $ installment_rate    : num [1:1000] 2 4 4 4 4 4 4 4 4 3 ...
##  $ personal_status     : chr [1:1000] "female" "married male" "single male" "married male" ...
##  $ other_debtors       : chr [1:1000] "none" "none" "none" "none" ...
##  $ residence_history   : num [1:1000] 2 4 4 1 1 4 4 3 4 3 ...
##  $ property            : chr [1:1000] "real estate" "real estate" "other" "other" ...
##  $ age                 : num [1:1000] 41 22 52 45 27 52 38 58 23 42 ...
##  $ installment_plan    : chr [1:1000] "bank" "none" "none" "bank" ...
##  $ housing             : chr [1:1000] "own" "rent" "own" "own" ...
##  $ existing_credits    : num [1:1000] 2 1 2 1 1 2 1 4 1 2 ...
##  $ default             : num [1:1000] 1 1 1 1 1 1 1 1 1 1 ...
##  $ dependents          : num [1:1000] 1 1 1 1 1 1 1 1 1 1 ...
##  $ telephone           : chr [1:1000] "none" "none" "none" "none" ...
##  $ foreign_worker      : chr [1:1000] "yes" "yes" "yes" "yes" ...
##  $ job                 : chr [1:1000] "unskilled resident" "unskilled resident" "skilled employee" "unskilled resident" ...
table(credits2$checking_balance)
## 
##           < 0 DM         > 200 DM       1 - 200 DM checking_balance 
##              274               63              268                1 
##          unknown 
##              394
prop.table(table(credits2$checking_balance))
## 
##           < 0 DM         > 200 DM       1 - 200 DM checking_balance 
##            0.274            0.063            0.268            0.001 
##          unknown 
##            0.394
credits2[156,1]= as.factor("1 - 200 DM")
credits2[155,1]= as.factor("1 - 200 DM")
credits2[155,1]
## # A tibble: 1 × 1
##   checking_balance
##   <chr>           
## 1 1 - 200 DM
prop.table(table(credits2$checking_balance))
## 
##     < 0 DM   > 200 DM 1 - 200 DM    unknown 
##      0.274      0.063      0.269      0.394
prop.table(table(credits2$savings_balance))
## 
##      < 100 DM     > 1000 DM  101 - 500 DM 501 - 1000 DM       unknown 
##         0.603         0.048         0.103         0.063         0.183
summary(credits2$months_loan_duration)
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##     4.0    12.0    18.0    20.9    24.0    72.0
summary(credits2$amount)
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##     250    1366    2320    3271    3972   18424
str(credits2$default)
##  num [1:1000] 1 1 1 1 1 1 1 1 1 1 ...
prop.table(table(credits2$default))
## 
##   1   2 
## 0.7 0.3
##3 Change to Factor
credits2$default <- factor(credits2$default)
str(credits2$default)
##  Factor w/ 2 levels "1","2": 1 1 1 1 1 1 1 1 1 1 ...
table(credits2$default)
## 
##   1   2 
## 700 300
set.seed(12345)

credits2_rand <- credits2[order(runif(1000)),]
head(credits2_rand)
## # A tibble: 6 × 21
##   checking_balance months_loan_duration credit_history         purpose   amount
##   <chr>                           <dbl> <chr>                  <chr>      <dbl>
## 1 > 200 DM                            6 fully repaid this bank education    433
## 2 unknown                             6 critical               car (new)   2080
## 3 < 0 DM                             12 critical               radio/tv    3573
## 4 1 - 200 DM                         12 repaid                 car (new)    640
## 5 unknown                            24 repaid                 business    1258
## 6 unknown                            18 repaid                 radio/tv     866
## # ℹ 16 more variables: savings_balance <chr>, employment_length <chr>,
## #   installment_rate <dbl>, personal_status <chr>, other_debtors <chr>,
## #   residence_history <dbl>, property <chr>, age <dbl>, installment_plan <chr>,
## #   housing <chr>, existing_credits <dbl>, default <fct>, dependents <dbl>,
## #   telephone <chr>, foreign_worker <chr>, job <chr>
summary(credits2_rand)
##  checking_balance   months_loan_duration credit_history       purpose         
##  Length:1000        Min.   : 4.0         Length:1000        Length:1000       
##  Class :character   1st Qu.:12.0         Class :character   Class :character  
##  Mode  :character   Median :18.0         Mode  :character   Mode  :character  
##                     Mean   :20.9                                              
##                     3rd Qu.:24.0                                              
##                     Max.   :72.0                                              
##      amount      savings_balance    employment_length  installment_rate
##  Min.   :  250   Length:1000        Length:1000        Min.   :1.000   
##  1st Qu.: 1366   Class :character   Class :character   1st Qu.:2.000   
##  Median : 2320   Mode  :character   Mode  :character   Median :3.000   
##  Mean   : 3271                                         Mean   :2.973   
##  3rd Qu.: 3972                                         3rd Qu.:4.000   
##  Max.   :18424                                         Max.   :4.000   
##  personal_status    other_debtors      residence_history   property        
##  Length:1000        Length:1000        Min.   :1.000     Length:1000       
##  Class :character   Class :character   1st Qu.:2.000     Class :character  
##  Mode  :character   Mode  :character   Median :3.000     Mode  :character  
##                                        Mean   :2.845                       
##                                        3rd Qu.:4.000                       
##                                        Max.   :4.000                       
##       age        installment_plan     housing          existing_credits default
##  Min.   :19.00   Length:1000        Length:1000        Min.   :1.000    1:700  
##  1st Qu.:27.00   Class :character   Class :character   1st Qu.:1.000    2:300  
##  Median :33.00   Mode  :character   Mode  :character   Median :1.000           
##  Mean   :35.55                                         Mean   :1.407           
##  3rd Qu.:42.00                                         3rd Qu.:2.000           
##  Max.   :75.00                                         Max.   :4.000           
##    dependents     telephone         foreign_worker         job           
##  Min.   :1.000   Length:1000        Length:1000        Length:1000       
##  1st Qu.:1.000   Class :character   Class :character   Class :character  
##  Median :1.000   Mode  :character   Mode  :character   Mode  :character  
##  Mean   :1.155                                                           
##  3rd Qu.:1.000                                                           
##  Max.   :2.000
summary(credits2)
##  checking_balance   months_loan_duration credit_history       purpose         
##  Length:1000        Min.   : 4.0         Length:1000        Length:1000       
##  Class :character   1st Qu.:12.0         Class :character   Class :character  
##  Mode  :character   Median :18.0         Mode  :character   Mode  :character  
##                     Mean   :20.9                                              
##                     3rd Qu.:24.0                                              
##                     Max.   :72.0                                              
##      amount      savings_balance    employment_length  installment_rate
##  Min.   :  250   Length:1000        Length:1000        Min.   :1.000   
##  1st Qu.: 1366   Class :character   Class :character   1st Qu.:2.000   
##  Median : 2320   Mode  :character   Mode  :character   Median :3.000   
##  Mean   : 3271                                         Mean   :2.973   
##  3rd Qu.: 3972                                         3rd Qu.:4.000   
##  Max.   :18424                                         Max.   :4.000   
##  personal_status    other_debtors      residence_history   property        
##  Length:1000        Length:1000        Min.   :1.000     Length:1000       
##  Class :character   Class :character   1st Qu.:2.000     Class :character  
##  Mode  :character   Mode  :character   Median :3.000     Mode  :character  
##                                        Mean   :2.845                       
##                                        3rd Qu.:4.000                       
##                                        Max.   :4.000                       
##       age        installment_plan     housing          existing_credits default
##  Min.   :19.00   Length:1000        Length:1000        Min.   :1.000    1:700  
##  1st Qu.:27.00   Class :character   Class :character   1st Qu.:1.000    2:300  
##  Median :33.00   Mode  :character   Mode  :character   Median :1.000           
##  Mean   :35.55                                         Mean   :1.407           
##  3rd Qu.:42.00                                         3rd Qu.:2.000           
##  Max.   :75.00                                         Max.   :4.000           
##    dependents     telephone         foreign_worker         job           
##  Min.   :1.000   Length:1000        Length:1000        Length:1000       
##  1st Qu.:1.000   Class :character   Class :character   Class :character  
##  Median :1.000   Mode  :character   Mode  :character   Mode  :character  
##  Mean   :1.155                                                           
##  3rd Qu.:1.000                                                           
##  Max.   :2.000
credits2_rand_train <- credits2_rand[1:900,]
credits2_rand_test <- credits2_rand[901:1000,]

summary(credits2_rand_train)
##  checking_balance   months_loan_duration credit_history       purpose         
##  Length:900         Min.   : 4.00        Length:900         Length:900        
##  Class :character   1st Qu.:12.00        Class :character   Class :character  
##  Mode  :character   Median :18.00        Mode  :character   Mode  :character  
##                     Mean   :21.02                                             
##                     3rd Qu.:24.00                                             
##                     Max.   :72.00                                             
##      amount      savings_balance    employment_length  installment_rate
##  Min.   :  250   Length:900         Length:900         Min.   :1.000   
##  1st Qu.: 1388   Class :character   Class :character   1st Qu.:2.000   
##  Median : 2333   Mode  :character   Mode  :character   Median :3.000   
##  Mean   : 3317                                         Mean   :2.961   
##  3rd Qu.: 4059                                         3rd Qu.:4.000   
##  Max.   :18424                                         Max.   :4.000   
##  personal_status    other_debtors      residence_history   property        
##  Length:900         Length:900         Min.   :1.000     Length:900        
##  Class :character   Class :character   1st Qu.:2.000     Class :character  
##  Mode  :character   Mode  :character   Median :3.000     Mode  :character  
##                                        Mean   :2.848                       
##                                        3rd Qu.:4.000                       
##                                        Max.   :4.000                       
##       age        installment_plan     housing          existing_credits default
##  Min.   :19.00   Length:900         Length:900         Min.   :1.000    1:627  
##  1st Qu.:27.00   Class :character   Class :character   1st Qu.:1.000    2:273  
##  Median :33.00   Mode  :character   Mode  :character   Median :1.000           
##  Mean   :35.66                                         Mean   :1.412           
##  3rd Qu.:42.00                                         3rd Qu.:2.000           
##  Max.   :75.00                                         Max.   :4.000           
##    dependents     telephone         foreign_worker         job           
##  Min.   :1.000   Length:900         Length:900         Length:900        
##  1st Qu.:1.000   Class :character   Class :character   Class :character  
##  Median :1.000   Mode  :character   Mode  :character   Mode  :character  
##  Mean   :1.158                                                           
##  3rd Qu.:1.000                                                           
##  Max.   :2.000
str(credits2_rand_train)
## tibble [900 × 21] (S3: tbl_df/tbl/data.frame)
##  $ checking_balance    : chr [1:900] "> 200 DM" "unknown" "< 0 DM" "1 - 200 DM" ...
##  $ months_loan_duration: num [1:900] 6 6 12 12 24 18 18 36 24 48 ...
##  $ credit_history      : chr [1:900] "fully repaid this bank" "critical" "critical" "repaid" ...
##  $ purpose             : chr [1:900] "education" "car (new)" "radio/tv" "car (new)" ...
##  $ amount              : num [1:900] 433 2080 3573 640 1258 ...
##  $ savings_balance     : chr [1:900] "> 1000 DM" "501 - 1000 DM" "< 100 DM" "< 100 DM" ...
##  $ employment_length   : chr [1:900] "0 - 1 yrs" "1 - 4 yrs" "1 - 4 yrs" "1 - 4 yrs" ...
##  $ installment_rate    : num [1:900] 4 1 1 4 4 4 4 4 4 2 ...
##  $ personal_status     : chr [1:900] "female" "married male" "female" "divorced male" ...
##  $ other_debtors       : chr [1:900] "none" "none" "none" "none" ...
##  $ residence_history   : num [1:900] 2 2 1 2 1 2 3 3 2 1 ...
##  $ property            : chr [1:900] "building society savings" "other" "real estate" "real estate" ...
##  $ age                 : num [1:900] 24 24 23 49 25 25 36 30 33 35 ...
##  $ installment_plan    : chr [1:900] "bank" "none" "none" "none" ...
##  $ housing             : chr [1:900] "rent" "own" "own" "own" ...
##  $ existing_credits    : num [1:900] 1 1 1 1 1 1 2 1 2 2 ...
##  $ default             : Factor w/ 2 levels "1","2": 2 1 1 1 1 1 1 1 1 1 ...
##  $ dependents          : num [1:900] 2 1 1 1 1 1 1 1 1 1 ...
##  $ telephone           : chr [1:900] "none" "none" "none" "none" ...
##  $ foreign_worker      : chr [1:900] "yes" "yes" "yes" "yes" ...
##  $ job                 : chr [1:900] "skilled employee" "skilled employee" "unskilled resident" "unskilled resident" ...
str(credits2_rand_test)
## tibble [100 × 21] (S3: tbl_df/tbl/data.frame)
##  $ checking_balance    : chr [1:100] "1 - 200 DM" "1 - 200 DM" "1 - 200 DM" "< 0 DM" ...
##  $ months_loan_duration: num [1:100] 18 15 10 48 6 18 24 24 24 10 ...
##  $ credit_history      : chr [1:100] "fully repaid" "fully repaid" "critical" "repaid" ...
##  $ purpose             : chr [1:100] "car (new)" "car (new)" "furniture" "car (new)" ...
##  $ amount              : num [1:100] 2278 950 2069 8487 672 ...
##  $ savings_balance     : chr [1:100] "101 - 500 DM" "< 100 DM" "unknown" "unknown" ...
##  $ employment_length   : chr [1:100] "0 - 1 yrs" "> 7 yrs" "1 - 4 yrs" "4 - 7 yrs" ...
##  $ installment_rate    : num [1:100] 3 4 2 1 1 4 4 4 2 4 ...
##  $ personal_status     : chr [1:100] "female" "single male" "married male" "female" ...
##  $ other_debtors       : chr [1:100] "none" "none" "none" "none" ...
##  $ residence_history   : num [1:100] 3 3 1 2 4 2 2 3 2 2 ...
##  $ property            : chr [1:100] "other" "other" "other" "other" ...
##  $ age                 : num [1:100] 28 33 26 24 54 30 25 26 24 27 ...
##  $ installment_plan    : chr [1:100] "none" "none" "none" "none" ...
##  $ housing             : chr [1:100] "own" "rent" "own" "own" ...
##  $ existing_credits    : num [1:100] 2 2 2 1 1 2 2 1 1 2 ...
##  $ default             : Factor w/ 2 levels "1","2": 2 2 1 1 1 2 1 1 1 1 ...
##  $ dependents          : num [1:100] 1 2 1 1 1 1 1 1 1 1 ...
##  $ telephone           : chr [1:100] "none" "none" "none" "none" ...
##  $ foreign_worker      : chr [1:100] "yes" "yes" "no" "yes" ...
##  $ job                 : chr [1:100] "skilled employee" "skilled employee" "skilled employee" "skilled employee" ...
table(credits2_rand_train$default)
## 
##   1   2 
## 627 273
table(credits2_rand_test$default)
## 
##  1  2 
## 73 27
prop.table(table(credits2_rand_train$default))
## 
##         1         2 
## 0.6966667 0.3033333
prop.table(table(credits2_rand_test$default))
## 
##    1    2 
## 0.73 0.27
install.packages("C50")
## Warning: package 'C50' is in use and will not be installed
library(C50)

View(credits2_rand_train)

M <- C5.0(x=credits2_rand_train , y=credits2_rand_train$default, trial=1 , costs=NULL)

M <- C5.0(x=credits2_rand_train , y=credits2_rand_train$default, trial=1 ,rules = FALSE, weights = NULL, control = C5.0Control(), costs = NULL)
str(credits2_rand_train$checking_balance)
##  chr [1:900] "> 200 DM" "unknown" "< 0 DM" "1 - 200 DM" "unknown" "unknown" ...