options(scipen=10)
::p_load(latex2exp,Matrix,dplyr,tidyr,ggplot2,caTools,plotly)
pacmanrm(list=ls(all=TRUE))
load("data/tf4.rdata")
B$Buy
: 預期再購機率 Re-Purchase ProbabilityB$Rev
: 預期購買金額 Expected Revenue Contributionpar(mfrow=c(1,2), cex=0.8)
hist(B$Buy)
hist(log(B$Rev,10))
group_by(B,age) %>%
summarise(n=n(), Buy=mean(Buy), Rev=mean(Rev)) %>%
ggplot(aes(Buy,Rev,size=n,label=age)) +
geom_point(alpha=0.5,color='gold') +
geom_text(size=4) +
labs(title="Age Group Comparison (size: no. customers)") +
xlab("Avg. Buying Probability") + ylab("Avg. Expected Revenue") +
scale_size(range=c(4,20)) + theme_bw() -> p
ggplotly(p)
🌻
🌻 我們可以用R內建的邏輯式函數(plogis()
)來模擬S曲線
\[\Delta P(x|m,b,a) = m \cdot
Logis(\frac{10(x - b)}{a})\]
= function(x,m0,b0,a0) {m0*plogis((10/a0)*(x-b0))}
DP par(mar=c(4,4,2,1),cex=0.7)
curve(DP(x,m=0.20,b=30,a=40), 0, 60, lwd=2, ylim=c(0, 0.25),
main="F( x | m=0.2, b=30, a=40 )", ylab="delta P")
abline(h=seq(0,0.2,0.05),v=seq(0,60,5),col='lightgrey',lty=2)
🌻
🌻 透過這3個
m
: 最大效果b
: 效果的位置(上升波段的中點)a
: 效果的範圍(上升波段的寬度)我們可以寫『一支程式』來模擬『所有可能』的
藉以描述
有了行銷工具的成本效益函數之後,我們就可以估計將這個工具用在每一位顧客上的時候的預期效益:
\[\hat{R}(x) = \left\{\begin{matrix} \Delta P \cdot M \cdot margin - x & , & P + \Delta P \leq 1\\ (1-P) \cdot M \cdot margin - x & , & else \end{matrix}\right.\]
🌻 結合 …
我們就可以估計這個工具用在每位顧客上的預期效益 \(\hat{R}(x)\)。
🌻 Note that both \(\Delta P\) and \(\hat{R}\) are functions of \(x\) given \(m,b,a\)
估計毛利率 \(m\)
# load(data/tf0.rdata)
# group_by(Z0, age) %>% summarise(sum(price)/sum(cost) - 1)
= 0.17 # assume margin = 0.17 margin
估計每位顧客的淨收益 \(\hat{R}(x)\)
=0.2; b=25; a=40; x=30
m= pmin(1-B$Buy, DP(x,m,b,a))
dp = dp*B$Rev*margin - x
eR hist(eR,main="預期淨收益分佈",xlab="預期淨收益",ylab="顧客人數")
根據以上的分析結果 …
🚴 有多少顧客的淨預期報償大於零? (eR > 0
)?
sum(eR>0)
## [1] 6679
🚴 如果我們針對所有顧客做促銷,預期報償將是?
sum(eR)
## [1] -202435.4
🚴 如果我們針對預期報償大於零的顧客做促銷,預期報償將是?
sum(eR[ eR>0 ])
## [1] 80358.8
🚴 如果我們只針對預期報償大於10的顧客做促銷,預期報償將是?
sum(eR[ eR>10 ])
## [1] 63812.07
🚴
如果我們只針對預期報償大於10的南港(z115
)顧客做促銷,預期報償將是?
sum(eR[ eR>10 & B$area=="z115" ])
## [1] 12532.01
給定工具參數(\(m,b,a\)),我們可在其有效成本範圍(\(x \in [b-\frac{a}{2}, b+\frac{a}{2}]\))之內,估計工具的效果:
eReturn
: 對所有的人行銷的總預期收益N
: 預期收益大於零的人數eReturn2
: 只對期收益大於零的人做行銷的總預期收益如何隨成本變化。
=0.2; b=25; a=40; X = seq(10,45,1)
m
= sapply(X, function(x) {
df = pmin(DP(x,m,b,a),1-B$Buy)
dp = dp*B$Rev*margin - x
eR c(x=x, eReturn=sum(eR), N=sum(eR > 0), eReturn2=sum(eR[eR > 0]))
%>% t %>% data.frame
})
%>% gather('key','value',-x) %>%
df ggplot(aes(x=x, y=value, col=key)) +
geom_hline(yintercept=0,linetype='dashed') +
geom_line(size=1.5,alpha=0.5) +
facet_wrap(~key,ncol=1,scales='free_y') + theme_bw()
## Warning: Using `size` aesthetic for lines was deprecated in ggplot2 3.4.0.
## i Please use `linewidth` instead.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.
稍微改一下程式,我們可以同時模擬多(4)個行銷工具,並比較他們的成本效益 With some modification of the code, we can define multiple (4) instruments
=c(0.20, 0.25, 0.15, 0.25)
mm=c( 25, 30, 15, 30)
bb=c( 40, 40, 30, 60)
aa= seq(0,60,2)
X do.call(rbind, lapply(1:length(mm), function(i) data.frame(
Inst=paste0('Inst',i), Cost=X,
Gain=DP(X,mm[i],bb[i],aa[i])
%>% data.frame %>%
))) ggplot(aes(x=Cost, y=Gain, col=Inst)) +
geom_line(size=1.5,alpha=0.5) + theme_bw() +
ggtitle("Prob. Function: f(x|m,b,a)")
and run simulation on multiple instrument to compare their cost effectiveness.
= seq(10, 60, 1)
X = do.call(rbind, lapply(1:length(mm), function(i) {
df sapply(X, function(x) {
= pmin(1-B$Buy, DP(x,mm[i],bb[i],aa[i]))
dp = dp*B$Rev*margin - x
eR c(i=i, x=x, eR.ALL=sum(eR), N=sum(eR>0), eR.SEL=sum(eR[eR > 0]) )
%>% t %>% data.frame
})
}))
%>%
df mutate_at(vars(eR.ALL, eR.SEL), function(y) round(y/1000)) %>%
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('預期收益($K)') +
ggtitle('行銷工具優化','假設行銷工具的效果是其成本的函數') +
facet_wrap(~key,ncol=1,scales='free_y') + theme_bw() -> p
::ggplotly(p) plotly
group_by(df, i) %>% top_n(1,eR.SEL)
## # A tibble: 4 x 5
## # Groups: i [4]
## i x eR.ALL N eR.SEL
## <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 1 34 -215958. 7027 98033.
## 2 2 40 -194886. 8344 146568.
## 3 3 22 -49192. 10262 108802.
## 4 4 43 -305864. 6495 112184.
🚴 從模擬的結果我們可以很容易看出每一個工具的:
先看一下啊每一個年齡族群的顧客人數 …
par(cex=0.7, mar=c(2,2,1,2))
table(B$age) %>% barplot
🚴 討論:
如果上述4組工具參數分別是某折價券對4個不同年齡族群的效果:
■
I1 : a24, a29
■ I2 : a34, a39
■
I3 : a44, a49
■
I4 : a54, a59, a64, a69
如果你可以在這4個年齡族群之中選擇行銷對象,你應該如何:
■
選擇行銷對象(N
)?
■
設定折價券的面額(x
)?
■
估計預期報償(eR.SEL
)?
參考程式
= lapply( # B's index for the 4 age groups
cidx list(c("a24","a29"),c("a34","a39"),
c("a44","a49"),c("a54","a59","a64","a69")),
function(v) B$age %in% v)
= seq(10, 60, 1)
X = do.call(rbind, lapply(1:length(mm), function(i) {
df sapply(X, function(x) {
= pmin(1- B$Buy[ cidx[[i]] ] , DP(x,mm[i],bb[i],aa[i]))
dp = dp * B$Rev[ cidx[[i]] ] * margin - x
eR c(i=i, x=x, eR.ALL=sum(eR), N=sum(eR>0), eR.SEL=sum(eR[eR > 0]) )
%>% t %>% data.frame
})
}))
group_by(df, i) %>% top_n(1,eR.SEL)
## # A tibble: 4 x 5
## # Groups: i [4]
## i x eR.ALL N eR.SEL
## <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 1 34 -51966. 560 6472.
## 2 2 40 -29651. 4083 74282.
## 3 3 22 -4068. 3131 34746.
## 4 4 43 -84668. 643 9403.