Introduction

Packages

Data Packages Required:

library(tidyverse)
library(dplyr)
library(ggplot2)
library(lubridate)
library(completejourney)

transactions <- get_transactions()

Question 1

1. Do households with or without kids tend to buy more dairy products?

Grouping All Dairy Products:

Dairy <- products %>%
  filter(
    str_detect(product_category, regex("( cheese|milk|icecream|cream|butter|yogurt )", ignore_case = TRUE))
         ) %>%
  select(product_category, everything())
Test_kids <- Dairy %>%
  inner_join(transactions, by = "product_id") %>%
  inner_join(demographics, by = "household_id") %>%
  filter(kids_count > 0) %>%
  group_by(product_id, product_category, income, transaction_timestamp) %>%
  summarize(total_sales = sum(sales_value, na.rm = TRUE) ) %>%
  arrange(desc(total_sales)) 
## `summarise()` has grouped output by 'product_id', 'product_category', 'income'.
## You can override using the `.groups` argument.
ggplot(Test_kids, aes(x = product_category, y = total_sales, fill = product_category)) +
  geom_bar(stat = "identity") +
  scale_y_continuous(labels = scales::dollar, limits = c(0, 40000)) +
  labs(title = "Total Sales of Dairy Products for Households with Kids",
       x = "Product Category",
       y = "Total Sales") +
  theme_minimal() +
  theme(axis.text.x = element_blank())

Test_NOkids <- Dairy %>%
  inner_join(transactions, by = "product_id") %>%
  inner_join(demographics, by = "household_id") %>%
  filter(kids_count == 0) %>%
  group_by(product_id, product_category, income, transaction_timestamp) %>%
  summarize(total_sales = sum(sales_value, na.rm = TRUE) ) %>%
  arrange(desc(total_sales))
## `summarise()` has grouped output by 'product_id', 'product_category', 'income'.
## You can override using the `.groups` argument.
  ggplot(Test_NOkids, aes(x = product_category, y = total_sales, fill = product_category)) +
  geom_bar(stat = "identity") +
  scale_y_continuous(labels = scales::dollar, limits = c(0, 40000)) +
  labs(title = "Total Sales of Dairy Products for Households with No Kids",
       x = "Product Category",
       y = "Total Sales") +
  theme_minimal() +
  theme(axis.text.x = element_blank())

Total Sales of Dairy Products for Households with Kids:

Households_kids <- Dairy %>%
  inner_join(transactions, by = "product_id") %>%
  inner_join(demographics, by = "household_id") %>%
  filter(kids_count > 0) %>%
  group_by(product_id, product_category, income, transaction_timestamp) %>%
  summarize(total_sales = sum(sales_value, na.rm = TRUE) ) %>%
  arrange(desc(total_sales)) 

ggplot(Households_kids, aes(x = product_category, y = total_sales, fill = product_category)) +
  geom_bar(stat = "identity") +
  facet_wrap(~ income) +
  scale_y_continuous(labels = scales::dollar, limits = c(0, 10000)) +
  labs(title = "Total Sales of Dairy Products for Households with Kids",
       x = "Product Category",
       y = "Total Sales") +
  theme_minimal() +
  theme(axis.text.x = element_blank())

Total Sales of Dairy Products for Households with No Kids:

Households_NOkids <- Dairy %>%
  inner_join(transactions, by = "product_id") %>%
  inner_join(demographics, by = "household_id") %>%
  filter(kids_count == 0) %>%
  group_by(product_id, product_category, income, transaction_timestamp) %>%
  summarize(total_sales = sum(sales_value, na.rm = TRUE) ) %>%
  arrange(desc(total_sales))

  ggplot(Households_NOkids, aes(x = product_category, y = total_sales, fill = product_category)) +
  geom_bar(stat = "identity") +
  facet_wrap(~ income) +
  scale_y_continuous(labels = scales::dollar, limits = c(0, 10000)) +
  labs(title = "Total Sales of Dairy Products for Households with No Kids",
       x = "Product Category",
       y = "Total Sales") +
  theme_minimal() +
  theme(axis.text.x = element_blank())

CONCLUSION: Households without kids tend to buy more dairy products.

Question 2

2. What day of the week has the largest sales of dairy products?

Consumption of Dairy Products for Households with Kids in The Week:

