THỐNG KÊ MÔ TẢ

1. Nạp thư viện, dữ liệu và thống kê mô tả

library(openxlsx)
library(psych)
library(ggplot2)
library(patchwork)
library(questionr)

my_data <- openxlsx::read.xlsx("04_Ket_qua.xlsx")

# Thống kê mô tả các biến định lượng
ket_qua <- psych::describe(my_data[, c("order_total", "coupon_discount",
                                       "distance_to_nearest_warehouse", 
                                       "calculated_total", "delivery_charges")])
ket_qua[, c("min", "max", "mean", "median", "sd")]
##                                  min       max     mean   median       sd
## order_total                   639.29  33947.06 12206.42 10885.19  7265.28
## coupon_discount                 0.00     25.00    10.96    10.00     8.60
## distance_to_nearest_warehouse   0.11     94.97     2.24     1.03     8.97
## calculated_total              530.54 853012.72 22792.49 11182.26 76859.69
## delivery_charges               46.35    114.04    76.64    76.22    14.56

2. Biểu đồ phân phối trước khi xử lý ngoại lai

ggplot(data = my_data, aes(x = distance_to_nearest_warehouse)) +
  geom_histogram(bins = 30, fill = "seagreen", color = "black") +
  labs(title = "Phân phối Khoảng cách đến kho hàng",
       x = "Khoảng cách (Distance)",
       y = "Số lượng đơn (Tần số)") +
  theme_minimal()

ggplot(data = my_data, aes(x = delivery_charges)) +
  geom_histogram(bins = 30, fill = "mediumpurple", color = "black") +
  labs(title = "Phân phối Phí giao hàng",
       x = "Phí giao hàng (Delivery Charges)",
       y = "Số lượng đơn (Tần số)") +
  theme_minimal()

3. Xử lý ngoại lai bằng phương pháp IQR (2 biến chính)

xlnlppxoa_data <- my_data

rm.out <- function(x, na.rm = TRUE, ...) {
  qnt <- quantile(x, probs = c(0.25, 0.75), na.rm = na.rm, ...)
  H <- 1.5 * IQR(x, na.rm = na.rm)
  y <- x
  y[x < (qnt[1] - H)] <- NA
  y[x > (qnt[2] + H)] <- NA
  y
}

cols_to_check <- c("distance_to_nearest_warehouse", "delivery_charges")
for (col in cols_to_check) {
  xlnlppxoa_data[[col]] <- rm.out(xlnlppxoa_data[[col]])
}

p_distance <- ggplot(xlnlppxoa_data, aes(x = distance_to_nearest_warehouse)) +
  geom_histogram(bins = 30, fill = "seagreen", color = "black", na.rm = TRUE) +
  labs(title = "Phân phối Distance to nearest warehouse",
       subtitle = "Đã xử lý ngoại lai (IQR)",
       x = "Khoảng cách (Distance)",
       y = "Số lượng đơn") +
  theme_minimal()

p_delivery <- ggplot(xlnlppxoa_data, aes(x = delivery_charges)) +
  geom_histogram(bins = 30, fill = "mediumpurple", color = "black", na.rm = TRUE) +
  labs(title = "Phân phối Delivery charges",
       subtitle = "Đã xử lý ngoại lai (IQR)",
       x = "Phí giao hàng (Delivery charges)",
       y = "Số lượng đơn") +
  theme_minimal()

p_distance / p_delivery

4. Biểu đồ phân tán (Scatter Plot) trước khi xử lý ngoại lai

p1 <- ggplot(my_data, aes(x = order_total, y = delivery_charges)) +
  geom_point(color = "steelblue", alpha = 0.5) +
  labs(x = "Order Total", y = "Delivery Charges") +
  theme_minimal()

p2 <- ggplot(my_data, aes(x = coupon_discount, y = delivery_charges)) +
  geom_point(color = "coral", alpha = 0.5) +
  labs(x = "Coupon Discount", y = "Delivery Charges") +
  theme_minimal()

p3 <- ggplot(my_data, aes(x = calculated_total, y = delivery_charges)) +
  geom_point(color = "seagreen", alpha = 0.5) +
  labs(x = "Calculated Total", y = "Delivery Charges") +
  theme_minimal()

p4 <- ggplot(my_data, aes(x = distance_to_nearest_warehouse, y = delivery_charges)) +
  geom_point(color = "mediumpurple", alpha = 0.5) +
  labs(x = "Distance to nearest warehouse", y = "Delivery Charges") +
  theme_minimal()

(p1 + p2) / (p3 + p4)

5. Xử lý ngoại lai toàn bộ các biến và vẽ lại Scatter Plot

xlnlppxoa_data <- my_data

cols_to_check <- c("delivery_charges", "coupon_discount", "order_total",
                   "distance_to_nearest_warehouse", "calculated_total")

for (col in cols_to_check) {
  xlnlppxoa_data[[col]] <- rm.out(xlnlppxoa_data[[col]])
}

# Kiểm tra tần số giá trị khuyết thiếu (NA) sau khi lọc ngoại lai và loại bỏ NA
freq.na(xlnlppxoa_data)
##                               missing %
## calculated_total                   15 3
## delivery_charges                   12 2
## distance_to_nearest_warehouse      12 2
## nearest_warehouse                   0 0
## coupon_discount                     0 0
## order_total                         0 0
## season                              0 0
## is_expedited_delivery               0 0
## is_happy_customer                   0 0
xlnlppxoa_data <- na.omit(xlnlppxoa_data)

