# Đọc bộ dữ liệu
d <- read.csv("D:/naaaaaa/PTDLDT/Supermarket Transactions.csv", header = T)
# Cấu trúc bộ dữ liệu
str(d)
## 'data.frame': 14059 obs. of 16 variables:
## $ X : int 1 2 3 4 5 6 7 8 9 10 ...
## $ PurchaseDate : chr "2007-12-18" "2007-12-20" "2007-12-21" "2007-12-21" ...
## $ CustomerID : int 7223 7841 8374 9619 1900 6696 9673 354 1293 7938 ...
## $ Gender : chr "F" "M" "F" "M" ...
## $ MaritalStatus : chr "S" "M" "M" "M" ...
## $ Homeowner : chr "Y" "Y" "N" "Y" ...
## $ Children : int 2 5 2 3 3 3 2 2 3 1 ...
## $ AnnualIncome : chr "$30K - $50K" "$70K - $90K" "$50K - $70K" "$30K - $50K" ...
## $ City : chr "Los Angeles" "Los Angeles" "Bremerton" "Portland" ...
## $ StateorProvince : chr "CA" "CA" "WA" "OR" ...
## $ Country : chr "USA" "USA" "USA" "USA" ...
## $ ProductFamily : chr "Food" "Food" "Food" "Food" ...
## $ ProductDepartment: chr "Snack Foods" "Produce" "Snack Foods" "Snacks" ...
## $ ProductCategory : chr "Snack Foods" "Vegetables" "Snack Foods" "Candy" ...
## $ UnitsSold : int 5 5 3 4 4 3 4 6 1 2 ...
## $ Revenue : num 27.38 14.9 5.52 4.44 14 ...
Tập dữ liệu “Supermarket Transactions” gồm tổng cộng 14.059 dòng dữ liệu, tương ứng với 14.059 giao dịch mua hàng được ghi nhận tại siêu thị. Mỗi dòng dữ liệu đại diện cho một giao dịch riêng lẻ và bao gồm thông tin chi tiết về khách hàng, sản phẩm, cũng như các đặc điểm liên quan đến hành vi tiêu dùng. Bộ dữ liệu này có tổng cộng 16 biến (cột), bao gồm cả biến định tính và định lượng, phản ánh đầy đủ các yếu tố nhân khẩu học, địa lý và kết quả kinh doanh từ mỗi giao dịch.
# Hiện thị các dòng đầu của bộ dữ liệu
head(d)
## X PurchaseDate CustomerID Gender MaritalStatus Homeowner Children
## 1 1 2007-12-18 7223 F S Y 2
## 2 2 2007-12-20 7841 M M Y 5
## 3 3 2007-12-21 8374 F M N 2
## 4 4 2007-12-21 9619 M M Y 3
## 5 5 2007-12-22 1900 F S Y 3
## 6 6 2007-12-22 6696 F M Y 3
## AnnualIncome City StateorProvince Country ProductFamily
## 1 $30K - $50K Los Angeles CA USA Food
## 2 $70K - $90K Los Angeles CA USA Food
## 3 $50K - $70K Bremerton WA USA Food
## 4 $30K - $50K Portland OR USA Food
## 5 $130K - $150K Beverly Hills CA USA Drink
## 6 $10K - $30K Beverly Hills CA USA Food
## ProductDepartment ProductCategory UnitsSold Revenue
## 1 Snack Foods Snack Foods 5 27.38
## 2 Produce Vegetables 5 14.90
## 3 Snack Foods Snack Foods 3 5.52
## 4 Snacks Candy 4 4.44
## 5 Beverages Carbonated Beverages 4 14.00
## 6 Deli Side Dishes 3 4.37
# Hiển thị các dòng cuối của bộ dữ liệu
tail(d)
## X PurchaseDate CustomerID Gender MaritalStatus Homeowner Children
## 14054 14054 2009-12-29 2032 F M N 3
## 14055 14055 2009-12-29 9102 F M Y 2
## 14056 14056 2009-12-29 4822 F M Y 3
## 14057 14057 2009-12-31 250 M S Y 1
## 14058 14058 2009-12-31 6153 F S N 4
## 14059 14059 2009-12-31 3656 M S N 3
## AnnualIncome City StateorProvince Country ProductFamily
## 14054 $10K - $30K Yakima WA USA Non-Consumable
## 14055 $10K - $30K Bremerton WA USA Food
## 14056 $10K - $30K Walla Walla WA USA Food
## 14057 $30K - $50K Portland OR USA Drink
## 14058 $50K - $70K Spokane WA USA Drink
## 14059 $50K - $70K Portland OR USA Non-Consumable
## ProductDepartment ProductCategory UnitsSold Revenue
## 14054 Household Paper Products 5 14.50
## 14055 Baking Goods Baking Goods 3 9.64
## 14056 Frozen Foods Vegetables 3 7.45
## 14057 Beverages Pure Juice Beverages 4 3.24
## 14058 Dairy Dairy 2 4.00
## 14059 Household Electrical 5 25.53
# Kiểm tra dữ liệu thiếu
sum(is.na(d))
## [1] 0
d$Gender <- as.factor(d$Gender)
d$MaritalStatus <- as.factor(d$MaritalStatus)
d$Homeowner <- as.factor(d$Homeowner)
d$AnnualIncome <- as.factor(d$AnnualIncome)
# Tạo biến định tính
bdt <- c( "PurchaseDate",
"CustomerID", "Gender", "MaritalStatus", "Homeowner", "AnnualIncome", "City", "StateorProvince","Country", "ProductFamily", "ProductDepartment", "ProductCategory" )
# Tạo bộ dữ liệu mới chỉ có biến định tính
dt <- d[, bdt]
# Xuất bộ dữ liệu định tính
head(dt)
## PurchaseDate CustomerID Gender MaritalStatus Homeowner AnnualIncome
## 1 2007-12-18 7223 F S Y $30K - $50K
## 2 2007-12-20 7841 M M Y $70K - $90K
## 3 2007-12-21 8374 F M N $50K - $70K
## 4 2007-12-21 9619 M M Y $30K - $50K
## 5 2007-12-22 1900 F S Y $130K - $150K
## 6 2007-12-22 6696 F M Y $10K - $30K
## City StateorProvince Country ProductFamily ProductDepartment
## 1 Los Angeles CA USA Food Snack Foods
## 2 Los Angeles CA USA Food Produce
## 3 Bremerton WA USA Food Snack Foods
## 4 Portland OR USA Food Snacks
## 5 Beverly Hills CA USA Drink Beverages
## 6 Beverly Hills CA USA Food Deli
## ProductCategory
## 1 Snack Foods
## 2 Vegetables
## 3 Snack Foods
## 4 Candy
## 5 Carbonated Beverages
## 6 Side Dishes
# Lập bảng tần số
gioitinh <- table(dt$Gender)
gioitinh
##
## F M
## 7170 6889
# Lập bảng tần suất
table(dt$Gender)/sum(nrow(dt))
##
## F M
## 0.5099936 0.4900064
library(ggplot2)
# Đưa về dạng data frame
df_gioitinh <- as.data.frame(gioitinh)
colnames(df_gioitinh) <- c("Gender", "Count")
# Tính phần trăm và thêm vào data frame
df_gioitinh$Percent <- round(df_gioitinh$Count / sum(df_gioitinh$Count) * 100, 1)
# Tạo nhãn ghép với %
df_gioitinh$Label <- paste0(df_gioitinh$Percent, "%")
# Vẽ biểu đồ tròn
ggplot(df_gioitinh, aes(x = "", y = Count, fill = Gender)) +
geom_bar(stat = "identity", width = 1) +
coord_polar(theta = "y") +
geom_text(aes(label = Label), position = position_stack(vjust = 0.5)) +
labs(title = "Biểu đồ 1: Biểu đồ phân bố giới tính") + # 👈 Tiêu đề
theme_void() + # bỏ đi các viền, mấy cái chú thích x, y đồ á
theme(plot.title = element_text(hjust = 0.5, size = 14, face = "bold")) # 👈 Cho hiện và chỉnh tiêu đề
Vậy trong bộ dữ liệu này có 7170 khách hàng là nữ chiếm tỉ lệ 51% và 6889 khách hàng là nam chiếm tỉ lệ 49% .
# Lập bảng tần số
honnhan <- table(dt$MaritalStatus)
honnhan
##
## M S
## 6866 7193
# Lập bảng tần suất
table(dt$MaritalStatus)/sum(nrow(dt))
##
## M S
## 0.4883704 0.5116296
library(ggplot2)
df_honnhan <- as.data.frame(honnhan)
colnames(df_honnhan) <- c("MaritalStatus", "Count")
df_honnhan$Percent <- round(df_honnhan$Count / sum(df_honnhan$Count) * 100, 1)
df_honnhan$Label <- paste0(df_honnhan$Percent, "%")
ggplot(df_honnhan, aes(x = "", y = Count, fill = MaritalStatus)) +
geom_bar(stat = "identity", width = 1) +
coord_polar(theta = "y") +
geom_text(aes(label = Label), position = position_stack(vjust = 0.5)) +
labs(title = "Biểu đồ 2: Biểu đồ phân bố tình trạng hôn nhân") +
theme_void() +
scale_fill_manual(
values = c("M" = "red", "S" = "green"),
labels = c("M" = "Đã kết hôn", "S" = "Độc thân")
) +
theme (plot.title = element_text(hjust = 0.5, size = 14, face = "bold"))
Vậy trong bộ dữ liệu này có 6866 khách hàng đã kết hôn chiếm tỉ lệ 48.8% và 7193 khách hàng độc thân chiếm tỉ lệ 51.2%.
# Lập bảng tần số
nha <- table(dt$Homeowner)
nha
##
## N Y
## 5615 8444
# Lập bảng tần suất
table(dt$Homeowner)/sum(nrow(dt))
##
## N Y
## 0.3993883 0.6006117
# Đưa về dạng data.frame
df_nha <- as.data.frame(nha)
colnames(df_nha) <- c("Homeowner", "Count")
df_nha$Percent <- round(df_nha$Count / sum(df_nha$Count) * 100, 1)
df_nha$Label <- paste0(df_nha$Percent, "%")
ggplot(df_nha, aes(x = "", y = Count, fill = Homeowner)) +
geom_bar(stat = "identity", width = 1) +
coord_polar(theta = "y") +
geom_text(aes(label = Label), position = position_stack(vjust = 0.5)) +
labs(title = "Biểu đồ 3: Biểu đồ thể hiện tình trạng nhà ở") +
theme_void() +
scale_fill_manual(
values = c("Y" = "steelblue", "N" = "tomato"),
labels = c("Y" = "Đã có nhà", "N" = "Chưa có nhà")
) +
theme(plot.title = element_text(hjust = 0.5, size = 14, face = "bold"))
Vậy trong bộ dữ liệu này có 5615 khách hàng chưa có nhà chiếm tỉ lệ 39.9% và 8444 khách hàng đã có nhà chiếm tỉ lệ 60.1%.
# Lập bảng tần số
thunhap <- table(dt$AnnualIncome)
thunhap
##
## $10K - $30K $110K - $130K $130K - $150K $150K + $30K - $50K
## 3090 643 760 273 4601
## $50K - $70K $70K - $90K $90K - $110K
## 2370 1709 613
# Lập bảng tần suất
table(dt$AnnualIncome)/sum(nrow(dt))
##
## $10K - $30K $110K - $130K $130K - $150K $150K + $30K - $50K
## 0.21978804 0.04573583 0.05405790 0.01941817 0.32726367
## $50K - $70K $70K - $90K $90K - $110K
## 0.16857529 0.12155914 0.04360196
library(ggplot2)
# Chuyển sang dạng data frame
df_thunhap <- as.data.frame(thunhap)
colnames(df_thunhap) <- c("AnnualIncome", "Count")
df_thunhap$Percent <- round(df_thunhap$Count / sum(df_thunhap$Count) * 100, 1)
df_thunhap$Label <- paste0(df_thunhap$Percent, "%")
# Vẽ biểu đồ cột
ggplot(df_thunhap, aes(x = AnnualIncome, y = Count, fill = AnnualIncome)) +
geom_col(width = 0.6) +
geom_text(aes(label = Count), vjust = -0.5) +
labs(title = "Biểu đồ 4: Biểu đồ phân bố mức thu nhập",
x = "Khoảng thu nhập theo nhóm",
y = "Số người") +
theme_minimal() +
theme(plot.title = element_text(hjust = 0.5, face = "bold", size = 14),
legend.position = "none") # Ẩn chú thích màu vì đã hiện trên trục x
Vậy trong bộ dữ liệu này có:
# Lập bảng tần số
thanhpho <- table(dt$City)
thanhpho
##
## Acapulco Bellingham Beverly Hills Bremerton Camacho
## 383 143 811 834 452
## Guadalajara Hidalgo Los Angeles Merida Mexico City
## 75 845 926 654 194
## Orizaba Portland Salem San Andres San Diego
## 464 876 1386 621 866
## San Francisco Seattle Spokane Tacoma Vancouver
## 130 922 875 1257 633
## Victoria Walla Walla Yakima
## 176 160 376
# Lập bảng tần suất
table(dt$City)/sum(nrow(dt))
##
## Acapulco Bellingham Beverly Hills Bremerton Camacho
## 0.027242336 0.010171420 0.057685468 0.059321431 0.032150224
## Guadalajara Hidalgo Los Angeles Merida Mexico City
## 0.005334661 0.060103848 0.065865282 0.046518245 0.013798990
## Orizaba Portland Salem San Andres San Diego
## 0.033003770 0.062308841 0.098584537 0.044170994 0.061597553
## San Francisco Seattle Spokane Tacoma Vancouver
## 0.009246746 0.065580767 0.062237712 0.089408920 0.045024539
## Victoria Walla Walla Yakima
## 0.012518671 0.011380610 0.026744434
library(ggplot2)
# Chuyển sang dạng data frame
df_thanhpho <- as.data.frame(thanhpho)
colnames(df_thanhpho) <- c("City", "Count")
df_thanhpho$Percent <- round(df_thanhpho$Count / sum(df_thanhpho$Count) * 100, 1)
df_thanhpho$Label <- paste0(df_thanhpho$Percent, "%")
# Vẽ biểu đồ cột
ggplot(df_thanhpho, aes(x = City, y = Count, fill = City)) +
geom_col(width = 0.8) +
geom_text(aes(label = Count), vjust = -0.5) +
labs(title = "Biểu đồ 5: Biểu đồ phân bố theo thành phố",
x = "Thành phố",
y = "Số người",
fill = "City") +
theme_minimal() +
theme(
plot.title = element_text(hjust = 0.5, face = "bold", size = 14),
axis.text.x = element_blank(), # Ẩn chữ dưới trục X
legend.position = "bottom")
Trong bộ dữ liệu này có tổng cộng 23 thành phố. Trong đó:
Thành phố có số lượng khách hàng nhiều nhất là Salem với 1386 người chiếm tỉ lệ 9.9%,
Tiếp theo là Tacoma với 8.9% , Los Angeles và Seattle với tỉ lệ lần lượt là 6.6% và 6.6%.
Các thành phố còn lại chiếm tỷ lệ nhỏ hơn, dao động từ r df_thanhpho\(Label[4] đến r df_thanhpho\)Label[10].
# Lập bảng tần số
tinh <- table(dt$StateorProvince)
tinh
##
## BC CA DF Guerrero Jalisco OR Veracruz WA
## 809 2733 815 383 75 2262 464 4567
## Yucatan Zacatecas
## 654 1297
# Lập bảng tần suất
table(dt$StateorProvince)/sum(nrow(dt))
##
## BC CA DF Guerrero Jalisco OR
## 0.057543211 0.194395049 0.057969984 0.027242336 0.005334661 0.160893378
## Veracruz WA Yucatan Zacatecas
## 0.033003770 0.324845295 0.046518245 0.092254072
library(ggplot2)
# Chuyển sang dạng data frame
df_tinh <- as.data.frame(tinh)
colnames(df_tinh) <- c("StateorProvince", "Count")
df_tinh$Percent <- round(df_tinh$Count / sum(df_tinh$Count) * 100, 1)
df_tinh$Label <- paste0(df_tinh$Percent, "%")
# Vẽ biểu đồ cột
ggplot(df_tinh, aes(x = StateorProvince, y = Count, fill = StateorProvince)) +
geom_col(width = 0.8) +
geom_text(aes(label = Count), vjust = -0.5) +
labs(title = "Biểu đồ 6: Biểu đồ phân bố theo tỉnh",
x = "Tỉnh",
y = "Số người",
fill = "StateorProvince") +
theme_minimal() +
theme(
plot.title = element_text(hjust = 0.5, face = "bold", size = 14),
axis.text.x = element_blank()) # Ẩn chữ dưới trục X
Trong bộ dữ liệu này có tổng cộng 10 tỉnh. Trong đó:
Tỉnh có số lượng khách hàng nhiều nhất là WA với 4567 người chiếm tỉ lệ 32.5%,
Tiếp theo là CA, OR và Zacatecas với tỉ lệ lần lượt là 19.4%, 16.1%, 9.2%.
Các tỉnh còn lại chiếm tỷ lệ nhỏ hơn, dao động từ 0.5% đến 5.8%.
# Lập bảng tần số
quocgia <- table(dt$Country)
quocgia
##
## Canada Mexico USA
## 809 3688 9562
# Lập bảng tần suất
table(dt$Country)/sum(nrow(dt))
##
## Canada Mexico USA
## 0.05754321 0.26232307 0.68013372
library(ggplot2)
# Bước 1: Đưa về data.frame
df_quocgia <- as.data.frame(quocgia)
colnames(df_quocgia) <- c("Country", "Count")
# Bước 2: Tính phần trăm và nhãn
df_quocgia$Percent <- round(df_quocgia$Count / sum(df_quocgia$Count) * 100, 1)
df_quocgia$Label <- paste0(df_quocgia$Percent, "%")
# Bước 3: Vẽ biểu đồ tròn
ggplot(df_quocgia, aes(x = "", y = Count, fill = Country)) +
geom_bar(stat = "identity", width = 1) +
coord_polar(theta = "y") +
geom_text(aes(label = Label), position = position_stack(vjust = 0.5)) +
labs(title = "Biểu đồ 7: Biểu đồ thể hiện nơi cư trú theo quốc gia") +
theme_void() +
scale_fill_manual(
values = c("beige","darkgreen","darkred")
) +
theme(plot.title = element_text(hjust = 0.5, size = 14, face = "bold"))
Vậy trong bộ dữ liệu này có
# Lập bảng tần số
family <- table(dt$ProductFamily)
family
##
## Drink Food Non-Consumable
## 1250 10153 2656
# Lập bảng tần suất
table(dt$ProductFamily)/sum(nrow(dt))
##
## Drink Food Non-Consumable
## 0.08891102 0.72217085 0.18891813
# Tạo bảng và đưa về dạng data.frame
df_family <- as.data.frame(family)
colnames(df_family) <- c("ProductFamily", "Count")
# Tính phần trăm và nhãn
df_family$Percent <- round(df_family$Count / sum(df_family$Count) * 100, 1)
df_family$Label <- paste0(df_family$Percent, "%")
# Vẽ biểu đồ tròn
ggplot(df_family, aes(x = "", y = Count, fill = ProductFamily)) +
geom_bar(stat = "identity", width = 1) +
coord_polar(theta = "y") +
geom_text(aes(label = Label), position = position_stack(vjust = 0.5)) +
labs(title = "Biểu đồ 8: Phân bố nhóm sản phẩm") +
theme_void() +
theme(plot.title = element_text(hjust = 0.5, size = 14, face = "bold"))
Vậy trong bộ dữ liệu này có
# Lập bảng tần số
department <- table(dt$ProductDepartment)
department
##
## Alcoholic Beverages Baked Goods Baking Goods Beverages
## 356 425 1072 680
## Breakfast Foods Canned Foods Canned Products Carousel
## 188 977 109 59
## Checkout Dairy Deli Eggs
## 82 903 699 198
## Frozen Foods Health and Hygiene Household Meat
## 1382 893 1420 89
## Periodicals Produce Seafood Snack Foods
## 202 1994 102 1600
## Snacks Starchy Foods
## 352 277
# Lập bảng tần suất
table(dt$ProductDepartment)/sum(nrow(dt))
##
## Alcoholic Beverages Baked Goods Baking Goods Beverages
## 0.025321858 0.030229746 0.076250089 0.048367594
## Breakfast Foods Canned Foods Canned Products Carousel
## 0.013372217 0.069492852 0.007753041 0.004196600
## Checkout Dairy Deli Eggs
## 0.005832563 0.064229319 0.049719041 0.014083505
## Frozen Foods Health and Hygiene Household Meat
## 0.098300021 0.063518031 0.101002916 0.006330464
## Periodicals Produce Seafood Snack Foods
## 0.014368020 0.141830856 0.007255139 0.113806103
## Snacks Starchy Foods
## 0.025037343 0.019702682
library(ggplot2)
# Chuyển sang dạng data frame
df_department <- as.data.frame(department)
colnames(df_department) <- c("ProductDepartment", "Count")
df_department$Percent <- round(df_department$Count / sum(df_department$Count) * 100, 1)
df_department$Label <- paste0(df_department$Percent, "%")
# Vẽ biểu đồ cột
ggplot(df_department, aes(x = ProductDepartment, y = Count, fill = ProductDepartment)) +
geom_col(width = 0.8) +
geom_text(aes(label = Count), vjust = -0.5) +
labs(title = "Biểu đồ 9: Biểu đồ phân bố bộ phận sản phẩm",
x = "Các bộ phận sản phẩm",
y = "Số người",
fill = "ProductDepartment") +
theme_minimal() +
theme(
plot.title = element_text(hjust = 0.5, face = "bold", size = 14),
axis.text.x = element_blank(), # Ẩn nhãn dưới trục X nếu cần
legend.position = "bottom"
)
Trong bộ dữ liệu này có tổng cộng 22 bộ phận sản phẩm. Trong đó:
🔶 Nhóm bộ phận sản phẩm có số lượng khách hàng nhiều nhất gồm:
Produce với 1994 người chiếm tỷ lệ 14.2%,
Snack Foods với 1600 người chiếm tỷ lệ 11.4%.
🟠 Nhóm bộ phận sản phẩm có số lượng khách hàng trung bình gồm:
Household,Frozen Foods, Baking Goods, Canned Foods, Dairy, Health and Hygiene với tỉ lệ giao động từ 6.4% đến 10.1%.
🟢 Nhóm bộ phận sản phẩm có số lượng khách hàng ít nhất gồm:
Các bộ phận sản phẩm còn lại với số khách hàng rất ít giao động ở mức 0.4% đến 4.8%.
# Lập bảng tần số
category <- table(dt$ProductCategory)
category
##
## Baking Goods Bathroom Products Beer and Wine
## 484 365 356
## Bread Breakfast Foods Candles
## 425 417 45
## Candy Canned Anchovies Canned Clams
## 352 44 53
## Canned Oysters Canned Sardines Canned Shrimp
## 35 40 38
## Canned Soup Canned Tuna Carbonated Beverages
## 404 87 154
## Cleaning Supplies Cold Remedies Dairy
## 189 93 903
## Decongestants Drinks Eggs
## 85 135 198
## Electrical Frozen Desserts Frozen Entrees
## 355 323 118
## Fruit Hardware Hot Beverages
## 765 129 226
## Hygiene Jams and Jellies Kitchen Products
## 197 588 217
## Magazines Meat Miscellaneous
## 202 761 42
## Packaged Vegetables Pain Relievers Paper Products
## 48 192 345
## Pizza Plastic Products Pure Juice Beverages
## 194 141 165
## Seafood Side Dishes Snack Foods
## 102 153 1600
## Specialty Starchy Foods Vegetables
## 289 277 1728
# Lập bảng tần suất
table(dt$ProductCategory)/sum(nrow(dt))
##
## Baking Goods Bathroom Products Beer and Wine
## 0.034426346 0.025962017 0.025321858
## Bread Breakfast Foods Candles
## 0.030229746 0.029660716 0.003200797
## Candy Canned Anchovies Canned Clams
## 0.025037343 0.003129668 0.003769827
## Canned Oysters Canned Sardines Canned Shrimp
## 0.002489508 0.002845153 0.002702895
## Canned Soup Canned Tuna Carbonated Beverages
## 0.028736041 0.006188207 0.010953837
## Cleaning Supplies Cold Remedies Dairy
## 0.013443346 0.006614980 0.064229319
## Decongestants Drinks Eggs
## 0.006045949 0.009602390 0.014083505
## Electrical Frozen Desserts Frozen Entrees
## 0.025250729 0.022974607 0.008393200
## Fruit Hardware Hot Beverages
## 0.054413543 0.009175617 0.016075112
## Hygiene Jams and Jellies Kitchen Products
## 0.014012376 0.041823743 0.015434953
## Magazines Meat Miscellaneous
## 0.014368020 0.054129028 0.002987410
## Packaged Vegetables Pain Relievers Paper Products
## 0.003414183 0.013656732 0.024539441
## Pizza Plastic Products Pure Juice Beverages
## 0.013798990 0.010029163 0.011736254
## Seafood Side Dishes Snack Foods
## 0.007255139 0.010882709 0.113806103
## Specialty Starchy Foods Vegetables
## 0.020556227 0.019702682 0.122910591
library(ggplot2)
# Đưa dữ liệu về dạng data frame
df_category <- as.data.frame(category)
colnames(df_category) <- c("ProductCategory", "Count")
# Vẽ biểu đồ với nhiều màu
ggplot(df_category, aes(x = reorder(ProductCategory, Count), y = Count, fill = ProductCategory)) +
geom_col() +
coord_flip() +
labs(title = "Biểu đồ 10: Tần suất giao dịch theo Product Category",
x = "Loại sản phẩm",
y = "Số giao dịch") +
theme_minimal() +
theme(
plot.title = element_text(hjust = 0.5, face = "bold", size = 14),
legend.position = "none" # Ẩn chú thích màu nếu không cần
)
Kiểm định giả thuyết: H0: tỷ lệ Female = 0.5
H1: tỷ lệ Female ≠ 0.5
# Lấy số lượng Female và tổng số quan sát
count_female <- gioitinh["F"]
n <- sum(gioitinh)
# 1. Ước lượng khoảng tin cậy 95% cho tỷ lệ Female
prop.test_female <- prop.test(count_female, n, conf.level = 0.95)
# Kết quả khoảng tin cậy
cat("Khoảng tin cậy 95% cho tỷ lệ Female là: ",
round(prop.test_female$conf.int[1],3), " đến ", round(prop.test_female$conf.int[2],3), "\n")
## Khoảng tin cậy 95% cho tỷ lệ Female là: 0.502 đến 0.518
# 2. Kiểm định giả thuyết H0: p = 0.5
# prop.test cũng trả luôn p-value
cat("Giá trị p của kiểm định là: ", prop.test_female$p.value, "\n")
## Giá trị p của kiểm định là: 0.01820308
if (prop.test_female$p.value < 0.05) {
cat("=> Bác bỏ H0, tỷ lệ Female khác 0.5 với mức ý nghĩa 0.05\n")
} else {
cat("=> Không bác bỏ H0, không có đủ bằng chứng để khẳng định tỷ lệ Female khác 0.5\n")
}
## => Bác bỏ H0, tỷ lệ Female khác 0.5 với mức ý nghĩa 0.05
# Lấy số người có nhà ("Y")
count_yes <- nha["Y"]
n_home <- sum(nha)
# Tính khoảng tin cậy 95%
prop.test_yes <- prop.test(count_yes, n_home, conf.level = 0.95)
# Kết quả khoảng tin cậy
cat("Khoảng tin cậy 95% cho tỷ lệ người có nhà:\n")
## Khoảng tin cậy 95% cho tỷ lệ người có nhà:
cat("Từ", round(prop.test_yes$conf.int[1], 3), "đến", round(prop.test_yes$conf.int[2], 3), "\n")
## Từ 0.592 đến 0.609
cat("Tỷ lệ ước lượng:", round(prop.test_yes$estimate, 3), "\n\n")
## Tỷ lệ ước lượng: 0.601
# Kiểm định H₀: p = 0.5 (tỷ lệ người có nhà bằng 50%)
test_home <- prop.test(count_yes, n_home, p = 0.5, conf.level = 0.95)
# Kết quả kiểm định
cat("Kết quả kiểm định giả thuyết H0: tỷ lệ người có nhà = 0.5\n")
## Kết quả kiểm định giả thuyết H0: tỷ lệ người có nhà = 0.5
cat("Giá trị p-value:", round(test_home$p.value, 4), "\n")
## Giá trị p-value: 0
if (test_home$p.value < 0.05) {
cat("=> Bác bỏ H0: Tỷ lệ người có nhà khác 0.5 (có ý nghĩa thống kê ở mức 5%)\n")
} else {
cat("=> Không bác bỏ H0: Không có đủ bằng chứng để kết luận tỷ lệ người có nhà khác 0.5\n")
}
## => Bác bỏ H0: Tỷ lệ người có nhà khác 0.5 (có ý nghĩa thống kê ở mức 5%)
count_married <- honnhan["M"]
n_marital <- sum(honnhan)
prop.test_married <- prop.test(count_married, n_marital, conf.level = 0.95)
cat("Khoảng tin cậy 95% cho tỷ lệ người đã kết hôn:\n")
## Khoảng tin cậy 95% cho tỷ lệ người đã kết hôn:
cat("Từ", round(prop.test_married$conf.int[1], 3), "đến", round(prop.test_married$conf.int[2], 3), "\n")
## Từ 0.48 đến 0.497
cat("Tỷ lệ ước lượng:", round(prop.test_married$estimate, 3), "\n\n")
## Tỷ lệ ước lượng: 0.488
test_married <- prop.test(count_married, n_marital, p = 0.5, conf.level = 0.95)
cat("Kết quả kiểm định
giả thuyết H0: tỷ lệ người đã kết hôn = 0.5\n",
"giả thuyết H1: tỷ lệ người đã kết hôn ≠ 0.5\n")
## Kết quả kiểm định
## giả thuyết H0: tỷ lệ người đã kết hôn = 0.5
## giả thuyết H1: tỷ lệ người đã kết hôn ≠ 0.5
cat("Giá trị p-value:", round(test_married$p.value, 4), "\n")
## Giá trị p-value: 0.006
if (test_married$p.value < 0.05) {
cat("=> Bác bỏ H0: Tỷ lệ người đã kết hôn khác 0.5 (có ý nghĩa thống kê ở mức 5%)\n")
} else {
cat("=> Không bác bỏ H0: Không có đủ bằng chứng để kết luận tỷ lệ người đã kết hôn khác 0.5\n")
}
## => Bác bỏ H0: Tỷ lệ người đã kết hôn khác 0.5 (có ý nghĩa thống kê ở mức 5%)
# Bảng tần suất chéo giữa Gender và Homeowner
table_gender_home <- table(dt$Gender, dt$Homeowner)
table_gender_home
##
## N Y
## F 2826 4344
## M 2789 4100
# Tỷ lệ phần trăm theo hàng (trong từng giới tính, có nhà chiếm bao nhiêu %)
prop_gender_home_row <- prop.table(table_gender_home, 1) * 100
round(prop_gender_home_row, 2)
##
## N Y
## F 39.41 60.59
## M 40.48 59.52
library(ggplot2)
# Chuyển bảng sang data frame
df_gender_home <- as.data.frame(table_gender_home)
colnames(df_gender_home) <- c("Gender", "Homeowner", "Count")
# Vẽ biểu đồ cột nhóm
ggplot(df_gender_home, aes(x = Gender, y = Count, fill = Homeowner)) +
geom_bar(stat = "identity", position = "dodge") +
labs(title = "Mối quan hệ giữa Gender và Homeowner",
x = "Gender", y = "Số lượng", fill = "Homeowner") +
theme_minimal()
chi_gender_home <- chisq.test(table_gender_home)
chi_gender_home
##
## Pearson's Chi-squared test with Yates' continuity correction
##
## data: table_gender_home
## X-squared = 1.6344, df = 1, p-value = 0.2011
cat("Giá trị Chi-squared:", round(chi_gender_home$statistic, 2), "\n")
## Giá trị Chi-squared: 1.63
cat("Bậc tự do:", chi_gender_home$parameter, "\n")
## Bậc tự do: 1
cat("P-value:", round(chi_gender_home$p.value, 4), "\n")
## P-value: 0.2011
if (chi_gender_home$p.value < 0.05) {
cat("=> Bác bỏ H0: Có mối liên hệ có ý nghĩa thống kê giữa Gender và Homeowner\n")
} else {
cat("=> Không bác bỏ H0: Không có bằng chứng thống kê về mối liên hệ giữa Gender và Homeowner\n")
}
## => Không bác bỏ H0: Không có bằng chứng thống kê về mối liên hệ giữa Gender và Homeowner
# Bảng tần suất chéo giữa MaritalStatus và Homeowner
table_marital_home <- table(dt$MaritalStatus, dt$Homeowner)
table_marital_home
##
## N Y
## M 1719 5147
## S 3896 3297
# Tỷ lệ phần trăm theo hàng
prop_marital_home_row <- prop.table(table_marital_home, 1) * 100
round(prop_marital_home_row, 2)
##
## N Y
## M 25.04 74.96
## S 54.16 45.84
library(ggplot2)
# Chuyển sang data frame
df_marital_home <- as.data.frame(table_marital_home)
colnames(df_marital_home) <- c("MaritalStatus", "Homeowner", "Count")
# Vẽ biểu đồ
ggplot(df_marital_home, aes(x = MaritalStatus, y = Count, fill = Homeowner)) +
geom_bar(stat = "identity", position = "dodge") +
labs(title = "Mối quan hệ giữa MaritalStatus và Homeowner",
x = "Tình trạng hôn nhân", y = "Số lượng", fill = "Có nhà") +
theme_minimal()
chi_marital_home <- chisq.test(table_marital_home)
chi_marital_home
##
## Pearson's Chi-squared test with Yates' continuity correction
##
## data: table_marital_home
## X-squared = 1241.2, df = 1, p-value < 2.2e-16
# Tạo bảng tần suất chéo giữa Gender và MaritalStatus
table_gender_marital <- table(dt$Gender, dt$MaritalStatus)
table_gender_marital
##
## M S
## F 3602 3568
## M 3264 3625
# Theo hàng: trong từng giới tính, tỉ lệ từng tình trạng hôn nhân
prop_gender_marital_row <- prop.table(table_gender_marital, 1) * 100
round(prop_gender_marital_row, 2)
##
## M S
## F 50.24 49.76
## M 47.38 52.62
library(ggplot2)
df_gender_marital <- as.data.frame(table_gender_marital)
colnames(df_gender_marital) <- c("Gender", "MaritalStatus", "Count")
ggplot(df_gender_marital, aes(x = Gender, y = Count, fill = MaritalStatus)) +
geom_bar(stat = "identity", position = "fill") + # hoặc "dodge" nếu muốn số lượng
labs(title = "Tỷ lệ MaritalStatus theo Gender",
x = "Gender", y = "Tỷ lệ", fill = "Tình trạng hôn nhân") +
scale_y_continuous(labels = scales::percent) +
theme_minimal()
chi_gender_marital <- chisq.test(table_gender_marital)
chi_gender_marital
##
## Pearson's Chi-squared test with Yates' continuity correction
##
## data: table_gender_marital
## X-squared = 11.365, df = 1, p-value = 0.0007485
cat("Giá trị Chi-squared:", round(chi_gender_marital$statistic, 2), "\n")
## Giá trị Chi-squared: 11.36
cat("Bậc tự do:", chi_gender_marital$parameter, "\n")
## Bậc tự do: 1
cat("P-value:", round(chi_gender_marital$p.value, 4), "\n")
## P-value: 7e-04
if (chi_gender_marital$p.value < 0.05) {
cat("=> Bác bỏ H₀: Có mối liên hệ có ý nghĩa thống kê giữa Gender và MaritalStatus\n")
} else {
cat("=> Không bác bỏ H₀: Không có bằng chứng thống kê về mối liên hệ giữa Gender và MaritalStatus\n")
}
## => Bác bỏ H₀: Có mối liên hệ có ý nghĩa thống kê giữa Gender và MaritalStatus