This is not about asking syntax, but rather asking concept of using R.

Let's assume I am a HR person and I have a huge set of data, for example:

EmployeeID      Date-Join      Date-Terminated      Current Base Pay    Last Increment Date   ManagerID
123             11-Sept-1990   NA                   8,000               1-April-2016          ABC
456             31-Dec-2010    1-May-2016           11,000              NA                    ABC
789             12-May-2010    3-June-2015          3,000               5-Feb-2015            ABC

I wish to analyze the reasons of why employees are leaving the company. For instance, it could be the employee are not happy with the current pay. But the factor however are not limited to the amount purely. The same amount carry different meaning to everyone and every context.

Besides the base pay, there are still a lot of possibilities contributed to the termination. For example, how long has it been since the last promotion/increment? How much is the increment amount? And human factors like the employee is not feeling happy with the manager etc.

So I am confused right now. I have no issue at plotting graphs. But I need guidance in how to combine them to show me some useful picture.

In short, eventually what I wish to see is: when I input a new data set, the program written could predict how likely the employee is about leaving the company.


2 Answers 2


In general, preprocessing your dataset into the suggested form is the first thing you should do. Also, conducting a logistic regression is the method of choice for your research question.

However, since you want to know which independent variables (e.g. base payment or years of employment) have a significant influence on your dependent variable (i.e. staying in or leaving the company), I suggest to use a stepAIC algorithm in order to find the best fitting model. Following this way you could answer questions such as "Are particular managers connected to significantly high leave rates?" or "Does an interaction between base pay and the time since the last increment have an influence on leaving the company?".

In particular you could do something like this:

# load or install MASS (contains stepAIC)
# dependent variable (0 = has not left the company, 1 = has left the company)
has.left <- sample(rep(c(1,0,1),1000),100)
# independent variables (i.e. possible predictors)
years.of.employment <- sample(rep(c(1,2,3),1000),100)
base.pay <- sample(rep(c(1000,2000,3000),1000),100)
managaer.id <- sample(rep(c(1,2,3),1000),100)
# put everything in a data.frame
df <- data.frame(years.of.employment, base.pay, managaer.id)
# generate a model containing all possible variable interactions (.^2)
model <- lm(has.left~.^2,data=df)
# run algorithm
best.fit <- stepAIC(model)
# show model summary

# OUTPUT       
# Residuals:
#   Min      1Q  Median      3Q     Max 
# -0.8169 -0.5673  0.2902  0.3723  0.5615 
# Coefficients:
#   Estimate Std. Error t value Pr(>|t|)
# (Intercept)                   3.488e-01  3.304e-01   1.055    0.294
# years.of.employment           1.469e-01  1.598e-01   0.919    0.360
# base.pay                      2.191e-04  1.571e-04   1.395    0.166
# years.of.employment:base.pay -1.120e-04  7.496e-05  -1.494    0.138
# Residual standard error: 0.4834 on 96 degrees of freedom
# Multiple R-squared:  0.03746, Adjusted R-squared:  0.00738 
# F-statistic: 1.245 on 3 and 96 DF,  p-value: 0.2977

Briefly explained, including the variables years.of.employment, base.pay as well as their interaction would represent the best fitting model here. Not to mention that this is just to show the basic principle since all data is randomly generated. Please note that you can also manipulate the direction of the stepAIC algorithm (i.e. forward, backward, forward/backward) which leads to different results and depends on both your data and your research question (by default the direction is backward).

Overall, it should further be mentioned that exploratory data analysis should be conducted with caution and you should have hypotheses before testing. However, this is a point which rather goes into a methodological direction.

  • $\begingroup$ Thanks dude, a very detail explanation. I will take time to study on your suggestion. :) $\endgroup$ Sep 23, 2016 at 8:48
  • $\begingroup$ Hi, may I ask for a more detail explanation on this line: model <- lm(has.left~.^2,data=df) 'lm' stands for linear model. But I am not sure what is 'has.left~.^2' any why are you writing in such way. $\endgroup$ Sep 25, 2016 at 8:26
  • $\begingroup$ For calculating a linear model you have to define a model formula. has.left is here the dependent variable while df holds all independent variables (i.e. predictors). ".^2" just means that you'll include all variables in df as well as all possible interactions (which are two in the given example). $\endgroup$
    – jd1338
    Sep 25, 2016 at 14:54
  • $\begingroup$ Wow, now I understood. Thanks a lot! Highly appreciated. :) $\endgroup$ Sep 25, 2016 at 16:22

Preprocess your dataset to create a labeled dataset as follows, the dataset you are starting with must have both existing employees and the employees already left.

EmployeeID      Years_of_Employment   Base_Pay  Years_Since_Last_Increment JobTitle  Has_Left
123             10                    8000                     1           Manager   0
456             2                     11000                    2           Developer 1

Here the Has_left is the binary response variable (0: the employee has left, 1: the employee is still working) that you are interested to learn from the other feature variables (add some more predictors that you think can have impact on the attrition).

Now use the above dataset to train a logistic regression model. You can use the model to predict the probability that a new employee will leave, given the values of all the dependent feature variables.

  • $\begingroup$ Alright, thanks Sandipan. I will take time to study on your suggestion. :) $\endgroup$ Sep 23, 2016 at 8:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.