CHUẨN BỊ DỮ LIỆU
library(readr)
unzip("C:/Users/HP/Downloads/archive (3).zip", exdir = "C:/Users/HP/Downloads/archive_data")
data <- read_csv("C:/Users/HP/Downloads/archive_data/new_retail_data.csv")
## Rows: 302010 Columns: 30
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (19): Name, Email, Address, City, State, Country, Gender, Income, Custo...
## dbl (10): Transaction_ID, Customer_ID, Phone, Zipcode, Age, Year, Total_Pur...
## time (1): Time
##
## ℹ 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.
head(data)
## # A tibble: 6 × 30
## Transaction_ID Customer_ID Name Email Phone Address City State Zipcode
## <dbl> <dbl> <chr> <chr> <dbl> <chr> <chr> <chr> <dbl>
## 1 8691788 37249 Michelle … Ebon… 1.41e9 3959 A… Dort… Berl… 77985
## 2 2174773 69749 Kelsey Hi… Mark… 6.85e9 82072 … Nott… Engl… 99071
## 3 6679610 30192 Scott Jen… Shan… 8.36e9 4133 Y… Geel… New … 75929
## 4 7232460 62101 Joseph Mi… Mary… 2.78e9 8148 T… Edmo… Onta… 88420
## 5 4983775 27901 Debra Col… Char… 9.10e9 5813 L… Bris… Engl… 48704
## 6 6095326 41289 Ryan John… Hale… 3.29e9 532 As… Bris… New … 74430
## # ℹ 21 more variables: Country <chr>, Age <dbl>, Gender <chr>, Income <chr>,
## # Customer_Segment <chr>, Date <chr>, Year <dbl>, Month <chr>, Time <time>,
## # Total_Purchases <dbl>, Amount <dbl>, Total_Amount <dbl>,
## # Product_Category <chr>, Product_Brand <chr>, Product_Type <chr>,
## # Feedback <chr>, Shipping_Method <chr>, Payment_Method <chr>,
## # Order_Status <chr>, Ratings <dbl>, products <chr>
str (data)
## spc_tbl_ [302,010 × 30] (S3: spec_tbl_df/tbl_df/tbl/data.frame)
## $ Transaction_ID : num [1:302010] 8691788 2174773 6679610 7232460 4983775 ...
## $ Customer_ID : num [1:302010] 37249 69749 30192 62101 27901 ...
## $ Name : chr [1:302010] "Michelle Harrington" "Kelsey Hill" "Scott Jensen" "Joseph Miller" ...
## $ Email : chr [1:302010] "Ebony39@gmail.com" "Mark36@gmail.com" "Shane85@gmail.com" "Mary34@gmail.com" ...
## $ Phone : num [1:302010] 1.41e+09 6.85e+09 8.36e+09 2.78e+09 9.10e+09 ...
## $ Address : chr [1:302010] "3959 Amanda Burgs" "82072 Dawn Centers" "4133 Young Canyon" "8148 Thomas Creek Suite 100" ...
## $ City : chr [1:302010] "Dortmund" "Nottingham" "Geelong" "Edmonton" ...
## $ State : chr [1:302010] "Berlin" "England" "New South Wales" "Ontario" ...
## $ Zipcode : num [1:302010] 77985 99071 75929 88420 48704 ...
## $ Country : chr [1:302010] "Germany" "UK" "Australia" "Canada" ...
## $ Age : num [1:302010] 21 19 48 56 22 58 29 29 46 25 ...
## $ Gender : chr [1:302010] "Male" "Female" "Male" "Male" ...
## $ Income : chr [1:302010] "Low" "Low" "Low" "High" ...
## $ Customer_Segment: chr [1:302010] "Regular" "Premium" "Regular" "Premium" ...
## $ Date : chr [1:302010] "9/18/2023" "12/31/2023" "4/26/2023" "5/8/2023" ...
## $ Year : num [1:302010] 2023 2023 2023 2023 2024 ...
## $ Month : chr [1:302010] "September" "December" "April" "May" ...
## $ Time : 'hms' num [1:302010] 22:03:55 08:42:04 04:06:29 14:55:17 ...
## ..- attr(*, "units")= chr "secs"
## $ Total_Purchases : num [1:302010] 3 2 3 7 2 4 2 1 8 10 ...
## $ Amount : num [1:302010] 108 403 354 352 124 ...
## $ Total_Amount : num [1:302010] 324 807 1063 2467 249 ...
## $ Product_Category: chr [1:302010] "Clothing" "Electronics" "Books" "Home Decor" ...
## $ Product_Brand : chr [1:302010] "Nike" "Samsung" "Penguin Books" "Home Depot" ...
## $ Product_Type : chr [1:302010] "Shorts" "Tablet" "Children's" "Tools" ...
## $ Feedback : chr [1:302010] "Excellent" "Excellent" "Average" "Excellent" ...
## $ Shipping_Method : chr [1:302010] "Same-Day" "Standard" "Same-Day" "Standard" ...
## $ Payment_Method : chr [1:302010] "Debit Card" "Credit Card" "Credit Card" "PayPal" ...
## $ Order_Status : chr [1:302010] "Shipped" "Processing" "Processing" "Processing" ...
## $ Ratings : num [1:302010] 5 4 2 4 1 4 1 1 1 4 ...
## $ products : chr [1:302010] "Cycling shorts" "Lenovo Tab" "Sports equipment" "Utility knife" ...
## - attr(*, "spec")=
## .. cols(
## .. Transaction_ID = col_double(),
## .. Customer_ID = col_double(),
## .. Name = col_character(),
## .. Email = col_character(),
## .. Phone = col_double(),
## .. Address = col_character(),
## .. City = col_character(),
## .. State = col_character(),
## .. Zipcode = col_double(),
## .. Country = col_character(),
## .. Age = col_double(),
## .. Gender = col_character(),
## .. Income = col_character(),
## .. Customer_Segment = col_character(),
## .. Date = col_character(),
## .. Year = col_double(),
## .. Month = col_character(),
## .. Time = col_time(format = ""),
## .. Total_Purchases = col_double(),
## .. Amount = col_double(),
## .. Total_Amount = col_double(),
## .. Product_Category = col_character(),
## .. Product_Brand = col_character(),
## .. Product_Type = col_character(),
## .. Feedback = col_character(),
## .. Shipping_Method = col_character(),
## .. Payment_Method = col_character(),
## .. Order_Status = col_character(),
## .. Ratings = col_double(),
## .. products = col_character()
## .. )
## - attr(*, "problems")=<externalptr>
Bộ dữ liệu có 302010 quan sát với 30 biến.Mỗi dòng là một giao dịch mua hàng, chưa thông tin về khách hàng, sản phẩm, giá tiền, phương thức thanh toán,..
Có 10 biến thuộc kiểu dữ liệu numeric: Transaction_ID, Customer_ID, Phone, Zipcode, Age, Year, Total_Purchases, Amout, Total_Amount, Rating.
Có 19 biến thuộc kiểu dữ liệu character: Name, Email, Address, City, State, Country, Gender, Income, Customer_Segment, Date,Product_Category, Product_Brand, Product_Type, Feedback, Shipping_Method, Payment_Method, Order_Status, products.
Có 1 biến kiểu dữ liệu hms: Time.
sum(duplicated(data))
## [1] 4
Kết quả trùng lặp được liệt kê dưới đây:
data[duplicated(data), ]
## # A tibble: 4 × 30
## Transaction_ID Customer_ID Name Email Phone Address City State Zipcode
## <dbl> <dbl> <chr> <chr> <dbl> <chr> <chr> <chr> <dbl>
## 1 4476510 20103 Christine… Jame… 2.38e9 8176 R… Kitc… Onta… 7099
## 2 5340129 29920 Collin Da… Cour… 4.09e9 371 Cy… Woll… New … 47758
## 3 4942326 25416 Pamela Ma… Chri… 6.66e9 9851 M… Leic… Engl… 57655
## 4 3200766 49598 Mikayla M… Kenn… 6.98e9 716 Jo… Brem… Berl… 64747
## # ℹ 21 more variables: Country <chr>, Age <dbl>, Gender <chr>, Income <chr>,
## # Customer_Segment <chr>, Date <chr>, Year <dbl>, Month <chr>, Time <time>,
## # Total_Purchases <dbl>, Amount <dbl>, Total_Amount <dbl>,
## # Product_Category <chr>, Product_Brand <chr>, Product_Type <chr>,
## # Feedback <chr>, Shipping_Method <chr>, Payment_Method <chr>,
## # Order_Status <chr>, Ratings <dbl>, products <chr>
Sau khi kiểm tra, tôi sẽ tiến hành loại bỏ các quan sát bị trùng để đảm bảo dữ liệu sạch và chính xác cho các bước phân tích tiếp theo:
data <- data[!duplicated(data), ]
nrow(data)
## [1] 302006
Tổng số quan sát giá trị bị thiếu trong toàn bộ dữ liệu
sum(is.na(data))
## [1] 8382
Số lượng giá trị bị thiếu theo từng cột
colSums(is.na(data))
## Transaction_ID Customer_ID Name Email
## 333 308 382 347
## Phone Address City State
## 362 315 248 281
## Zipcode Country Age Gender
## 340 271 173 317
## Income Customer_Segment Date Year
## 290 215 359 350
## Month Time Total_Purchases Amount
## 273 350 361 356
## Total_Amount Product_Category Product_Brand Product_Type
## 350 283 281 0
## Feedback Shipping_Method Payment_Method Order_Status
## 184 337 297 235
## Ratings products
## 184 0
Sau khi kiểm tra số quan sát bị thiếu tôi tiến hành xử lí các quan sát bị thiếu có kiểu dữ liệu là numeric bằng cách thay NA bằng giá trị trung bình của cột đó
library(dplyr)
## Warning: package 'dplyr' was built under R version 4.5.1
##
## 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
data <- data %>% mutate( across( .cols = where(is.numeric), .fns = ~ ifelse(is.na(.), mean(., na.rm = TRUE), .)))
Sau khi thay thế tiến hành kiểm tra lại
colSums(is.na(data))
## Transaction_ID Customer_ID Name Email
## 0 0 382 347
## Phone Address City State
## 0 315 248 281
## Zipcode Country Age Gender
## 0 271 0 317
## Income Customer_Segment Date Year
## 290 215 359 0
## Month Time Total_Purchases Amount
## 273 350 0 0
## Total_Amount Product_Category Product_Brand Product_Type
## 0 283 281 0
## Feedback Shipping_Method Payment_Method Order_Status
## 184 337 297 235
## Ratings products
## 0 0
Tiếp tục xử lí quan sát dữ liệu bị NA có kiểu character tôi tiến hành thay thế các dữ liệu bị thiếu đó bằng “Nothing”
data <- data %>% mutate( across( .cols = where(is.character), ~ ifelse(is.na(.), "Nothing", .)))
Tiến hành kiểm tra lại
colSums(is.na(data))
## Transaction_ID Customer_ID Name Email
## 0 0 0 0
## Phone Address City State
## 0 0 0 0
## Zipcode Country Age Gender
## 0 0 0 0
## Income Customer_Segment Date Year
## 0 0 0 0
## Month Time Total_Purchases Amount
## 0 350 0 0
## Total_Amount Product_Category Product_Brand Product_Type
## 0 0 0 0
## Feedback Shipping_Method Payment_Method Order_Status
## 0 0 0 0
## Ratings products
## 0 0
Xử lí NA của biến Time
library(hms)
data$Time <- ifelse(is.na(data$Time), hms::as_hms("00:00:00"),data$Time)
Kiểm tra lại
colSums(is.na(data))
## Transaction_ID Customer_ID Name Email
## 0 0 0 0
## Phone Address City State
## 0 0 0 0
## Zipcode Country Age Gender
## 0 0 0 0
## Income Customer_Segment Date Year
## 0 0 0 0
## Month Time Total_Purchases Amount
## 0 0 0 0
## Total_Amount Product_Category Product_Brand Product_Type
## 0 0 0 0
## Feedback Shipping_Method Payment_Method Order_Status
## 0 0 0 0
## Ratings products
## 0 0
Bảng giá trị duy nhất của từng biến trong bộ dữ liệu
library(dplyr)
library(knitr)
unique_info <- data.frame(
Bien = names(data),
So_luong_gia_tri = sapply(data, function(x) length(unique(x))),
Gia_tri_cu_the = sapply(data, function(x) {
vals <- as.character(unique(x))
if (length(vals) > 10) {
paste(c(vals[1:10], "..."), collapse = ", ")
} else {
paste(vals, collapse = ", ")
}
}),
stringsAsFactors = FALSE
)
kable(unique_info, caption = "Bảng: Giá trị duy nhất của từng biến trong bộ dữ liệu")
Bien | So_luong_gia_tri | Gia_tri_cu_the | |
---|---|---|---|
Transaction_ID | Transaction_ID | 294462 | 8691788, 2174773, 6679610, 7232460, 4983775, 6095326, 5434096, 2344675, 4155845, 4926148, … |
Customer_ID | Customer_ID | 86767 | 37249, 69749, 30192, 62101, 27901, 41289, 97285, 26603, 80175, 31878, … |
Name | Name | 159391 | Michelle Harrington, Kelsey Hill, Scott Jensen, Joseph Miller, Debra Coleman, Ryan Johnson, Erin Lewis, Angela Fields, Diane Clark, Lori Bell, … |
52898 | Ebony39@gmail.com, Mark36@gmail.com, Shane85@gmail.com, Mary34@gmail.com, Charles30@gmail.com, Haley12@gmail.com, Arthur76@gmail.com, Tanya94@gmail.com, Martin39@gmail.com, Jessica33@gmail.com, … | ||
Phone | Phone | 299296 | 1414786801, 6852899987, 8362160449, 2776751724, 9098267635, 3292677006, 1578355423, 3668096144, 6219779557, 6004895059, … |
Address | Address | 299330 | 3959 Amanda Burgs, 82072 Dawn Centers, 4133 Young Canyon, 8148 Thomas Creek Suite 100, 5813 Lori Ports Suite 269, 532 Ashley Crest Suite 014, 600 Brian Prairie Suite 497, 237 Young Curve, 8823 Mariah Heights Apt. 263, 6225 William Lodge, … |
City | City | 131 | Dortmund, Nottingham, Geelong, Edmonton, Bristol, Brisbane, Kitchener, Munich, Wollongong, Cologne, … |
State | State | 55 | Berlin, England, New South Wales, Ontario, Virginia, Nevada, Colorado, Arkansas, Texas, Oklahoma, … |
Zipcode | Zipcode | 93979 | 77985, 99071, 75929, 88420, 48704, 74430, 47545, 86862, 39820, 64317, … |
Country | Country | 6 | Germany, UK, Australia, Canada, USA, Nothing |
Age | Age | 54 | 21, 19, 48, 56, 22, 58, 29, 46, 25, 64, … |
Gender | Gender | 3 | Male, Female, Nothing |
Income | Income | 4 | Low, High, Medium, Nothing |
Customer_Segment | Customer_Segment | 4 | Regular, Premium, New, Nothing |
Date | Date | 367 | 9/18/2023, 12/31/2023, 4/26/2023, 5/8/2023, 1/10/2024, 9/21/2023, 6/26/2023, 3/24/2023, 1/6/2024, 10/4/2023, … |
Year | Year | 3 | 2023, 2024, 2023.16511523059 |
Month | Month | 13 | September, December, April, May, January, June, March, October, July, November, … |
Time | Time | 83677 | 79435, 31324, 14789, 53717, 60847, 84267, 48951, 36776, 52706, 80860, … |
Total_Purchases | Total_Purchases | 11 | 3, 2, 7, 4, 1, 8, 10, 9, 6, 5, … |
Amount | Amount | 299298 | 108.0287567, 403.3539073, 354.4775997, 352.4077173, 124.2765245, 296.2918059, 315.0576476, 46.58807038, 328.8393017, 397.6112295, … |
Total_Amount | Total_Amount | 299306 | 324.08627, 806.7078147, 1063.432799, 2466.854021, 248.5530491, 1185.167224, 630.1152951, 46.58807038, 2630.714413, 3976.112295, … |
Product_Category | Product_Category | 6 | Clothing, Electronics, Books, Home Decor, Grocery, Nothing |
Product_Brand | Product_Brand | 19 | Nike, Samsung, Penguin Books, Home Depot, Nestle, Apple, Zara, Random House, Coca-Cola, Adidas, … |
Product_Type | Product_Type | 33 | Shorts, Tablet, Children’s, Tools, Chocolate, Television, Shirt, Decorations, Non-Fiction, Water, … |
Feedback | Feedback | 5 | Excellent, Average, Bad, Good, Nothing |
Shipping_Method | Shipping_Method | 4 | Same-Day, Standard, Express, Nothing |
Payment_Method | Payment_Method | 5 | Debit Card, Credit Card, PayPal, Cash, Nothing |
Order_Status | Order_Status | 5 | Shipped, Processing, Pending, Delivered, Nothing |
Ratings | Ratings | 6 | 5, 4, 2, 1, 3, 3.16268529133065 |
products | products | 318 | Cycling shorts, Lenovo Tab, Sports equipment, Utility knife, Chocolate cookies, QLED TV, Dress shirt, Dark chocolate, Candles, Screwdriver set, … |
Bảng mô tả ý nghĩa các biến
variable_description <- data.frame(
Bien = c(
"Transaction_ID", "Customer_ID", "Name", "Email", "Phone", "Address",
"City", "State", "Zipcode", "Country", "Age", "Gender", "Income",
"Customer_Segment", "Date", "Year", "Month", "Time", "Total_Purchases",
"Amount", "Total_Amount", "Product_Category", "Product_Brand",
"Product_Type", "Feedback", "Shipping_Method", "Payment_Method",
"Order_Status", "Ratings", "Products"
),
YNghia = c(
"Mã giao dịch duy nhất cho mỗi đơn hàng.",
"Mã khách hàng duy nhất để định danh từng người mua.",
"Tên khách hàng thực hiện giao dịch.",
"Địa chỉ email của khách hàng.",
"Số điện thoại liên hệ của khách hàng.",
"Địa chỉ nơi khách hàng sinh sống hoặc giao hàng.",
"Thành phố nơi khách hàng sinh sống.",
"Tỉnh hoặc bang tương ứng với địa chỉ khách hàng.",
"Mã bưu điện (Zipcode) của địa chỉ khách hàng.",
"Quốc gia của khách hàng.",
"Độ tuổi của khách hàng.",
"Giới tính của khách hàng (Nam/Nữ).",
"Thu nhập của khách hàng",
"Phân khúc khách hàng.",
"Ngày giao dịch diễn ra.",
"Năm thực hiện giao dịch.",
"Tháng thực hiện giao dịch.",
"Thời gian cụ thể của giao dịch.",
"Tổng số lần mua hàng của khách trong khoảng thời gian nhất định.",
"Số tiền của đơn hàng hiện tại.",
"Tổng số tiền khách hàng đã chi tiêu trong toàn bộ thời gian mua hàng.",
"Danh mục sản phẩm mà khách hàng đã mua.",
"Thương hiệu sản phẩm mà khách hàng đã mua.",
"Loại sản phẩm chi tiết trong danh mục.",
"Phản hồi hoặc đánh giá bằng lời từ khách hàng về sản phẩm/dịch vụ.",
"Phương thức vận chuyển được lựa chọn.",
"Phương thức thanh toán.",
"Trạng thái đơn hàng.",
"Đánh giá sao mà khách hàng dành cho sản phẩm hoặc dịch vụ.",
"Tên sản phẩm chi tiết mà khách hàng đã mua."
)
)
View(variable_description)
library(DT)
## Warning: package 'DT' was built under R version 4.5.1
datatable(variable_description,
caption = "Bảng mô tả ý nghĩa các biến trong bộ dữ liệu",
options = list(pageLength = 10))
customer_income_gender <- data %>%
group_by(Gender, Income) %>%
summarise(
Count = n(),
Avg_Age = mean(Age, na.rm = TRUE)
) %>%
arrange(desc(Count))
## `summarise()` has grouped output by 'Gender'. You can override using the
## `.groups` argument.
customer_income_gender
## # A tibble: 11 × 4
## # Groups: Gender [3]
## Gender Income Count Avg_Age
## <chr> <chr> <int> <dbl>
## 1 Male Medium 79677 34.6
## 2 Male Low 59436 37.9
## 3 Female Medium 50412 35.0
## 4 Male High 48304 32.6
## 5 Female Low 36719 38.9
## 6 Female High 26851 34.0
## 7 Male Nothing 179 38.4
## 8 Nothing Medium 141 35.2
## 9 Female Nothing 111 39.6
## 10 Nothing Low 102 40.5
## 11 Nothing High 74 35.8
bussiness_efficiency <- data %>%
group_by(Customer_Segment, Product_Category) %>%
summarise(
Total_Revenue = sum(Total_Amount, na.rm = TRUE),
Avg_Rating = mean(Ratings, na.rm = TRUE)
) %>%
arrange(desc(Total_Revenue))
## `summarise()` has grouped output by 'Customer_Segment'. You can override using
## the `.groups` argument.
bussiness_efficiency
## # A tibble: 23 × 4
## # Groups: Customer_Segment [4]
## Customer_Segment Product_Category Total_Revenue Avg_Rating
## <chr> <chr> <dbl> <dbl>
## 1 Regular Electronics 48884199. 3.16
## 2 Regular Grocery 41935155. 3.06
## 3 Regular Home Decor 36512437. 2.99
## 4 Regular Books 36406687. 2.98
## 5 Regular Clothing 36275729. 2.98
## 6 New Grocery 28171047. 3.22
## 7 New Electronics 27675840. 3.29
## 8 New Clothing 23135079. 3.14
## 9 New Books 22957435. 3.17
## 10 New Home Decor 22663776. 3.15
## # ℹ 13 more rows
buying_behavior <- data %>%
group_by(Shipping_Method, Payment_Method, Customer_Segment, Product_Category) %>%
summarise(
Orders = n(),
Avg_Amount = mean(Amount, na.rm = TRUE),
Avg_Rating = mean(Ratings, na.rm = TRUE)
) %>%
arrange(desc(Orders))
## `summarise()` has grouped output by 'Shipping_Method', 'Payment_Method',
## 'Customer_Segment'. You can override using the `.groups` argument.
buying_behavior
## # A tibble: 377 × 7
## # Groups: Shipping_Method, Payment_Method, Customer_Segment [71]
## Shipping_Method Payment_Method Customer_Segment Product_Category Orders
## <chr> <chr> <chr> <chr> <int>
## 1 Same-Day Credit Card Regular Electronics 4534
## 2 Express Cash Regular Electronics 3409
## 3 Express Credit Card Regular Electronics 3378
## 4 Same-Day Credit Card New Electronics 3343
## 5 Same-Day Cash Regular Electronics 2966
## 6 Express Credit Card Regular Grocery 2943
## 7 Standard Credit Card Regular Electronics 2915
## 8 Express Debit Card Regular Electronics 2890
## 9 Same-Day Credit Card Premium Electronics 2851
## 10 Same-Day Debit Card Regular Electronics 2823
## # ℹ 367 more rows
## # ℹ 2 more variables: Avg_Amount <dbl>, Avg_Rating <dbl>
library(ggplot2)
## Warning: package 'ggplot2' was built under R version 4.5.1
ggplot(buying_behavior, aes(x = Shipping_Method, y = Orders, fill = Payment_Method)) +
geom_bar(stat = "identity", position = "dodge") +
labs(
title = "Số lượng đơn hàng theo phương thức giao hàng và thanh toán",
x = "Phương thức giao hàng",
y = "Số lượng đơn hàng",
fill = "Thanh toán"
) +
theme_minimal(base_size = 13) +
theme(
plot.title = element_text(face = "bold", hjust = 0.5),
axis.text.x = element_text(angle = 30, hjust = 1)
)
Đếm tuần suất và tỷ lệ phần trăm từng giá trị
table(data$Payment_Method)
##
## Cash Credit Card Debit Card Nothing PayPal
## 73807 90115 76790 297 60997
prop.table(table(data$Payment_Method))
##
## Cash Credit Card Debit Card Nothing PayPal
## 0.2443891843 0.2983881115 0.2542664715 0.0009834242 0.2019728085
Biểu đồ cột thể hiện sự phân bố của biến Payment_Method
barplot(table(data$Payment_Method))
summary(data$Amount)
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 10.0 133.0 255.2 255.2 377.5 500.0
sd(data$Amount)
## [1] 141.3068
Theo kết quả ta nhận được:
Khoảng giá trị: từ 10 đến 500
Trung vị = 255.2, trung bình = 255.2 gần như bằng nhau dữ liệu gần như không bị lệch
1st Qu. = 133.0, 3rd Qu. = 377.5 → 50% giá trị nằm trong khoảng 133 – 377.
Độ lệch chuẩn = 141.31 → dữ liệu có độ biến thiên khá lớn
Phân bố dữ liệu
hist(data$Amount, main="Histogram of Amount", xlab="Amount", col="skyblue", breaks=30)
plot(density(data$Amount), main="Density Plot of Amount")
Đường mật độ có dạng phẳng và gần như hình chữ nhật
Mật độ phân bố đều trên khoảng từ 10 đến 500 chỉ giảm mạnh ở hai đầu (0 và 500)
Không có đỉnh nhọn hay vùng tập trung rõ ràng ở bất kỳ giá trị nào
Kết luận - Biểu đồ mật độ cho thấy dữ liệu Amuont phân bố gần như đồng đều
Phát hiện giá trị bất thường (outliers)
boxplot(data$Amount, main="Boxplot of Amount", ylab="Amount")
Ngoài ra tôi phân tích biến Amount theo phân khúc khách hàng và thu nhập
amount_customer <- data %>%
mutate(
Amount_Group = cut(Amount,
breaks = c(0, 133, 255, 377, Inf),
labels = c("Low (≤133)", "Medium (134-255)", "High (256-377)", "Very High (>377)"),
right = TRUE,
include.lowest = TRUE
)
)
Amount_Group_customer <- amount_customer %>%
group_by(Customer_Segment, Income, Amount_Group) %>%
summarise(
Count = n()
) %>%
arrange(desc(Count))
## `summarise()` has grouped output by 'Customer_Segment', 'Income'. You can
## override using the `.groups` argument.
Amount_Group_customer
## # A tibble: 61 × 4
## # Groups: Customer_Segment, Income [16]
## Customer_Segment Income Amount_Group Count
## <chr> <chr> <fct> <int>
## 1 Regular Medium Very High (>377) 15811
## 2 Regular Medium Low (≤133) 15738
## 3 Regular Medium High (256-377) 15620
## 4 Regular Medium Medium (134-255) 15552
## 5 Regular High High (256-377) 11515
## 6 Regular High Very High (>377) 11503
## 7 Regular High Low (≤133) 11412
## 8 Regular High Medium (134-255) 11355
## 9 New Medium Low (≤133) 10447
## 10 New Medium Very High (>377) 10425
## # ℹ 51 more rows
Nhận xét:Kết quả cho thấy khách hàng Regular có thu nhập trung bình (Medium) chiếm tỷ lệ cao nhất và có mức chi tiêu đa dạng, đặc biệt nhiều ở nhóm chi tiêu rất cao (>377).Khách hàng New chủ yếu có mức chi tiêu thấp đến trung bình, cho thấy đây là nhóm mới, còn thận trọng trong chi tiêu.
amount_summary <- data %>%
group_by(Customer_Segment, Income) %>%
summarise(
Mean_Amount = mean(Amount, na.rm = TRUE),
Median_Amount = median(Amount, na.rm = TRUE),
Min_Amount = min(Amount, na.rm = TRUE),
Max_Amount = max(Amount, na.rm = TRUE),
Count = n()
) %>%
arrange(desc(Count))
## `summarise()` has grouped output by 'Customer_Segment'. You can override using
## the `.groups` argument.
amount_summary
## # A tibble: 16 × 7
## # Groups: Customer_Segment [4]
## Customer_Segment Income Mean_Amount Median_Amount Min_Amount Max_Amount Count
## <chr> <chr> <dbl> <dbl> <dbl> <dbl> <int>
## 1 Regular Medium 255. 255. 10.0 500. 62721
## 2 Regular High 255. 256. 10.0 500. 45785
## 3 New Medium 255. 255. 10.0 500. 41450
## 4 Regular Low 256. 256. 10.0 500. 37599
## 5 New Low 255. 255. 10.0 500. 33023
## 6 Premium Medium 254. 255. 10.0 500. 25977
## 7 Premium Low 256. 257. 10.0 500. 25539
## 8 New High 253. 253. 10.0 500. 16620
## 9 Premium High 256. 255. 10.1 500. 12788
## 10 Regular Nothi… 254. 255. 10.2 499. 114
## 11 Nothing Low 276. 278. 19.4 499. 96
## 12 New Nothi… 247. 233. 14.4 499. 92
## 13 Premium Nothi… 247. 234. 12.7 488. 83
## 14 Nothing Medium 293. 309. 15.7 498. 82
## 15 Nothing High 303. 341. 22.4 486. 36
## 16 Nothing Nothi… 440. 440. 440. 440. 1
Nhận xét:
Kết quả cho thấy mức chi tiêu trung bình của khách hàng khá đồng đều giữa các phân khúc, khoảng 255–256.
Nhóm Regular có thu nhập Medium chiếm số lượng lớn nhất và là nhóm đóng góp chính cho doanh thu.
Các nhóm Premium và New có mức chi tiêu tương đương nhưng số lượng ít hơn.
Nhìn chung, phân khúc Regular là nhóm ổn định và mang lại giá trị cao nhất cho doanh nghiệp.
Trước khi phân tích, tôi chia nhóm tuổi ra thành 4 nhóm và đánh giá 4 nhóm tuổi theo phân khúc khách hàng và thu nhập.
age_customer <- data%>%
mutate(
Age_Group = cut(Age,
breaks = c(18, 25, 40, 60, Inf),
labels = c("18-25", "26-40", "41-60", "60+"),
right = TRUE,
include.lowest = TRUE
)
)
Age_Group_customer <- age_customer %>%
group_by(Customer_Segment, Income, Age_Group) %>%
summarise(
Count = n()
) %>%
arrange(desc(Count))
## `summarise()` has grouped output by 'Customer_Segment', 'Income'. You can
## override using the `.groups` argument.
Age_Group_customer
## # A tibble: 61 × 4
## # Groups: Customer_Segment, Income [16]
## Customer_Segment Income Age_Group Count
## <chr> <chr> <fct> <int>
## 1 Regular Medium 18-25 33556
## 2 Regular High 18-25 22340
## 3 New Medium 18-25 18041
## 4 Regular High 26-40 15574
## 5 Regular Medium 41-60 13067
## 6 Regular Medium 26-40 11814
## 7 Regular Low 41-60 11778
## 8 New Medium 41-60 11209
## 9 Regular Low 18-25 11173
## 10 New Low 18-25 11071
## # ℹ 51 more rows
age_summary <- data %>%
group_by(Customer_Segment, Income) %>%
summarise(
Mean_Age = mean(Age, na.rm = TRUE),
Median_Age = median(Age, na.rm = TRUE),
Min_Age = min(Age, na.rm = TRUE),
Max_Age = max(Age, na.rm = TRUE),
Count = n()
) %>%
arrange(desc(Count))
## `summarise()` has grouped output by 'Customer_Segment'. You can override using
## the `.groups` argument.
age_summary
## # A tibble: 16 × 7
## # Groups: Customer_Segment [4]
## Customer_Segment Income Mean_Age Median_Age Min_Age Max_Age Count
## <chr> <chr> <dbl> <dbl> <dbl> <dbl> <int>
## 1 Regular Medium 31.9 24 18 70 62721
## 2 Regular High 29.5 26 18 70 45785
## 3 New Medium 34.7 31 18 70 41450
## 4 Regular Low 37.3 34 18 70 37599
## 5 New Low 36.9 34 18 70 33023
## 6 Premium Medium 41.6 43 18 70 25977
## 7 Premium Low 41.6 44 18 70 25539
## 8 New High 36.4 34 18 70 16620
## 9 Premium High 41.8 44 18 70 12788
## 10 Regular Nothing 36.7 34 19 68 114
## 11 Nothing Low 41.5 38.5 18 70 96
## 12 New Nothing 38.5 38.5 19 70 92
## 13 Premium Nothing 42.2 46 18 69 83
## 14 Nothing Medium 43.4 46 18 70 82
## 15 Nothing High 39.7 34.5 19 70 36
## 16 Nothing Nothing 46 46 46 46 1
Nhận xét: - Nhóm phân khúc khách hàng Regular thường tập trung ở độ tuổi trung bình 30 đến 32 tuổi và là nhóm có thu nhập từ trung bình (Medium) trở lên. - Nhóm phân khúc khách hàng Premium là nhóm có độ tuổi trung nhiên trong khoảng 41 đến 42 tuổi, là nhóm có thu nhập ổn định (Medium, High) phù hợp với những phân khúc sản phẩm cao cấp. - Nhóm phân khúc khách hàng New cũng có xu hướng là những người trẻ tuổi, độ tuổi trung bình khoảng 35 đến 37 tuổi.