Nina Arh

Data and research questions

This dataset contains information about the salaries of employees at a company. Each row represents a different employee, and the columns include information about age, gender, years of experience, and salary.

Source of data: Kaggle

(Url: https://www.kaggle.com/datasets/sinhasatwik/salary-base-data/data)

#importing data
data1 <- read_csv("~/Desktop/IMB/Multivariate /HW/H3/Salary_Data.csv")
## Rows: 6704 Columns: 6
## ── Column specification ────────────────────────────────────────────────────────────────────────────────────────────────
## Delimiter: ","
## chr (3): Gender, Education Level, Job Title
## dbl (3): Age, Years of Experience, Salary
## 
## ℹ 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.
#selecting random 200 units
set.seed(1)  
data1 <- data1[sample(nrow(data1), 200), ]

#removing unwanted variables
data1 <- data1[, c(-3, -4)]

#changing the name of the variable
colnames(data1)[3] <- "Experience"

#creating new variable ID
data1$ID <- 1:nrow(data1)
data1 <- data1[, c("ID", names(data1)[-length(data1)])]

head(data1)
## # A tibble: 6 × 5
##      ID   Age Gender Experience Salary
##   <int> <dbl> <chr>       <dbl>  <dbl>
## 1     1    33 Female         11 195000
## 2     2    36 Male           11 135000
## 3     3    30 Female          5 105000
## 4     4    34 Male           12 170000
## 5     5    32 Male            7 135000
## 6     6    34 Male            8  90000

Unit of observation: one person- employee

Sample size: original sample size was 6704 but I will only be using a sample of 200 randomly selected observations.

Number of variables: 5

Explanation of variables:

Discriptive statistics

summary(data1[,-c(1,3)])
##       Age          Experience         Salary      
##  Min.   :21.00   Min.   : 0.000   Min.   : 25000  
##  1st Qu.:28.00   1st Qu.: 3.000   1st Qu.: 68750  
##  Median :31.00   Median : 6.000   Median :120000  
##  Mean   :32.97   Mean   : 7.628   Mean   :116010  
##  3rd Qu.:36.00   3rd Qu.:11.000   3rd Qu.:160000  
##  Max.   :52.00   Max.   :24.000   Max.   :250000

RQ: The main goal of the data analysis is to see how average age, gender and average years of experience influence the salary of employees.

The dependent variable in the regression model is salary and the three explanatory variables are age, years of experience and gender.

Increasing age and experience should have a positive effect on salary of an employee and the salary should be higher for male employees because on the gender pay gap. This hypothesis is supported by an article by Elise Gould, posted on Economic Policy Institute, retrieved on January 23, 2024 from: https://www.epi.org/publication/what-is-the-gender-pay-gap-and-is-it-real/.

#transforming categorical variable into factor
data1$GenderF <- factor(data1$Gender, 
                         levels = c("Female", "Male"), 
                         labels = c("F", "M"))

Linearity and correlation

scatterplotMatrix(data1[ , c(-1, -3, -6)], 
                  smooth = FALSE) 

From the scatter plot we can observe a positive linear relationship between the dependent variable and both explanatory variables. We can not present categorical variables in scatter plot, that is why gender is excluded.

rcorr(as.matrix(data1[ , c(-1, -3, -6)]))
##             Age Experience Salary
## Age        1.00       0.94   0.67
## Experience 0.94       1.00   0.79
## Salary     0.67       0.79   1.00
## 
## n= 200 
## 
## 
## P
##            Age Experience Salary
## Age             0          0    
## Experience  0              0    
## Salary      0   0

Multicolinearity

fit1 <- lm( Salary ~ Age + Gender + Experience, 
           data = data1)
vif(fit1) 
##        Age     Gender Experience 
##   8.164109   1.002316   8.159536
mean(vif(fit1))
## [1] 5.77532

For VIF for variables age and experience is bigger than 5, meaning there is a problem with multicolinearity. The solution for this violation is removing one of the variables. I will remove years of experience.

data1 <- data1[, c(-4)]
fit2 <- lm( Salary ~ Age + Gender, 
           data = data1)

vif(fit2) 
##      Age   Gender 
## 1.002222 1.002222
mean(vif(fit2))
## [1] 1.002222

By checking once again we can see that both VIFs are close to 1.

Normal distribution and outliers

data1$StdResid <- round(rstandard(fit2), 3) #Standardized residuals
data1$CooksD <- round(cooks.distance(fit2), 3) #Cooks distances

hist(data1$StdResid, 
     xlab = "Standardized residuals", 
     ylab = "Frequency", 
     main = "Histogram of standardized residuals")

Histogram suggest that standardized residuals are not normally distributed (skewed to the right), but we will confirm this with the Shapiro-Wilk normality test. We can also see that there are no outliers, as no value spreads beyond -/+ 3.

shapiro.test(data1$StdResid)
## 
##  Shapiro-Wilk normality test
## 
## data:  data1$StdResid
## W = 0.9591, p-value = 1.582e-05

Hypothesis bot Shapiro-Wilk test:

We reject the null hypothesis at p<0.001 meaning the distribution od standardized residuals is not normal. However, since we have a big sample, violation of normality is not a problem.

Units with high impact

hist(data1$CooksD, 
     xlab = "Cooks distance", 
     ylab = "Frequency", 
     main = "Histogram of Cooks distances")

With the histogram of Cooks distances, we check for potential units with high impact. While no unit has Cooks distance above 1, we see a gap between the observed units and therefore have to remove the separated units. We do that by showing the units with the highest distances and removing every unit that is over the gap. We remove it by ID.

head(data1[order(-data1$CooksD),], 5) #Six units with highest value of Cooks distance
## # A tibble: 5 × 7
##      ID   Age Gender Salary GenderF StdResid CooksD
##   <int> <dbl> <chr>   <dbl> <fct>      <dbl>  <dbl>
## 1    24    29 Female 180000 F           2.09  0.019
## 2    92    29 Female 180000 F           2.09  0.019
## 3   147    29 Female 180000 F           2.09  0.019
## 4    23    31 Male   195000 M           2.27  0.016
## 5    54    33 Female 198000 F           2.02  0.016

In our case we remove the top 3 units in the table above.

data1 <- data1 %>% filter(!(ID %in% c(24, 92, 147)))
hist(data1$CooksD, 
     xlab = "Cooks distance", 
     ylab = "Frequency", 
     main = "Histogram of Cooks distances")

We check the histogram once again and see that there are no outliers left.

Homoskedasticity

fit2 <- lm( Salary ~ Age + Gender, 
           data = data1)

data1$StdFitted <- scale(fit2$fitted.values)


scatterplot(y = data1$StdResid, x = data1$StdFitted,
            ylab = "Standardized residuals",
            xlab = "Standardized fitted values",
            boxplots = FALSE,
            regLine = FALSE,
            smooth = FALSE)

From the scatter plot we can assume that homoskedasticity is violated but we will do a Breusch Pagan test to confirm it. We see that the model is linear as there are no shapes being formed.

library(olsrr)
## 
## Attaching package: 'olsrr'
## The following object is masked from 'package:MASS':
## 
##     cement
## The following object is masked from 'package:datasets':
## 
##     rivers
ols_test_breusch_pagan(fit2)
## 
##  Breusch Pagan Test for Heteroskedasticity
##  -----------------------------------------
##  Ho: the variance is constant            
##  Ha: the variance is not constant        
## 
##                Data                
##  ----------------------------------
##  Response : Salary 
##  Variables: fitted values of Salary 
## 
##         Test Summary          
##  -----------------------------
##  DF            =    1 
##  Chi2          =    5.503456 
##  Prob > Chi2   =    0.01897893

Hypothesis for BP test:

We reject H0 at p=0.019 and can say that there is no homoskedasticity in the model. Because of violated assumption of homoskedasticity, robust standard errors will have to be implemented.

Estimation of the regression model

Checking if we need to include interactions, but from the graph we can see that the lines have similar slope.

scatterplot(Salary ~ Age | GenderF,
            main = "Y ~ X", 
            smooth = FALSE,
            data = data1)

fit_robust <- lm_robust(Salary ~ Age + Gender,  
                        se_type = "HC1", #White's robust standard errors
                        data = data1)

summary(fit_robust)
## 
## Call:
## lm_robust(formula = Salary ~ Age + Gender, data = data1, se_type = "HC1")
## 
## Standard error type:  HC1 
## 
## Coefficients:
##             Estimate Std. Error  t value  Pr(>|t|) CI Lower CI Upper  DF
## (Intercept) -58575.2    11020.7 -5.31501 2.915e-07   -80311   -36839 194
## Age           5248.3      292.5 17.94412 4.457e-43     4671     5825 194
## GenderMale     444.6     5715.7  0.07778 9.381e-01   -10828    11717 194
## 
## Multiple R-squared:  0.4738 ,    Adjusted R-squared:  0.4684 
## F-statistic: 164.5 on 2 and 194 DF,  p-value: < 2.2e-16

Explanation of the coefficients:

Age: H0 = beta1 is equal to 0, H1 = beta1 is not equal to 0 We reject H0 at p<0.001- significant, positive effect, as we expected. - If age of an employee increases by one year, salary increases on average by 5248.3 US dollars per year (p<0.001), assuming everything else stays the same.

Gender: H0 = beta2 is equal to 0, H1 = beta2 is not equal to 0 We can not reject H0 at p= 0.939 - not significant (For the practice o this homework i will show how it would be explained if it would be significant: Given the age of an employee, male employees earn on average 444.6 more US dollars per year than female employees at p=___ .)

H0: beta1 = beta2 = 0

H1: At least one beta is different from 0

We can reject the null hypothesis at p<0.001 and conclude that there is a relationship between the dependent variable and at least one explanatory variable. The regression model is a good fit because we explain at least something.

Conclusion

BSD we can say that only Age has a significant effect (p<0.001) on salary. The effect is positive as we assumed. Gender does not have a significant effect (p=0.939), which rejects our assumption about males having a bigger salary than females.