Đọc dữ liệu

data <- read.csv("D:\\Downloads\\Supermarket Transactions.csv", header = T)

Chọn các biến định tính

data <- data[,c(4,5,6,8,9,10,11,12,13,14)]

Phân tích các biến định tính

Gender

Tần số

table(data$Gender)
## 
##    F    M 
## 7170 6889

Tần suất

table(data$Gender)/sum(table(data$Gender))
## 
##         F         M 
## 0.5099936 0.4900064

Biểu đồ cột

library(ggplot2)
library(dplyr)
ggplot(data = data, aes(x = Gender)) +
  geom_bar(fill = "skyblue", color = "black", width = 0.4) +
  theme_minimal(base_size = 14)

ggplot(data =data, aes(x=Gender)) +
  geom_bar(aes(y = (..count..)/sum(..count..)),fill = "skyblue", color = "black", width = 0.4) +
  ylab('%') + xlab('Gender')+
  theme_minimal(base_size = 14)

Biểu đồ tròn

gender_count <- data %>%
  count(Gender) %>%
  mutate(prop = n / sum(n),
         label = paste0(Gender, " (", scales::percent(prop), ")"))

ggplot(gender_count, aes(x = "", y = prop, fill = Gender)) +
  geom_col(width = 1, color = "white") +
  coord_polar(theta = "y") +
  geom_text(aes(label = label), position = position_stack(vjust = 0.5)) +
  theme_minimal(base_size = 14)+
  theme(
    axis.title.x = element_blank(),
    axis.title.y = element_blank(),
    axis.text = element_blank(),
    axis.ticks = element_blank()
  )

Nhận xét: Phân bố giới tính trong tập dữ liệu khá đồng đều, với tỷ lệ nam giới chiếm 50.21% và nữ giới chiếm 49.79%. Sự cân bằng này cho thấy không có thiên lệch đáng kể trong cơ cấu khách hàng theo giới tính.

Biến MaritalStatus

Tần số

## 
##    M    S 
## 6866 7193

Tần suất

table(data$MaritalStatus)/sum(table(data$MaritalStatus))
## 
##         M         S 
## 0.4883704 0.5116296

Biểu đồ cột

library(ggplot2)
ggplot(data = data, aes(x = MaritalStatus)) +
  geom_bar(fill = "lightgreen", color = "black", width = 0.4) +
  theme_minimal(base_size = 14)

ggplot(data =data, aes(x=MaritalStatus)) +
  geom_bar(aes(y = (..count..)/sum(..count..)),fill = "lightgreen", color = "black", width = 0.4) +
  ylab('%') + xlab('MaritalStatus')+
  theme_minimal(base_size = 14)

Biểu đồ tròn

marital_count <- data %>%
  count(MaritalStatus) %>%
  mutate(prop = n / sum(n),
         label = paste0(MaritalStatus, " (", scales::percent(prop), ")"))

ggplot(marital_count, aes(x = "", y = prop, fill = MaritalStatus)) +
  geom_col(width = 1, color = "white") +
  coord_polar(theta = "y") +
  geom_text(aes(label = label), position = position_stack(vjust = 0.5)) +
  theme_minimal(base_size = 14)+
  theme(
    axis.title.x = element_blank(),
    axis.title.y = element_blank(),
    axis.text = element_blank(),
    axis.ticks = element_blank()
  )

Nhận xét: Khách hàng độc thân chiếm tỷ lệ nhỉnh hơn một chút (51.16%) so với nhóm đã kết hôn (48.84%).

Biến Homeowner

Tần số

## 
##    N    Y 
## 5615 8444

Tần suất

table(data$Homeowner)/sum(table(data$Homeowner))
## 
##         N         Y 
## 0.3993883 0.6006117

Biểu đồ cột

library(ggplot2)
ggplot(data = data, aes(x = Homeowner)) +
  geom_bar(fill = "lightblue", color = "black", width = 0.4) +
  theme_minimal(base_size = 14)

ggplot(data =data, aes(x=Homeowner)) +
  geom_bar(aes(y = (..count..)/sum(..count..)),fill = "lightgreen", color = "black", width = 0.4) +
  ylab('%') + xlab('Homeowner')+
  theme_minimal(base_size = 14)

homeowner_count <- data %>%
  count(Homeowner) %>%
  mutate(prop = n / sum(n),
         label = paste0(Homeowner, " (", scales::percent(prop), ")"))

ggplot(homeowner_count, aes(x = "", y = prop, fill = Homeowner)) +
  geom_col(width = 1, color = "white") +
  coord_polar(theta = "y") +
  geom_text(aes(label = label), position = position_stack(vjust = 0.5)) +
  theme_minimal(base_size = 14)+
  theme(
    axis.title.x = element_blank(),
    axis.title.y = element_blank(),
    axis.text = element_blank(),
    axis.ticks = element_blank()
  )

Nhận xét: Phần lớn khách hàng (60%) là chủ sở hữu nhà, chỉ có khoảng 40% không sở hữu nhà. Điều này phản ánh rằng đa số người tiêu dùng trong tập dữ liệu có mức độ ổn định tài chính tương đối cao.

Biến AnnualIncome

Tần số

table(data$AnnualIncome)
## 
##   $10K - $30K $110K - $130K $130K - $150K       $150K +   $30K - $50K 
##          3090           643           760           273          4601 
##   $50K - $70K   $70K - $90K  $90K - $110K 
##          2370          1709           613

Tần suất

table(data$AnnualIncome) / sum(table(data$AnnualIncome))
## 
##   $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

Biểu đồ cột

ggplot(data = data, aes(x = AnnualIncome)) +
  geom_bar(fill = "yellow", color = "black", width = 0.4) +
  theme_minimal(base_size = 10)

ggplot(data = data, aes(x = AnnualIncome)) +
  geom_bar(aes(y = (..count..) / sum(..count..)), 
           fill = "yellow", color = "black", width = 0.4) +
  ylab('%') + xlab('AnnualIncome') +
  theme_minimal(base_size = 10)

Nhận xét: nhóm khách hàng có thu nhập $30K – $50K chiếm tỷ trọng lớn nhất, đạt khoảng 32.73%, nhóm tiếp theo là $10K – $30K (21.98%) và $50K – $70K (16.86%). Trong khi đó, các nhóm thu nhập cao như $110K – $130K, $130K – $150K và $150K+ chỉ chiếm lần lượt 4.57%, 5.41% và 1.94%, cho thấy đối tượng khách hàng giàu có chiếm tỷ lệ nhỏ trong tập dữ liệu. Nhóm $90K – $110K cũng chỉ chiếm 4.36%, thấp hơn đáng kể so với các nhóm thu nhập thấp hơn.

Biến City

Tần số

table(data$City)
## 
##      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

Tần suất

table(data$City) / sum(table(data$City))
## 
##      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

Nhận xét: các giá trị này dao động từ mức thấp nhất khoảng 0.0053 (Camacho) đến mức cao nhất gần 0.0986 (Salem), cho thấy sự khác biệt khá lớn về mức độ xuất hiện của từng khu vực. Một số thành phố lớn như Los Angeles, Seattle, San Diego và Portland có tỉ lệ khá cao, đều trên 6%, phản ánh sự tập trung dữ liệu chủ yếu vào những vùng đô thị lớn. Điều này có thể minh chứng cho vai trò quan trọng hoặc sự phổ biến cao của các địa phương này trong tổng thể dữ liệu. Ngược lại, một số địa phương như Guadalajara, San Francisco, và Victoria có tỉ lệ thấp hơn nhiều, chỉ khoảng dưới 1.5%, cho thấy sự phân bố dữ liệu tại các vùng này tương đối hạn chế hoặc ít được đại diện trong mẫu.các giá trị này dao động từ mức thấp nhất khoảng 0.0053 (Camacho) đến mức cao nhất gần 0.0986 (Salem), cho thấy sự khác biệt khá lớn về mức độ xuất hiện của từng khu vực. Một số thành phố lớn như Los Angeles, Seattle, San Diego và Portland có tỉ lệ khá cao, đều trên 6%.

Biến StateorProvince

Tần số

table(data$StateorProvince)
## 
##        BC        CA        DF  Guerrero   Jalisco        OR  Veracruz        WA 
##       809      2733       815       383        75      2262       464      4567 
##   Yucatan Zacatecas 
##       654      1297

Tần suất

table(data$StateorProvince) / sum(table(data$StateorProvince))
## 
##          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

Biểu đồ cột

ggplot(data = data, aes(x = StateorProvince)) +
  geom_bar(fill = "lightgray", color = "black", width = 0.4) +
  theme_minimal(base_size = 14)

ggplot(data = data, aes(x = StateorProvince)) +
  geom_bar(aes(y = (..count..) / sum(..count..)), 
           fill = "lightgray", color = "black", width = 0.4) +
  ylab('%') + xlab('StateorProvince') +
  theme_minimal(base_size = 14)

Biểu đồ tròn

library(ggrepel)
state_count <- data %>%
  count(StateorProvince) %>%
  mutate(prop = n / sum(n),
         label = paste0(percent(prop)))

