# 添加相关的包
load('/Users/milin/COD\ 建模/markdown.RData')
library(scorecard)
library(tidyverse)
library(DT) # For Data Tables
library(lattice) # The lattice add-on of Trellis graphics for R
library(knitr) # For Dynamic Report Generation in R
library(gplots) # Various R Programming Tools for Plotting Data
library(ggplot2) # An Implementation of the Grammar of Graphics
library(ClustOfVar) # Clustering of variables
library(ape) # Analyses of Phylogenetics and Evolution (as.phylo)
library(Information) # Data Exploration with Information Theory (Weight-of-Evidence and Information Value)
library(ROCR) # Model Performance and ROC curve
library(caret) # Classification and Regression Training - for any machine learning algorithms
library(rpart) # Recursive partitioning for classification, regression and survival trees
library(rpart.utils) # Tools for parsing and manipulating rpart objects, including generating machine readable rules
library(rpart.plot) # Plot 'rpart' Models: An Enhanced Version of 'plot.rpart'
library(randomForest)# Leo Breiman and Cutler's Random Forests for Classification and Regression
library(party) # A computational toolbox for recursive partitioning - Conditional inference Trees
library(bnlearn) # Bayesian Network Structure Learning, Parameter Learning and Inference
library(DAAG) # Data Analysis and Graphics Data and Functions
library(vcd) # Visualizing Categorical Data
library(kernlab) # Support Vector Machine
# Following libraries we have load for model 8 and model 9
#library(neuralnet) # Neural Network
#library(lars) # For Least Angle Regression, Lasso and Forward Stagewise
#library(glmnet) # Lasso and Elastic-Net Regularized Generalized Linear Models
# 自定义函数
# Function 1: Create function to calculate percent distribution for factors
pct <- function(x){
tbl <- table(x)
tbl_pct <- cbind(tbl,round(prop.table(tbl)*100,2))
colnames(tbl_pct) <- c('Count','Percentage')
kable(tbl_pct)
}
# pct(cdata$good_bad_21)
# Function 2: Own function to calculate IV, WOE and Eefficiency
gbpct <- function(x, y=Model_data$label){
mt <- as.matrix(table(as.factor(x), as.factor(y))) # x -> independent variable(vector), y->dependent variable(vector)
Total <- mt[,1] + mt[,2] # Total observations
Total_Pct <- round(Total/sum(mt)*100, 2) # Total PCT
Bad_pct <- round((mt[,1]/sum(mt[,1]))*100, 2) # PCT of BAd or event or response
Good_pct <- round((mt[,2]/sum(mt[,2]))*100, 2) # PCT of Good or non-event
Bad_Rate <- round((mt[,1]/(mt[,1]+mt[,2]))*100, 2) # Bad rate or response rate
grp_score <- round((Good_pct/(Good_pct + Bad_pct))*10, 2) # score for each group
WOE <- round(log(Good_pct/Bad_pct)*10, 2) # Weight of Evidence for each group
g_b_comp <- ifelse(mt[,1] == mt[,2], 0, 1)
IV <- ifelse(g_b_comp == 0, 0, (Good_pct - Bad_pct)*(WOE/10)) # Information value for each group
Efficiency <- abs(Good_pct - Bad_pct)/2 # Efficiency for each group
otb<-as.data.frame(cbind(mt, Good_pct, Bad_pct, Total,
Total_Pct, Bad_Rate, grp_score,
WOE, IV, Efficiency ))
otb$Names <- rownames(otb)
rownames(otb) <- NULL
otb[,c(12,2,1,3:11)] # return IV table
}
# Function 3: Normalize using Range
normalize <- function(x) {
return((x - min(x)) / (max(x) - min(x)))
}
模型具体的代码如下:
R Markdown
货到付款是一种是一种非常受用户青睐的支付方式,对于客户而言,货到付款更加安全,特别是对于一些电商不发达的的确,货到付款能够有效的打消用户对于网购的不信任。
对于商家而言,货到付款这种支付方式不利于现金的流动,并且,有一部分人会在货到了之后不付款,也就是拒收。拒收的原因很多,很简单的就是不想要了。
一般而言,货到付款的拒收率可以高达20%,这将造成很大的运营成本。因此,本文利用机器学习的方法,对用户是否回拒收进行预测。
1 介绍
1.1 初步了解使用到的数据
1.1.2 查看数据的结构
1.1.3 查看数据的基本信息
2 初步的数据分析
2.1 修改数据的类型
2.2 查看标签的比例
3 单变量分析
WOE(Weight of Evidence):WOE显示了自变量对因变量的预测能力
WOE=ln(Distribution of Non-Events(Good)Distribution of Events(Bad))
其通过更基本的比率计算而来:
(Distribution of Good Credit Outcomes) / (Distribution of Bad Credit Outcomes)
Information Value(IV):
信息值有利于通过变量的重要性进行筛选变量
IV=∑(%Non-Events - %Events)∗WOE
Efficiency:
Efficiency=Abs(%Non-Events - %Events)/2
3.1 发货方式
3.2 州
3.3 用户性别
3.4 用户设备
3.5 操作系统版本
3.6 用户类别
3.7 地址类型
3.8 下单时间(小时)
3.9 付款时间(小时)
计算信息值(Information Value) 和 WOE (Weight of Evidence)
4 选取进行建模的变量
5 多元数据分析 - 聚类,降维
对变量的聚类可以讲含有相同信息的变量聚为同一个族类
当我们有大量的变量的时候,这种方法可以很好的用于进行降维。同样可以用于降维的方法还有主成分分析和因子分析。
6 变量的层次聚类
7 通过聚类选取部分变量
8 划分选来集合测试集合
9 训练模型以及模型选择
9.1 逻辑回归以及逐步回归
9.2 随即森林
不平衡的数据会造成非常低AUC,需要尝试解决样本不平衡的问题
欠抽样
重抽样
附录 A: 使用到的包
A.1 library(lattice)
The lattice add-on of Trellis graphics for R. Ref:https://cran.r-project.org/web/packages/lattice/lattice.pdf
A.2 library(knitr)
For Dynamic Report Generation in R. Ref: https://cran.r-project.org/web/packages/knitr/knitr.pdf
A.3 library(gplots)
Various R Programming Tools for Plotting Data. Ref:https://cran.r-project.org/web/packages/gplots/gplots.pdf
A.4 library(ggplot2)
An Implementation of the Grammar of Graphics. Ref: https://cran.rstudio.com/web/packages/ggplot2/ggplot2.pdf
A.5 library(ClustOfVar)
Clustering of variables. Ref: https://cran.r-project.org/web/packages/ClustOfVar/ClustOfVar.pdf
A.6 library(ape)
Analyses of Phylogenetics and Evolution (as.phylo). Ref: https://cran.r-project.org/web/packages/ape/ape.pdf
A.7 library(Information)
Data Exploration with Information Theory (Weight-of-Evidence and Information Value). Ref: https://cran.r-project.org/web/packages/Information/Information.pdf
A.8 library(ROCR)
Visualizing the Performance of Scoring Classifiers. Ref: https://cran.r-project.org/web/packages/ROCR/ROCR.pdf
A.9 library(caret)
Classification and Regression Training - for any machine learning algorithms. Ref: ftp://cran.r-project.org/pub/R/web/packages/caret/caret.pdf
A.10 library(rpart)
Recursive partitioning for classification, regression and survival trees. Ref: https://cran.r-project.org/web/packages/rpart/rpart.pdf
A.10.1 library(rpart.utils)
Tools for parsing and manipulating rpart objects, including generating machine readable rules. Ref: https://cran.r-project.org/web/packages/rpart.utils/rpart.utils.pdf
A.10.2 library(rpart.plot)
Plot ‘rpart’ Models: An Enhanced Version of ‘plot.rpart’. Ref: https://cran.r-project.org/web/packages/knitr/knitr.pdf
A.11 library(randomForest)
Leo Breiman and Cutler’s Random Forests for Classification and Regression. Ref: https://cran.r-project.org/web/packages/randomForest/randomForest.pdf
A.12 library(party)
A computational toolbox for recursive partitioning - Conditional inference Trees. Ref: https://cran.r-project.org/web/packages/party/party.pdf
A.13 library(bnlearn)
Bayesian Network Structure Learning, Parameter Learning and Inference. Ref: https://cran.r-project.org/web/packages/bnlearn/bnlearn.pdf
A.14 library(DAAG)
Data Analysis and Graphics Data and Functions. Ref: https://cran.r-project.org/web/packages/DAAG/DAAG.pdf
A.15 library(vcd)
Visualizing Categorical Data. Ref: https://cran.r-project.org/web/packages/vcd/vcd.pdf
A.16 library(neuralnet)
Neural Network implementation. Ref: https://cran.r-project.org/web/packages/neuralnet/neuralnet.pdf
A.17 library(kernlab)
Kernel-Based Machine Learning Lab. Ref: https://cran.r-project.org/web/packages/kernlab/kernlab.pdf
A.18 library(glmnet)
Lasso and Elastic-Net Regularized Generalized Linear Models. Ref: https://cran.r-project.org/web/packages/glmnet/glmnet.pdf
A.19 library(lars)
Least Angle Regression, Lasso and Forward Stagewise. Ref: ftp://cran.r-project.org/pub/R/web/packages/lars/lars.pdf
参考
WOE变换与信息值