library(readr)
library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
library(ggplot2)

hr <- read_csv("https://raw.githubusercontent.com/aiplanethub/Datasets/refs/heads/master/HR_comma_sep.csv")
## Rows: 14999 Columns: 10
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (2): Department, salary
## dbl (8): satisfaction_level, last_evaluation, number_project, average_montly...
## 
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
# Fix the known misspelling so code can use average_monthly_hours safely
if ("average_montly_hours" %in% names(hr)) {
  hr <- hr |> dplyr::rename(average_monthly_hours = average_montly_hours)
}

##Test One- One Sample T-test (Satisfaction vs .60) ## Question: Is tthe average satisfaction level different from 0.60?

t1 <- t.test(hr$satisfaction_level, mu = 0.60)
t1
## 
##  One Sample t-test
## 
## data:  hr$satisfaction_level
## t = 6.3215, df = 14998, p-value = 2.663e-10
## alternative hypothesis: true mean is not equal to 0.6
## 95 percent confidence interval:
##  0.6088542 0.6168128
## sample estimates:
## mean of x 
## 0.6128335

Technical Interpretation: The test shows r if else(p.value < 0.05, “a significant difference”, “no significant difference”) from 0.60 (p = r signif(t1$p.value, 4)).

Non-Technical Explanation: r if (p.value < 0.05) {“Employee satisfaction differs from 0.60.”} else Employee satisfaction is about the same as 0.60

ggplot(hr, aes(x = satisfaction_level)) +
geom_histogram(bins = 30) +
labs(title = "Distribution of Satisfaction Level", x = "Satisfaction (0–1)", y = "Count")

Test 2- Two- Sample T-Test (Satisfaction: Low vs High Salary)

Question: Do employees with low vs high salary differ in satisfaction?

group_lh <- hr %>% filter(salary %in% c("low", "high")) %>% droplevels()
t2 <- t.test(satisfaction_level ~ salary, data = group_lh)
t2
## 
##  Welch Two Sample t-test
## 
## data:  satisfaction_level by salary
## t = 5.1698, df = 1805, p-value = 2.603e-07
## alternative hypothesis: true difference in means between group high and group low is not equal to 0
## 95 percent confidence interval:
##  0.02278737 0.05064571
## sample estimates:
## mean in group high  mean in group low 
##          0.6374697          0.6007531

Technical Interpretation: We test whether the mean satisfaction is the same for low- and high-salary employees. If else (p.value < 0.05, “reject”, “fail to reject”) H₀ (p = r signif(p.value, 4)).

Non-Technical Explanation: r if (p.value < 0.05) {“Satisfaction differs between low- and high-salary employees.”} else Satisfaction is similar for low- and high-salary employees.

ggplot(group_lh, aes(x = salary, y = satisfaction_level)) +
geom_boxplot() +
labs(title = "Satisfaction by Salary Group (Low vs High)",
x = "Salary", y = "Satisfaction")

Test 3 — Two-Sample t-test (Last Evaluation: Accident vs No Accident)

Question: Do employees who had a work accident differ in their last evaluation scores?

t3 <- t.test(last_evaluation ~ as.factor(Work_accident), data = hr)
t3
## 
##  Welch Two Sample t-test
## 
## data:  last_evaluation by as.factor(Work_accident)
## t = 0.889, df = 3000.5, p-value = 0.3741
## alternative hypothesis: true difference in means between group 0 and group 1 is not equal to 0
## 95 percent confidence interval:
##  -0.004168157  0.011082974
## sample estimates:
## mean in group 0 mean in group 1 
##       0.7166017       0.7131443

Technical Interpretation: We test whether the mean evaluation scores are the same for employees with and without accidents (H₀: μ_accident=0 = μ_accident=1). We r ifelse(t3\(p.value < 0.05, "reject", "fail to reject") H₀ (p = r signif(t3\)p.value, 4)).

Non-Technical Explanation: r if (p.value < 0.05) Evaluation scores differ for employees with and without accidents.”} else- Evaluation scores are similar regardless of work accidents.

ggplot(hr, aes(x = as.factor(Work_accident), y = last_evaluation)) +
geom_boxplot() +
labs(title = "Last Evaluation by Work Accident",
x = "Work Accident (0=No, 1=Yes)", y = "Last Evaluation")

Test 4 — Two-Sample t-test (Avg Monthly Hours: Promoted vs Not)

Question: Do employees promoted in the last 5 years differ in their average monthly hours?

t4 <- t.test(average_monthly_hours ~ as.factor(promotion_last_5years), data = hr)
t4
## 
##  Welch Two Sample t-test
## 
## data:  average_monthly_hours by as.factor(promotion_last_5years)
## t = 0.44937, df = 333.03, p-value = 0.6535
## alternative hypothesis: true difference in means between group 0 and group 1 is not equal to 0
## 95 percent confidence interval:
##  -4.143788  6.597589
## sample estimates:
## mean in group 0 mean in group 1 
##        201.0764        199.8495

Technical Interpretation: We test whether the mean monthly hours are the same for promoted and non-promoted employees. If else (p.value < 0.05, “reject”, “fail to reject”) H₀ (p = r signif(p.value, 4)).

Non-Technical Explanation: r if (p.value < 0.05) Promoted employees work significantly different hours per month else promoted and non-promoted employees work similar monthly hours.

ggplot(hr, aes(x = as.factor(promotion_last_5years), y = average_monthly_hours)) +
geom_boxplot() +
labs(title = "Average_Monthly_Hours by Promotion (Last 5 Years)",
x = "Promoted (0=No, 1=Yes)", y = "Average_Monthly_Hours")