# Step 1: Load Packages and Create Employee Dataset

library(class)      # For KNN predictions
library(rpart)      # For decision trees
library(rpart.plot) # For drawing decision trees

demo_data_text <- "
Age,YearsAtCompany,JobSatisfaction,WorkLifeBalance,Salary,Overtime,Attrition
25,1,3,3,50000,No,No
30,5,4,2,60000,Yes,Yes
28,3,2,3,55000,No,No
45,20,5,4,120000,No,No
32,7,3,2,65000,Yes,Yes
29,4,2,3,58000,No,No
40,15,4,3,90000,No,No
35,10,3,3,75000,Yes,Yes
26,2,3,4,52000,No,No
31,6,4,2,62000,Yes,Yes
38,12,5,3,85000,No,No
27,3,2,3,54000,No,No
42,18,4,4,95000,No,No
33,8,3,3,68000,Yes,Yes
24,1,2,4,48000,No,No
36,9,4,2,72000,Yes,Yes
39,14,5,3,87000,No,No
29,5,3,3,60000,Yes,Yes
41,16,4,4,91000,No,No
30,7,3,3,63000,Yes,Yes
"

employee_data <- read.csv(text = demo_data_text, stringsAsFactors = FALSE)
employee_data$Overtime <- factor(employee_data$Overtime)
employee_data$Attrition <- factor(employee_data$Attrition, levels = c("No","Yes"))

print("First few employees:")
head(employee_data)

print("Summary of all data:")
summary(employee_data)

# Step 2: Explore the Data

print("How many employees left the company?")
table(employee_data$Attrition)

print("Average age by attrition:")
aggregate(Age ~ Attrition, data = employee_data, mean)

print("Average salary by attrition:")
aggregate(Salary ~ Attrition, data = employee_data, mean)

print("Overtime vs Attrition:")
table(employee_data$Overtime, employee_data$Attrition)

# Step 3: Split Data into Training and Test Sets

set.seed(123)
train_indices <- sample(seq_len(nrow(employee_data)), size = 0.7 * nrow(employee_data))
train_data <- employee_data[train_indices, ]
test_data <- employee_data[-train_indices, ]

print(paste("Training data has", nrow(train_data), "employees"))
print(paste("Test data has", nrow(test_data), "employees"))

# Step 4: Logistic Regression

logistic_model <- glm(Attrition ~ Age + YearsAtCompany + Salary + Overtime,
                      data = train_data, family = binomial)
print("Logistic Regression Results:")
summary(logistic_model)

logistic_probabilities <- predict(logistic_model, newdata = test_data, type = "response")
logistic_predictions <- factor(ifelse(logistic_probabilities > 0.5, "Yes", "No"), levels = c("No","Yes"))

print("Logistic Regression Predictions vs Reality:")
confusion_matrix_logistic <- table(Predicted = logistic_predictions, Actual = test_data$Attrition)
print(confusion_matrix_logistic)

logistic_accuracy <- mean(logistic_predictions == test_data$Attrition)
print(paste("Logistic Regression Accuracy:", round(logistic_accuracy * 100, 1), "%"))

# Step 5: Decision Tree

tree_model <- rpart(Attrition ~ Age + YearsAtCompany + Salary + Overtime,
                    data = train_data, method = "class")
rpart.plot(tree_model, main = "Decision Tree for Employee Attrition")

tree_predictions <- predict(tree_model, test_data, type = "class")
print("Decision Tree Predictions vs Reality:")
confusion_matrix_tree <- table(Predicted = tree_predictions, Actual = test_data$Attrition)
print(confusion_matrix_tree)

tree_accuracy <- mean(tree_predictions == test_data$Attrition)
print(paste("Decision Tree Accuracy:", round(tree_accuracy * 100, 1), "%"))

# Step 6: K-Nearest Neighbors (KNN)

train_X <- scale(train_data[, c("Age","YearsAtCompany","Salary")])
test_X <- scale(test_data[, c("Age","YearsAtCompany","Salary")],
                center = attr(train_X, "scaled:center"),
                scale = attr(train_X, "scaled:scale"))
train_Y <- train_data$Attrition

knn_predictions <- knn(train_X, test_X, cl = train_Y, k = 3)
print("KNN Predictions vs Reality:")
confusion_matrix_knn <- table(Predicted = knn_predictions, Actual = test_data$Attrition)
print(confusion_matrix_knn)

knn_accuracy <- mean(knn_predictions == test_data$Attrition)
print(paste("KNN Accuracy:", round(knn_accuracy * 100, 1), "%"))
