样本内模型拟合与阈值设定

用 R 进行市场营销分析的机器学习

Verena Pflieger

Data Scientist at INWT Statistics

伪 R^2 统计 I

$$ \text{McFadden:~} \quad R^2 =1-\frac{lnL_{\text{full}}}{lnL_{\text{null}}} $$

$$ \text{Cox \& Snell:~} R^2 = 1-\left(\frac{L_{\text{null}}}{L_{\text{full}}}\right)^{\frac{2}{n}} $$

$$ \text{Nagelkerke:~} R^2 = \frac{1-\left(\frac{L_{\text{null}}}{L_{\text{full}}}\right)^{\frac{2}{n}}}{1-(L_{\text{null}})^{\frac{2}{n}}} $$

解释:

  • 合理:> 0.2

  • 良好:> 0.4

  • 很好:> 0.5

用 R 进行市场营销分析的机器学习

伪 R^2 统计 II

library(descr)

LogRegR2(logitModelNew)
Chi2                     1321.717
Df                       19
Sig.                     0
Cox and Snell Index      0.02879553
Nagelkerke Index         0.0469131
McFadden's R2            0.03071032
用 R 进行市场营销分析的机器学习

预测概率

churnData$predNew <- predict(logitModelNew, 
                             type = "response", 
                              na.action = na.exclude)
data %>% select(returnCustomer, predNew) %>% tail()      
      returnCustomer   predNew
45231              0 0.2843944
45232              0 0.1552756
45233              1 0.2522597
45234              1 0.1454276
45235              0 0.2698819
45236              0 0.2886988
用 R 进行市场营销分析的机器学习

混淆矩阵

library(SDMTools)
# Note that `SDMTools` cannot be downloaded from CRAN anymore. 
# Install it instead via `remotes::install_version("SDMTools", "1.1-221.2")`
confMatrixNew <- confusion.matrix(churnData$returnCustomer, 
                    churnData$predNew, threshold = 0.5)
confMatrixNew
     obs
pred 0     1
   0 36921 8242
   1 43    30
预测 \ 真实 负类 正类
负类 真负 假负
正类 假正 真正
用 R 进行市场营销分析的机器学习

准确率

accuracyNew <- sum(diag(confMatrixNew)) / sum(confMatrixNew)
accuracyNew
0.8168494
用 R 进行市场营销分析的机器学习

寻找最优阈值

预测 \ 真实 returnCustomer = 0 returnCustomer = 1
returnCustomer = 0 5 -15
returnCustomer = 1 0 0

payoff = 5 * 真负 - 15 * 假负

阈值 准确率 回报
0.5 0.817 60975
0.4 0.815 62180
[0.3] [0.794] [65740]
0.2 0.668 65670
0.1 0.241 10550
用 R 进行市场营销分析的机器学习

过拟合

用 R 进行市场营销分析的机器学习

Let's try it out!

用 R 进行市场营销分析的机器学习

Preparing Video For Download...