Market Basket Analysis v R
Christopher Bruffaerts
Statistician
Co je v obchodě?

Co si dnes dáte?

{"Bread", "Cheese", "Cheese", "Cheese"}
Zaměření analýzy tržního košíku

{"Bread", "Cheese"}
Můj obchod – množina
X = {"Bread", "Butter", "Cheese", "Wine"}

Podmnožiny X – itemsety
Nadmnožiny
Otázka:
Jaká je množina všech možných podmnožin X?
X = {A, B, C, D}

{"Bread"} $\cap$ {"Butter"} = $\emptyset$
{"Bread", "Butter"} $\cap$ {"Butter", "Wine"} = {"Butter"}
library(dplyr)
A = c("Bread", "Butter")
B = c("Bread", "Wine")
intersect(A,B)
[1] "Bread"
{"Bread"} $\cup$ {"Butter"} = {"Bread", "Butter"}
union(A,B)
[1] "Bread" "Butter" "Wine"
Otázka:
Kolik podmnožin velikosti k lze vytvořit z množiny velikosti n ?
„n nad k"
$${n \choose k} = \dfrac{n!}{(n-k)! k!},$$ kde
$n! = n \times (n-1) \times (n-2) \times ...\times 2 \times 1$
Příklad:
Počet košíků se 2 různými položkami z obchodu:

$${4 \choose 2} = \dfrac{4!}{(4-2)! 2!} = 6$$
Otázka
Kolik možných košíků lze vytvořit z množiny velikosti n ?
Newtonův binomický vzorec
$$\sum_{k=0}^n{n \choose k} = 2^n$$
2^(n_items)
Příklad
Celkový počet košíků:
$$2^4 = 16$$

Kombinace v R
n_items = 4
basket_size = 2
choose(n_items, basket_size)
[1] 6
# Looping through all possible values
store = matrix(NA, nrow=5, ncol=2)
for (i in 0:n_items){
store[i+1,] = c(i, choose(n_items,i))}
Výstup
colnames(store)=c("size", "nb_combi")
store
size nb_combi
[1,] 0 1
[2,] 1 4
[3,] 2 6
[4,] 3 4
[5,] 4 1
Přehled o rychlosti růstu počtu kombinací
n_items = 50
fun_nk = function(x) choose(n_items, x)
# Plotting
ggplot(data = data.frame(x = 0),
mapping = aes(x=x))+
stat_function(fun = fun_nk)+
xlim(0, n_items)+
xlab("Subset size")+
ylab("Number of subsets")

Market Basket Analysis v R