Market basket introduction

Market Basket Analysis in R

Christopher Bruffaerts

Statistician

Overview

Market Basket course

  • Chapter 1: Introduction to market basket analysis
  • Chapter 2: Metrics and techniques in market basket analysis
  • Chapter 3: Visualization in market basket analysis
  • Chapter 4: Case study: Movie recommendations @ movieLens

movie_lens_logo

Market Basket Analysis in R

What is a basket?

Basket = collection of items

Items

  1. Products at the supermarket
  2. Products on online website
  3. DataCamp courses
  4. Movies watched by users

supermarket

Examples of baskets:

  1. Your basket @ the grocery store

  2. Your Amazon shopping cart

  3. Your courses @ DataCamp

  4. The movies you watched on Netflix

Market Basket Analysis in R

Grocery store example

What's in the store?

all_products_emoticons

What are you up for today?

  • One bread
  • Three pieces of cheese

bread_3cheese

Market Basket Analysis in R

Grocery store example in R

What's in the store?

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 output

my_basket
  TID Product
1   1   Bread
2   1  Cheese
3   1  Cheese
4   1  Cheese
Market Basket Analysis in R

What's in my basket?

My original basket

One record per item purchased

  TID Product
1   1   Bread
2   1  Cheese
3   1  Cheese
4   1  Cheese

My adjusted basket

One record per distinct item purchased

# A tibble: 2 x 3
    TID Product Quantity
  <dbl> <fct>      <int>
1     1 Bread          1
2     1 Cheese         3
Market Basket Analysis in R

What's in my R basket?

Reshaping the basket data

# Adjusting my basket
my_basket = my_basket %>%
                add_count(Product) %>%
                unique() %>% 
                rename(Quantity = n)
# Number of distinct items
n_distinct(my_basket$Product)
2
# Total basket size
my_basket %>% summarize(sum(Quantity))
4
Market Basket Analysis in R

Visualizing items in my basket

Visualizing items in my basket

# Plotting items 
ggplot(my_basket,
    aes(x=reorder(Product, Quantity),
        y = Quantity)) +
          geom_col() +
        coord_flip() +
        xlab("Items") +
        ggtitle("Summary of items
                in my basket")

distribution_basket_bread_cheese

Market Basket Analysis in R

Why are we looking at my basket?

Question: Is there any relationship between items within a basket ?

baskets

Back to examples

  1. Your basket @ the grocery store, e.g. Spaghetti and Tomato sauce

  2. Your Amazon shopping cart, e.g. Phone and a phone case

  3. Your courses @ DataCamp e.g. "Introduction to R" and "Intermediate R"

Market Basket Analysis in R

Happy shopping!

Market Basket Analysis in R

Preparing Video For Download...