El Market Analysis es una técnica en el ámbito de análisis y minería de datos en el campo del comercio. Su objetivo principal es descubrir patrones de asociación entre productos que suelen ser comprados juntos por los clientes.
Las 3 métricas principales para evaluar reglas de asociación son:
Este caso de estudio utiliza el dataset real de Instacrt en Kaggle, el cual registra más de 3 millones de pedidos de 200,000 usuarios con un catálogo de 50,000 productos, con el objetivo de descrubrir los hábitos de consumo y predecir las compras repetidas de los clientes. Este ecosistema carece de datos de precios, por lo que se utiliza el Market Basket Analysis. Las empresas de comercio electrónico utilizan esta información para optimizar recomendaciones en la app, diseñar promociones y organizar sus inventarios.
#install.packages("tidyverse") #Paquete global para manipulación y análisis de datos
library(tidyverse)
#install.packages("janitor") #Examinar y limpiar bases de datos sucias
library(janitor)
#install.packages("Matrix") #Para trabajar con matrices
library(Matrix)
#install.packages("arules") #Genera reglas de asociación
library(arules)
#install.packages("arulesViz") #Visualizar reglas de asociación
library(arulesViz)
#install.packages("plyr")
library(plyr)
#file.choose()
products <- read.csv("/Users/annaluisarochalopez/Desktop/archive (1)/products.csv")
#file.choose()
aisles <- read.csv("/Users/annaluisarochalopez/Desktop/archive (1)/aisles.csv")
#file.choose()
departments <- read.csv("/Users/annaluisarochalopez/Desktop/archive (1)/departments.csv")
#file.choose()
order_products_prior <- read.csv("/Users/annaluisarochalopez/Desktop/archive (1)/order_products__prior.csv")
#file.choose()
order_products_train<- read.csv("/Users/annaluisarochalopez/Desktop/archive (1)/order_products__train.csv")
#file.choose()
orders <- read.csv("/Users/annaluisarochalopez/Desktop/archive (1)/orders.csv")
#Resumen de las bases
summary(orders)
## order_id user_id eval_set order_number
## Min. : 1 Min. : 1 Length:3421083 Min. : 1.00
## 1st Qu.: 855272 1st Qu.: 51394 Class :character 1st Qu.: 5.00
## Median :1710542 Median :102689 Mode :character Median : 11.00
## Mean :1710542 Mean :102978 Mean : 17.15
## 3rd Qu.:2565812 3rd Qu.:154385 3rd Qu.: 23.00
## Max. :3421083 Max. :206209 Max. :100.00
##
## order_dow order_hour_of_day days_since_prior_order
## Min. :0.000 Min. : 0.00 Min. : 0.00
## 1st Qu.:1.000 1st Qu.:10.00 1st Qu.: 4.00
## Median :3.000 Median :13.00 Median : 7.00
## Mean :2.776 Mean :13.45 Mean :11.12
## 3rd Qu.:5.000 3rd Qu.:16.00 3rd Qu.:15.00
## Max. :6.000 Max. :23.00 Max. :30.00
## NA's :206209
summary(order_products_prior)
## order_id product_id add_to_cart_order reordered
## Min. : 2 Min. : 1 Min. : 1.000 Min. :0.0000
## 1st Qu.: 855943 1st Qu.:13530 1st Qu.: 3.000 1st Qu.:0.0000
## Median :1711048 Median :25256 Median : 6.000 Median :1.0000
## Mean :1710748 Mean :25576 Mean : 8.351 Mean :0.5897
## 3rd Qu.:2565514 3rd Qu.:37935 3rd Qu.: 11.000 3rd Qu.:1.0000
## Max. :3421083 Max. :49688 Max. :145.000 Max. :1.0000
summary(products)
## product_id product_name aisle_id department_id
## Min. : 1 Length:49688 Min. : 1.00 Min. : 1.00
## 1st Qu.:12423 Class :character 1st Qu.: 35.00 1st Qu.: 7.00
## Median :24844 Mode :character Median : 69.00 Median :13.00
## Mean :24844 Mean : 67.77 Mean :11.73
## 3rd Qu.:37266 3rd Qu.:100.00 3rd Qu.:17.00
## Max. :49688 Max. :134.00 Max. :21.00
#Estructura de las bases
str(orders)
## 'data.frame': 3421083 obs. of 7 variables:
## $ order_id : int 2539329 2398795 473747 2254736 431534 3367565 550135 3108588 2295261 2550362 ...
## $ user_id : int 1 1 1 1 1 1 1 1 1 1 ...
## $ eval_set : chr "prior" "prior" "prior" "prior" ...
## $ order_number : int 1 2 3 4 5 6 7 8 9 10 ...
## $ order_dow : int 2 3 3 4 4 2 1 1 1 4 ...
## $ order_hour_of_day : int 8 7 12 7 15 7 9 14 16 8 ...
## $ days_since_prior_order: num NA 15 21 29 28 19 20 14 0 30 ...
str(order_products_prior)
## 'data.frame': 32434489 obs. of 4 variables:
## $ order_id : int 2 2 2 2 2 2 2 2 2 3 ...
## $ product_id : int 33120 28985 9327 45918 30035 17794 40141 1819 43668 33754 ...
## $ add_to_cart_order: int 1 2 3 4 5 6 7 8 9 1 ...
## $ reordered : int 1 1 0 1 0 1 1 1 0 1 ...
str(products)
## 'data.frame': 49688 obs. of 4 variables:
## $ product_id : int 1 2 3 4 5 6 7 8 9 10 ...
## $ product_name : chr "Chocolate Sandwich Cookies" "All-Seasons Salt" "Robust Golden Unsweetened Oolong Tea" "Smart Ones Classic Favorites Mini Rigatoni With Vodka Cream Sauce" ...
## $ aisle_id : int 61 104 94 38 5 11 98 116 120 115 ...
## $ department_id: int 19 13 7 1 13 11 7 1 16 7 ...
head(orders, 10)
## order_id user_id eval_set order_number order_dow order_hour_of_day
## 1 2539329 1 prior 1 2 8
## 2 2398795 1 prior 2 3 7
## 3 473747 1 prior 3 3 12
## 4 2254736 1 prior 4 4 7
## 5 431534 1 prior 5 4 15
## 6 3367565 1 prior 6 2 7
## 7 550135 1 prior 7 1 9
## 8 3108588 1 prior 8 1 14
## 9 2295261 1 prior 9 1 16
## 10 2550362 1 prior 10 4 8
## days_since_prior_order
## 1 NA
## 2 15
## 3 21
## 4 29
## 5 28
## 6 19
## 7 20
## 8 14
## 9 0
## 10 30
head(order_products_prior, 10)
## order_id product_id add_to_cart_order reordered
## 1 2 33120 1 1
## 2 2 28985 2 1
## 3 2 9327 3 0
## 4 2 45918 4 1
## 5 2 30035 5 0
## 6 2 17794 6 1
## 7 2 40141 7 1
## 8 2 1819 8 1
## 9 2 43668 9 0
## 10 3 33754 1 1
head(products, 10)
## product_id product_name
## 1 1 Chocolate Sandwich Cookies
## 2 2 All-Seasons Salt
## 3 3 Robust Golden Unsweetened Oolong Tea
## 4 4 Smart Ones Classic Favorites Mini Rigatoni With Vodka Cream Sauce
## 5 5 Green Chile Anytime Sauce
## 6 6 Dry Nose Oil
## 7 7 Pure Coconut Water With Orange
## 8 8 Cut Russet Potatoes Steam N' Mash
## 9 9 Light Strawberry Blueberry Yogurt
## 10 10 Sparkling Orange Juice & Prickly Pear Beverage
## aisle_id department_id
## 1 61 19
## 2 104 13
## 3 94 7
## 4 38 1
## 5 5 13
## 6 11 11
## 7 98 7
## 8 116 1
## 9 120 16
## 10 115 7
tail(orders, 10)
## order_id user_id eval_set order_number order_dow order_hour_of_day
## 3421074 2307371 206209 prior 5 4 15
## 3421075 3186442 206209 prior 6 0 16
## 3421076 550836 206209 prior 7 2 13
## 3421077 2129269 206209 prior 8 3 17
## 3421078 2558525 206209 prior 9 4 15
## 3421079 2266710 206209 prior 10 5 18
## 3421080 1854736 206209 prior 11 4 10
## 3421081 626363 206209 prior 12 1 12
## 3421082 2977660 206209 prior 13 1 12
## 3421083 272231 206209 train 14 6 14
## days_since_prior_order
## 3421074 3
## 3421075 3
## 3421076 9
## 3421077 22
## 3421078 22
## 3421079 29
## 3421080 30
## 3421081 18
## 3421082 7
## 3421083 30
tail(order_products_prior, 10)
## order_id product_id add_to_cart_order reordered
## 32434480 3421083 7854 1 0
## 32434481 3421083 45309 2 0
## 32434482 3421083 21162 3 0
## 32434483 3421083 18176 4 1
## 32434484 3421083 35211 5 0
## 32434485 3421083 39678 6 1
## 32434486 3421083 11352 7 0
## 32434487 3421083 4600 8 0
## 32434488 3421083 24852 9 1
## 32434489 3421083 5020 10 1
tail(products, 10)
## product_id product_name aisle_id
## 49679 49679 Famous Chocolate Wafers 61
## 49680 49680 All Natural Creamy Caesar Dressing 89
## 49681 49681 Spaghetti with Meatballs and Sauce Meal 38
## 49682 49682 California Limeade 98
## 49683 49683 Cucumber Kirby 83
## 49684 49684 Vodka, Triple Distilled, Twist of Vanilla 124
## 49685 49685 En Croute Roast Hazelnut Cranberry 42
## 49686 49686 Artisan Baguette 112
## 49687 49687 Smartblend Healthy Metabolism Dry Cat Food 41
## 49688 49688 Fresh Foaming Cleanser 73
## department_id
## 49679 19
## 49680 13
## 49681 1
## 49682 7
## 49683 4
## 49684 5
## 49685 1
## 49686 3
## 49687 8
## 49688 11
order_products <- order_products_prior %>%
left_join(products, by = "product_id")
df <- order_products %>%
left_join(orders, by = "order_id")
df <- df %>%
select(order_id, user_id, product_id, product_name)
#Eliminar valores repetidos
df <- df %>%
distinct()
#Ver si hay valores faltantes
colSums(is.na(df))
## order_id user_id product_id product_name
## 0 0 0 0
#Revisar tipo de datos
str(df)
## 'data.frame': 32434489 obs. of 4 variables:
## $ order_id : int 2 2 2 2 2 2 2 2 2 3 ...
## $ user_id : int 202279 202279 202279 202279 202279 202279 202279 202279 202279 205970 ...
## $ product_id : int 33120 28985 9327 45918 30035 17794 40141 1819 43668 33754 ...
## $ product_name: chr "Organic Egg Whites" "Michigan Organic Kale" "Garlic Powder" "Coconut Butter" ...
#Todos los indicadores sean numéricos
df$order_id <- as.numeric(df$order_id)
df$user_id <- as.numeric(df$user_id)
df$product_id <- as.numeric(df$product_id)
#Crear una lista de productos por pedido
transactions_list <- split(
df$product_name,
df$order_id
)
#Convertirlo a formato de transacciones
transactions <- as(
transactions_list,
"transactions"
)
summary(transactions)
## transactions as itemMatrix in sparse format with
## 3214874 rows (elements/itemsets/transactions) and
## 49677 columns (items) and a density of 0.0002030896
##
## most frequent items:
## Banana Bag of Organic Bananas Organic Strawberries
## 472565 379450 264683
## Organic Baby Spinach Organic Hass Avocado (Other)
## 241921 213584 30862286
##
## element (itemset/transaction) length distribution:
## sizes
## 1 2 3 4 5 6 7 8 9 10 11
## 156748 186993 207027 222081 228330 227675 220006 203374 184347 165550 147461
## 12 13 14 15 16 17 18 19 20 21 22
## 131580 116871 103683 91644 81192 71360 62629 54817 48096 41863 36368
## 23 24 25 26 27 28 29 30 31 32 33
## 31672 27065 23613 20283 17488 15102 13033 11251 9571 8035 6991
## 34 35 36 37 38 39 40 41 42 43 44
## 6041 5164 4407 3681 3169 2653 2272 1978 1642 1412 1227
## 45 46 47 48 49 50 51 52 53 54 55
## 1048 895 743 608 563 491 394 348 288 275 224
## 56 57 58 59 60 61 62 63 64 65 66
## 175 159 165 119 121 100 79 67 57 53 49
## 67 68 69 70 71 72 73 74 75 76 77
## 44 39 24 33 30 23 22 24 9 11 15
## 78 79 80 81 82 83 84 85 86 87 88
## 9 7 7 5 9 4 10 5 7 4 7
## 89 90 91 92 93 94 95 96 98 99 100
## 4 1 4 9 4 1 4 3 4 2 4
## 101 102 104 105 108 109 112 114 115 116 121
## 2 3 2 1 2 2 1 1 1 1 1
## 127 137 145
## 1 1 1
##
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 1.00 5.00 8.00 10.09 14.00 145.00
##
## includes extended item information - examples:
## labels
## 1 .5\\" Waterproof Tape
## 2 'Swingtop' Premium Lager
## 3 (70% Juice!) Mountain Raspberry Juice Squeeze
##
## includes extended transaction information - examples:
## transactionID
## 1 2
## 2 3
## 3 4
inspect(transactions[1:10])
## items transactionID
## [1] {All Natural No Stir Creamy Almond Butter,
## Carrots,
## Classic Blend Cole Slaw,
## Coconut Butter,
## Garlic Powder,
## Michigan Organic Kale,
## Natural Sweetener,
## Organic Egg Whites,
## Original Unflavored Gelatine Mix} 2
## [2] {Air Chilled Organic Boneless Skinless Chicken Breasts,
## Lemons,
## Organic Baby Spinach,
## Organic Ezekiel 49 Bread Cinnamon Raisin,
## Organic Ginger Root,
## Total 2% with Strawberry Lowfat Greek Strained Yogurt,
## Unsweetened Almondmilk,
## Unsweetened Chocolate Almond Breeze Almond Milk} 3
## [3] {Chewy 25% Low Sugar Chocolate Chip Granola,
## Energy Drink,
## Goldfish Cheddar Baked Snack Crackers,
## Honey/Lemon Cough Drops,
## Kellogg's Nutri-Grain Apple Cinnamon Cereal,
## Kellogg's Nutri-Grain Blueberry Cereal,
## Nutri-Grain Soft Baked Strawberry Cereal Breakfast Bars,
## Oats & Chocolate Chewy Bars,
## Original Orange Juice,
## Plain Pre-Sliced Bagels,
## Sugarfree Energy Drink,
## Tiny Twists Pretzels,
## Traditional Snack Mix} 4
## [4] {2% Reduced Fat Milk,
## American Slices Cheese,
## Apricot Preserves,
## Artichokes,
## Bag of Organic Bananas,
## Bag of Organic Lemons,
## Biscuits Orange Pim's,
## Boneless Skinless Chicken Breast Fillets,
## Clementines,
## Dairy Milk Fruit & Nut Chocolate Bar,
## Everyday Facial Tissues,
## French Lavender Hand Wash,
## Fresh Fruit Salad,
## Just Crisp, Parmesan,
## Macaroni And Cheese,
## Matzos, Thin, Tea,
## Meyer Lemon,
## Mini Original Babybel Cheese,
## Natural Artesian Water, Mini & Mobile,
## One Ply Choose A Size Big Roll Paper Towel Rolls,
## Organic Hass Avocado,
## Organic Raspberries,
## Original Black Box Tablewater Cracker,
## Sensitive Toilet Paper,
## Spaghetti Pasta,
## Wafer, Chocolate} 5
## [5] {Clean Day Lavender Scent Room Freshener Spray,
## Cleanse,
## Dryer Sheets Geranium Scent} 6
## [6] {Orange Juice,
## Pineapple Chunks} 7
## [7] {Original Hawaiian Sweet Rolls} 8
## [8] {100% Apple Juice Original,
## Baby Spinach,
## Distilled Water,
## Extra Virgin Olive Oil,
## French Baguettes, Take & Bake, Twin Pack,
## Fruit & Nutty Almonds Raisins Cranberries Pecans Granola,
## Green Beans,
## Low Fat Kefir Cultured Milk Smoothie Lowfat Probiotic Blueberry,
## Natural Applesauce Snack & Go Pouches,
## Natural Sharp Cheddar Sliced Cheese,
## Organic Bread with 21 Whole Grains,
## Organic Red Radish, Bunch,
## Snak-Saks Crackers,
## Vanilla Almond Breeze Almond Milk,
## Whole White Mushrooms} 9
## [9] {Baby Portabella Mushrooms,
## Banana,
## Boneless Beef Sirloin Steak,
## Green Beans,
## Organic Avocado,
## Organic Black Beans,
## Organic Butterhead (Boston, Butter, Bibb) Lettuce,
## Organic Cilantro,
## Organic Half & Half,
## Organic Strawberries,
## Organic Sunchoke,
## Parsley, Italian (Flat), New England Grown,
## Spinach Peas & Pear Stage 2 Baby Food,
## Stage 2 Green Bean Pear Greek Yogurt Baby Food,
## Yellow Onions} 10
## [10] {Extra Virgin Olive Oil,
## Mango Pineapple Salsa,
## Teriyaki & Pineapple Chicken Meatballs,
## Tortilla Strips Restaurant Style,
## Traditional Refried Beans} 11
rules <- apriori(
transactions,
parameter = list(
support = 0.0015,
confidence = 0.40,
maxlen= 6
)
)
## Apriori
##
## Parameter specification:
## confidence minval smax arem aval originalSupport maxtime support minlen
## 0.4 0.1 1 none FALSE TRUE 5 0.0015 1
## maxlen target ext
## 6 rules TRUE
##
## Algorithmic control:
## filter tree heap memopt load sort verbose
## 0.1 TRUE TRUE FALSE TRUE 2 TRUE
##
## Absolute minimum support count: 4822
##
## set item appearances ...[0 item(s)] done [0.00s].
## set transactions ...[49677 item(s), 3214874 transaction(s)] done [6.94s].
## sorting and recoding items ... [1146 item(s)] done [0.24s].
## creating transaction tree ... done [2.31s].
## checking subsets of size 1 2 3 4 done [0.51s].
## writing ... [11 rule(s)] done [0.00s].
## creating S4 object ... done [0.62s].
#Ver reglas
#summary(rules)
#Ordenar reglas por cofianza
rules_confidence <- sort(rules, by = "confidence", decreasing = TRUE)
inspect(rules_confidence)
## lhs rhs support confidence coverage lift count
## [1] {Total 2% Lowfat Greek Strained Yogurt With Blueberry} => {Total 2% with Strawberry Lowfat Greek Strained Yogurt} 0.002902447 0.4495134 0.006456863 48.343394 9331
## [2] {Organic Yellow Squash} => {Organic Zucchini} 0.001552782 0.4448009 0.003490961 13.641841 4992
## [3] {Organic Hass Avocado,
## Organic Raspberries} => {Bag of Organic Bananas} 0.003548817 0.4423293 0.008023020 3.747616 11409
## [4] {Non Fat Raspberry Yogurt} => {Icelandic Style Skyr Blueberry Non-fat Yogurt} 0.002247055 0.4411064 0.005094134 73.640837 7224
## [5] {Organic Hass Avocado,
## Organic Large Extra Fancy Fuji Apple} => {Bag of Organic Bananas} 0.001805358 0.4346589 0.004153506 3.682629 5804
## [6] {Apple Honeycrisp Organic,
## Organic Hass Avocado} => {Bag of Organic Bananas} 0.002068510 0.4285070 0.004827250 3.630507 6650
## [7] {Large Lemon,
## Strawberries} => {Banana} 0.001502081 0.4238568 0.003543840 2.883510 4829
## [8] {Cucumber Kirby,
## Organic Avocado} => {Banana} 0.002051091 0.4235067 0.004843114 2.881129 6594
## [9] {Organic Avocado,
## Strawberries} => {Banana} 0.001645477 0.4150322 0.003964697 2.823476 5290
## [10] {Cucumber Kirby,
## Organic Strawberries} => {Banana} 0.001541273 0.4113740 0.003746648 2.798590 4955
## [11] {Total 2% Lowfat Greek Strained Yogurt with Peach} => {Total 2% with Strawberry Lowfat Greek Strained Yogurt} 0.002492788 0.4025517 0.006192467 43.292848 8014
inspect(rules_confidence[1:10])
## lhs rhs support confidence coverage lift count
## [1] {Total 2% Lowfat Greek Strained Yogurt With Blueberry} => {Total 2% with Strawberry Lowfat Greek Strained Yogurt} 0.002902447 0.4495134 0.006456863 48.343394 9331
## [2] {Organic Yellow Squash} => {Organic Zucchini} 0.001552782 0.4448009 0.003490961 13.641841 4992
## [3] {Organic Hass Avocado,
## Organic Raspberries} => {Bag of Organic Bananas} 0.003548817 0.4423293 0.008023020 3.747616 11409
## [4] {Non Fat Raspberry Yogurt} => {Icelandic Style Skyr Blueberry Non-fat Yogurt} 0.002247055 0.4411064 0.005094134 73.640837 7224
## [5] {Organic Hass Avocado,
## Organic Large Extra Fancy Fuji Apple} => {Bag of Organic Bananas} 0.001805358 0.4346589 0.004153506 3.682629 5804
## [6] {Apple Honeycrisp Organic,
## Organic Hass Avocado} => {Bag of Organic Bananas} 0.002068510 0.4285070 0.004827250 3.630507 6650
## [7] {Large Lemon,
## Strawberries} => {Banana} 0.001502081 0.4238568 0.003543840 2.883510 4829
## [8] {Cucumber Kirby,
## Organic Avocado} => {Banana} 0.002051091 0.4235067 0.004843114 2.881129 6594
## [9] {Organic Avocado,
## Strawberries} => {Banana} 0.001645477 0.4150322 0.003964697 2.823476 5290
## [10] {Cucumber Kirby,
## Organic Strawberries} => {Banana} 0.001541273 0.4113740 0.003746648 2.798590 4955
length(transactions)
## [1] 3214874
summary(transactions)
## transactions as itemMatrix in sparse format with
## 3214874 rows (elements/itemsets/transactions) and
## 49677 columns (items) and a density of 0.0002030896
##
## most frequent items:
## Banana Bag of Organic Bananas Organic Strawberries
## 472565 379450 264683
## Organic Baby Spinach Organic Hass Avocado (Other)
## 241921 213584 30862286
##
## element (itemset/transaction) length distribution:
## sizes
## 1 2 3 4 5 6 7 8 9 10 11
## 156748 186993 207027 222081 228330 227675 220006 203374 184347 165550 147461
## 12 13 14 15 16 17 18 19 20 21 22
## 131580 116871 103683 91644 81192 71360 62629 54817 48096 41863 36368
## 23 24 25 26 27 28 29 30 31 32 33
## 31672 27065 23613 20283 17488 15102 13033 11251 9571 8035 6991
## 34 35 36 37 38 39 40 41 42 43 44
## 6041 5164 4407 3681 3169 2653 2272 1978 1642 1412 1227
## 45 46 47 48 49 50 51 52 53 54 55
## 1048 895 743 608 563 491 394 348 288 275 224
## 56 57 58 59 60 61 62 63 64 65 66
## 175 159 165 119 121 100 79 67 57 53 49
## 67 68 69 70 71 72 73 74 75 76 77
## 44 39 24 33 30 23 22 24 9 11 15
## 78 79 80 81 82 83 84 85 86 87 88
## 9 7 7 5 9 4 10 5 7 4 7
## 89 90 91 92 93 94 95 96 98 99 100
## 4 1 4 9 4 1 4 3 4 2 4
## 101 102 104 105 108 109 112 114 115 116 121
## 2 3 2 1 2 2 1 1 1 1 1
## 127 137 145
## 1 1 1
##
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 1.00 5.00 8.00 10.09 14.00 145.00
##
## includes extended item information - examples:
## labels
## 1 .5\\" Waterproof Tape
## 2 'Swingtop' Premium Lager
## 3 (70% Juice!) Mountain Raspberry Juice Squeeze
##
## includes extended transaction information - examples:
## transactionID
## 1 2
## 2 3
## 3 4
length(rules)
## [1] 11
summary(rules)
## set of 11 rules
##
## rule length distribution (lhs + rhs):sizes
## 2 3
## 4 7
##
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 2.000 2.000 3.000 2.636 3.000 3.000
##
## summary of quality measures:
## support confidence coverage lift
## Min. :0.001502 Min. :0.4026 Min. :0.003491 Min. : 2.799
## 1st Qu.:0.001599 1st Qu.:0.4193 1st Qu.:0.003856 1st Qu.: 2.882
## Median :0.002051 Median :0.4285 Median :0.004827 Median : 3.683
## Mean :0.002123 Mean :0.4288 Mean :0.004940 Mean :18.306
## 3rd Qu.:0.002370 3rd Qu.:0.4417 3rd Qu.:0.005643 3rd Qu.:28.467
## Max. :0.003549 Max. :0.4495 Max. :0.008023 Max. :73.641
## count
## Min. : 4829
## 1st Qu.: 5141
## Median : 6594
## Mean : 6827
## 3rd Qu.: 7619
## Max. :11409
##
## mining info:
## data ntransactions support confidence
## transactions 3214874 0.0015 0.4
## call
## apriori(data = transactions, parameter = list(support = 0.0015, confidence = 0.4, maxlen = 6))
inspect(rules_confidence)
## lhs rhs support confidence coverage lift count
## [1] {Total 2% Lowfat Greek Strained Yogurt With Blueberry} => {Total 2% with Strawberry Lowfat Greek Strained Yogurt} 0.002902447 0.4495134 0.006456863 48.343394 9331
## [2] {Organic Yellow Squash} => {Organic Zucchini} 0.001552782 0.4448009 0.003490961 13.641841 4992
## [3] {Organic Hass Avocado,
## Organic Raspberries} => {Bag of Organic Bananas} 0.003548817 0.4423293 0.008023020 3.747616 11409
## [4] {Non Fat Raspberry Yogurt} => {Icelandic Style Skyr Blueberry Non-fat Yogurt} 0.002247055 0.4411064 0.005094134 73.640837 7224
## [5] {Organic Hass Avocado,
## Organic Large Extra Fancy Fuji Apple} => {Bag of Organic Bananas} 0.001805358 0.4346589 0.004153506 3.682629 5804
## [6] {Apple Honeycrisp Organic,
## Organic Hass Avocado} => {Bag of Organic Bananas} 0.002068510 0.4285070 0.004827250 3.630507 6650
## [7] {Large Lemon,
## Strawberries} => {Banana} 0.001502081 0.4238568 0.003543840 2.883510 4829
## [8] {Cucumber Kirby,
## Organic Avocado} => {Banana} 0.002051091 0.4235067 0.004843114 2.881129 6594
## [9] {Organic Avocado,
## Strawberries} => {Banana} 0.001645477 0.4150322 0.003964697 2.823476 5290
## [10] {Cucumber Kirby,
## Organic Strawberries} => {Banana} 0.001541273 0.4113740 0.003746648 2.798590 4955
## [11] {Total 2% Lowfat Greek Strained Yogurt with Peach} => {Total 2% with Strawberry Lowfat Greek Strained Yogurt} 0.002492788 0.4025517 0.006192467 43.292848 8014
plot(
rules_confidence,
method = "graph"
)
## Warning: ggrepel: 6 unlabeled data points (too many overlaps). Consider
## increasing max.overlaps
# Ordenar reglas por confianza
rules_confidence <- sort(
rules,
by = "confidence",
decreasing = TRUE
)
# Seleccionar las 10 mejores reglas
top10reglas <- head(
rules_confidence,
n = 10
)
# Graficar las reglas
plot(
top10reglas,
method = "graph",
engine = "htmlwidget"
)
Como conclusión, el análisis de canasta nos permitió encontrar productos que los clientes suelen comprar juntos. Por ejemplo, encontramos una relación entre las bananas y productos como el aguacate y las manzanas Fuji orgánicas. Estas relaciones pueden ser útiles para que Instacart genere recomendaciones, promociones o estrategias de venta más personalizadas