数据来源:
http://archive.ics.uci.edu/ml/datasets/Bank+Marketing
数据预处理:
导入数据:
getwd()
setwd("d:/Rdata/bank")
getwd()
bankdata<-read.csv("bank-additional-full.csv",sep=";")
sum(is.na(bankdata))
查看原始数据状态
summary(bankdata)
用众数插补
table(bankdata=="unknown")
max(table(bankdata$job))
max(table(bankdata$education))
bankdata$marital[which(bankdata$marital=="unknown")]<-"married"
bankdata$default[which(bankdata$default=="unknown")]<-"no"
bankdata$housing[which(bankdata$housing=="unknown")]<-"yes"
bankdata$loan[which(bankdata$loan=="unknown")]<-"no"
bankdata$education[which(bankdata$education=="unknown")]<-"university.degree"
bankdata$job[which(bankdata$job=="unknown")]<-"admin."
1对age进行重编码
attach(bankdata)
lab<-c("younge","wrinkly","elder")
bankdata$age_cat=cut(age,breaks = c(0,35,55,100),right = FALSE,labels = lab)
table(bankdata$age_cat)
2对job进行重编码
c1<-c("admin.","blue-collar","entrepreneur")
c2<-c("housemaid","management","self-employed","services","technician")
c3<-c("retired","student","unemployed")
bankdata<-within(bankdata,{
job_cat<-NA
job_cat[job %in% c1]<-"high-income"
job_cat[job %in% c2]<-"middle-income"
job_cat[job %in% c3]<-"low-income"
})
table(bankdata$job_cat)
3对education进行重编码
c4<-c("basic.4y","basic.6y","basic.9y")
c5<-c("high.school","professional.course","university.degree")
bankdata<-within(bankdata,{
edu_cat<-NA
edu_cat[education %in% "illiterate"]<-"high-income"
edu_cat[education %in% c4]<-"middle-income"
edu_cat[education %in% c5]<-"low-income"
})
table(bankdata$edu_cat)
4对month进行重编码
c6<-c("jan","feb","mar")
c7<-c("apr","may","jun")
c8<-c("jul","aug","sep")
c9<-c("oct","nov","dec")
bankdata<-within(bankdata,{
mon_cat<-NA
mon_cat[month %in% c6]<-"q1"
mon_cat[month %in% c7]<-"q2"
mon_cat[month %in% c8]<-"q3"
mon_cat[month %in% c9]<-"q4"
})
table(bankdata$mon_cat)
5查看pdays的数据情况并对pdays进行重编码
table(bankdata$pdays)
bankdata<-within(bankdata,{
pdays_cat<-NA
pdays_cat[pdays<28]<-"long"
pdays_cat[pdays<8]<-"short"
pdays_cat[pdays==999]<-"never"
})
detach(bankdata)
将字符型的字段改为因子型的
bankdata$job_cat<-factor(bankdata$job_cat)
bankdata$edu_cat<-factor(bankdata$edu_cat)
bankdata$mon_cat<-factor(bankdata$mon_cat)
bankdata$pdays_cat<-factor(bankdata$pdays_cat)
查看数据状态
summary(bankdata)
进行标准正态化
tosacle<-function(x){
return (scale(x,center = TRUE,scale=TRUE))
}
bankdata$camp<-scale(bankdata$campaign,center = TRUE,scale=TRUE)
bankdata$pre<-scale(bankdata$previous,center = TRUE,scale=TRUE)
bankdata[,16:20]<-apply(bankdata[,16:20], 2, tosacle)
筛选字段形成新的数据框,用于相关性分析
attach(bankdata)
c10<-c("age_cat","job_cat","marital","edu_cat","default",
"housing","loan,contact","mon_cat","day_of_week","camp",
"pdays_cat","pre","poutcome",
"emp.var.rate","cons.price.idx",
"cons.conf.idx","euribor3m","nr.employed","y")
newdata<-temp[which(names(temp)%in%c10)]
newdata<-bankdata[which(names(bankdata)%in%c10)]
detach(bankdata)
summary(newdata)
str(newdata[,1:18])
相关性分析
将数据集中的每个列的相关系数统计出来并保存在一个corr的参数中
newtmp<-data.frame(newdata[,6:10],newdata[,17:18])
corr <- cor(newtmp)
install.packages("corrplot",dependencies = T)
corrplot(corr)
选取变量
c11<-c("euribor3m","nr.employed","emp.var.rate")
tmp<-names(newdata)%in%c11
newdata2<-newdata[,!tmp]
summary(newdata2)
==========================================================
建立模型:
数据分区,按照变量accept变量进行等比抽样,80%为训练集,20%为测试集
library(caret)
ind <- createDataPartition(newdata2$y,times=1,p=0.8,list=F)
train <- newdata2[ind,] # 训练集
test <- newdata2[-ind,] # 测试集
prop.table(table(newdata2$y))
prop.table(table(train$y))
prop.table(table(test$y))
构建分类模型cart
library(rpart)
mod <- rpart(train$y~.,data=train)
对测试集数据进行预测
pred <- predict(mod,test,type="class")
pred
构建混淆矩阵,查看预测效果
查看训练集的误差率
(a <- table(train$y,predict(mod,train,type="class")))
paste0(round((sum(a)-sum(diag(a)))/sum(a),4)*100,"%")
查看测试集的误差率
(b <- table(test$y,predict(mod,test,type="class")))
paste0(round((sum(b)-sum(diag(b)))/sum(b),4)*100,"%")