# Load necessary libraries
library(readr)
library(ggplot2)

# Load dataset
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.
# View structure
str(hr)
## spc_tbl_ [14,999 × 10] (S3: spec_tbl_df/tbl_df/tbl/data.frame)
##  $ satisfaction_level   : num [1:14999] 0.38 0.8 0.11 0.72 0.37 0.41 0.1 0.92 0.89 0.42 ...
##  $ last_evaluation      : num [1:14999] 0.53 0.86 0.88 0.87 0.52 0.5 0.77 0.85 1 0.53 ...
##  $ number_project       : num [1:14999] 2 5 7 5 2 2 6 5 5 2 ...
##  $ average_montly_hours : num [1:14999] 157 262 272 223 159 153 247 259 224 142 ...
##  $ time_spend_company   : num [1:14999] 3 6 4 5 3 3 4 5 5 3 ...
##  $ Work_accident        : num [1:14999] 0 0 0 0 0 0 0 0 0 0 ...
##  $ left                 : num [1:14999] 1 1 1 1 1 1 1 1 1 1 ...
##  $ promotion_last_5years: num [1:14999] 0 0 0 0 0 0 0 0 0 0 ...
##  $ Department           : chr [1:14999] "sales" "sales" "sales" "sales" ...
##  $ salary               : chr [1:14999] "low" "medium" "medium" "low" ...
##  - attr(*, "spec")=
##   .. cols(
##   ..   satisfaction_level = col_double(),
##   ..   last_evaluation = col_double(),
##   ..   number_project = col_double(),
##   ..   average_montly_hours = col_double(),
##   ..   time_spend_company = col_double(),
##   ..   Work_accident = col_double(),
##   ..   left = col_double(),
##   ..   promotion_last_5years = col_double(),
##   ..   Department = col_character(),
##   ..   salary = col_character()
##   .. )
##  - attr(*, "problems")=<externalptr>
#1. Correlation: Satisfaction Level vs Last Evaluation
cor_test1 <- cor.test(hr$satisfaction_level, hr$last_evaluation)
print(cor_test1)
## 
##  Pearson's product-moment correlation
## 
## data:  hr$satisfaction_level and hr$last_evaluation
## t = 12.933, df = 14997, p-value < 2.2e-16
## alternative hypothesis: true correlation is not equal to 0
## 95 percent confidence interval:
##  0.08916727 0.12082195
## sample estimates:
##       cor 
## 0.1050212
# Technical interpretation
# p-value interpretation: If p < 0.05, the correlation is statistically significant.
# Here, we check p-value and correlation direction/strength.

# Non-technical interpretation:
# Employees who are more satisfied tend to have higher performance evaluations.

# Plot
ggplot(hr, aes(x = satisfaction_level, y = last_evaluation)) +
  geom_point(alpha = 0.3) +
  geom_smooth(method = "lm", se = FALSE, color = "blue") +
  labs(title = "More Satisfied Employees Have Higher Performance Scores",
       x = "Satisfaction Level",
       y = "Last Evaluation")
## `geom_smooth()` using formula = 'y ~ x'

#2. Correlation: Average Monthly Hours vs Number of Projects
cor_test2 <- cor.test(hr$average_montly_hours, hr$number_project)
print(cor_test2)
## 
##  Pearson's product-moment correlation
## 
## data:  hr$average_montly_hours and hr$number_project
## t = 56.219, df = 14997, p-value < 2.2e-16
## alternative hypothesis: true correlation is not equal to 0
## 95 percent confidence interval:
##  0.4039037 0.4303411
## sample estimates:
##       cor 
## 0.4172106
# Technical interpretation
# Significant p-value and positive correlation suggest more projects are linked to more hours worked.

# Non-technical interpretation:
# Employees working on more projects tend to work longer hours per month.

# Plot
ggplot(hr, aes(x = number_project, y = average_montly_hours)) +
  geom_point(alpha = 0.3) +
  geom_smooth(method = "lm", se = FALSE, color = "red") +
  labs(title = "More Projects Mean More Monthly Work Hours",
       x = "Number of Projects",
       y = "Average Monthly Hours")
## `geom_smooth()` using formula = 'y ~ x'

#3. Correlation: Time Spent at Company vs Satisfaction Level
cor_test3 <- cor.test(hr$time_spend_company, hr$satisfaction_level)
print(cor_test3)
## 
##  Pearson's product-moment correlation
## 
## data:  hr$time_spend_company and hr$satisfaction_level
## t = -12.416, df = 14997, p-value < 2.2e-16
## alternative hypothesis: true correlation is not equal to 0
## 95 percent confidence interval:
##  -0.11668153 -0.08499948
## sample estimates:
##        cor 
## -0.1008661
# Technical interpretation
# Negative correlation and significant p-value indicate that longer tenure may be linked to lower satisfaction.

# Non-technical interpretation:
# The longer employees stay, the less satisfied they tend to be.

# Plot
ggplot(hr, aes(x = time_spend_company, y = satisfaction_level)) +
  geom_point(alpha = 0.3) +
  geom_smooth(method = "lm", se = FALSE, color = "purple") +
  labs(title = "Longer Time at Company is Linked to Lower Satisfaction",
       x = "Years at Company",
       y = "Satisfaction Level")
## `geom_smooth()` using formula = 'y ~ x'

#4. Correlation: Last Evaluation vs Number of Projects
cor_test4 <- cor.test(hr$last_evaluation, hr$number_project)
print(cor_test4)
## 
##  Pearson's product-moment correlation
## 
## data:  hr$last_evaluation and hr$number_project
## t = 45.656, df = 14997, p-value < 2.2e-16
## alternative hypothesis: true correlation is not equal to 0
## 95 percent confidence interval:
##  0.3352028 0.3633053
## sample estimates:
##       cor 
## 0.3493326
# Technical interpretation
# Positive and significant correlation shows that employees with more projects get higher evaluations.

# Non-technical interpretation:
# Employees who work on more projects receive higher performance evaluations.

# Plot
ggplot(hr, aes(x = number_project, y = last_evaluation)) +
  geom_point(alpha = 0.3) +
  geom_smooth(method = "lm", se = FALSE, color = "green") +
  labs(title = "More Projects Lead to Better Evaluations",
       x = "Number of Projects",
       y = "Last Evaluation Score")
## `geom_smooth()` using formula = 'y ~ x'