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:
library(readxl)
## Warning: package 'readxl' was built under R version 4.4.3
library(C50)
## Warning: package 'C50' was built under R version 4.4.3
library(readr)
## Warning: package 'readr' was built under R version 4.4.3
library(gmodels)
## Warning: package 'gmodels' was built under R version 4.4.3
credit <- read_csv("C:/Users/khalil2823/Downloads/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.
credit_rand<-credit[order(runif(1000)),]
set.seed(12345)
credit_train <- credit_rand[1:900,]
credit_test<-credit_rand[901:1000,]
stringAsFactors=TRUE
prop.table(table(credit_train$default))
##
## 1 2
## 0.7066667 0.2933333
prop.table(table(credit_test$default))
##
## 1 2
## 0.64 0.36
credit_train$default <- as.factor(credit_train$default)
credit_test$default <-as.factor(credit_test$default)
credit_model<-C5.0(credit_train[-17], credit_train$default)
summary(credit_model)
##
## Call:
## C5.0.default(x = credit_train[-17], y = credit_train$default)
##
##
## C5.0 [Release 2.07 GPL Edition] Thu Apr 17 13:03:47 2025
## -------------------------------
##
## Class specified by attribute `outcome'
##
## Read 900 cases (21 attributes) from undefined.data
##
## Decision tree:
##
## checking_balance in {unknown,> 200 DM}: 1 (416/52)
## checking_balance in {1 - 200 DM,< 0 DM}:
## :...other_debtors = guarantor:
## :...months_loan_duration <= 16: 1 (18)
## : months_loan_duration > 16:
## : :...dependents > 1: 1 (4)
## : dependents <= 1:
## : :...purpose in {business,car (new)}: 2 (3)
## : purpose in {education,repairs,furniture,others,car (used),
## : : domestic appliances,retraining}: 1 (3)
## : purpose = radio/tv:
## : :...months_loan_duration <= 21: 1 (4)
## : months_loan_duration > 21: 2 (4)
## other_debtors in {none,co-applicant}:
## :...credit_history in {delayed,critical}:
## :...savings_balance in {unknown,101 - 500 DM}: 1 (31/2)
## : savings_balance = > 1000 DM:
## : :...residence_history <= 3: 2 (3/1)
## : : residence_history > 3: 1 (4)
## : savings_balance = 501 - 1000 DM:
## : :...months_loan_duration <= 21: 1 (3)
## : : months_loan_duration > 21: 2 (2)
## : savings_balance = < 100 DM:
## : :...months_loan_duration <= 22: 1 (62/12)
## : months_loan_duration > 22:
## : :...existing_credits > 2: 1 (2)
## : existing_credits <= 2:
## : :...purpose in {business,education,furniture,car (new),
## : : others,domestic appliances,
## : : retraining}: 2 (22/4)
## : purpose = repairs: 1 (1)
## : purpose = radio/tv:
## : :...checking_balance = 1 - 200 DM: 1 (3)
## : : checking_balance = < 0 DM: 2 (2)
## : purpose = car (used):
## : :...installment_rate <= 3: 1 (4)
## : installment_rate > 3: 2 (3)
## credit_history in {repaid,fully repaid this bank,fully repaid}:
## :...savings_balance = 501 - 1000 DM: 1 (13/3)
## savings_balance = > 1000 DM:
## :...other_debtors = none: 1 (10)
## : other_debtors = co-applicant: 2 (1)
## savings_balance = unknown:
## :...checking_balance = 1 - 200 DM:
## : :...property = other: 1 (8)
## : : property = building society savings:
## : : :...residence_history <= 3: 2 (4/1)
## : : : residence_history > 3: 1 (3)
## : : property = real estate:
## : : :...age <= 30: 2 (2)
## : : : age > 30: 1 (3)
## : : property = unknown/none:
## : : :...installment_rate <= 2: 2 (2)
## : : installment_rate > 2: 1 (5)
## : checking_balance = < 0 DM:
## : :...foreign_worker = no: 1 (2)
## : foreign_worker = yes:
## : :...months_loan_duration <= 13: 1 (3)
## : months_loan_duration > 13: 2 (18/5)
## savings_balance in {< 100 DM,101 - 500 DM}:
## :...months_loan_duration > 42:
## :...employment_length in {1 - 4 yrs,> 7 yrs,unemployed,
## : : 0 - 1 yrs}: 2 (19)
## : employment_length = 4 - 7 yrs:
## : :...residence_history <= 3: 1 (4/1)
## : residence_history > 3: 2 (4)
## months_loan_duration <= 42:
## :...installment_rate <= 3:
## :...personal_status = divorced male: 2 (11/3)
## : personal_status = married male: 1 (12/5)
## : personal_status = female:
## : :...employment_length in {unemployed,
## : : : 4 - 7 yrs}: 1 (7/1)
## : : employment_length = 0 - 1 yrs: 2 (11/2)
## : : employment_length = 1 - 4 yrs: [S1]
## : : employment_length = > 7 yrs:
## : : :...checking_balance = 1 - 200 DM: 2 (3)
## : : checking_balance = < 0 DM: 1 (1)
## : personal_status = single male:
## : :...property = building society savings:
## : :...installment_rate <= 2: 2 (6/1)
## : : installment_rate > 2: 1 (3)
## : property = real estate:
## : :...months_loan_duration <= 21: 1 (13)
## : : months_loan_duration > 21: 2 (2)
## : property = other:
## : :...other_debtors = co-applicant: 2 (1)
## : : other_debtors = none:
## : : :...age <= 25: 2 (3)
## : : age > 25: 1 (12/1)
## : property = unknown/none:
## : :...housing in {own,rent}: 2 (2)
## : housing = for free:
## : :...savings_balance = 101 - 500 DM: 2 (1)
## : savings_balance = < 100 DM:
## : :...installment_rate <= 2: 1 (6)
## : installment_rate > 2: 2 (3/1)
## installment_rate > 3:
## :...foreign_worker = no: 1 (2)
## foreign_worker = yes:
## :...credit_history = fully repaid: 2 (7)
## credit_history in {repaid,fully repaid this bank}:
## :...checking_balance = < 0 DM: 2 (53/12)
## checking_balance = 1 - 200 DM:
## :...installment_plan = bank: 2 (5)
## installment_plan in {none,stores}: [S2]
##
## SubTree [S1]
##
## credit_history in {repaid,fully repaid this bank}: 2 (15/5)
## credit_history = fully repaid: 1 (2)
##
## SubTree [S2]
##
## other_debtors = co-applicant: 2 (1)
## other_debtors = none:
## :...residence_history > 3: 1 (8)
## residence_history <= 3:
## :...age > 32: 2 (6)
## age <= 32:
## :...installment_plan = none: 1 (8/2)
## installment_plan = stores: 2 (1)
##
##
## Evaluation on training data (900 cases):
##
## Decision Tree
## ----------------
## Size Errors
##
## 64 114(12.7%) <<
##
##
## (a) (b) <-classified as
## ---- ----
## 601 35 (a): class 1
## 79 185 (b): class 2
##
##
## Attribute usage:
##
## 100.00% checking_balance
## 53.78% other_debtors
## 49.78% credit_history
## 49.78% savings_balance
## 43.67% months_loan_duration
## 24.33% installment_rate
## 12.67% personal_status
## 12.67% foreign_worker
## 8.78% property
## 7.33% employment_length
## 5.44% purpose
## 5.00% residence_history
## 4.11% existing_credits
## 3.89% age
## 3.22% installment_plan
## 2.00% dependents
## 1.33% housing
##
##
## Time: 0.0 secs
credit_pred <- predict(credit_model, 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 | 10 | 64 |
## | 0.540 | 0.100 | |
## ---------------|-----------|-----------|-----------|
## 2 | 21 | 15 | 36 |
## | 0.210 | 0.150 | |
## ---------------|-----------|-----------|-----------|
## Column Total | 75 | 25 | 100 |
## ---------------|-----------|-----------|-----------|
##
##
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)
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_pred <- predict(credit_cost, credit_test[-17])
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 | 32 | 32 | 64 |
## | 0.320 | 0.320 | |
## ---------------|-----------|-----------|-----------|
## 2 | 8 | 28 | 36 |
## | 0.080 | 0.280 | |
## ---------------|-----------|-----------|-----------|
## Column Total | 40 | 60 | 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.