RQ1:
RQ2:
#install.packages("readxl")
library(readxl)
mydata <- read_xlsx("~/IMB/MVA/Homework 2/Data2.xlsx")
head(mydata)
## # A tibble: 6 × 9
## EmployeeID Age Gender MaritalStatus MonthlyIncome JobSatisfaction
## <dbl> <dbl> <chr> <chr> <dbl> <dbl>
## 1 1313919 41 Female Single 5993 4
## 2 1200302 49 Male Married 5130 2
## 3 1060315 37 Male Single 2090 3
## 4 1272912 33 Female Married 2909 3
## 5 1414939 27 Male Married 3468 2
## 6 1633361 32 Male Single 3068 4
## # ℹ 3 more variables: Attrition <chr>, OverTime <chr>,
## # YearsAtCompany <dbl>
Description:
Unit of observation: An employee at the healthcare company
Sample size: 1676
EmployeeID: ID of an employee at the company
Age: Age of an employee in years
Gender: Gender of an employee
MaritalStatus : Marital status of an employee (Married, Single, Divorced)
MonthlyIncome: Monthly income of an employee in $
JobSatisfaction: Measurement of an employee’s job satisfaction, based on a numeric value from 1 to 4
Attrition: Indicator whether an employee has left the company (attrition = Yes) or has stayed (attrition = No)
OverTime: Indicator whether an employee has worked overtime (Yes) or he has not (No)
YearsAtCompany: Number of years an employee has been with the company
Source of data: Kaggle’s dataset on Employee Attrition for Healthcare
set.seed(1)
mydata <- mydata[sample(nrow(mydata), 100), ]
# For later use for Pearson Chi2 test
mydata$StatusFactor <- factor(mydata$MaritalStatus,
levels = c("Single", "Married", "Divorced"),
labels = c("Single", "Married", "Divorced"))
mydata$GenderFactor <- factor(mydata$Gender,
levels = c("Female", "Male"),
labels = c("Female", "Male"))
head(mydata, 5)
## # A tibble: 5 × 11
## EmployeeID Age Gender MaritalStatus MonthlyIncome JobSatisfaction
## <dbl> <dbl> <chr> <chr> <dbl> <dbl>
## 1 1537620 36 Female Married 5647 1
## 2 1674536 36 Male Single 4678 4
## 3 1176986 22 Male Married 2523 4
## 4 1100849 29 Female Divorced 3339 3
## 5 1028734 39 Female Single 10938 3
## # ℹ 5 more variables: Attrition <chr>, OverTime <chr>,
## # YearsAtCompany <dbl>, StatusFactor <fct>, GenderFactor <fct>
#install.packages("psych")
library (psych)
subset_df <- mydata[c("Age", "MonthlyIncome" , "JobSatisfaction" , "YearsAtCompany")]
mydata2 <- describe(subset_df)
print(mydata2)
## vars n mean sd median trimmed mad min
## Age 1 100 35.19 7.74 35 35.16 7.41 19
## MonthlyIncome 2 100 5563.08 4060.73 4402 4797.30 2505.59 1706
## JobSatisfaction 3 100 2.72 1.18 3 2.78 1.48 1
## YearsAtCompany 4 100 6.40 5.17 5 5.68 4.45 0
## max range skew kurtosis se
## Age 52 33 0.07 -0.59 0.77
## MonthlyIncome 19613 17907 1.85 3.20 406.07
## JobSatisfaction 4 3 -0.40 -1.36 0.12
## YearsAtCompany 22 22 1.10 0.73 0.52
# Shapiro-Wilk test for checking normality assumption
shapiro.test(mydata$MonthlyIncome)
##
## Shapiro-Wilk normality test
##
## data: mydata$MonthlyIncome
## W = 0.76706, p-value = 2.712e-11
shapiro.test(mydata$Age)
##
## Shapiro-Wilk normality test
##
## data: mydata$Age
## W = 0.98569, p-value = 0.3556
H0: Monthly income is normally distributed
H1: Monthly income is not normally distributed
H0: Age is normally distributed
H1: Age is not normally distributed
We cannot reject H0. Age is normally distributed.
Based on the finding of violation of assumption of normality of both variables. I need to use the non-parametric version, Spearman correlation coefficient.
#Ckeking the range of my data for x and y axes for scatterplot
range(mydata$Age, na.rm = TRUE)
## [1] 19 52
range(mydata$MonthlyIncome, na.rm = TRUE)
## [1] 1706 19613
#install.packages("psych")
library(car)
## Loading required package: carData
##
## Attaching package: 'car'
## The following object is masked from 'package:psych':
##
## logit
scatterplot(mydata$Age, mydata$MonthlyIncome,
smooth = FALSE,
ylim = c(1000, 21000),
xlim = c(15, 60),
col = "violet")
round(cor(mydata$Age, mydata$MonthlyIncome), 3)
## [1] 0.321
library(Hmisc)
##
## Attaching package: 'Hmisc'
## The following object is masked from 'package:psych':
##
## describe
## The following objects are masked from 'package:base':
##
## format.pval, units
rcorr(as.matrix(mydata[, c("Age", "MonthlyIncome")]),
type = "spearman")
## Age MonthlyIncome
## Age 1.00 0.37
## MonthlyIncome 0.37 1.00
##
## n= 100
##
##
## P
## Age MonthlyIncome
## Age 1e-04
## MonthlyIncome 1e-04
H0: Population correlation coefficient of age and monthly income of an employee = 0
H1: Population correlation coefficient of age and monthly income of an employee =/ 0
Reject H0, at p<0.001.
CONCLUSION: There is a statistically significant correlation between the age of a person working in healthcare and their monthly income.
BUT: I shouldn’t even use this test from the 1st place due to type of the variable, and no linearity between them.
# Pearson Chi2 test
results <- chisq.test(mydata$GenderFactor, mydata$StatusFactor,
correct = TRUE)
results
##
## Pearson's Chi-squared test
##
## data: mydata$GenderFactor and mydata$StatusFactor
## X-squared = 2.9213, df = 2, p-value = 0.2321
H0:There is no association between marital status and gender
H1:There is association between marital status and gender
addmargins(results$observed)
## mydata$StatusFactor
## mydata$GenderFactor Single Married Divorced Sum
## Female 8 27 8 43
## Male 19 31 7 57
## Sum 27 58 15 100
round(results$expected, 2)
## mydata$StatusFactor
## mydata$GenderFactor Single Married Divorced
## Female 11.61 24.94 6.45
## Male 15.39 33.06 8.55
round(results$res, 2)
## mydata$StatusFactor
## mydata$GenderFactor Single Married Divorced
## Female -1.06 0.41 0.61
## Male 0.92 -0.36 -0.53
addmargins(round(prop.table(results$observed), 3))
## mydata$StatusFactor
## mydata$GenderFactor Single Married Divorced Sum
## Female 0.08 0.27 0.08 0.43
## Male 0.19 0.31 0.07 0.57
## Sum 0.27 0.58 0.15 1.00
addmargins(round(prop.table(results$observed, 1), 3), 2)
## mydata$StatusFactor
## mydata$GenderFactor Single Married Divorced Sum
## Female 0.186 0.628 0.186 1.000
## Male 0.333 0.544 0.123 1.000
addmargins(round(prop.table(results$observed, 2), 3), 1)
## mydata$StatusFactor
## mydata$GenderFactor Single Married Divorced
## Female 0.296 0.466 0.533
## Male 0.704 0.534 0.467
## Sum 1.000 1.000 1.000
library(effectsize)
##
## Attaching package: 'effectsize'
## The following object is masked from 'package:psych':
##
## phi
effectsize::cramers_v(mydata$StatusFactor, mydata$GenderFactor)
## Cramer's V (adj.) | 95% CI
## --------------------------------
## 0.10 | [0.00, 1.00]
##
## - One-sided CIs: upper bound fixed at [1.00].
interpret_cramers_v(0.10)
## [1] "small"
## (Rules: funder2019)
# Just a robustness check
fisher.test(mydata$StatusFactor, mydata$GenderFactor)
##
## Fisher's Exact Test for Count Data
##
## data: mydata$StatusFactor and mydata$GenderFactor
## p-value = 0.2317
## alternative hypothesis: two.sided
H0: Odds ratio = 1
H1: Odds ratio =/ 1
BSD we cannot reject H0. Both genders have equal odds for each marital status.