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:
set.seed(12345)
library(C50)
## Warning: package 'C50' was built under R version 4.4.3
library(readr)
library(class)
## Warning: package 'class' was built under R version 4.4.3
library(gmodels)
## Warning: package 'gmodels' was built under R version 4.4.3
creditDS <- read_csv("C:/Users/ijiol/OneDrive/Documents/R projects/R for Advanced Topics/Datasets/credit.csv")
## Rows: 1000 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(creditDS)
credit <- data.frame(creditDS, stringsAsFactors = TRUE)
str(credit)
## 'data.frame': 1000 obs. of 21 variables:
## $ checking_balance : chr "< 0 DM" "1 - 200 DM" "unknown" "< 0 DM" ...
## $ months_loan_duration: num 6 48 12 42 24 36 24 36 12 30 ...
## $ credit_history : chr "critical" "repaid" "critical" "repaid" ...
## $ purpose : chr "radio/tv" "radio/tv" "education" "furniture" ...
## $ amount : num 1169 5951 2096 7882 4870 ...
## $ savings_balance : chr "unknown" "< 100 DM" "< 100 DM" "< 100 DM" ...
## $ employment_length : chr "> 7 yrs" "1 - 4 yrs" "4 - 7 yrs" "4 - 7 yrs" ...
## $ installment_rate : num 4 2 2 2 3 2 3 2 2 4 ...
## $ personal_status : chr "single male" "female" "single male" "single male" ...
## $ other_debtors : chr "none" "none" "none" "guarantor" ...
## $ residence_history : num 4 2 3 4 4 4 4 2 4 2 ...
## $ property : chr "real estate" "real estate" "real estate" "building society savings" ...
## $ age : num 67 22 49 45 53 35 53 35 61 28 ...
## $ installment_plan : chr "none" "none" "none" "none" ...
## $ housing : chr "own" "own" "own" "for free" ...
## $ existing_credits : num 2 1 1 1 2 1 1 1 1 2 ...
## $ default : num 1 2 1 1 2 1 1 1 1 2 ...
## $ dependents : num 1 1 2 2 2 2 1 1 1 1 ...
## $ telephone : chr "yes" "none" "none" "none" ...
## $ foreign_worker : chr "yes" "yes" "yes" "yes" ...
## $ job : chr "skilled employee" "skilled employee" "unskilled resident" "skilled employee" ...
summary(credit$checking_balance)
## Length Class Mode
## 1000 character character
summary(credit$savings_balance)
## Length Class Mode
## 1000 character character
credit_rand <- credit[order(runif(1000)),]
summary( credit)
## 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
## Min. :19.00 Length:1000 Length:1000 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
## default dependents telephone foreign_worker
## Min. :1.0 Min. :1.000 Length:1000 Length:1000
## 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
## job
## Length:1000
## Class :character
## Mode :character
##
##
##
summary(credit_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
## Min. :19.00 Length:1000 Length:1000 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
## default dependents telephone foreign_worker
## Min. :1.0 Min. :1.000 Length:1000 Length:1000
## 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
## job
## Length:1000
## Class :character
## Mode :character
##
##
##
head(credit[1:10], 10)
## checking_balance months_loan_duration credit_history purpose amount
## 1 < 0 DM 6 critical radio/tv 1169
## 2 1 - 200 DM 48 repaid radio/tv 5951
## 3 unknown 12 critical education 2096
## 4 < 0 DM 42 repaid furniture 7882
## 5 < 0 DM 24 delayed car (new) 4870
## 6 unknown 36 repaid education 9055
## 7 unknown 24 repaid furniture 2835
## 8 1 - 200 DM 36 repaid car (used) 6948
## 9 unknown 12 repaid radio/tv 3059
## 10 1 - 200 DM 30 critical car (new) 5234
## savings_balance employment_length installment_rate personal_status
## 1 unknown > 7 yrs 4 single male
## 2 < 100 DM 1 - 4 yrs 2 female
## 3 < 100 DM 4 - 7 yrs 2 single male
## 4 < 100 DM 4 - 7 yrs 2 single male
## 5 < 100 DM 1 - 4 yrs 3 single male
## 6 unknown 1 - 4 yrs 2 single male
## 7 501 - 1000 DM > 7 yrs 3 single male
## 8 < 100 DM 1 - 4 yrs 2 single male
## 9 > 1000 DM 4 - 7 yrs 2 divorced male
## 10 < 100 DM unemployed 4 married male
## other_debtors
## 1 none
## 2 none
## 3 none
## 4 guarantor
## 5 none
## 6 none
## 7 none
## 8 none
## 9 none
## 10 none
head(credit_rand[1:10], 10)
## checking_balance months_loan_duration credit_history purpose amount
## 14 < 0 DM 24 critical car (new) 1199
## 448 1 - 200 DM 7 repaid radio/tv 2576
## 697 1 - 200 DM 12 repaid radio/tv 1103
## 32 < 0 DM 24 repaid furniture 4020
## 196 1 - 200 DM 9 critical education 1501
## 83 unknown 18 repaid business 1568
## 119 < 0 DM 33 critical furniture 4281
## 602 1 - 200 DM 9 repaid furniture 918
## 443 1 - 200 DM 20 delayed others 2629
## 945 < 0 DM 15 repaid furniture 1845
## savings_balance employment_length installment_rate personal_status
## 14 < 100 DM > 7 yrs 4 single male
## 448 < 100 DM 1 - 4 yrs 2 single male
## 697 < 100 DM 4 - 7 yrs 4 single male
## 32 < 100 DM 1 - 4 yrs 2 single male
## 196 < 100 DM > 7 yrs 2 female
## 83 101 - 500 DM 1 - 4 yrs 3 female
## 119 501 - 1000 DM 1 - 4 yrs 1 female
## 602 < 100 DM 1 - 4 yrs 4 female
## 443 < 100 DM 1 - 4 yrs 2 single male
## 945 < 100 DM 0 - 1 yrs 4 female
## other_debtors
## 14 none
## 448 guarantor
## 697 guarantor
## 32 none
## 196 none
## 83 none
## 119 none
## 602 none
## 443 none
## 945 guarantor
#splitting the dataset
Credit_train <- credit_rand[1:900,]
Credit_test <- credit_rand[901:1000,]
Credit_train$default <- as.factor(Credit_train$default)
Credit_test$default <- as.factor(Credit_test$default)
cm <- C5.0(Credit_train[-17], Credit_train$default)
cm
##
## Call:
## C5.0.default(x = Credit_train[-17], y = Credit_train$default)
##
## Classification Tree
## Number of samples: 900
## Number of predictors: 20
##
## Tree size: 57
##
## Non-standard options: attempt to group attributes
summary(cm)
##
## Call:
## C5.0.default(x = Credit_train[-17], y = Credit_train$default)
##
##
## C5.0 [Release 2.07 GPL Edition] Tue Apr 15 19:54:27 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,1 - 200 DM,> 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,repaid,delayed}:
## :...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 {repaid,delayed}: 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 {repaid,delayed}: 1 (11/1)
## : other_debtors in {none,co-applicant}:
## : :...job = mangement self-employed: 1 (26/6)
## : job in {unskilled resident,skilled employee,
## : : unemployed non-resident}:
## : :...purpose in {radio/tv,others,repairs,
## : : domestic appliances,
## : : retraining}: 2 (33/10)
## : purpose = education: [S1]
## : purpose = business:
## : :...job in {unskilled resident,
## : : : unemployed non-resident}: 1 (3)
## : : job = skilled employee: 2 (3)
## : 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 = guarantor: 1 (7/1)
## other_debtors = co-applicant: 2 (3/1)
## other_debtors = none:
## :...employment_length = 4 - 7 yrs:
## :...age <= 41: 1 (16)
## : age > 41: 2 (3/1)
## employment_length in {> 7 yrs,
## : 1 - 4 yrs,
## : 0 - 1 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 {single male,female,divorced male}: 2 (29/6)
## personal_status = married male: 1 (2)
##
## SubTree [S3]
##
## job = unskilled resident: 2 (1)
## job in {skilled employee,unemployed non-resident}: 1 (8/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 = unskilled resident: 2 (1)
## : job in {skilled employee,unemployed non-resident}: 1 (4)
## 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 in {none,guarantor}: 1 (3)
## other_debtors = co-applicant: 2 (1)
##
## SubTree [S6]
##
## housing = for free: 1 (2)
## housing = rent:
## :...credit_history = critical: 1 (1)
## : credit_history in {repaid,delayed}: 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 {1 - 4 yrs,0 - 1 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
#using the model to predict/ Evaluation
credit_pred <- predict(cm, Credit_test[-17])
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 |
## ---------------|-----------|-----------|-----------|
##
##
#improving the model
Credit_boost10<-C5.0(Credit_train[-17], Credit_train$default, trials =10 )
Credit_boost10
##
## Call:
## C5.0.default(x = Credit_train[-17], y = Credit_train$default, trials = 10)
##
## Classification Tree
## Number of samples: 900
## Number of predictors: 20
##
## Number of boosting iterations: 10
## Average tree size: 47.3
##
## Non-standard options: attempt to group attributes
#pedicting with boost
Credit_boost_predict_10 <- predict(Credit_boost10,Credit_test)
CrossTable(Credit_test$default, Credit_boost_predict_10, 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 |
## ---------------|-----------|-----------|-----------|
##
##
#Error cost
error_cost <- matrix(c(0,1,4,0), nrow=2)
M <- C5.0(Credit_train, Credit_train$default, trails = 1, costs = error_cost)
## Warning: no dimnames were given for the cost matrix; the factor levels will be
## used
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
Credit_cost_predict <- predict(credit_cost, Credit_test)
Credit_test$default <- factor(Credit_test$default, levels = c(1,2), labels = c("No", "Yes"))
credit_pred <- factor(credit_pred, levels = c(1,2), labels = c("No", "Yes"))
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 | No | Yes | Row Total |
## ---------------|-----------|-----------|-----------|
## No | 54 | 14 | 68 |
## | 0.540 | 0.140 | |
## ---------------|-----------|-----------|-----------|
## Yes | 11 | 21 | 32 |
## | 0.110 | 0.210 | |
## ---------------|-----------|-----------|-----------|
## Column Total | 65 | 35 | 100 |
## ---------------|-----------|-----------|-----------|
##
##
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.