## [1] "LC_COLLATE=Chinese (Traditional)_Taiwan.950;LC_CTYPE=Chinese (Traditional)_Taiwan.950;LC_MONETARY=Chinese (Traditional)_Taiwan.950;LC_NUMERIC=C;LC_TIME=Chinese (Traditional)_Taiwan.950"
rm(list=ls(all=TRUE))
pacman::p_load(Matrix, vcd, magrittr, readr, caTools, ggplot2, dplyr, tidyr, vcd, d3heatmap, arules, arulesViz)
load("data/tf0.rdata") #1_Prep notebook有把整理過的資料存起來,這邊就可直接讀取(A0:顧客 X0:訂單 Z0:品項)
sapply(list(cust=A0,tid=X0,items=Z0), nrow)
## cust tid items
## 32241 119328 817182
par(mfrow=c(1,2),cex=0.7)
table(A0$age) %>% barplot(las=2,main="Age Groups")
table(A0$area) %>% barplot(las=2,main="Areas") #115南港,39歲級距的顧客最多
Fig-2: Zip Codes
由於a99
(沒有年齡資料的顧客)人數不多,而且特徵很獨特,探索時我們可以考慮濾掉這群顧客
A0 %>% filter(age!="a99") %>% # 濾掉沒有年齡資料的顧客('a99')
group_by(age) %>% summarise(
Group.Size = n(), # 族群人數
avg.Freq = mean(f), # 平均購買次數
avg.Revenue = sum(f*m)/sum(f) # 平均客單價
) %>%
ggplot(aes(y=avg.Freq, x=avg.Revenue)) +
geom_point(aes(col=age, size=Group.Size), alpha=0.5) +
geom_text(aes(label=age)) +
scale_size(range=c(5,25)) +
theme_bw() + theme(legend.position="none") +
ggtitle("年齡區隔特徵 (泡泡大小:族群人數)") +
ylab("平均購買次數") + xlab("平均客單價")
A0 %>% filter(age!="a99") %>% # 濾掉沒有年齡資料的顧客('a99')
group_by(area) %>% summarise(
Group.Size = n(), # 族群人數
avg.Freq = mean(f), # 平均購買次數
avg.Revenue = sum(f*m)/sum(f) # 平均客單價
) %>%
ggplot(aes(y=avg.Freq, x=avg.Revenue)) +
geom_point(aes(col=area, size=Group.Size), alpha=0.5) +
geom_text(aes(label=area)) +
scale_size(range=c(5,25)) +
theme_bw() + theme(legend.position="none") +
ggtitle("地理區隔特徵 (泡泡大小:族群人數)") +
ylab("平均購買次數") + xlab("平均客單價")
cats = Z0 %>% group_by(cat) %>% summarise(
noProd = n_distinct(prod),
totalQty = sum(qty),
totalRev = sum(price),
totalGross = sum(price) - sum(cost),
grossMargin = totalGross/totalRev,
avgPrice = totalRev/totalQty
)
par(mfrow=c(2,1), cex=0.7)
cats$totalRev %>% sort(dec=T) %>% {cumsum(.)[1:40]/sum(.)} %>%
barplot(names=1:40,las=2,main="acc. percentage of reveune")
abline(h=seq(0,1,0.1),col='green') #營收貢獻最大的40個品類佔總營收未達40%
cats$totalGross %>% sort(dec=T) %>% {cumsum(.)[1:40]/sum(.)} %>%
barplot(names=1:40,las=2,main="acc. percentage of gross profit")
abline(h=seq(0,1,0.1),col='green') #雜貨店通常沒有單一貢獻大的品類(明星產品)
品類的營收和毛利貢獻相當分散
X0$wday = format(X0$date, "%u")
par(cex=0.7, mar=c(2,3,2,1))
table(X0$wday) %>% barplot(main="No. Transactions in Week Days")
##
## 1 2 3 4
## 18478 7781 1612 4370
group_by(A0, grp) %>% summarise(
recent=mean(r),
freq=mean(f),
money=mean(m),
size=n() ) %>%
mutate( revenue = size*money/1000 ) %>%
filter(size > 1) %>%
ggplot(aes(x=freq, y=money)) +
geom_point(aes(size=revenue, col=recent),alpha=0.5) +
scale_size(range=c(4,30)) +
scale_color_gradient(low="green",high="red") +
scale_x_log10() + scale_y_log10(limits=c(500,3000)) +
geom_text(aes(label = size ),size=3) +
theme_bw() + guides(size=F) +
labs(title="Customer Segements",
subtitle="(bubble_size:revenue_contribution; text:group_size)",
color="Recency") +
xlab("Frequency (log)") + ylab("Average Transaction Amount (log)")
## r s m f grossmargin
## 2.067317e-17 7.687163e-17 3.120824e-17 -1.795684e-18 -8.202183e-17
## avgpieces
## 2.899279e-17
## r s m f grossmargin avgpieces
## 1 1 1 1 1 1
#查看原始尺度中各數值對應各族群的大小
names(AS) = names(AN) =c(
"Recency","Seniority","Monetary","Frequency","Gross_margin",
"Avgpieces")
sapply(split(AS,gp), colMeans) %>% round(3) # 原始尺度
## 1 2 3 4
## Recency 19.014 85.051 6.341 42.134
## Seniority 74.528 92.041 114.490 74.706
## Monetary 731.580 682.965 627.520 2786.316
## Frequency 3.617 1.467 19.561 2.185
## Gross_margin 0.126 0.115 0.140 0.172
## Avgpieces 7.951 7.689 7.251 26.806
## 1 2 3 4
## Recency -0.547 1.413 -0.924 0.139
## Seniority -0.183 0.330 0.989 -0.178
## Monetary -0.273 -0.323 -0.381 1.869
## Frequency -0.017 -0.463 3.285 -0.314
## Gross_margin -0.030 -0.119 0.071 0.312
## Avgpieces -0.277 -0.307 -0.356 1.851
#繪製彩虹圖,檢視各群特徵
par(cex=0.8)
split(AN,gp) %>% sapply(colMeans) %>% barplot(beside=T,col=rainbow(6))
legend('topleft',legend=colnames(AS),fill=rainbow(6))
## # A tibble: 20 x 7
## cat noProd totalQty totalRev totalGross grossMargin avgPrice
## <dbl> <int> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 100102 136 11175 820440 131083 0.160 73.4
## 2 100201 85 9910 545784 82592 0.151 55.1
## 3 100205 275 24553 1222044 200685 0.164 49.8
## 4 100312 33 12924 429412 45114 0.105 33.2
## 5 100505 38 14580 573466 70386 0.123 39.3
## 6 110106 7 13327 227899 -32746 -0.144 17.1
## 7 110117 60 10241 594221 26814 0.0451 58.0
## 8 110217 36 14325 2201258 -87223 -0.0396 154.
## 9 110401 73 15614 801041 131450 0.164 51.3
## 10 110411 105 21203 522463 87826 0.168 24.6
## 11 120103 79 21145 667070 92287 0.138 31.5
## 12 130106 17 10861 191566 31363 0.164 17.6
## 13 130204 46 12063 950573 96784 0.102 78.8
## 14 130206 76 14352 911146 128736 0.141 63.5
## 15 130315 12 18852 375198 -122632 -0.327 19.9
## 16 500201 17 16970 2204325 56892 0.0258 130.
## 17 500210 5 9440 979403 31282 0.0319 104.
## 18 500804 86 11923 650290 98508 0.151 54.5
## 19 530101 88 14335 1161968 184621 0.159 81.1
## 20 560201 68 14752 4329366 288855 0.0667 293.
prod1=AZ%>%filter(grp=="1")%>%group_by(prod)%>%summarise(vol1=sum(qty))%>%arrange(desc(vol1))%>%head(20)
# `support` : `lhs`品項被購買的基礎機率
# `confidence` : `lhs`品項被購買時`rhs`被購買的機率
# `lift` : `lhs`品項被購買時,`rhs`被購買所增加機率的倍數
# `count` : 交易筆數(交易筆數如果太少,分析就沒有實質意義)
# G1 購物籃分析
# 購物籃分析 : 有些商品毛利為負,推測是為了用來吸引顧客進而購買其他商品。
# 例如: 4711856000125 被購買時會增加 4711856000088 被購買的機率(吸引顧客購買其他商品)
tr = as(split(Z0[,"prod"], Z0[,"tid"]), "transactions")
R <- apriori(tr, parameter=list(supp=0.0001, conf=0.25))
## Apriori
##
## Parameter specification:
## confidence minval smax arem aval originalSupport maxtime support minlen
## 0.25 0.1 1 none FALSE TRUE 5 1e-04 1
## maxlen target ext
## 10 rules TRUE
##
## Algorithmic control:
## filter tree heap memopt load sort verbose
## 0.1 TRUE TRUE FALSE TRUE 2 TRUE
##
## Absolute minimum support count: 11
##
## set item appearances ...[0 item(s)] done [0.00s].
## set transactions ...[23789 item(s), 119422 transaction(s)] done [0.45s].
## sorting and recoding items ... [10166 item(s)] done [0.01s].
## creating transaction tree ... done [0.05s].
## checking subsets of size 1 2 3 4 5 6 7 8 9 done [0.58s].
## writing ... [9795 rule(s)] done [0.16s].
## creating S4 object ... done [0.04s].
## lhs rhs support confidence coverage lift count
## [1] {4711856000125} => {4711856000088} 0.0024786053 0.3296214 0.007519553 61.50632 296
## [2] {4711856000125} => {4711856020215} 0.0021687796 0.2884187 0.007519553 58.08354 259
## [3] {4711856000125} => {4711856020208} 0.0021269113 0.2828508 0.007519553 54.30644 254
## [4] {4710011401128} => {4710011409056} 0.0044464169 0.3243739 0.013707692 51.03738 531
## [5] {4710011401128} => {4710011401135} 0.0058615665 0.4276115 0.013707692 54.90991 700
## [6] {4711856000088,
## 4711856000125} => {4711856020215} 0.0008959823 0.3614865 0.002478605 72.79838 107
## [7] {4711856000125,
## 4711856020215} => {4711856000088} 0.0008959823 0.4131274 0.002168780 77.08828 107
## [8] {4711856000088,
## 4711856000125} => {4711856020208} 0.0009378506 0.3783784 0.002478605 72.64743 112
## [9] {4711856000125,
## 4711856020208} => {4711856000088} 0.0009378506 0.4409449 0.002126911 82.27894 112
## [10] {4711856000125,
## 4711856020215} => {4711856020208} 0.0008792350 0.4054054 0.002168780 77.83653 105
## [11] {4711856000125,
## 4711856020208} => {4711856020215} 0.0008792350 0.4133858 0.002126911 83.25019 105
## [12] {4710011401128,
## 4710011401142} => {4710011409056} 0.0011974343 0.5396226 0.002219022 84.90489 143
## [13] {4710011401128,
## 4710011409056} => {4710011401142} 0.0011974343 0.2693032 0.004446417 72.59758 143
## [14] {4710011401128,
## 4710011401142} => {4710011401135} 0.0013900286 0.6264151 0.002219022 80.43843 166
## [15] {4710011401128,
## 4710011401142} => {4710011405133} 0.0012476763 0.5622642 0.002219022 71.43267 149
## [16] {4710011401128,
## 4710011401142} => {4710011406123} 0.0010132136 0.4566038 0.002219022 55.64136 121
## [17] {4710085120628,
## 4710085172702} => {4710085120093} 0.0012811710 0.5204082 0.002461858 69.20733 153
## [18] {4710085120093,
## 4710085120628} => {4710085172702} 0.0012811710 0.3234672 0.003960744 71.93502 153
## [19] {4710085120628,
## 4710085172702} => {4710085172696} 0.0014905126 0.6054422 0.002461858 69.52223 178
## [20] {4710085120628,
## 4710085172696} => {4710085172702} 0.0014905126 0.3423077 0.004354307 76.12490 178
## [21] {4710011401128,
## 4710011409056} => {4710011401135} 0.0027214416 0.6120527 0.004446417 78.59415 325
## [22] {4710011401128,
## 4710011401135} => {4710011409056} 0.0027214416 0.4642857 0.005861567 73.05129 325
## [23] {4710011401128,
## 4710011409056} => {4710011405133} 0.0022776373 0.5122411 0.004446417 65.07750 272
## [24] {4710011401128,
## 4710011405133} => {4710011409056} 0.0022776373 0.4387097 0.005191673 69.02712 272
## [25] {4710011401128,
## 4710011409056} => {4710011406123} 0.0019929326 0.4482109 0.004446417 54.61862 238
## [26] {4710011401128,
## 4710011406123} => {4710011409056} 0.0019929326 0.4103448 0.004856727 64.56416 238
## [27] {4710085120093,
## 4710085120628} => {4710085172696} 0.0021352850 0.5391121 0.003960744 61.90561 255
## [28] {4710085120628,
## 4710085172696} => {4710085120093} 0.0021352850 0.4903846 0.004354307 65.21460 255
## [29] {4710011401128,
## 4710011401135} => {4710011405133} 0.0028386729 0.4842857 0.005861567 61.52592 339
## [30] {4710011401128,
## 4710011405133} => {4710011401135} 0.0028386729 0.5467742 0.005191673 70.21169 339
## [31] {4710011401128,
## 4710011401135} => {4710011406123} 0.0024534843 0.4185714 0.005861567 51.00677 293
## [32] {4710011401128,
## 4710011406123} => {4710011401135} 0.0024534843 0.5051724 0.004856727 64.86957 293
## [33] {4710011401128,
## 4710011405133} => {4710011406123} 0.0022273953 0.4290323 0.005191673 52.28152 266
## [34] {4710011401128,
## 4710011406123} => {4710011405133} 0.0022273953 0.4586207 0.004856727 58.26532 266
## [35] {4710011401128,
## 4710011401142,
## 4710011409056} => {4710011401135} 0.0009462243 0.7902098 0.001197434 101.47143 113
## [36] {4710011401128,
## 4710011401135,
## 4710011401142} => {4710011409056} 0.0009462243 0.6807229 0.001390029 107.10578 113
## [37] {4710011401128,
## 4710011401135,
## 4710011409056} => {4710011401142} 0.0009462243 0.3476923 0.002721442 93.72937 113
## [38] {4710085120093,
## 4710085120628,
## 4710085172702} => {4710085172696} 0.0008792350 0.6862745 0.001281171 78.80411 105
## [39] {4710085120628,
## 4710085172696,
## 4710085172702} => {4710085120093} 0.0008792350 0.5898876 0.001490513 78.44717 105
## [40] {4710085120093,
## 4710085120628,
## 4710085172696} => {4710085172702} 0.0008792350 0.4117647 0.002135285 91.57126 105
## [41] {4710011401128,
## 4710011401135,
## 4710011409056} => {4710011405133} 0.0015742493 0.5784615 0.002721442 73.49046 188
## [42] {4710011401128,
## 4710011405133,
## 4710011409056} => {4710011401135} 0.0015742493 0.6911765 0.002277637 88.75449 188
## [43] {4710011401128,
## 4710011401135,
## 4710011405133} => {4710011409056} 0.0015742493 0.5545723 0.002838673 87.25709 188
## [44] {4710011401128,
## 4710011401135,
## 4710011409056} => {4710011406123} 0.0013397866 0.4923077 0.002721442 59.99221 160
## [45] {4710011401128,
## 4710011406123,
## 4710011409056} => {4710011401135} 0.0013397866 0.6722689 0.001992933 86.32656 160
## [46] {4710011401128,
## 4710011401135,
## 4710011406123} => {4710011409056} 0.0013397866 0.5460751 0.002453484 85.92013 160
## [47] {4710011401128,
## 4710011405133,
## 4710011409056} => {4710011406123} 0.0011639396 0.5110294 0.002277637 62.27363 139
## [48] {4710011401128,
## 4710011406123,
## 4710011409056} => {4710011405133} 0.0011639396 0.5840336 0.001992933 74.19836 139
## [49] {4710011401128,
## 4710011405133,
## 4710011406123} => {4710011409056} 0.0011639396 0.5225564 0.002227395 82.21967 139
## [50] {4710011401128,
## 4710011401135,
## 4710011405133} => {4710011406123} 0.0013565340 0.4778761 0.002838673 58.23359 162
## [51] {4710011401128,
## 4710011401135,
## 4710011406123} => {4710011405133} 0.0013565340 0.5529010 0.002453484 70.24313 162
## [52] {4710011401128,
## 4710011405133,
## 4710011406123} => {4710011401135} 0.0013565340 0.6090226 0.002227395 78.20504 162
## [53] {4710011401128,
## 4710011401135,
## 4710011405133,
## 4710011409056} => {4710011406123} 0.0008624876 0.5478723 0.001574249 66.76328 103
## [54] {4710011401128,
## 4710011401135,
## 4710011406123,
## 4710011409056} => {4710011405133} 0.0008624876 0.6437500 0.001339787 81.78501 103
## [55] {4710011401128,
## 4710011405133,
## 4710011406123,
## 4710011409056} => {4710011401135} 0.0008624876 0.7410072 0.001163940 95.15329 103
## [56] {4710011401128,
## 4710011401135,
## 4710011405133,
## 4710011406123} => {4710011409056} 0.0008624876 0.6358025 0.001356534 100.03795 103
prod2=AZ%>%filter(grp=="2")%>%group_by(prod)%>%summarise(vol2=sum(qty))%>%arrange(desc(vol2))%>%head(20)
# G2 購物籃分析
rx = subset(R, lhs %in% prod2$prod & lift >= 50 & count > 100 )
df = inspect(rx)
## lhs rhs support confidence coverage lift count
## [1] {4710085120628,
## 4710085172702} => {4710085120093} 0.001281171 0.5204082 0.002461858 69.20733 153
## [2] {4710085120093,
## 4710085120628} => {4710085172702} 0.001281171 0.3234672 0.003960744 71.93502 153
## [3] {4710085120628,
## 4710085172702} => {4710085172696} 0.001490513 0.6054422 0.002461858 69.52223 178
## [4] {4710085120628,
## 4710085172696} => {4710085172702} 0.001490513 0.3423077 0.004354307 76.12490 178
## [5] {4710085120093,
## 4710085120628} => {4710085172696} 0.002135285 0.5391121 0.003960744 61.90561 255
## [6] {4710085120628,
## 4710085172696} => {4710085120093} 0.002135285 0.4903846 0.004354307 65.21460 255
## [7] {4710085120093,
## 4710085120628,
## 4710085172702} => {4710085172696} 0.000879235 0.6862745 0.001281171 78.80411 105
## [8] {4710085120628,
## 4710085172696,
## 4710085172702} => {4710085120093} 0.000879235 0.5898876 0.001490513 78.44717 105
## [9] {4710085120093,
## 4710085120628,
## 4710085172696} => {4710085172702} 0.000879235 0.4117647 0.002135285 91.57126 105
prod3=AZ%>%filter(grp=="3")%>%group_by(prod)%>%summarise(vol3=sum(qty))%>%arrange(desc(vol3))%>%head(20)
# G3 購物籃分析
rx = subset(R, lhs %in% prod3$prod & lift >= 50 & count > 100 )
df = inspect(rx)
## lhs rhs support confidence coverage lift count
## [1] {4710011401128} => {4710011409056} 0.0044464169 0.3243739 0.013707692 51.03738 531
## [2] {4710011401128} => {4710011401135} 0.0058615665 0.4276115 0.013707692 54.90991 700
## [3] {4710011401128,
## 4710011401142} => {4710011409056} 0.0011974343 0.5396226 0.002219022 84.90489 143
## [4] {4710011401128,
## 4710011409056} => {4710011401142} 0.0011974343 0.2693032 0.004446417 72.59758 143
## [5] {4710011401128,
## 4710011401142} => {4710011401135} 0.0013900286 0.6264151 0.002219022 80.43843 166
## [6] {4710011401128,
## 4710011401142} => {4710011405133} 0.0012476763 0.5622642 0.002219022 71.43267 149
## [7] {4710011401128,
## 4710011401142} => {4710011406123} 0.0010132136 0.4566038 0.002219022 55.64136 121
## [8] {4710011401128,
## 4710011409056} => {4710011401135} 0.0027214416 0.6120527 0.004446417 78.59415 325
## [9] {4710011401128,
## 4710011401135} => {4710011409056} 0.0027214416 0.4642857 0.005861567 73.05129 325
## [10] {4710011401128,
## 4710011409056} => {4710011405133} 0.0022776373 0.5122411 0.004446417 65.07750 272
## [11] {4710011401128,
## 4710011405133} => {4710011409056} 0.0022776373 0.4387097 0.005191673 69.02712 272
## [12] {4710011401128,
## 4710011409056} => {4710011406123} 0.0019929326 0.4482109 0.004446417 54.61862 238
## [13] {4710011401128,
## 4710011406123} => {4710011409056} 0.0019929326 0.4103448 0.004856727 64.56416 238
## [14] {4710011401128,
## 4710011401135} => {4710011405133} 0.0028386729 0.4842857 0.005861567 61.52592 339
## [15] {4710011401128,
## 4710011405133} => {4710011401135} 0.0028386729 0.5467742 0.005191673 70.21169 339
## [16] {4710011401128,
## 4710011401135} => {4710011406123} 0.0024534843 0.4185714 0.005861567 51.00677 293
## [17] {4710011401128,
## 4710011406123} => {4710011401135} 0.0024534843 0.5051724 0.004856727 64.86957 293
## [18] {4710011401128,
## 4710011405133} => {4710011406123} 0.0022273953 0.4290323 0.005191673 52.28152 266
## [19] {4710011401128,
## 4710011406123} => {4710011405133} 0.0022273953 0.4586207 0.004856727 58.26532 266
## [20] {4710011401128,
## 4710011401142,
## 4710011409056} => {4710011401135} 0.0009462243 0.7902098 0.001197434 101.47143 113
## [21] {4710011401128,
## 4710011401135,
## 4710011401142} => {4710011409056} 0.0009462243 0.6807229 0.001390029 107.10578 113
## [22] {4710011401128,
## 4710011401135,
## 4710011409056} => {4710011401142} 0.0009462243 0.3476923 0.002721442 93.72937 113
## [23] {4710011401128,
## 4710011401135,
## 4710011409056} => {4710011405133} 0.0015742493 0.5784615 0.002721442 73.49046 188
## [24] {4710011401128,
## 4710011405133,
## 4710011409056} => {4710011401135} 0.0015742493 0.6911765 0.002277637 88.75449 188
## [25] {4710011401128,
## 4710011401135,
## 4710011405133} => {4710011409056} 0.0015742493 0.5545723 0.002838673 87.25709 188
## [26] {4710011401128,
## 4710011401135,
## 4710011409056} => {4710011406123} 0.0013397866 0.4923077 0.002721442 59.99221 160
## [27] {4710011401128,
## 4710011406123,
## 4710011409056} => {4710011401135} 0.0013397866 0.6722689 0.001992933 86.32656 160
## [28] {4710011401128,
## 4710011401135,
## 4710011406123} => {4710011409056} 0.0013397866 0.5460751 0.002453484 85.92013 160
## [29] {4710011401128,
## 4710011405133,
## 4710011409056} => {4710011406123} 0.0011639396 0.5110294 0.002277637 62.27363 139
## [30] {4710011401128,
## 4710011406123,
## 4710011409056} => {4710011405133} 0.0011639396 0.5840336 0.001992933 74.19836 139
## [31] {4710011401128,
## 4710011405133,
## 4710011406123} => {4710011409056} 0.0011639396 0.5225564 0.002227395 82.21967 139
## [32] {4710011401128,
## 4710011401135,
## 4710011405133} => {4710011406123} 0.0013565340 0.4778761 0.002838673 58.23359 162
## [33] {4710011401128,
## 4710011401135,
## 4710011406123} => {4710011405133} 0.0013565340 0.5529010 0.002453484 70.24313 162
## [34] {4710011401128,
## 4710011405133,
## 4710011406123} => {4710011401135} 0.0013565340 0.6090226 0.002227395 78.20504 162
## [35] {4710011401128,
## 4710011401135,
## 4710011405133,
## 4710011409056} => {4710011406123} 0.0008624876 0.5478723 0.001574249 66.76328 103
## [36] {4710011401128,
## 4710011401135,
## 4710011406123,
## 4710011409056} => {4710011405133} 0.0008624876 0.6437500 0.001339787 81.78501 103
## [37] {4710011401128,
## 4710011405133,
## 4710011406123,
## 4710011409056} => {4710011401135} 0.0008624876 0.7410072 0.001163940 95.15329 103
## [38] {4710011401128,
## 4710011401135,
## 4710011405133,
## 4710011406123} => {4710011409056} 0.0008624876 0.6358025 0.001356534 100.03795 103
prod4=AZ%>%filter(grp=="4")%>%group_by(prod)%>%summarise(vol4=sum(qty))%>%arrange(desc(vol4))%>%head(20)
# G4 購物籃分析
rx = subset(R, lhs %in% prod4$prod & lift >= 50 & count > 100 )
df = inspect(rx)
## lhs rhs support confidence coverage lift count
## [1] {4710011401128} => {4710011409056} 0.0044464169 0.3243739 0.013707692 51.03738 531
## [2] {4710011401128} => {4710011401135} 0.0058615665 0.4276115 0.013707692 54.90991 700
## [3] {4710011401128,
## 4710011401142} => {4710011409056} 0.0011974343 0.5396226 0.002219022 84.90489 143
## [4] {4710011401128,
## 4710011409056} => {4710011401142} 0.0011974343 0.2693032 0.004446417 72.59758 143
## [5] {4710011401128,
## 4710011401142} => {4710011401135} 0.0013900286 0.6264151 0.002219022 80.43843 166
## [6] {4710011401128,
## 4710011401142} => {4710011405133} 0.0012476763 0.5622642 0.002219022 71.43267 149
## [7] {4710011401128,
## 4710011401142} => {4710011406123} 0.0010132136 0.4566038 0.002219022 55.64136 121
## [8] {4710085120628,
## 4710085172702} => {4710085120093} 0.0012811710 0.5204082 0.002461858 69.20733 153
## [9] {4710085120093,
## 4710085120628} => {4710085172702} 0.0012811710 0.3234672 0.003960744 71.93502 153
## [10] {4710085120628,
## 4710085172702} => {4710085172696} 0.0014905126 0.6054422 0.002461858 69.52223 178
## [11] {4710085120628,
## 4710085172696} => {4710085172702} 0.0014905126 0.3423077 0.004354307 76.12490 178
## [12] {4710011401128,
## 4710011409056} => {4710011401135} 0.0027214416 0.6120527 0.004446417 78.59415 325
## [13] {4710011401128,
## 4710011401135} => {4710011409056} 0.0027214416 0.4642857 0.005861567 73.05129 325
## [14] {4710011401128,
## 4710011409056} => {4710011405133} 0.0022776373 0.5122411 0.004446417 65.07750 272
## [15] {4710011401128,
## 4710011405133} => {4710011409056} 0.0022776373 0.4387097 0.005191673 69.02712 272
## [16] {4710011401128,
## 4710011409056} => {4710011406123} 0.0019929326 0.4482109 0.004446417 54.61862 238
## [17] {4710011401128,
## 4710011406123} => {4710011409056} 0.0019929326 0.4103448 0.004856727 64.56416 238
## [18] {4710085120093,
## 4710085120628} => {4710085172696} 0.0021352850 0.5391121 0.003960744 61.90561 255
## [19] {4710085120628,
## 4710085172696} => {4710085120093} 0.0021352850 0.4903846 0.004354307 65.21460 255
## [20] {4710011401128,
## 4710011401135} => {4710011405133} 0.0028386729 0.4842857 0.005861567 61.52592 339
## [21] {4710011401128,
## 4710011405133} => {4710011401135} 0.0028386729 0.5467742 0.005191673 70.21169 339
## [22] {4710011401128,
## 4710011401135} => {4710011406123} 0.0024534843 0.4185714 0.005861567 51.00677 293
## [23] {4710011401128,
## 4710011406123} => {4710011401135} 0.0024534843 0.5051724 0.004856727 64.86957 293
## [24] {4710011401128,
## 4710011405133} => {4710011406123} 0.0022273953 0.4290323 0.005191673 52.28152 266
## [25] {4710011401128,
## 4710011406123} => {4710011405133} 0.0022273953 0.4586207 0.004856727 58.26532 266
## [26] {4710011401128,
## 4710011401142,
## 4710011409056} => {4710011401135} 0.0009462243 0.7902098 0.001197434 101.47143 113
## [27] {4710011401128,
## 4710011401135,
## 4710011401142} => {4710011409056} 0.0009462243 0.6807229 0.001390029 107.10578 113
## [28] {4710011401128,
## 4710011401135,
## 4710011409056} => {4710011401142} 0.0009462243 0.3476923 0.002721442 93.72937 113
## [29] {4710085120093,
## 4710085120628,
## 4710085172702} => {4710085172696} 0.0008792350 0.6862745 0.001281171 78.80411 105
## [30] {4710085120628,
## 4710085172696,
## 4710085172702} => {4710085120093} 0.0008792350 0.5898876 0.001490513 78.44717 105
## [31] {4710085120093,
## 4710085120628,
## 4710085172696} => {4710085172702} 0.0008792350 0.4117647 0.002135285 91.57126 105
## [32] {4710011401128,
## 4710011401135,
## 4710011409056} => {4710011405133} 0.0015742493 0.5784615 0.002721442 73.49046 188
## [33] {4710011401128,
## 4710011405133,
## 4710011409056} => {4710011401135} 0.0015742493 0.6911765 0.002277637 88.75449 188
## [34] {4710011401128,
## 4710011401135,
## 4710011405133} => {4710011409056} 0.0015742493 0.5545723 0.002838673 87.25709 188
## [35] {4710011401128,
## 4710011401135,
## 4710011409056} => {4710011406123} 0.0013397866 0.4923077 0.002721442 59.99221 160
## [36] {4710011401128,
## 4710011406123,
## 4710011409056} => {4710011401135} 0.0013397866 0.6722689 0.001992933 86.32656 160
## [37] {4710011401128,
## 4710011401135,
## 4710011406123} => {4710011409056} 0.0013397866 0.5460751 0.002453484 85.92013 160
## [38] {4710011401128,
## 4710011405133,
## 4710011409056} => {4710011406123} 0.0011639396 0.5110294 0.002277637 62.27363 139
## [39] {4710011401128,
## 4710011406123,
## 4710011409056} => {4710011405133} 0.0011639396 0.5840336 0.001992933 74.19836 139
## [40] {4710011401128,
## 4710011405133,
## 4710011406123} => {4710011409056} 0.0011639396 0.5225564 0.002227395 82.21967 139
## [41] {4710011401128,
## 4710011401135,
## 4710011405133} => {4710011406123} 0.0013565340 0.4778761 0.002838673 58.23359 162
## [42] {4710011401128,
## 4710011401135,
## 4710011406123} => {4710011405133} 0.0013565340 0.5529010 0.002453484 70.24313 162
## [43] {4710011401128,
## 4710011405133,
## 4710011406123} => {4710011401135} 0.0013565340 0.6090226 0.002227395 78.20504 162
## [44] {4710011401128,
## 4710011401135,
## 4710011405133,
## 4710011409056} => {4710011406123} 0.0008624876 0.5478723 0.001574249 66.76328 103
## [45] {4710011401128,
## 4710011401135,
## 4710011406123,
## 4710011409056} => {4710011405133} 0.0008624876 0.6437500 0.001339787 81.78501 103
## [46] {4710011401128,
## 4710011405133,
## 4710011406123,
## 4710011409056} => {4710011401135} 0.0008624876 0.7410072 0.001163940 95.15329 103
## [47] {4710011401128,
## 4710011401135,
## 4710011405133,
## 4710011406123} => {4710011409056} 0.0008624876 0.6358025 0.001356534 100.03795 103
best=inner_join(prod1, prod2, by= "prod")%>%inner_join(prod3, by ="prod")%>% inner_join(prod4, by ="prod")%>%data.frame()
# 4711271000014:鹽 4710421090059:舒跑 4710908131589:衛生紙
# 購物籃分析 : 有些商品毛利為負,推測是為了用來吸引顧客進而購買其他商品。
rx = subset(R, lhs %in% best$prod & lift >= 50 & count > 30 )
df = inspect(rx)
## lhs rhs support confidence
## [1] {4711271000014,4711524000457} => {4711524000419} 0.0002595837 0.7209302
## [2] {4711271000014,4711524000419} => {4711524000457} 0.0002595837 0.6595745
## [3] {4711271000014,4711524000419} => {4711524000396} 0.0002930783 0.7446809
## [4] {4711271000014,4711524000396} => {4711524000419} 0.0002930783 0.4666667
## [5] {4710762101063,4711271000014} => {4710762101018} 0.0002595837 0.5438596
## [6] {4710762101018,4711271000014} => {4710762101063} 0.0002595837 0.2980769
## [7] {4714981010038,4719090106016} => {4719090106009} 0.0002595837 0.6078431
## [8] {4714981010038,4719090106009} => {4719090106016} 0.0002595837 0.3647059
## [9] {4710762101018,4711271000014} => {4710762101025} 0.0004689253 0.5384615
## [10] {4710762101025,4711271000014} => {4710762101018} 0.0004689253 0.4827586
## [11] {4710085120697,4714981010038} => {4710085120680} 0.0004270570 0.7968750
## [12] {4710085120680,4714981010038} => {4710085120697} 0.0004270570 0.5100000
## [13] {4710085120093,4714981010038} => {4710085172696} 0.0002847047 0.5312500
## [14] {4710085172696,4714981010038} => {4710085120093} 0.0002847047 0.4927536
## [15] {4710011401135,4714981010038} => {4710011401128} 0.0004438043 0.7571429
## [16] {4710011406123,4714981010038} => {4710011405133} 0.0002679573 0.4102564
## coverage lift count
## [1] 0.0003600677 354.30012 31
## [2] 0.0003935623 386.11619 31
## [3] 0.0003935623 240.35480 35
## [4] 0.0006280250 229.34266 35
## [5] 0.0004772990 155.37992 31
## [6] 0.0008708613 149.56698 31
## [7] 0.0004270570 166.49047 31
## [8] 0.0007117617 140.95115 31
## [9] 0.0008708613 134.80955 56
## [10] 0.0009713453 137.92344 56
## [11] 0.0005359147 103.10337 51
## [12] 0.0008373666 114.05472 51
## [13] 0.0005359147 61.00282 34
## [14] 0.0005777830 65.52965 34
## [15] 0.0005861567 55.23489 53
## [16] 0.0006531460 52.12089 32
prods=Z0 %>% group_by(prod) %>% summarise(
totalRev=sum(price),
totalQty=sum(qty),
avgPrice=totalRev/totalQty,
gross=sum(price)-sum(cost),
grossmargin=gross/totalRev)
## prod vol1 vol2 vol3 vol4 totalRev totalQty avgPrice gross
## 1 4714981010038 8709 1136 3637 1054 246747 14537 16.973722 -131002
## 2 4711271000014 5682 1875 1921 1136 102397 10615 9.646444 -46213
## 3 4710421090059 3911 1408 1241 674 85181 7235 11.773462 -1639
## 4 4711663700010 2020 400 818 482 33397 3720 8.977688 3637
## 5 4710908131589 1200 364 521 489 259397 2576 100.697593 -1020
## grossmargin
## 1 -0.530916283
## 2 -0.451312050
## 3 -0.019241380
## 4 0.108901997
## 5 -0.003932197
I1:增加黏著度,分級會員,一加入就享優惠以吸引入會員
I2:feng panda新客免運額度,創造接觸機會
I3:line官方帳號廣告推播,即時得知優惠活動期間,吸引來店
# G1 行銷成效
# 不同參數組合比較
mm=c(0.14,0.2,0.12) #最高點
bb=c(40,60,40) #轉折點
aa=c(60,80,40) #寬度
X = seq(10, 250, 1)
df = do.call(rbind, lapply(1:3, function(i) {
sapply(X, function(x) {
# eR = (min(B$Buy+DP(x,mm[i],bb[i],aa[i]),1) - B$Buy) * B$Rev - x
dp = DP(x,mm[i],bb[i],aa[i])
dp = ifelse(B$Buy+dp>1, 1-B$Buy, dp)
eR = dp*B$Rev - x*(B$Buy+dp)
c(i=i,x=x, eR.ALL=sum(eR[B$grp==1]), N=sum(eR[B$grp==1] > 0), eR.SEL=sum(eR[eR[B$grp==1] > 0]))
}) %>% t %>% data.frame
}))
df %>% gather('key','value',-i,-x) %>%
mutate(Instrument = paste0('I',i)) %>%
ggplot(aes(x=x, y=value, col=Instrument)) +
geom_hline(yintercept=0, linetype='dashed', col='blue') +
geom_line(size=1.5,alpha=0.5) +
xlab('工具選項(成本)') + ylab('預期報償') +
ggtitle('行銷工具優化','假設行銷工具的效果是其成本的函數') +
facet_wrap(~key,ncol=1,scales='free_y') + theme_bw()
## # A tibble: 3 x 5
## # Groups: i [3]
## i x eR.ALL N eR.SEL
## <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 1 59 1286879. 17640 2238574.
## 2 2 85 1689293. 17242 2903809.
## 3 3 54 1107450. 17602 1930771.
## # A tibble: 3 x 5
## # Groups: i [3]
## i x eR.ALL N eR.SEL
## <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 1 58 1285575. 17673 2238627.
## 2 2 84 1689094. 17275 2905635.
## 3 3 54 1107450. 17602 1930771.
I1:體驗行銷,平日試吃,增加購買機率、銷量,成本可轉嫁廠商
I2:週間挑一天設成含飴弄孫日,帶孫子一起購物享折扣(小孩什麼都想要,爺奶又疼孫)
# G3 行銷成效
mm=c(0.14,0.18) #最高點
bb=c(40,40) #轉折點
aa=c(80,50) #寬度
X = seq(10, 250, 1)
df = do.call(rbind, lapply(1:2, function(i) {
sapply(X, function(x) {
# eR = (min(B$Buy+DP(x,mm[i],bb[i],aa[i]),1) - B$Buy) * B$Rev - x
dp = DP(x,mm[i],bb[i],aa[i])
dp = ifelse(B$Buy+dp>1, 1-B$Buy, dp)
eR = dp*B$Rev - x*(B$Buy+dp)
c(i=i,x=x, eR.ALL=sum(eR[B$grp==3]), N=sum(eR[B$grp==3] > 0), eR.SEL=sum(eR[eR[B$grp==3] > 0]))
}) %>% t %>% data.frame
}))
df %>% gather('key','value',-i,-x) %>%
mutate(Instrument = paste0('I',i)) %>%
ggplot(aes(x=x, y=value, col=Instrument)) +
geom_hline(yintercept=0, linetype='dashed', col='blue') +
geom_line(size=1.5,alpha=0.5) +
xlab('工具選項(成本)') + ylab('預期報償') +
ggtitle('行銷工具優化','假設行銷工具的效果是其成本的函數') +
facet_wrap(~key,ncol=1,scales='free_y') + theme_bw()
## # A tibble: 2 x 5
## # Groups: i [2]
## i x eR.ALL N eR.SEL
## <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 1 43 14476. 854 763394.
## 2 2 42 22757. 872 1100286.
## # A tibble: 2 x 5
## # Groups: i [2]
## i x eR.ALL N eR.SEL
## <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 1 53 6863. 730 933505.
## 2 2 51 15001. 758 1460471.
I1:週間設一天家庭自煮日,食材不定優惠
I2:平日晚上友善時光,打折吸引客人+清即期品
I3:line官方帳號廣告推播,即時得知優惠活動期間,吸引來店
# G4 行銷成效
mm=c(0.14,0.18,0.12) #最高點
bb=c(40,50,40) #轉折點
aa=c(60,80,50) #寬度
X = seq(10, 250, 1)
df = do.call(rbind, lapply(1:3, function(i) {
sapply(X, function(x) {
# eR = (min(B$Buy+DP(x,mm[i],bb[i],aa[i]),1) - B$Buy) * B$Rev - x
dp = DP(x,mm[i],bb[i],aa[i])
dp = ifelse(B$Buy+dp>1, 1-B$Buy, dp)
eR = dp*B$Rev - x*(B$Buy+dp)
c(i=i,x=x, eR.ALL=sum(eR[B$grp==4]), N=sum(eR[B$grp==4] > 0), eR.SEL=sum(eR[eR[B$grp==4] > 0]))
}) %>% t %>% data.frame
}))
df %>% gather('key','value',-i,-x) %>%
mutate(Instrument = paste0('I',i)) %>%
ggplot(aes(x=x, y=value, col=Instrument)) +
geom_hline(yintercept=0, linetype='dashed', col='blue') +
geom_line(size=1.5,alpha=0.5) +
xlab('工具選項(成本)') + ylab('預期報償') +
ggtitle('行銷工具優化','假設行銷工具的效果是其成本的函數') +
facet_wrap(~key,ncol=1,scales='free_y') + theme_bw()
## # A tibble: 2 x 5
## # Groups: i [2]
## i x eR.ALL N eR.SEL
## <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 1 66 758265. 3885 2306841.
## 2 2 83 961116. 3884 2843353.
## # A tibble: 1 x 5
## # Groups: i [1]
## i x eR.ALL N eR.SEL
## <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 3 57 639461. 3885 1966887.