Households_kids %>%
  mutate(day_of_week = wday(transaction_timestamp, label = TRUE)) %>%
  group_by(day_of_week, product_category, income) %>%
  summarize(total_purchases = n()) %>%
  ggplot(aes(x = day_of_week, y = total_purchases, color = product_category, group = product_category)) +
  geom_line(linewidth = 1.5) +
  facet_wrap(~ income) +
  labs(title = "Consumption of Dairy Products for Households with Kids in The Week") +
  scale_y_continuous(name = "Total Purchases", labels = scales::dollar, limits = c(0, 1000)) +
  scale_x_discrete(name = "Day of Week", labels = c("Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat")) +
  theme_minimal()

Consumption of Dairy Products for Households with No Kids in The Week:

Households_NOkids %>%
  mutate(day_of_week = wday(transaction_timestamp, label = TRUE)) %>%
  group_by(day_of_week, product_category, income) %>%
  summarize(total_purchases = n()) %>%
  ggplot(aes(x = day_of_week, y = total_purchases, color = product_category, group = product_category)) +
  geom_line(linewidth = 1.5) +
  facet_wrap(~ income) +
  labs(title = "Consumption of Dairy Products for Households with No Kids in The Week:") +
  scale_y_continuous(name = "Total Purchases", labels = scales::dollar, limits = c(0, 1000)) +
  scale_x_discrete(name = "Day of Week", labels = c("Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat")) +
  theme_minimal()

CONCLUSION: Households that have income range of 50-74K regardless of kids count tend to buy more dairy products during the weekend.

Question 3

3. Which demographic group is more sensitive to dairy coupons?

Dairy Product Coupon Redemption on Weekends for Households With Kids That Have Income Range of 50-74K:

Cp_kids <- Households_kids %>%
  inner_join(coupons, by = "product_id") %>%
  inner_join(coupon_redemptions, by = "coupon_upc") %>%
  mutate(day_of_week = wday(transaction_timestamp, label = TRUE)) %>%
  filter(day_of_week %in% c("Sat", "Sun")) %>%
  group_by(product_category, income) %>%
  summarize(coupon_upc = n()) %>%
  filter(income == "50-74K") %>%
  mutate(Kids = "YES") %>%
  print(n = 50)
## # A tibble: 4 × 4
## # Groups:   product_category [4]
##   product_category       income coupon_upc Kids 
##   <chr>                  <ord>       <int> <chr>
## 1 BUTTER                 50-74K       1414 YES  
## 2 FLUID MILK PRODUCTS    50-74K      57008 YES  
## 3 ICE CREAM/MILK/SHERBTS 50-74K      13201 YES  
## 4 MILK BY-PRODUCTS       50-74K      15958 YES

Dairy Product Coupon Redemption on Weekends for Households With No Kids That Have Income Range of 50-74K:

Cp_NOkids <- Households_NOkids %>%
  inner_join(coupons, by = "product_id") %>%
  inner_join(coupon_redemptions, by = "coupon_upc") %>%
  mutate(day_of_week = wday(transaction_timestamp, label = TRUE)) %>%
  filter(day_of_week %in% c("Sat", "Sun")) %>%
  group_by(product_category, income) %>%
  summarize(coupon_upc = n()) %>%
  filter(income == "50-74K") %>%
  mutate(Kids = "NO") %>%
  print(n = 50)
## # A tibble: 4 × 4
## # Groups:   product_category [4]
##   product_category       income coupon_upc Kids 
##   <chr>                  <ord>       <int> <chr>
## 1 BUTTER                 50-74K       4780 NO   
## 2 FLUID MILK PRODUCTS    50-74K     145942 NO   
## 3 ICE CREAM/MILK/SHERBTS 50-74K      34076 NO   
## 4 MILK BY-PRODUCTS       50-74K      51629 NO

Dairy Product Coupon Redemption on Weekends for Households That Have Income Range of 50-74K:

union(Cp_kids, Cp_NOkids) %>%
  ggplot(aes(reorder(product_category, coupon_upc), coupon_upc, fill = Kids)) + 
  geom_col(position = "dodge") +
  coord_flip() +
  facet_wrap(~ income) +
  scale_y_continuous(labels = scales::comma) +
  labs(title = "Dairy Product Coupon Redemption for Households With Income Range 50-74K",
       x = "Product Category",
       y = "Coupon Redemptions")

CONCLUSION: Households with no kids tend to redeem more coupons for dairy products on the weekends than households with kids do.

Summary