p1 <- ggplot(xlnlppxoa_data, aes(x = order_total, y = delivery_charges)) +
  geom_point(color = "steelblue", alpha = 0.5, na.rm = TRUE) +
  labs(x = "Order Total", y = "Delivery Charges") +
  theme_minimal()

p2 <- ggplot(xlnlppxoa_data, aes(x = coupon_discount, y = delivery_charges)) +
  geom_point(color = "coral", alpha = 0.5, na.rm = TRUE) +
  labs(x = "Coupon Discount", y = "Delivery Charges") +
  theme_minimal()

p3 <- ggplot(xlnlppxoa_data, aes(x = calculated_total, y = delivery_charges)) +
  geom_point(color = "seagreen", alpha = 0.5, na.rm = TRUE) +
  labs(x = "Calculated Total", y = "Delivery Charges") +
  theme_minimal()

p4 <- ggplot(xlnlppxoa_data, aes(x = distance_to_nearest_warehouse, y = delivery_charges)) +
  geom_point(color = "mediumpurple", alpha = 0.5, na.rm = TRUE) +
  labs(x = "Distance to nearest warehouse", y = "Delivery Charges") +
  theme_minimal()

(p1 + p2) / (p3 + p4)

6. Biểu đồ Boxplot theo các biến phân loại

Biến nearest_warehouse và distance_to_nearest_warehouse

my_data$nearest_warehouse <- as.factor(my_data$nearest_warehouse)

ggplot(my_data, aes(x = nearest_warehouse, y = distance_to_nearest_warehouse, fill = nearest_warehouse)) +
  geom_boxplot() +
  labs(title = "Boxplot of distance_to_nearest_warehouse",
       x = "Nearest Warehouse",
       y = "Distance to Nearest Warehouse") +
  theme_minimal()

Biến is_expedited_delivery và delivery_charges

# Chuyển sang factor (xử lý an toàn cho cả dữ liệu dạng 0/1 hoặc TRUE/FALSE)
my_data$is_expedited_delivery <- factor(as.numeric(as.logical(my_data$is_expedited_delivery)),
                                        levels = c(0, 1),
                                        labels = c("False", "True"))

xlnlppxoa_data$is_expedited_delivery <- factor(as.numeric(as.logical(xlnlppxoa_data$is_expedited_delivery)),
                                               levels = c(0, 1),
                                               labels = c("False", "True"))

# Chưa xử lý ngoại lai
ggplot(my_data, aes(x = is_expedited_delivery, y = delivery_charges, fill = is_expedited_delivery)) +
  geom_boxplot() +
  labs(title = "Boxplot of delivery_charges (Chưa xử lý ngoại lai)",
       x = "Is expedited delivery",
       y = "Delivery charges") +
  theme_minimal()

# Đã xử lý ngoại lai
ggplot(xlnlppxoa_data, aes(x = is_expedited_delivery, y = delivery_charges, fill = is_expedited_delivery)) +
  geom_boxplot() +
  labs(title = "Boxplot of delivery_charges (Đã xử lý ngoại lai)",
       x = "Is expedited delivery", 
       y = "Delivery charges") +
  theme_minimal()

Biến calculated_total và season

# Sắp xếp thứ tự các mùa (nếu dữ liệu gốc đã là chữ Spring, Summer, Autumn, Winter)
season_levels <- c("Spring", "Summer", "Autumn", "Winter")

if (all(na.omit(my_data$season) %in% c(1, 2, 3, 4))) {
  my_data$season <- factor(my_data$season, levels = c(1, 2, 3, 4), labels = season_levels)
  xlnlppxoa_data$season <- factor(xlnlppxoa_data$season, levels = c(1, 2, 3, 4), labels = season_levels)
} else {
  my_data$season <- factor(my_data$season, levels = season_levels)
  xlnlppxoa_data$season <- factor(xlnlppxoa_data$season, levels = season_levels)
}

# Chưa xử lý ngoại lai
ggplot(my_data, aes(x = season, y = calculated_total, fill = season)) +
  geom_boxplot() +
  labs(title = "Boxplot of calculated_total by Season (Chưa xử lý ngoại lai)",
       x = "Season",
       y = "Calculated Total") +
  theme_minimal()

# Đã xử lý ngoại lai
ggplot(xlnlppxoa_data, aes(x = season, y = calculated_total, fill = season)) +
  geom_boxplot() +
  labs(title = "Boxplot of calculated_total by Season (Đã xử lý ngoại lai)",
       x = "Season",
       y = "Calculated Total") +
  theme_minimal()

7. Đồ thị tương quan (Correlation Matrix)

library(corrplot)

cor_matrix <- cor(xlnlppxoa_data[, c("delivery_charges", "coupon_discount", "order_total", 
                                    "distance_to_nearest_warehouse", "calculated_total")], 
                  use = "complete.obs")

corrplot(cor_matrix,
         method = "color",
         type = "upper",
         tl.col = "black",
         tl.srt = 45,
         addCoef.col = "black",
         number.cex = 0.7,
         diag = FALSE)