library(dplyr) library(tidyr) library(ggplot2) library(e1071)

set.seed(42) n <- 300 creditdata <- data.frame( CustomerID = paste0(“C”, 1:n), Balance = rgamma(n, shape = 2, scale = 1000), # lệch phải Income = rnorm(n, mean = 1500, sd = 500), # gần chuẩn Transactions = rpois(n, lambda = 3) # biến rời rạc )

#2. Histogram – Phân phối lệch phải (Số dư tài khoản) ggplot(creditdata, aes(x = Balance)) + geom_histogram(bins = 30, fill = “#002f6c”, color = “white”, aes(y = ..density..)) + geom_density(color = “red”, size = 1) + labs( title = “Phân phối lệch phải: Số dư tài khoản”, x = “Số dư (VND)”, y = “Mật độ” ) + theme( plot.title = element_text(family = “Arial”, size = 24, hjust = 0.5), axis.title = element_text(family = “Arial”, size = 18))

#3. Histogram – Phân phối gần chuẩn (Thu nhập) ggplot(creditdata, aes(x = Income)) + geom_histogram(bins = 30, fill = “#e31c3d”, color = “white”, aes(y = ..density..)) + geom_density(color = “black”, size = 1) + labs( title = “Phân phối gần chuẩn: Thu nhập”, x = “Thu nhập (VND)”, y = “Mật độ” ) + theme( plot.title = element_text(family = “Arial”, size = 24, hjust = 0.5), axis.title = element_text(family = “Arial”, size = 18)) ) #4. Barplot – Tần suất giao dịch hàng tháng ggplot(creditdata, aes(x = factor(Transactions))) + geom_bar(fill = “#0072ce”) + labs( title = “Tần suất giao dịch hàng tháng”, x = “Số giao dịch”, y = “Số lượng khách hàng” ) + theme( plot.title = element_text(family = “Arial”, size = 24, hjust = 0.5), axis.title = element_text(family = “Arial”, size = 18) )

#5. Biểu đồ + Skewness & Kurtosis: Thu nhập ggplot(creditdata, aes(x = Income)) + geom_histogram(aes(y = ..density..), bins = 30, fill = “#e94e77”, color = “white”) + geom_density(color = “darkred”, size = 1.2) + labs( title = “Phân phối thu nhập khách hàng”, x = “Thu nhập (VND)”, y = “Mật độ” ) + annotate( “text”, x = max(creditdata\(Income, na.rm = TRUE) * 0.6, y = 0.0008, label = paste0( "Skewness: ", round(skewness(creditdata\)Income, na.rm = TRUE), 2), “:”, round(kurtosis(creditdata$Income, na.rm = TRUE), 2) ), size = 4, hjust = 0 ) + theme( plot.title = element_text(family = “Arial”, size = 24, hjust = 0.5), axis.title = element_text(family = “Arial”, size = 18) )

Kiểm tra

ggplot(creditdata, aes(sample = Balance)) + stat_qq(color = “#2a80b9”, size = 4) + stat_qq_line(color = “red”, linetype = “dashed”, size = 0.8) + labs(title = “Q–Q plot: Số dư tài khoản”, x = “Lý thuyết chuẩn”, y = “Phân vị quan sát”) + theme( plot.title = element_text(family = “Arial”, size = 24, hjust = 0.5), axis.title = element_text(family = “Arial”, size = 18) )

outliers <- boxplot.stats(creditdata\(Balance)\)out length(outliers) # Số lượng ngoại lệ