用R语言写一个员工离职预测代码
时间: 2023-03-25 13:03:50 浏览: 145
Python源码集锦-员工离职预测模型
5星 · 资源好评率100%
你好,以下是R语言的员工离职预测代码:
#导入数据
data <- read.csv("employee_data.csv")
#数据预处理
data$Attrition <- as.factor(data$Attrition)
data$BusinessTravel <- as.factor(data$BusinessTravel)
data$Department <- as.factor(data$Department)
data$EducationField <- as.factor(data$EducationField)
data$Gender <- as.factor(data$Gender)
data$JobRole <- as.factor(data$JobRole)
data$MaritalStatus <- as.factor(data$MaritalStatus)
data$OverTime <- as.factor(data$OverTime)
#建立模型
library(caret)
set.seed(123)
trainIndex <- createDataPartition(data$Attrition, p = .7, list = FALSE, times = 1)
train <- data[ trainIndex,]
test <- data[-trainIndex,]
model <- train(Attrition ~ ., data = train, method = "glm", family = "binomial")
#预测
predictions <- predict(model, newdata = test)
confusionMatrix(predictions, test$Attrition)
希望能对你有所帮助。
阅读全文