ggplot(state_count, aes(x = "", y = prop, fill = StateorProvince)) +
  geom_col(width = 1, color = "white") +
  coord_polar(theta = "y") +
  geom_text_repel(
    aes(label = label),
    position = position_stack(vjust = 0.5),
    size = 2.3,
    show.legend = FALSE,
    box.padding = 0.2
  ) +
  scale_fill_brewer(palette = "Pastel1") +
  theme_minimal(base_size = 14) +
  theme(legend.position = "right") +
  theme(
    axis.title.x = element_blank(),
    axis.title.y = element_blank(),
    axis.text = element_blank(),
    axis.ticks = element_blank()
  )

ProductFamily

Bảng tần số

table(data$ProductFamily)
## 
##          Drink           Food Non-Consumable 
##           1250          10153           2656

Bảng tần suất

table(data$ProductFamily)/length(data$ProductFamily)
## 
##          Drink           Food Non-Consumable 
##     0.08891102     0.72217085     0.18891813

Biểu đồ cột

library(ggplot2)
ggplot(data = data, aes(x = ProductFamily)) +
  geom_bar(fill = "lightyellow", color = "black", width = 0.4) +
  theme_minimal(base_size = 14)

ggplot(data = data, aes(x = ProductFamily)) +
  geom_bar(aes(y = (..count..) / sum(..count..)), 
           fill = "lightyellow", color = "black", width = 0.4) +
  ylab('%') + xlab('ProductFamilly') +
  theme_minimal(base_size = 14)

Biểu đồ tròn

data$ProductFamily <- factor(data$ProductFamily)

product_count <- data %>%
  count(ProductFamily) %>%
  mutate(prop = n / sum(n),
         label = paste0(scales::percent(prop)))

ggplot(product_count, aes(x = "", y = prop, fill = ProductFamily)) +
  geom_col(width = 1, color = "white") +
  coord_polar(theta = "y") +
  geom_text(aes(label = label), position = position_stack(vjust = 0.5), size = 4) +
  scale_fill_brewer(palette = "Pastel1") +
  theme_minimal(base_size = 14) +
  theme(legend.position = "right") +
  theme(
    axis.title.x = element_blank(),
    axis.title.y = element_blank(),
    axis.text = element_blank(),
    axis.ticks = element_blank()
  )

Nhận xét: phần lớn sản phẩm được tiêu thụ thuộc nhóm Food (thực phẩm), chiếm tới 72% tổng số giao dịch. Nhóm này bao gồm các mặt hàng thiết yếu hàng ngày như rau củ, thịt, sữa và thực phẩm đóng gói – phản ánh rõ xu hướng tiêu dùng tập trung vào nhu cầu cơ bản. Nhóm Non-Consumable (hàng không tiêu dùng) chiếm 19%, cho thấy vẫn có một phần đáng kể khách hàng quan tâm đến các sản phẩm như đồ dùng gia đình, vệ sinh cá nhân, v.v. Trong khi đó, Drink (đồ uống) chỉ chiếm 9%, là tỷ lệ thấp nhất trong ba nhóm.

Biến ProductDepartment

Tần số

table(data$ProductDepartment)
## 
## 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

Tần suất

table(data$ProductDepartment) / sum(table(data$ProductDepartment))
## 
## 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

Biến ProductCategory

Tần số

table(data$ProductCategory)
## 
##         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

Tần suất

table(data$ProductCategory) / sum(table(data$ProductCategory))
## 
##         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

Nhận xét: nhóm Vegetables (rau củ) chiếm tỷ lệ cao nhất với 12.29%, theo sau là Snack Foods (đồ ăn vặt) với 11.38%, và Dairy (sản phẩm từ sữa) ở mức 6.42%. Đây đều là các mặt hàng thiết yếu và có tần suất tiêu dùng cao, thường xuất hiện trong giỏ hàng mua sắm hàng ngày. Một số nhóm khác cũng có tỷ trọng đáng chú ý như Fruit (trái cây) chiếm 5.44%, Meat (thịt) chiếm 5.41%, và Jams and Jellies (mứt) chiếm 4.18%, cho thấy khách hàng có xu hướng tiêu dùng thực phẩm tươi sống hoặc chế biến sẵn nhiều hơn các mặt hàng khác. Trong khi đó, các sản phẩm như Canned Seafood (cá/hải sản đóng hộp), Candles, Miscellaneous, hay các sản phẩm rất đặc thù như Canned Oysters hoặc Canned Sardines đều có tỷ lệ dưới 1%, phản ánh rằng chúng chỉ phục vụ cho một số nhóm khách hàng nhỏ hoặc nhu cầu không thường xuyên.

`