<jupyter_start><jupyter_text>1. 读入数据并了解自变量含义<jupyter_code>dataRaw <- read.csv('simudata.csv')
head(dataRaw)<jupyter_output><empty_output><jupyter_text>所有自变量的意义如下：age	年龄cardnum	银行卡数creded	借贷比率billnum	交易笔数meanpay	所有行为均值maxpay	所有行为最大值xiaofeiF	消费类消费频率jinkaF	金卡类消费频率youxiM	游戏类消费金额平均值debitM	借记类消费金额平均值debitF	借记类消费频率gongjiaoR	公缴类最近一次消费gongjiaoF	公缴类消费频率gongjiaoM	公缴类金额平均值zhongxingR	中型银行最近一次消费zhongxingF	中型银行消费频率zhongxingM	中型银行金额平均值sidaR	四大行最近一次消费sidaF	四大行消费频率sidaM	四大行金额平均值zhuanzhangR	转账类最近一次消费zhuanzhangF	转账类消费频率zhuanzhangM	转账类金额平均值xindaiR	信贷类最近一次消费xindaiF	信贷类消费频率xindaiS	信贷类消费波动性  2. 多种机器学习模型进行建模<jupyter_code>set.seed(1234)
SELECT <- sample(0:nrow(dataRaw), size=nrow(dataRaw))
dataTrain <- dataRaw[SELECT[1:(nrow(dataRaw)*0.7)],]
dataTest <- dataRaw[SELECT[-(1:(nrow(dataRaw)*0.7))],]
# dataTrain$black <- as.factor(dataTrain$black)<jupyter_output><empty_output><jupyter_text>- Logistic Regression<jupyter_code>logModel <- glm(data=dataTrain, formula=black~., family='binomial')
logPred <- predict(object=logModel, newdata=dataTest, type='response')
logROC <- roc(dataTest$black, logPred, plot.roc=T)
ggplot(mapping=aes(x=1-logROC$specificities, y=logROC$sensitivities)) + geom_line(
    color='red') + geom_line(
    color='blue', aes(x=logROC$specificities,y=logROC$specificities)) + labs(
    x='FPR', y='TPR', title=paste('AUC:',logROC$auc))<jupyter_output>Setting levels: control = 0, case = 1
Setting direction: controls < cases<jupyter_text>- Decision Tree<jupyter_code>library(rpart)
treeModel <- rpart(data=dataTrain, formula=black~., method='class')
treePred <- predict(object=treeModel, newdata=dataTest)[,1]
treeROC <- roc(dataTest$black, treePred)
ggplot(mapping=aes(x=1-treeROC$specificities, y=treeROC$sensitivities)) + geom_line(
    color='red') + geom_line(
    color='blue', aes(x=treeROC$specificities,y=treeROC$specificities)) + labs(
    x='FPR', y='TPR', title=paste('AUC:',treeROC$auc))<jupyter_output>Setting levels: control = 0, case = 1
Setting direction: controls > cases<jupyter_text>- Boosting模型<jupyter_code>library(gbm)
boostModel <-  gbm(data=dataTrain, formula=black~.)
boostPred <- predict(object=boostModel, newdata=dataTest)
boostROC <- roc(as.numeric(dataTest$black), as.numeric(boostPred))
ggplot(mapping=aes(x=1-boostROC$specificities, y=boostROC$sensitivities)) + geom_line(
    color='red') + geom_line(
    color='blue', aes(x=boostROC$specificities,y=boostROC$specificities)) + labs(
    x='FPR', y='TPR', title=paste('AUC:',boostROC$auc))<jupyter_output>Distribution not specified, assuming bernoulli ...<jupyter_text>- Random Forest<jupyter_code>library(randomForest)
forestModel <- randomForest(black~., data=dataTrain, importance=T)
forestPred <- predict(object=forestModel, newdata=dataTest)
forestROC <- roc(dataTest$black, as.numeric(forestPred))
ggplot(mapping=aes(x=1-forestROC$specificities, y=forestROC$sensitivities)) + geom_line(
    color='red') + geom_line(
    color='blue', aes(x=forestROC$specificities,y=forestROC$specificities)) + labs(
    x='FPR', y='TPR', title=paste('AUC:', forestROC$auc))<jupyter_output>Warning message in randomForest.default(m, y, ...):
"The response has five or fewer unique values.  Are you sure you want to do regression?"Setting levels: control = 0, case = 1
Setting direction: controls < cases<jupyter_text>- SVM<jupyter_code>library(e1071)
svmModel <- svm(black~., data=dataTrain, importance=T)
svmPred <- predict(object=svmModel, newdata=dataTest)
svmROC <- roc(dataTest$black, svmPred)
ggplot(mapping=aes(x=1-svmROC$specificities, y=svmROC$sensitivities)) + geom_line(
    color='red') + geom_line(
    color='blue', aes(x=svmROC$specificities,y=svmROC$specificities)) + labs(
    x='FPR', y='TPR', title=paste('AUC:', svmROC$auc))<jupyter_output>Warning message:
"package 'e1071' was built under R version 3.6.3"Setting levels: control = 0, case = 1
Setting direction: controls < cases<jupyter_text>3. 选择一个最优的模型<jupyter_code>ROC <- data.frame(FPR=c(1-logROC$specificities, 1-boostROC$specificities, 1-treeROC$specificities, 
                        1-forestROC$specificities, 1-svmROC$specificities), 
                  TPR=c(logROC$sensitivities, boostROC$sensitivities, treeROC$sensitivities, 
                        forestROC$sensitivities, svmROC$sensitivities),
                  method=c(rep('Logistic Regression', length(logROC$sensitivities)), 
                           rep('Boosting', length(boostROC$sensitivities)), 
                           rep('Decision Tree', length(treeROC$sensitivities)), 
                           rep('Random Forest', length(forestROC$sensitivities)), 
                           rep('SVM', length(svmROC$sensitivities))))
ggplot(ROC, aes(x=FPR, y=TPR, group=method, color=method)) + geom_line() + geom_abline(
    color='black', intercept=0, slope=1) + labs(title="ROC")
AUC <- data.frame(method=c('Logistic Regression', 'Boosting', 'Decision Tree', 'Random Forest', 'SVM'), 
                  auc=c(logROC$auc, boostROC$auc, treeROC$auc, forestROC$auc, svmROC$auc))
ggplot(AUC) + geom_bar(aes(x=method, y=auc, color=method, fill=method), stat='identity')<jupyter_output><empty_output>
StarcoderdataJupyterStructuredCleanDedup