```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```


```{r, message=FALSE}
## Computation Setup

# import libraries
library(knitr)
library(tidyverse)
library(GGally)
library(gridExtra)
library(RColorBrewer)
library(gplots)
library(corrplot)
library(ggthemes)
library(caret)

set.seed(3456)

# import helper functions
source('../helper/data_munging.R')
source('../helper/visual.R')

# read in data
train <- read_csv('../data/CaseStudy2-data_train.csv')

# create a vector of numeric features
features.numeric <- c('DailyRate', 'DistanceFromHome', 'Age', 'HourlyRate', 'MonthlyIncome', 'MonthlyRate',
           'NumCompaniesWorked','PercentSalaryHike', 'TotalWorkingYears', 'TrainingTimesLastYear',
           'YearsAtCompany','YearsInCurrentRole','YearsSinceLastPromotion', 'YearsWithCurrManager')

# create a vector of numeric features
features.factor <- c('BusinessTravel', 'Department', 'Education', 'EducationField', 'EnvironmentSatisfaction', 'Gender', 'JobInvolvement', 'JobLevel', 'JobRole', 'JobSatisfaction', 'MaritalStatus', 'OverTime', 'PerformanceRating', 'RelationshipSatisfaction', 'StockOptionLevel', 'WorkLifeBalance')
```

## Variable Munging

Based on EDA several categorical levels will be releveled. Levels of variables that appear to have similar rates of attrition will be combined.

**Groupings**

* JobInvolvement
  * Level 3 is similar to level 2
  * Level 4 is similar to level 2
* EnvironmentSatisfaction
  * Level 3 is similar to level 2
  * Level 4 is similar to level 2
* WorkLifeBalance
  * Level 3 is similar to level 2
  * Level 4 is similar to level 2
* StockOptionLevel
  * Level 0 is similar to level 3
  * Level 1 is similar to level 2
* Education
  * Level 2 is similar to level 1
  * Level 3 is similar to level 1
  * Level 4 is similar to level 1
* JobLevel
  * Level 2 is similar to level 5
  * Level 3 is similar to level 5
* JobRole
  * Sales Executive, Human Resources, Laboratory Technician, and Research Scientist are similar
  * Manufacturing Director, Research Director, Manager, and Healthcare Representative are similar
* BusinessTravel
  * Non-Travel and Travel_Rarely are similar
* EducationField
  * Technical Degree and Human Resources are similar
  * Life Sciences and Medical are similar
  * Marketing and Other are similar
* JobSatisfaction
  * Level 3 is similar to level 1
  * Level 4 is similar to level 1

```{r}
# drop variables that are not useful - static or unique
train <- select(train, -c('EmployeeCount', 'EmployeeNumber','StandardHours', 'Over18'))

# factor categorical variables
train[, features.factor] <- lapply(train[, features.factor], as.factor)

train$Attrition <- as.factor(train$Attrition)

train$JobInvolvement <- recode(train$JobInvolvement, '3' = '2', '4' = '2')
train$EnvironmentSatisfaction <- recode(train$EnvironmentSatisfaction, '3' = '2', '4' = '2')
train$WorkLifeBalance <- recode(train$WorkLifeBalance, '3' = '2', '4' = '2')
train$StockOptionLevel <- recode(train$StockOptionLevel, '0' = '3', '1' = '2')
train$Education <- recode(train$Education, '2' = '1', '3' = '1', '4' = '1')
train$JobLevel <- recode(train$JobLevel,  '2' = '5', '3' = '5')
train$JobRole <- as.factor(recode(train$JobRole, "Sales Representative" = 3,
                                        "Research Scientist" = 2,
                                        "Sales Executive" = 2,
                                        "Human Resources" = 2,
                                        "Laboratory Technician" = 2,
                                        "Manufacturing Director" = 1,
                                        "Research Director" = 1,
                                        "Manager" = 1,
                                        "Healthcare Representative" = 1))
train$BusinessTravel <- recode(train$BusinessTravel, "Non-Travel" = "Travel_Rarely" )
train$EducationField <- recode(train$EducationField,
                              "Technical Degree" = '1',
                              "Human Resources"  = '1',
                              "Life Sciences"    = '2',
                              "Marketing"        = '3',
                              "Medical"          = '2',
                              "Other"            = '3')
train$JobSatisfaction <- recode(train$JobSatisfaction,
                                '3' = '1',
                                '2' = '1')
```

## Data Partitioning

