| Student ID Number | 127039 … |
| Student Name | Ayan Ahmed … |
| BBIT 4.2 Group | .C.. |
#Loading dataset
## Rows: 4269 Columns: 13
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (3): education, self_employed, loan_status
## dbl (10): loan_id, no_of_dependents, income_annum, loan_amount, loan_term, c...
##
## ℹ 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.
#installing packages
if (!is.element("renv", installed.packages()[, 1])) {
install.packages("renv", dependencies = TRUE)
}
require("renv")## Loading required package: renv
##
## Attaching package: 'renv'
## The following objects are masked from 'package:stats':
##
## embed, update
## The following objects are masked from 'package:utils':
##
## history, upgrade
## The following objects are masked from 'package:base':
##
## autoload, load, remove
## - The project is out-of-sync -- use `renv::status()` for details.
## - The library is already synchronized with the lockfile.
## - The project is out-of-sync -- use `renv::status()` for details.
if (!is.element("languageserver", installed.packages()[, 1])) {
install.packages("languageserver", dependencies = TRUE)
}
require("languageserver")## Loading required package: languageserver
##
## Attaching package: 'languageserver'
## The following object is masked from 'package:renv':
##
## run
if (!is.element("mlbench", installed.packages()[, 1])) {
install.packages("mlbench", dependencies = TRUE)
}
require("mlbench")## Loading required package: mlbench
#preview data
## [1] 4269 13
## loan_id no_of_dependents education
## "numeric" "numeric" "character"
## self_employed income_annum loan_amount
## "character" "numeric" "numeric"
## loan_term cibil_score residential_assets_value
## "numeric" "numeric" "numeric"
## commercial_assets_value luxury_assets_value bank_asset_value
## "numeric" "numeric" "numeric"
## loan_status
## "character"
#measure of frequency
loan_dataset_freq <- loan_dataset$education
cbind(frequency = table(loan_dataset_freq),
percentage = prop.table(table(loan_dataset_freq)) * 100)## frequency percentage
## Graduate 2144 50.22253
## Not Graduate 2125 49.77747
#measure of central tendency-mode
loan_dataset_loan_amount_mode <- names(table(loan_dataset$loan_amount))[
which(table(loan_dataset$loan_amount) == max(table(loan_dataset$loan_amount)))
]
print(loan_dataset_loan_amount_mode)## [1] "10600000"
#-sd
## income_annum loan_amount loan_term
## 2.806840e+06 9.043363e+06 5.709187e+00
## cibil_score residential_assets_value commercial_assets_value
## 1.724304e+02 6.503637e+06 4.388966e+06
## luxury_assets_value bank_asset_value
## 9.103754e+06 3.250185e+06
#variance
## income_annum loan_amount loan_term
## 7.878350e+12 8.178241e+13 3.259482e+01
## cibil_score residential_assets_value commercial_assets_value
## 2.973224e+04 4.229729e+13 1.926302e+13
## luxury_assets_value bank_asset_value
## 8.287833e+13 1.056370e+13
#kurtosis
if (!is.element("e1071", installed.packages()[, 1])) {
install.packages("e1071", dependencies = TRUE)
}
require("e1071")## Loading required package: e1071
## income_annum loan_amount loan_term
## -1.1827290 -0.7436797 -1.2208527
## cibil_score residential_assets_value commercial_assets_value
## -1.1856696 0.1847380 0.1008126
## luxury_assets_value bank_asset_value
## -0.7380561 -0.3972774
#skewness
## income_annum loan_amount loan_term
## -0.012814425 0.308723885 0.036358907
## cibil_score residential_assets_value commercial_assets_value
## -0.009039277 0.978450597 0.957790887
## luxury_assets_value bank_asset_value
## 0.322207503 0.560725009
#measure of relationship-covariance, correlation
#covariance
loan_dataset_cov <- cov(loan_dataset[, 5:12])
View(loan_dataset_cov)
#correlation
loan_dataset_cor <- cor(loan_dataset[, 5:12])
View(loan_dataset_cor)#measure of distribution-anova
loan_dataset_one_way_anova <- aov(loan_term ~ loan_amount, data = loan_dataset)
summary(loan_dataset_one_way_anova)## Df Sum Sq Mean Sq F value Pr(>F)
## loan_amount 1 10 9.9 0.304 0.582
## Residuals 4267 139105 32.6
#visulaizations
#loan term frequency
loan_dataset_loan_term <- as.numeric(unlist(loan_dataset[, 7]))
hist(loan_dataset_loan_term, main = names(loan_dataset)[7])#loan final status
barplot(table(loan_dataset[, 13]), main = names(loan_dataset)[13])
barplot(table(loan_dataset[, 3]), main = names(loan_dataset)[3])#missingness
if (!is.element("Amelia", installed.packages()[, 1])) {
install.packages("Amelia", dependencies = TRUE)
}
require("Amelia")## Loading required package: Amelia
## Loading required package: Rcpp
## ##
## ## Amelia II: Multiple Imputation
## ## (Version 1.8.1, built: 2022-11-18)
## ## Copyright (C) 2005-2023 James Honaker, Gary King and Matthew Blackwell
## ## Refer to http://gking.harvard.edu/amelia/ for more information
## ##
## Warning: Unknown or uninitialised column: `arguments`.
## Unknown or uninitialised column: `arguments`.
## Warning: Unknown or uninitialised column: `imputations`.
if (!is.element("corrplot", installed.packages()[, 1])) {
install.packages("corrplot", dependencies = TRUE)
}
require("corrplot")## Loading required package: corrplot
## corrplot 0.92 loaded
corrplot(cor(loan_dataset[, 5:12]), method = "circle")
if (!is.element("ggcorrplot", installed.packages()[, 1])) {
install.packages("ggcorrplot", dependencies = TRUE)
}
require("ggcorrplot")## Loading required package: ggcorrplot
## Loading required package: ggplot2
#multivariate visualizations
if (!is.element("caret", installed.packages()[, 1])) {
install.packages("caret", dependencies = TRUE)
}
require("caret")## Loading required package: caret
## Loading required package: lattice
## NULL
#load dataset
## Rows: 4269 Columns: 13
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (3): education, self_employed, loan_status
## dbl (10): loan_id, no_of_dependents, income_annum, loan_amount, loan_term, c...
##
## ℹ 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.
#install packages
if (!is.element("languageserver", installed.packages()[, 1])) {
install.packages("languageserver", dependencies = TRUE,
repos = "https://cloud.r-project.org")
}
require("languageserver")
if (!is.element("NHANES", installed.packages()[, 1])) {
install.packages("NHANES", dependencies = TRUE,
repos = "https://cloud.r-project.org")
}
require("NHANES")## Loading required package: NHANES
## Warning: package 'NHANES' was built under R version 4.3.2
## dplyr ----
if (!is.element("dplyr", installed.packages()[, 1])) {
install.packages("dplyr", dependencies = TRUE,
repos = "https://cloud.r-project.org")
}
require("dplyr")## Loading required package: dplyr
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
if (!is.element("naniar", installed.packages()[, 1])) {
install.packages("naniar", dependencies = TRUE,
repos = "https://cloud.r-project.org")
}
require("naniar")## Loading required package: naniar
## Warning: package 'naniar' was built under R version 4.3.2
#create subset variable
loan_dataset <- loan_dataset %>%
select(loan_id, education, loan_amount, loan_term, self_employed, income_annum, cibil_score,
no_of_dependents, residential_assets_value, commercial_assets_value, luxury_assets_value, bank_asset_value, loan_status)#choose random variables to test for missing data
#confirm missing data
## [1] FALSE
#Data transformation-packages
if (require("mlbench")) {
require("mlbench")
} else {
install.packages("mlbench", dependencies = TRUE,
repos = "https://cloud.r-project.org")
}
## readr ----
if (require("readr")) {
require("readr")
} else {
install.packages("readr", dependencies = TRUE,
repos = "https://cloud.r-project.org")
}
## e1071 ----
if (require("e1071")) {
require("e1071")
} else {
install.packages("e1071", dependencies = TRUE,
repos = "https://cloud.r-project.org")
}
## factoextra ----
if (require("factoextra")) {
require("factoextra")
} else {
install.packages("factoextra", dependencies = TRUE,
repos = "https://cloud.r-project.org")
}## Loading required package: factoextra
## Warning: package 'factoextra' was built under R version 4.3.2
## Welcome! Want to learn more? See two factoextra-related books at https://goo.gl/ve3WBa
## FactoMineR ----
if (require("FactoMineR")) {
require("FactoMineR")
} else {
install.packages("FactoMineR", dependencies = TRUE,
repos = "https://cloud.r-project.org")
}## Loading required package: FactoMineR
## Warning: package 'FactoMineR' was built under R version 4.3.2
#data before sacle basic data transformation
if (!is.element("caret", installed.packages()[, 1])) {
install.packages("caret", dependencies = TRUE)
}
require("caret")
summary(loan_dataset)## loan_id education loan_amount loan_term
## Min. : 1 Length:4269 Min. : 300000 Min. : 2.0
## 1st Qu.:1068 Class :character 1st Qu.: 7700000 1st Qu.: 6.0
## Median :2135 Mode :character Median :14500000 Median :10.0
## Mean :2135 Mean :15133450 Mean :10.9
## 3rd Qu.:3202 3rd Qu.:21500000 3rd Qu.:16.0
## Max. :4269 Max. :39500000 Max. :20.0
## self_employed income_annum cibil_score no_of_dependents
## Length:4269 Min. : 200000 Min. :300.0 Min. :0.000
## Class :character 1st Qu.:2700000 1st Qu.:453.0 1st Qu.:1.000
## Mode :character Median :5100000 Median :600.0 Median :3.000
## Mean :5059124 Mean :599.9 Mean :2.499
## 3rd Qu.:7500000 3rd Qu.:748.0 3rd Qu.:4.000
## Max. :9900000 Max. :900.0 Max. :5.000
## residential_assets_value commercial_assets_value luxury_assets_value
## Min. : -100000 Min. : 0 Min. : 300000
## 1st Qu.: 2200000 1st Qu.: 1300000 1st Qu.: 7500000
## Median : 5600000 Median : 3700000 Median :14600000
## Mean : 7472617 Mean : 4973155 Mean :15126306
## 3rd Qu.:11300000 3rd Qu.: 7600000 3rd Qu.:21700000
## Max. :29100000 Max. :19400000 Max. :39200000
## bank_asset_value loan_status
## Min. : 0 Length:4269
## 1st Qu.: 2300000 Class :character
## Median : 4600000 Mode :character
## Mean : 4976692
## 3rd Qu.: 7100000
## Max. :14700000
loan_dataset_loan_status <- as.numeric(unlist(loan_dataset[, 4]))
hist(loan_dataset_loan_status, main = names(loan_dataset)[4])model_of_the_transform <- preProcess(loan_dataset, method = c("scale"))
print(model_of_the_transform)## Created from 4269 samples and 13 variables
##
## Pre-processing:
## - ignored (3)
## - scaled (10)
#data after scale basic data transformation
## loan_id education loan_amount loan_term
## Min. :0.000811 Length:4269 Min. :0.03317 Min. :0.3503
## 1st Qu.:0.866533 Class :character 1st Qu.:0.85145 1st Qu.:1.0509
## Median :1.732254 Mode :character Median :1.60339 Median :1.7516
## Mean :1.732254 Mean :1.67343 Mean :1.9093
## 3rd Qu.:2.597975 3rd Qu.:2.37743 3rd Qu.:2.8025
## Max. :3.463696 Max. :4.36784 Max. :3.5031
## self_employed income_annum cibil_score no_of_dependents
## Length:4269 Min. :0.07125 Min. :1.740 Min. :0.0000
## Class :character 1st Qu.:0.96194 1st Qu.:2.627 1st Qu.:0.5897
## Mode :character Median :1.81699 Median :3.480 Median :1.7690
## Mean :1.80243 Mean :3.479 Mean :1.4734
## 3rd Qu.:2.67204 3rd Qu.:4.338 3rd Qu.:2.3586
## Max. :3.52710 Max. :5.219 Max. :2.9483
## residential_assets_value commercial_assets_value luxury_assets_value
## Min. :-0.01538 Min. :0.0000 Min. :0.03295
## 1st Qu.: 0.33827 1st Qu.:0.2962 1st Qu.:0.82384
## Median : 0.86106 Median :0.8430 Median :1.60373
## Mean : 1.14899 Mean :1.1331 Mean :1.66155
## 3rd Qu.: 1.73749 3rd Qu.:1.7316 3rd Qu.:2.38363
## Max. : 4.47442 Max. :4.4202 Max. :4.30592
## bank_asset_value loan_status
## Min. :0.0000 Length:4269
## 1st Qu.:0.7077 Class :character
## Median :1.4153 Mode :character
## Mean :1.5312
## 3rd Qu.:2.1845
## Max. :4.5228
loan_dataset_loan_status <- as.numeric(unlist(loan_dataset_scale_transform[, 4]))
hist(loan_dataset_loan_status, main = names(loan_dataset_scale_transform)[4])#load dataset
## Rows: 4269 Columns: 13
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (3): education, self_employed, loan_status
## dbl (10): loan_id, no_of_dependents, income_annum, loan_amount, loan_term, c...
##
## ℹ 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.
#install packages
if (require("pROC")) {
require("pROC")
} else {
install.packages("pROC", dependencies = TRUE,
repos = "https://cloud.r-project.org")
}## Loading required package: pROC
## Type 'citation("pROC")' for a citation.
##
## Attaching package: 'pROC'
## The following objects are masked from 'package:stats':
##
## cov, smooth, var
## dplyr ----
if (require("dplyr")) {
require("dplyr")
} else {
install.packages("dplyr", dependencies = TRUE,
repos = "https://cloud.r-project.org")
}#check data frequency
loan_dataset_freq <- loan_dataset$loan_status
cbind(frequency =
table(loan_dataset_freq),
percentage = prop.table(table(loan_dataset_freq)) * 100)## frequency percentage
## Approved 2656 62.21598
## Rejected 1613 37.78402
#split the data
if (!is.element("caret", installed.packages()[, 1])) {
install.packages("caret", dependencies = TRUE)
}
require("caret")
train_index <- createDataPartition(loan_dataset$loan_status,
p = 0.75,
list = FALSE)
loan_dataset_train <- loan_dataset[train_index, ]
loan_dataset_test <- loan_dataset[-train_index, ]#train the model
#accuracy and kappa
set.seed(7)
loan_dataset_model_glm <-
train(loan_status ~ ., data = loan_dataset_train, method = "glm",
metric = "Accuracy", trControl = train_control)#print model performance
## Generalized Linear Model
##
## 3202 samples
## 12 predictor
## 2 classes: 'Approved', 'Rejected'
##
## No pre-processing
## Resampling: Cross-Validated (13 fold)
## Summary of sample sizes: 2956, 2955, 2954, 2956, 2955, 2956, ...
## Resampling results:
##
## Accuracy Kappa
## 0.9153656 0.820114
#applying 5-fold cross validation resampling method
train_control <- trainControl(method = "cv", number = 13)
loan_dataset_caret_model_rpart <- train(loan_status ~ ., data = loan_dataset,
method = "rpart", metric = "Accuracy",
trControl = train_control)#print the model
## CART
##
## 4269 samples
## 12 predictor
## 2 classes: 'Approved', 'Rejected'
##
## No pre-processing
## Resampling: Cross-Validated (13 fold)
## Summary of sample sizes: 3941, 3941, 3941, 3941, 3940, 3941, ...
## Resampling results across tuning parameters:
##
## cp Accuracy Kappa
## 0.01177929 0.9613488 0.9179467
## 0.01394916 0.9522089 0.8997485
## 0.87724737 0.7955542 0.4762886
##
## Accuracy was used to select the optimal model using the largest value.
## The final value used for the model was cp = 0.01177929.
#predictions
#model performance
##
## predictions Approved Rejected
## Approved 630 3
## Rejected 34 400
#comparing model performance - LDA,CART ,KNN
#LDA
set.seed(7)
loan_dataset_model_lda <- train(loan_status ~ ., data = loan_dataset,
method = "lda", trControl = train_control)#CART
set.seed(7)
loan_dataset_model_cart <- train(loan_status ~ ., data = loan_dataset,
method = "rpart", trControl = train_control)#KNN
set.seed(7)
loan_dataset_model_knn <- train(loan_status ~ ., data = loan_dataset,
method = "knn", trControl = train_control)#call resamples functions
results <- resamples(list(LDA = loan_dataset_model_lda, CART = loan_dataset_model_cart,
KNN = loan_dataset_model_knn))#results of the resample functions
##
## Call:
## summary.resamples(object = results)
##
## Models: LDA, CART, KNN
## Number of resamples: 30
##
## Accuracy
## Min. 1st Qu. Median Mean 3rd Qu. Max. NA's
## LDA 0.8992974 0.9140863 0.9261443 0.9258262 0.9379391 0.9530516 0
## CART 0.9462617 0.9555035 0.9648300 0.9629910 0.9695372 0.9789227 0
## KNN 0.5386417 0.5623907 0.5819672 0.5765562 0.5916799 0.6042155 0
##
## Kappa
## Min. 1st Qu. Median Mean 3rd Qu. Max. NA's
## LDA 0.79051194 0.82028532 0.84603157 0.84455057 0.86890084 0.90015235 0
## CART 0.88807531 0.90602323 0.92487331 0.92132012 0.93427843 0.95463185 0
## KNN -0.05034525 -0.01124975 0.03322478 0.01946604 0.04506238 0.08480503 0
#box plot of the results
scales <- list(x = list(relation = "free"), y = list(relation = "free"))
bwplot(results, scales = scales)#load dataset
## Rows: 4269 Columns: 13
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (3): education, self_employed, loan_status
## dbl (10): loan_id, no_of_dependents, income_annum, loan_amount, loan_term, c...
##
## ℹ 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.
#install packages
## mlbench ----
if (require("mlbench")) {
require("mlbench")
} else {
install.packages("mlbench", dependencies = TRUE,
repos = "https://cloud.r-project.org")
}
## caret ----
if (require("caret")) {
require("caret")
} else {
install.packages("caret", dependencies = TRUE,
repos = "https://cloud.r-project.org")
}
## caretEnsemble ----
if (require("caretEnsemble")) {
require("caretEnsemble")
} else {
install.packages("caretEnsemble", dependencies = TRUE,
repos = "https://cloud.r-project.org")
}## Loading required package: caretEnsemble
## Warning: package 'caretEnsemble' was built under R version 4.3.2
##
## Attaching package: 'caretEnsemble'
## The following object is masked from 'package:ggplot2':
##
## autoplot
## C50 ----
if (require("C50")) {
require("C50")
} else {
install.packages("C50", dependencies = TRUE,
repos = "https://cloud.r-project.org")
}## Loading required package: C50
## Warning: package 'C50' was built under R version 4.3.2
## adabag ----
if (require("adabag")) {
require("adabag")
} else {
install.packages("adabag", dependencies = TRUE,
repos = "https://cloud.r-project.org")
}## Loading required package: adabag
## Warning: package 'adabag' was built under R version 4.3.2
## Loading required package: rpart
## Loading required package: foreach
## Loading required package: doParallel
## Warning: package 'doParallel' was built under R version 4.3.2
## Loading required package: iterators
## Loading required package: parallel
#Baggging ensemble method
train_control <- trainControl(method = "repeatedcv", number = 10, repeats = 3)
seed <- 7
metric <- "Accuracy"#1.Bagged CART
set.seed(seed)
loan_dataset_model_bagged_cart <- train(loan_status ~ ., data = loan_dataset, method = "treebag",
metric = metric,
trControl = train_control)#2.Random Forest
set.seed(seed)
loan_dataset_model_rf <- train(loan_status ~ ., data = loan_dataset, method = "rf",
metric = metric, trControl = train_control)#summary results of each algorithm
bagging_results <-
resamples(list("Bagged Decision Tree" = loan_dataset_model_bagged_cart,
"Random Forest" = loan_dataset_model_rf))#total summary results of bagging
##
## Call:
## summary.resamples(object = bagging_results)
##
## Models: Bagged Decision Tree, Random Forest
## Number of resamples: 30
##
## Accuracy
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## Bagged Decision Tree 0.971897 0.9788732 0.9824164 0.9832114 0.9882904 0.9953162
## Random Forest 0.969555 0.9812317 0.9859155 0.9845385 0.9882904 0.9953162
## NA's
## Bagged Decision Tree 0
## Random Forest 0
##
## Kappa
## Min. 1st Qu. Median Mean 3rd Qu.
## Bagged Decision Tree 0.9398888 0.9547531 0.9625702 0.9642194 0.9749815
## Random Forest 0.9351117 0.9598547 0.9699409 0.9670091 0.9749815
## Max. NA's
## Bagged Decision Tree 0.9900536 0
## Random Forest 0.9900536 0
#plot of the summary