R로 배우는 Market Basket Analysis
Christopher Bruffaerts
Statistician
마켓 바스켓 코스

장바구니 = 품목의 모음
품목

장바구니 예시:
식료품점 장바구니
아마존 장바구니
DataCamp 수강 목록
넷플릭스에서 본 영화
가게에는 무엇이 있나요?

오늘은 무엇을 사시나요?

가게에는 무엇이 있나요?
store = c("Bread", "Butter",
"Cheese", "Wine")
set.seed(1234)
n_items = 4
my_basket = data.frame(
TID = rep(1,n_items),
Product = sample(
store, n_items,
replace = TRUE))
R 출력
my_basket
TID Product
1 1 Bread
2 1 Cheese
3 1 Cheese
4 1 Cheese
원본 장바구니
구매한 각 품목당 1개 레코드
TID Product
1 1 Bread
2 1 Cheese
3 1 Cheese
4 1 Cheese
조정된 장바구니
구매한 고유 품목당 1개 레코드
# A tibble: 2 x 3
TID Product Quantity
<dbl> <fct> <int>
1 1 Bread 1
2 1 Cheese 3
장바구니 데이터 재구조화
# 장바구니 조정
my_basket = my_basket %>%
add_count(Product) %>%
unique() %>%
rename(Quantity = n)
# 고유 품목 수
n_distinct(my_basket$Product)
2
# 총 장바구니 수량
my_basket %>% summarize(sum(Quantity))
4
장바구니 품목 시각화
# 품목 그리기
ggplot(my_basket,
aes(x=reorder(Product, Quantity),
y = Quantity)) +
geom_col() +
coord_flip() +
xlab("Items") +
ggtitle("장바구니 품목 요약")

질문: 한 장바구니 내 품목 간에 관계가 있을까요?

예시로 돌아가 봅시다
식료품점 장바구니, e.g. 스파게티와 토마토 소스
아마존 장바구니, e.g. 휴대폰과 케이스
DataCamp 수강 목록 e.g. "Introduction to R"와 "Intermediate R"
R로 배우는 Market Basket Analysis