Phân tích giỏ hàng trong R
Christopher Bruffaerts
Statistician
Giao dịch: Hoạt động mua hoặc bán.

Dữ liệu giao dịch: Danh sách tất cả mặt hàng một khách hàng mua trong một lần mua.
Ví dụ một giao dịch:
TID Product
1 1 Bread
2 1 Cheese
3 1 Cheese
4 1 Cheese
Lớp Transactions: đại diện dữ liệu giao dịch dùng để khai phá tập mục hoặc luật.
Ép kiểu từ:
Tuy nhiên, cần chuẩn bị dữ liệu trước.
Lưu ý khi làm việc với dữ liệu giao dịch
Trường/cột nhận diện sản phẩm
Trường/cột nhận diện giao dịch
Dữ liệu giao dịch từ cửa hàng
my_transactions = data.frame(
"TID" = c(1,1,1,1, 2,2,2, 3,3, 4,4,4, 5,5, 6,6, 7,7),
"Product" = c("Bread", "Cheese", "Cheese", "Cheese",
"Bread", "Butter", "Wine",
"Butter", "Butter",
"Butter", "Wine", "Wine",
"Butter", "Cheese",
"Cheese", "Wine",
"Wine", "Wine")
)
Xem nhanh giao dịch
head(my_transactions, 10)
TID Product
1 1 Bread
2 1 Butter
3 1 Cheese
4 1 Wine
5 2 Bread
6 2 Butter
7 2 Wine
8 3 Bread
9 3 Butter
10 4 Butter
Tạo danh sách với hàm split
# Chuyển TID thành factor
my_transactions$TID =
factor(my_transactions$TID)
# Chia theo nhóm
data_list = split(my_transactions$Product,
my_transactions$TID)
data_list
$`1`
[1] Bread Butter Cheese Wine
Levels: Bread Butter Cheese Wine
$`2`
[1] Bread Butter Wine
Levels: Bread Butter Cheese Wine
$`3`
[1] Bread Butter
Levels: Bread Butter Cheese Wine
Chuyển sang lớp transactions
# Chuyển thành bộ dữ liệu giao dịch
data_trx = as(data_list,"transactions")
# Kiểm tra giao dịch
inspect(data_trx)
Xem dữ liệu giao dịch
items transactionID
[1] {Bread,Butter,Cheese,Wine} 1
[2] {Bread,Butter,Wine} 2
[3] {Bread,Butter} 3
[4] {Butter,Cheese,Wine} 4
[5] {Butter,Cheese} 5
[6] {Cheese,Wine} 6
[7] {Butter,Wine} 7
Tổng quan giao dịch
inspect(head(data_trx))
items transactionID
[1] {Bread,Butter,Cheese,Wine} 1
[2] {Bread,Butter,Wine} 2
[3] {Bread,Butter} 3
[4] {Butter,Cheese,Wine} 4
[5] {Butter,Cheese} 5
[6] {Cheese,Wine} 6
Truy cập giao dịch cụ thể
inspect(data_trx[1])
inspect(data_trx[1:3])
Tóm tắt đối tượng giao dịch
summary(data_trx)
Vẽ ItemMatrix
image(data_trx)
Cảnh báo: chỉ dùng với số lượng giao dịch nhỏ
Hữu ích để nhận diện:
Mật độ = 18/28 = 0,64

Phân tích giỏ hàng trong R