[Rscript]逻辑回归识别学生群体的R实现

library(ggplot2)
library(pROC)

###读取数据并查看数据情况
setwd("D:\\student_recognition")
student<-read.csv("student.csv")
str(student) #查看变量
apply(student,2,function(x){
  mean(x=='NULL'|x=="")
})  #查看缺失值情况

###数据预处理
#略

###描述性分析
#绘制箱线图,可见https://blog.csdn.net/TOMOCAT/article/details/80559006
#分组查看变量
library(plyr) #可进行类似数据透视表的操作,将数据分割成更小的数据,对分割后的数据进行操作
ddply(student[,c("is_student","student_app")],c("is_student"),summarise,mean_app=mean(student_app))
##   whether_student  mean_app
## 1               0 0.1745353
## 2               1 0.4435253

###逻辑回归建模
model_student<-student[,c("is_student",cont_var,class_var)]
#这里cont_var是经过数据清洗和预处理之后的用于建模的连续变量,class_var是分类变量
#计算建模的平均auc
auc<-NULL
for(i in 1:5){
  ind<-sample(1:dim(model_student)[1],round(dim(model_student)[1]*0.3)) #对数据集按照记录数划分成训练集和测试集
  train<-model_student[-ind,]
  test<-model_student[ind,]
  
  for (var in cont_var){
    train[,var]<-(train[,var]-mean(train[,var]))/sd(train[,var])
    test[,var]<-(test[,var]-mean(test[,var]))/sd(test[,var])
  } #连续变量标准化
  
  for (var in class_var){
    train[,var]<-as.factor(train[,var])
    test[,var]<-as.factor(test[,var])
  } #离散变量转化为因子型
  
  lm_res<-glm(is_student~.,data=train,family="binomial")
  pre<-predict(lm_res,test,type="reponse")
  auc0<-auc(test$is_student,pre)
  print(auc0)
  auc=c(auc,auc0)
}
mean(auc) #相当于五次留出法的平均auc值
#从所有变量建模到用逐步回归筛选变量
lm_res_all<-glm(is_student~.,data=model_student,family="binomial")
step_lm<-step(lm_res_all) #逐步回归筛选变量
#绘制逻辑回归变量系数大小图
coef = as.data.frame(step_lm$coefficients)
coef$var = row.names(coef)
colnames(coef)[1] = "coef"
coef = coef[-1,]
coef$pos = coef$coef>0
ggplot(coef,aes(x = reorder(var,-coef),y = coef,fill = pos))+
  geom_bar(stat = 'identity',position = 'identity')+
  scale_x_discrete(labels = c("有无学生类APP","score","学校线路占比","总出行次数","平均出行距离","出行时段-早高峰","工作日出行时间标准差","出行时段-平峰","周末出行次数","周末出行时间标准差","工作日出行时段-平峰","周末出行时段-平峰","工作日出行时段-早高峰","学校线路出行次数","周末出行时段-早高峰","工作日出行时段-无","工作日平均出行距离","周末平均出行距离","出行时间标准差","工作日出行次数","周末出行时段-无"))+
  theme(axis.text.x = element_text(angle = 50,hjust = 1,vjust = 1))+
  labs(fill = "系数正负",x = "变量",y="系数")
#可视化结果参照我的R可视化相关博文

猜你喜欢

转载自blog.csdn.net/TOMOCAT/article/details/81631970