Research question:

H0:μ1 - μ2 = 0 (where μ1 is the mean monthly income of employees who left, and μ2 is the mean monthly income of employees who stayed)

H1:μ1 - μ2 =/ 0

#install.packages("readxl")
library(readxl)
mydata <- read_xlsx("~/IMB/MVA/Homework 1/Data1.xlsx")
head(mydata)
## # A tibble: 6 × 10
##   EmployeeID   Age Attrition MonthlyIncome Gender JobSatisfaction
##        <dbl> <dbl> <chr>             <dbl> <chr>            <dbl>
## 1    1313919    41 No                 5993 Female               4
## 2    1200302    49 No                 5130 Male                 2
## 3    1060315    37 Yes                2090 Male                 3
## 4    1272912    33 No                 2909 Female               3
## 5    1414939    27 No                 3468 Male                 2
## 6    1633361    32 No                 3068 Male                 4
## # ℹ 4 more variables: MaritalStatus <chr>, Department <chr>,
## #   RelationshipSatisfaction <dbl>, YearsAtCompany <dbl>

Description:

Source of data: Kaggle’s dataset on Employee Attrition for Healthcare

#Changing variable Attrition to factor variable, so R views it as a categorical variable
mydata$AttritionF <- factor(mydata$Attrition, 
                         levels = c("Yes" , "No"), 
                         labels = c("Yes" , "No"))
#I will make a random sample of 100 units, because my initial sample size is to big (1676) and even a small difference in observed variable would be statistically significant

set.seed(1) 
mydata <- mydata[sample(nrow(mydata), 100), ]
#Describing only my numeric variables

#install.packages("psych")
library (psych)

subset_df <- mydata[c("Age", "JobSatisfaction", "MonthlyIncome" , "RelationshipSatisfaction" , "YearsAtCompany")]

mydata2 <- describe(subset_df)

print(mydata2)
##                          vars   n    mean      sd median trimmed
## Age                         1 100   35.19    7.74     35   35.16
## JobSatisfaction             2 100    2.72    1.18      3    2.78
## MonthlyIncome               3 100 5563.08 4060.73   4402 4797.30
## RelationshipSatisfaction    4 100    2.92    1.03      3    3.02
## YearsAtCompany              5 100    6.40    5.17      5    5.68
##                              mad  min   max range  skew kurtosis
## Age                         7.41   19    52    33  0.07    -0.59
## JobSatisfaction             1.48    1     4     3 -0.40    -1.36
## MonthlyIncome            2505.59 1706 19613 17907  1.85     3.20
## RelationshipSatisfaction    1.48    1     4     3 -0.50    -0.98
## YearsAtCompany              4.45    0    22    22  1.10     0.73
##                              se
## Age                        0.77
## JobSatisfaction            0.12
## MonthlyIncome            406.07
## RelationshipSatisfaction   0.10
## YearsAtCompany             0.52
library(psych)
describeBy(mydata$MonthlyIncome, g = mydata$AttritionF)
## 
##  Descriptive statistics by group 
## group: Yes
##    vars  n    mean     sd median trimmed    mad  min   max range skew
## X1    1 14 4150.07 3049.4 2894.5 3527.08 755.38 2166 13610 11444 2.08
##    kurtosis     se
## X1     3.69 814.99
## ---------------------------------------------------- 
## group: No
##    vars  n   mean      sd median trimmed     mad  min   max range
## X1    1 86 5793.1 4171.44 4607.5 5066.91 2666.46 1706 19613 17907
##    skew kurtosis     se
## X1 1.78     2.88 449.82
library(ggplot2)
## 
## Attaching package: 'ggplot2'
## The following objects are masked from 'package:psych':
## 
##     %+%, alpha
ggplot(mydata, aes(x = MonthlyIncome)) +
  geom_histogram(binwidth = 300, colour="green") +
  facet_wrap(~AttritionF, ncol = 1) + 
  ylab("Frequency")

library(ggpubr)
ggqqplot(mydata,
         "MonthlyIncome",
         facet.by = "AttritionF")

#Checking for assumption of normality (assumptions of numeric variables, and big enough sample sizes are met)

library(rstatix)
## 
## Attaching package: 'rstatix'
## The following object is masked from 'package:stats':
## 
##     filter
mydata %>%
  group_by(AttritionF) %>%
  shapiro_test(MonthlyIncome)
## # A tibble: 2 × 4
##   AttritionF variable      statistic        p
##   <fct>      <chr>             <dbl>    <dbl>
## 1 Yes        MonthlyIncome     0.645 1.04e- 4
## 2 No         MonthlyIncome     0.778 4.52e-10

H0: Monthly income is normally distributed

H1: Monthly income is not normally distributed

# Just to compare I will first make parametric independent t-test, but because normality is violated I should not use it, and will in next R chunk provide the correct alternative when normality is violated in independent samples data (WILCOXON RANK SUM TEST)

t.test(mydata$MonthlyIncome ~ mydata$AttritionF, 
       paired = FALSE,
       var.equal = FALSE,
       alternative = "two.sided")
## 
##  Welch Two Sample t-test
## 
## data:  mydata$MonthlyIncome by mydata$AttritionF
## t = -1.765, df = 21.817, p-value = 0.09155
## alternative hypothesis: true difference in means between group Yes and group No is not equal to 0
## 95 percent confidence interval:
##  -3574.5005   288.4341
## sample estimates:
## mean in group Yes  mean in group No 
##          4150.071          5793.105

H0:μ1 - μ2 = 0 (where μ1 is the mean monthly income of employees who left, and μ2 is the mean monthly income of employees who stayed)

H1:μ1 - μ2 =/ 0

# Using the correct test now

wilcox.test(mydata$MonthlyIncome ~ mydata$AttritionF,
            paired = FALSE,
            correct = FALSE,
            exact = FALSE,
            alternative = "two.sided")
## 
##  Wilcoxon rank sum test
## 
## data:  mydata$MonthlyIncome by mydata$AttritionF
## W = 429, p-value = 0.08569
## alternative hypothesis: true location shift is not equal to 0

H0: Distribution location of monthly income of the employee who left the company and who stayed is the same.

H1: Distribution location of monthly income of the employee who left the company and who stayed is not the same.

library(effectsize)
## 
## Attaching package: 'effectsize'
## The following objects are masked from 'package:rstatix':
## 
##     cohens_d, eta_squared
## The following object is masked from 'package:psych':
## 
##     phi
effectsize(wilcox.test(mydata$MonthlyIncome ~ mydata$Attrition,
                       paired = FALSE,
                       correct = FALSE,
                       exact = FALSE,
                       alternative = "two.sided"))
## r (rank biserial) |        95% CI
## ---------------------------------
## 0.29              | [-0.03, 0.55]
interpret_rank_biserial(0.29)
## [1] "medium"
## (Rules: funder2019)

Effect size:

Conclusion: