项目背景:应对双十一促销,对目标用户精细划分
项目目标:利用精准营销提高转化,业绩体现为高转化率和低费用率
CRM用户购买或者搜索RFMTK数据,RFM详解见URL
library(sampling)
#计算应抽取测试样本数
a=round(1/4*sum(data[,D]=="0"))
b=round(1/4*sum(data[,D]=="1"))
sub=strata(data,stratanames="isBuy",size=c(a,b),method="srswor")
Train=data[-sub$ID_unit,]
Test=data[sub$ID_unit,]
nrow(Train);nrow(Test)
# 人工合成数据
library(ROSE)
Train_rose <- ROSE(formula, data = Train, N=1000, seed = 1)$data
table(Train_rose$isBuy)
names(data)
str(data)
summary(data)
library(Hmisc)
describe(data)
library(fBasics)
basicStats(data$R_visit)
#离散型(city_tier,R,F,...)
tbcity_tier <- with(data,table(city_tier))
tbcity_tier
barplot(tbcity_tier,xlab="Improvement",ylab="Frequency")
#连续型(regTime,birthday,...)
par(mfrow=c(2,2))
h <- hist(data$birthday,breaks=12,freq=FALSE,density=20)
# 离散型(T_weekday)
# a <- c(data$T_weekday)
# n=length(a)
# la=rep(0,n)
# la[which(a>0.5)]=1
# 连续型(regTime,birthday)
data$regTime <- scale(data$regTime)
data$birthday <- scale(data$birthday)
# 协方差矩阵
cov <- cov(data[,2:20])
cov
# 相关系数矩阵
cor <- cor(data[,2:21])
cor
使用多种分类方法,并给每种方法写入M、A、P、R、MSE,进行比较取适合模型。
# 选取最大值
c <- c(resultRP$P, resultSVM$P, resultKSVM$P, resultRF$P)
selectMaxFunc = function(c) {
temp <- c[1]
t = 1
for (i in 1:length(c)) {
if (temp >= c[i])
temp
else{
t = i
temp <- c[i]
}
}
print(t)
print(temp)
}
compareFunc(c)
# 选取最小值
c <- c(resultRP$MSE, resultSVM$MSE, resultKSVM$MSE, resultRF$MSE)
selectMinFunc = function(c) {
temp <- c[1]
t = 1
for (i in 1:length(c)) {
if (temp <= c[i])
temp
else{
t = i
temp <- c[i]
}
}
print(t)
print(temp)
}
compareFunc(c)
对预测数据集进行处理,使用训练模型预测数据,并写入数据库。
# 先删后插,提高数据库更新速度
sqlQuery(channel,
"TRUNCATE TABLE result_CRM_buy_201611")
sqlSave(channel,
cfPred,
"result_CRM_buy_201611",
rownames = FALSE,
append = TRUE)
利用a1/b1=a2/b2,在训练数据集上,返回训练数据集平衡前概率
##原始概率
probnull=table(data$isBuy)[1]/sum(table(data$isBuy))
probnull
library(ROSE)
data <- ROSE(formula,
data = data,
N = dim(data)[1]*0.8,
seed = 1)$data
table(data$isBuy)
library("CHAID")
set.seed(290875)
ctrl <- chaid_control(minsplit = 1 , minprob = 0.2,maxheight = 4)
chaidUS <- chaid(formulaModel, data = data,control=ctrl )
print(chaidUS)
plot(chaidUS)
prob=table(data$isBuy)[1]/sum(table(data$isBuy))