❝本节来介绍如何使用R语言来进行「逻辑回归与决策树模型分析」,下面小编通过一个案例来进行展示,结果仅供展示用,希望各位观众老爷能够喜欢。。❞
library(tidyverse)
library(caTools)
creditcard_data <- read_csv("creditcard.csv")
creditcard_data$Amount = scale(creditcard_data$Amount) # 对Amount列进行标准化处理
NewData = creditcard_data[, -c(1)]
set.seed(123) # 设置随机种子,以确保可重复性
# 使用sample.split函数对数据进行分割,80%用于训练,20%用于测试
data_sample = sample.split(NewData$Class, SplitRatio = 0.80)
train_data = subset(NewData, data_sample == TRUE) # 创建训练数据集
test_data = subset(NewData, data_sample == FALSE) # 创建测试数据集
# 使用逻辑回归模型进行训练,并将模型存储在Logistic_Model变量中
Logistic_Model = glm(Class ~ ., test_data, family = binomial())
summary(Logistic_Model) # 显示逻辑回归模型的摘要信息
plot(Logistic_Model) # 绘制逻辑回归模型的图形
绘制ROC曲线评估模型有效性
library(pROC)
lr.predict <- predict(Logistic_Model,test_data, probability = TRUE)
auc.gbm = roc(test_data$Class, lr.predict, plot = TRUE, col = "blue")
library(rpart)
# install.packages("rpart.plot")
library(rpart.plot) # 用于决策树的可视化
# 使用决策树模型进行训练,并将模型存储在decisionTree_model变量中
decisionTree_model <- rpart(Class ~ . , creditcard_data, method = 'class')
# 使用决策树模型进行预测,将预测值存储在predicted_val变量中
predicted_val <- predict(decisionTree_model, creditcard_data, type = 'class')
# 计算预测的概率,并存储在probability变量中
probability <- predict(decisionTree_model, creditcard_data, type = 'prob')
rpart.plot(decisionTree_model) # 使用rpart.plot函数绘制决策树模型