The training dataset will be split into a training set and a test set. The training set `dfTrain` will be used for model cross validation. The test set, `dfTest` will be used for final model selection.

```{r}
# split off a test set
trainIndex <- createDataPartition(train$Attrition , p = .75, 
                                  list = FALSE, 
                                  times = 1)
dfTrain <- train[trainIndex,]
dfTest <-  train[-trainIndex,]
```

## Model Training

With a small data set for training, the model will be initially trained with repeated 5-fold CV. Because there are a large number of categorical that appear to be useful for predicting attrition (based on EDA), a naive bayes model will be used.

```{r, trainPipeline, warning=FALSE}
# Set up repeated k-fold cross-validation
train.control <-trainControl(method = "repeatedcv",
                             number = 5,
                             repeats = 5,
                             summaryFunction = twoClassSummary,
                             classProbs = TRUE)
# Train the model
model.cv <-train(y = dfTrain$Attrition,
                 x = dfTrain[, c('OverTime' ,
                                 'JobInvolvement' ,
                                 'EnvironmentSatisfaction',
                                 'WorkLifeBalance' ,
                                 'StockOptionLevel' ,
                                 'Age',
                                 'YearsInCurrentRole', 
                                 'YearsAtCompany',
                                 'JobLevel',
                                 'JobRole',
                                 'TotalWorkingYears',
                                 'MonthlyIncome',
                                 'DistanceFromHome',
                                 'YearsWithCurrManager')],
                 method = 'nb',
                 metric = 'Spec',
                 trControl = train.control)
# print model summary
model.cv
```

## Model Validation

A previously unseen dataset will be used to verify that model is not overfit to the training data.

The current model provides a specificity of 0.66 on the validation data.

```{r, validation, warning=FALSE}

preds <- predict(model.cv, dfTest[, !names(dfTest) %in% ('Attrition')])

confusionMatrix(table(preds, dfTest$Attrition))
```

## Output Data for Audit

The following code is used to generate the predictions that will be used for model auditing. The output file will be named `Cas2PredictionsMiller Salary.csv` and will be output at the top of the repo.

```{r, message=FALSE, warning=FALSE}
# load the audit data
AuditData <- read_csv('../data/CaseStudy2CompSetNoAttrition_test.csv')

# Apply same changes to audit data
# drop variables that are not useful - static or unique
AuditData <- select(AuditData, -c('EmployeeCount', 'EmployeeNumber','StandardHours', 'Over18'))

# factor categorical variables
AuditData[, features.factor] <- lapply(AuditData[, features.factor], as.factor)


AuditData$JobInvolvement <- recode(AuditData$JobInvolvement, '3' = '2', '4' = '2')
AuditData$EnvironmentSatisfaction <- recode(AuditData$EnvironmentSatisfaction, '3' = '2', '4' = '2')
AuditData$WorkLifeBalance <- recode(AuditData$WorkLifeBalance, '3' = '2', '4' = '2')
AuditData$StockOptionLevel <- recode(AuditData$StockOptionLevel, '0' = '3', '1' = '2')
AuditData$Education <- recode(AuditData$Education, '2' = '1', '3' = '1', '4' = '1')
AuditData$JobLevel <- recode(AuditData$JobLevel,  '2' = '5', '3' = '5')
AuditData$JobRole <- as.factor(recode(AuditData$JobRole, "Sales Representative" = 3,
                                        "Research Scientist" = 2,
                                        "Sales Executive" = 2,
                                        "Human Resources" = 2,
                                        "Laboratory Technician" = 2,
                                        "Manufacturing Director" = 1,
                                        "Research Director" = 1,
                                        "Manager" = 1,
                                        "Healthcare Representative" = 1))
AuditData$BusinessTravel <- recode(AuditData$BusinessTravel, "Non-Travel" = "Travel_Rarely" )
AuditData$EducationField <- recode(AuditData$EducationField,
                              "Technical Degree" = '1',
                              "Human Resources"  = '1',
                              "Life Sciences"    = '2',
                              "Marketing"        = '3',
                              "Medical"          = '2',
                              "Other"            = '3')
AuditData$JobSatisfaction <- recode(AuditData$JobSatisfaction,
                                '3' = '1',
                                '2' = '1')

predictions <- predict(model.cv, AuditData)

write_csv(data.frame(
  ID = AuditData$ID,
  Attrition = predictions
  ), 
  path = '../../Cas2PredictionsMiller Attrition.csv')
```








