#MAKE SURE WHEN YOU START CODING YOUR SECTION, YOU RUN FROM THE TOP ALL THE WAY THROUGH BEFORE CODING YOUR SECTION.
options(repos = c(CRAN = "https://cran.rstudio.com/"))
#use this chunk to load in EVERY install.packages("), library(), and data("") for the project.
#This is so we can easily start coding on the next persons computer
#Before knitting, comment out any install.package()
#install.packages("tidyverse")
#install.packages("corrplot")
#install.packages("summarytools")
#install.packages("MASS")
if (!requireNamespace("conflicted", quietly = TRUE)) {
install.packages("conflicted")
}
library(conflicted)
# Force conflicts to become errors
conflict_prefer("filter", winner = "dplyr")
## [conflicted] Will prefer dplyr::filter over any other package.
conflict_prefer("lag", winner = "dplyr")
## [conflicted] Will prefer dplyr::lag over any other package.
library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## âś” dplyr 1.1.4 âś” readr 2.1.5
## âś” forcats 1.0.0 âś” stringr 1.5.1
## âś” ggplot2 3.4.4 âś” tibble 3.2.1
## âś” lubridate 1.9.3 âś” tidyr 1.3.0
## âś” purrr 1.0.2
library(corrplot)
## corrplot 0.92 loaded
library(summarytools)
library(MASS)
# Import of Dataset
credit_model <- read.csv("C:/Users/Hagop/Downloads/UCI_Credit_Card.csv/UCI_Credit_Card.csv")
# Dataset Variable Groupings
factor_variables <- sapply(credit_model, is.factor) # Groups factor variables
numeric_variables <- sapply(credit_model, is.numeric) # Groups numeric variables
binary_variables <- sapply(credit_model, function(x) is.numeric(x) && all(x %in% c(0, 1))) # Groups binary variables
numeric_data <- credit_model[, numeric_variables] # Subset the data to include only numeric variables
# Histogram
for (variable in names(numeric_data)) {
hist(numeric_data[[variable]], main = paste("Histogram of", variable), xlab = "Values", ylab = "Frequency")
}
# Fitted Distributions
fit_and_plot_distribution <- function(data, variable) {
data_to_fit <- data[[variable]]
fit_result <- suppressWarnings(fitdistr(data_to_fit, densfun = "normal"))
cat("Variable:", variable, "\n")
cat("Fitted Parameters:", fit_result$estimate, "\n\n")
hist(data_to_fit, main = paste("Histogram of", variable, "with Fitted Distribution"),
xlab = "Values",
ylab = "Frequency",
col = "lightblue", probability = TRUE)
curve(dnorm(x, mean = fit_result$estimate[1], sd = fit_result$estimate[2]), #Applies fitted distribution line
col = "darkblue", lwd = 2, add = TRUE)
}
# Loop through numeric variables and create fitted distribution histograms
for (variable in names(numeric_data)) {
fit_and_plot_distribution(numeric_data, variable)
}
## Variable: ID
## Fitted Parameters: 15000.5 8660.254
## Variable: LIMIT_BAL
## Fitted Parameters: 167484.3 129745.5
## Variable: SEX
## Fitted Parameters: 1.603733 0.489121
## Variable: EDUCATION
## Fitted Parameters: 1.853133 0.7903355
## Variable: MARRIAGE
## Fitted Parameters: 1.551867 0.5219609
## Variable: AGE
## Fitted Parameters: 35.4855 9.21775
## Variable: PAY_0
## Fitted Parameters: -0.0167 1.123783
## Variable: PAY_2
## Fitted Parameters: -0.1337667 1.197166
## Variable: PAY_3
## Fitted Parameters: -0.1662 1.196848
## Variable: PAY_4
## Fitted Parameters: -0.2206667 1.169119
## Variable: PAY_5
## Fitted Parameters: -0.2662 1.133169
## Variable: PAY_6
## Fitted Parameters: -0.2911 1.149968
## Variable: BILL_AMT1
## Fitted Parameters: 51223.33 73634.63
## Variable: BILL_AMT2
## Fitted Parameters: 49179.08 71172.58
## Variable: BILL_AMT3
## Fitted Parameters: 47013.15 69348.23
## Variable: BILL_AMT4
## Fitted Parameters: 43262.95 64331.78
## Variable: BILL_AMT5
## Fitted Parameters: 40311.4 60796.14
## Variable: BILL_AMT6
## Fitted Parameters: 38871.76 59553.11
## Variable: PAY_AMT1
## Fitted Parameters: 5663.581 16563
## Variable: PAY_AMT2
## Fitted Parameters: 5921.163 23040.49
## Variable: PAY_AMT3
## Fitted Parameters: 5225.681 17606.67
## Variable: PAY_AMT4
## Fitted Parameters: 4826.077 15665.9
## Variable: PAY_AMT5
## Fitted Parameters: 4799.388 15278.05
## Variable: PAY_AMT6
## Fitted Parameters: 5215.503 17777.17
## Variable: default.payment.next.month
## Fitted Parameters: 0.2212 0.4150549
##Correlation Plot
variables <- credit_model[, c("default.payment.next.month","SEX", "AGE", "EDUCATION", "MARRIAGE", "LIMIT_BAL", "PAY_0", "PAY_2", "PAY_3", "PAY_4", "PAY_5", "PAY_6")]
corvariables <- cor(variables)
corrplotvariables <- corrplot(corvariables,
tl.col = "black",
tl.srt = 45)
# Box Plots
options(scipen = 999) # Disables scientific notation on axes
boxplot(credit_model$LIMIT_BAL,
main = "Given Credit Limit")
boxplot(credit_model$EDUCATION,
main = "Education (1 = graduate school; 2 = university; 3 = high school; 4 = others)")
boxplot(credit_model$MARRIAGE,
main = "Marital status (0 = unknown, 1 = married; 2 = single; 3 = others)")
boxplot(credit_model$AGE,
main = "Age (Years)")
boxplot(credit_model$PAY_0,
main = "Repayment Status in September 2005 (Months Delayed)")
boxplot(credit_model$PAY_6,
main = "Repayment Status in April 2005 (Months Delayed)" )
boxplot(credit_model$BILL_AMT1,
main = "Amount of Bill Statement in September 2005")
boxplot(credit_model$BILL_AMT6,
main = "Amount of Bill Statement in April 2005")
boxplot(credit_model$PAY_AMT1,
main = "Amount Paid in September 2005")
boxplot(credit_model$PAY_AMT6,
main = "Amount Paid in April 2005")
boxplot(credit_model$default.payment.next.month,
main = "Default on Payment Next Month")
# Bar Plot
barplot(table(variables$default.payment.next.month),
main = "Bar Plot of Default Payment",
xlab = "Default Payment",
col = "skyblue")
# Scatter Plots
plot(credit_model$LIMIT_BAL, jitter(credit_model$default.payment.next.month, factor = 1),
main = "Scatter Plot",
xlab = "Credit Limit",
ylab = "Mean Default Rate",
pch = 16, col = "blue")
plot(jitter(credit_model$EDUCATION, factor = 2), jitter(credit_model$default.payment.next.month, factor = 1),
main = "Scatter Plot",
xlab = "Education (1 = graduate school; 2 = university; 3 = high school; 4 = others)",
ylab = "Default on Payment Next Month (0 = No, 1 = Yes)",
pch = 16, col = "red")
plot(jitter(credit_model$MARRIAGE, factor = 2), jitter(credit_model$default.payment.next.month, factor = 1),
main = "Scatter Plot",
xlab = "Marital status (0 = unknown, 1 = married; 2 = single; 3 = others)",
ylab = "Default on Payment Next Month (0 = No, 1 = Yes)",
pch = 16, col = "green")
plot(jitter(credit_model$AGE, factor = 2), jitter(credit_model$default.payment.next.month, factor = 1),
main = "Scatter Plot",
xlab = "AGE (Years)",
ylab = "Default on Payment Next Month (0 = No, 1 = Yes)",
pch = 16, col = "cyan")
plot(credit_model$PAY_0, jitter(credit_model$default.payment.next.month, factor = 1),
main = "Scatter Plot",
xlab = "Repayment Status in September 2005 (Months Delayed)",
ylab = "Default on Payment Next Month (0 = No, 1 = Yes)",
pch = 16, col = "purple")
plot(credit_model$PAY_6, jitter(credit_model$default.payment.next.month, factor = 1),
main = "Scatter Plot",
xlab = "Repayment Status in April 2005 (Months Delayed)",
ylab = "Default on Payment Next Month (0 = No, 1 = Yes)",
pch = 16, col = "purple")
plot(credit_model$BILL_AMT1, jitter(credit_model$default.payment.next.month, factor = 1),
main = "Scatter Plot",
xlab = "Amount of Bill Statement in September 2005",
ylab = "Default on Payment Next Month (0 = No, 1 = Yes)",
pch = 16, col = "pink")
plot(credit_model$BILL_AMT6, jitter(credit_model$default.payment.next.month, factor = 1),
main = "Scatter Plot",
xlab = "Amount of Bill Statement in September 2005",
ylab = "Default on Payment Next Month (0 = No, 1 = Yes)",
pch = 16, col = "pink")
plot(credit_model$PAY_AMT1, jitter(credit_model$default.payment.next.month, factor = 1),
main = "Scatter Plot",
xlab = "Amount of Bill Statement in September 2005",
ylab = "Default on Payment Next Month (0 = No, 1 = Yes)",
pch = 16, col = "yellow")
plot(jitter(credit_model$PAY_AMT6, factor = 2), jitter(credit_model$default.payment.next.month, factor = 1),
main = "Scatter Plot",
xlab = "Amount of Bill Statement in April 2005",
ylab = "Default on Payment Next Month (0 = No, 1 = Yes)",
pch = 16, col = "yellow")
# Five-Number Summary
summary(credit_model)
## ID LIMIT_BAL SEX EDUCATION
## Min. : 1 Min. : 10000 Min. :1.000 Min. :0.000
## 1st Qu.: 7501 1st Qu.: 50000 1st Qu.:1.000 1st Qu.:1.000
## Median :15000 Median : 140000 Median :2.000 Median :2.000
## Mean :15000 Mean : 167484 Mean :1.604 Mean :1.853
## 3rd Qu.:22500 3rd Qu.: 240000 3rd Qu.:2.000 3rd Qu.:2.000
## Max. :30000 Max. :1000000 Max. :2.000 Max. :6.000
## MARRIAGE AGE PAY_0 PAY_2
## Min. :0.000 Min. :21.00 Min. :-2.0000 Min. :-2.0000
## 1st Qu.:1.000 1st Qu.:28.00 1st Qu.:-1.0000 1st Qu.:-1.0000
## Median :2.000 Median :34.00 Median : 0.0000 Median : 0.0000
## Mean :1.552 Mean :35.49 Mean :-0.0167 Mean :-0.1338
## 3rd Qu.:2.000 3rd Qu.:41.00 3rd Qu.: 0.0000 3rd Qu.: 0.0000
## Max. :3.000 Max. :79.00 Max. : 8.0000 Max. : 8.0000
## PAY_3 PAY_4 PAY_5 PAY_6
## Min. :-2.0000 Min. :-2.0000 Min. :-2.0000 Min. :-2.0000
## 1st Qu.:-1.0000 1st Qu.:-1.0000 1st Qu.:-1.0000 1st Qu.:-1.0000
## Median : 0.0000 Median : 0.0000 Median : 0.0000 Median : 0.0000
## Mean :-0.1662 Mean :-0.2207 Mean :-0.2662 Mean :-0.2911
## 3rd Qu.: 0.0000 3rd Qu.: 0.0000 3rd Qu.: 0.0000 3rd Qu.: 0.0000
## Max. : 8.0000 Max. : 8.0000 Max. : 8.0000 Max. : 8.0000
## BILL_AMT1 BILL_AMT2 BILL_AMT3 BILL_AMT4
## Min. :-165580 Min. :-69777 Min. :-157264 Min. :-170000
## 1st Qu.: 3559 1st Qu.: 2985 1st Qu.: 2666 1st Qu.: 2327
## Median : 22382 Median : 21200 Median : 20089 Median : 19052
## Mean : 51223 Mean : 49179 Mean : 47013 Mean : 43263
## 3rd Qu.: 67091 3rd Qu.: 64006 3rd Qu.: 60165 3rd Qu.: 54506
## Max. : 964511 Max. :983931 Max. :1664089 Max. : 891586
## BILL_AMT5 BILL_AMT6 PAY_AMT1 PAY_AMT2
## Min. :-81334 Min. :-339603 Min. : 0 Min. : 0
## 1st Qu.: 1763 1st Qu.: 1256 1st Qu.: 1000 1st Qu.: 833
## Median : 18105 Median : 17071 Median : 2100 Median : 2009
## Mean : 40311 Mean : 38872 Mean : 5664 Mean : 5921
## 3rd Qu.: 50191 3rd Qu.: 49198 3rd Qu.: 5006 3rd Qu.: 5000
## Max. :927171 Max. : 961664 Max. :873552 Max. :1684259
## PAY_AMT3 PAY_AMT4 PAY_AMT5 PAY_AMT6
## Min. : 0 Min. : 0 Min. : 0.0 Min. : 0.0
## 1st Qu.: 390 1st Qu.: 296 1st Qu.: 252.5 1st Qu.: 117.8
## Median : 1800 Median : 1500 Median : 1500.0 Median : 1500.0
## Mean : 5226 Mean : 4826 Mean : 4799.4 Mean : 5215.5
## 3rd Qu.: 4505 3rd Qu.: 4013 3rd Qu.: 4031.5 3rd Qu.: 4000.0
## Max. :896040 Max. :621000 Max. :426529.0 Max. :528666.0
## default.payment.next.month
## Min. :0.0000
## 1st Qu.:0.0000
## Median :0.0000
## Mean :0.2212
## 3rd Qu.:0.0000
## Max. :1.0000