Anže Ambrožič

HOMEWORK 3

Research question: Can years of experiences and gender of an employee explain the differences in the salary?

mydata <- read.table("./Salary Data.csv", header=TRUE, sep=",") #Importing dataset
mydata$ID <- seq(1, nrow(mydata)) #Creating new variable called ID
mydata <- mydata[, c(7, 6, 5, 2)] #Keeping variables I will use for regression
mydata$Gender <- factor(mydata$Gender,
                         levels = c("Male", "Female"),
                         labels = c("Male", "Female"))
library(tidyr)
mydata <- drop_na(mydata) #Removing units with missing values.

I removed 2 units, since they had missing values.

head(mydata) #Showing 6 units
##   ID Salary Years.of.Experience Gender
## 1  1  65000                   3   Male
## 2  2 150000                  15   Male
## 3  3 200000                  20 Female
## 4  4 120000                  12   Male
## 5  5  45000                   1   Male
## 6  6 110000                  10   Male

EXPLAINING DATA: Unit of observation - Employee Sample size - 191 units Variables: Salary - Annual salary in dollars Gender - gender of an employee (Male or Female)

The data set was taken from kaggle.com, the data set is called Salary Prediction dataset and has 193 units.

summary(mydata)
##        ID             Salary       Years.of.Experience    Gender   
##  Min.   :  1.00   Min.   :   350   Min.   : 0.00       Male  :110  
##  1st Qu.: 48.50   1st Qu.: 60000   1st Qu.: 4.50       Female: 81  
##  Median : 97.00   Median :100000   Median :10.00                   
##  Mean   : 96.84   Mean   :103300   Mean   :10.46                   
##  3rd Qu.:145.50   3rd Qu.:140000   3rd Qu.:15.50                   
##  Max.   :193.00   Max.   :250000   Max.   :25.00

Minimum salary is 350 dollars and maximum salary is 250000 dollars. Minimum Years of experience is 0 years and maximum is 25 years. Average salary is 103300 dollars and average years of experience is 10.46 years. There are 110 males and 81 females.

DEFINING REGRESSION MODEL

In the regression model i will be analyzing the effect that years of experiences and gender have on the salary. I believe that a relationship will be positive between salary and years of experiences. It will be interesting to find out the effect of gender on the salary.

REGRESSION

ASSUMPTIONS: - Linearity in parameters (I WILL DO SCATTERPLOT) - Expected value of errors equals to 0 (WE INCLUDED ALL OF RELEVANT VARIABLES IN CORRECT FORM AND INCLUSION OF REGRESSION CONSTANT, SO WE ASSUME ASSAMPTION IS MET) - Homoskedasticity needs to be met - Errors are independent (MET - UNIT IS OBSERVED ONLY ONCE) - No perfect multiculinarity (MET BECAUSE THERE IS ONLY ONE EXPLANATORY NUMERICAL VARIABLE - YEARS OF EXPERIENCES)

THERE ARE ALSO ADDITIONAL ASSUMPTIONS Normal Distribution of errors - even if violated we can ignore since our sample is big enough - 191 units. Number of units must be greater than a number of variables - this is met

ADDITIONAL REQUIREMENTS Dependent variable is numerical, explanatory variables are numerical or dichotomous. Explanatory varible must vary.

Linearity

library(car)
## Loading required package: carData
scatterplot(Salary ~ Years.of.Experience, 
            ylab = "Salary", 
            xlab = "Years of Experience", 
            smooth = FALSE, 
            data = mydata)

Linearity is met as we see in the scatterplot.

Checking Homoscedasticity

fit <- lm(Salary ~ Years.of.Experience, 
          data = mydata)
library(olsrr)
## 
## Attaching package: 'olsrr'
## The following object is masked from 'package:datasets':
## 
##     rivers
ols_test_breusch_pagan(fit)
## 
##  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          =    30.97735 
##  Prob > Chi2   =    2.610573e-08

H0: Homoscedasticity is met. H1: Homoscedasticity is violated.

P value is less then 0.1% (2.610573e-08), so we reject H0. Homoscedasticity is violated. Since we rejected H0, I will later perform White’s robust correction.

Checking Std Residuals and Cooks Distances

mydata$StdResid <- round(rstandard(fit), 3) 
mydata$CooksD <- round(cooks.distance(fit), 3)

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

There are some units that fall out of +/- 3 range of Std Residuals so those units will be removed later. We will check the distributions of errors. We will use Shapiro-Wilk normality test.

shapiro.test(mydata$StdResid)
## 
##  Shapiro-Wilk normality test
## 
## data:  mydata$StdResid
## W = 0.94125, p-value = 5.021e-07

H0:Errors are normally distributed. H1:Errors are not normally distributed.

Calculated P-value is less than 1% (5.021e-07), so we reject the null hypothesis that Errors are normaly distributed. Even though that we rejected normal distributions of error we will ignore the violation since our sample has 191 observed units.

I will also check histogram of Cooks distances.

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

There is one gap between 0.10 and approximately 0.14, so we will remove some units.

head(mydata[order(- mydata$StdResid),], 10)
##      ID Salary Years.of.Experience Gender StdResid CooksD
## 43   43 250000                  24   Male    3.165  0.145
## 37   37 190000                  16   Male    2.809  0.036
## 27   27 200000                  19 Female    2.222  0.036
## 81   81 115000                   7 Female    2.011  0.014
## 3     3 200000                  20 Female    1.835  0.028
## 132 133 100000                   6   Male    1.543  0.009
## 117 118 120000                   9 Female    1.520  0.006
## 153 155 120000                   9   Male    1.520  0.006
## 178 180 110000                   8 Female    1.337  0.005
## 96   97  95000                   6   Male    1.257  0.006

I will remove unit 43.

head(mydata[order(mydata$StdResid),], 10)
##      ID Salary Years.of.Experience Gender StdResid CooksD
## 59   59  65000                15.0   Male   -3.948  0.061
## 51   51 110000                20.0 Female   -3.331  0.094
## 45   45  80000                15.0   Male   -3.091  0.038
## 57   57 130000                22.0   Male   -2.970  0.099
## 48   48 105000                18.0 Female   -2.833  0.050
## 70   70  55000                10.0   Male   -2.577  0.018
## 133 134    350                 1.5   Male   -2.415  0.045
## 36   36  70000                11.0   Male   -2.108  0.012
## 17   17  65000                10.0   Male   -2.006  0.011
## 31   31  80000                12.0   Male   -1.925  0.010

I will also remove units 59, 51 and 45 since they also fall out of +/- 3 range.

Now I will check for Cooks distances.

head(mydata[order(- mydata$CooksD),], 10)
##      ID Salary Years.of.Experience Gender StdResid CooksD
## 43   43 250000                24.0   Male    3.165  0.145
## 57   57 130000                22.0   Male   -2.970  0.099
## 51   51 110000                20.0 Female   -3.331  0.094
## 59   59  65000                15.0   Male   -3.948  0.061
## 48   48 105000                18.0 Female   -2.833  0.050
## 133 134    350                 1.5   Male   -2.415  0.045
## 45   45  80000                15.0   Male   -3.091  0.038
## 27   27 200000                19.0 Female    2.222  0.036
## 37   37 190000                16.0   Male    2.809  0.036
## 74   74 135000                20.0   Male   -1.896  0.030

Unit 43 will be removed since it was the reason for the gap in the histogram. This unit also falls out of +/- 3 range of Std Residuals.

library(dplyr)
## 
## Attaching package: 'dplyr'
## The following object is masked from 'package:car':
## 
##     recode
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
mydata <- mydata %>%
  filter(!ID %in% c(43, 45, 51, 59))

4 units has been removed.

head(mydata[order(- mydata$CooksD),], 10)
##      ID Salary Years.of.Experience Gender StdResid CooksD
## 54   57 130000                22.0   Male   -2.970  0.099
## 46   48 105000                18.0 Female   -2.833  0.050
## 129 134    350                 1.5   Male   -2.415  0.045
## 27   27 200000                19.0 Female    2.222  0.036
## 37   37 190000                16.0   Male    2.809  0.036
## 70   74 135000                20.0   Male   -1.896  0.030
## 3     3 200000                20.0 Female    1.835  0.028
## 58   62 180000                25.0 Female   -1.271  0.026
## 66   70  55000                10.0   Male   -2.577  0.018
## 39   39 160000                22.0   Male   -1.244  0.017
head(mydata[order(mydata$StdResid),], 10)
##      ID Salary Years.of.Experience Gender StdResid CooksD
## 54   57 130000                22.0   Male   -2.970  0.099
## 46   48 105000                18.0 Female   -2.833  0.050
## 66   70  55000                10.0   Male   -2.577  0.018
## 129 134    350                 1.5   Male   -2.415  0.045
## 36   36  70000                11.0   Male   -2.108  0.012
## 17   17  65000                10.0   Male   -2.006  0.011
## 31   31  80000                12.0   Male   -1.925  0.010
## 70   74 135000                20.0   Male   -1.896  0.030
## 52   55 100000                14.0   Male   -1.560  0.008
## 59   63  80000                11.0 Female   -1.538  0.006
head(mydata[order(- mydata$StdResid),], 10)
##      ID Salary Years.of.Experience Gender StdResid CooksD
## 37   37 190000                  16   Male    2.809  0.036
## 27   27 200000                  19 Female    2.222  0.036
## 77   81 115000                   7 Female    2.011  0.014
## 3     3 200000                  20 Female    1.835  0.028
## 128 133 100000                   6   Male    1.543  0.009
## 113 118 120000                   9 Female    1.520  0.006
## 149 155 120000                   9   Male    1.520  0.006
## 174 180 110000                   8 Female    1.337  0.005
## 92   97  95000                   6   Male    1.257  0.006
## 57   61 120000                  10 Female    1.133  0.003

We have to additionally remove units 57, 48, 37.

mydata <- mydata %>%
  filter(!ID %in% c(57, 48, 37))

We removed another 3 units. Now I will check again ig there are any problematic units left.

head(mydata[order(- mydata$StdResid),], 10)
##      ID Salary Years.of.Experience Gender StdResid CooksD
## 27   27 200000                  19 Female    2.222  0.036
## 74   81 115000                   7 Female    2.011  0.014
## 3     3 200000                  20 Female    1.835  0.028
## 125 133 100000                   6   Male    1.543  0.009
## 110 118 120000                   9 Female    1.520  0.006
## 146 155 120000                   9   Male    1.520  0.006
## 171 180 110000                   8 Female    1.337  0.005
## 89   97  95000                   6   Male    1.257  0.006
## 54   61 120000                  10 Female    1.133  0.003
## 99  107 120000                  10   Male    1.133  0.003
head(mydata[order(mydata$StdResid),], 10)
##      ID Salary Years.of.Experience Gender StdResid CooksD
## 63   70  55000                10.0   Male   -2.577  0.018
## 126 134    350                 1.5   Male   -2.415  0.045
## 36   36  70000                11.0   Male   -2.108  0.012
## 17   17  65000                10.0   Male   -2.006  0.011
## 31   31  80000                12.0   Male   -1.925  0.010
## 67   74 135000                20.0   Male   -1.896  0.030
## 50   55 100000                14.0   Male   -1.560  0.008
## 56   63  80000                11.0 Female   -1.538  0.006
## 51   56 110000                15.0   Male   -1.377  0.007
## 76   83  90000                12.0   Male   -1.355  0.005
head(mydata[order(- mydata$StdResid),], 10)
##      ID Salary Years.of.Experience Gender StdResid CooksD
## 27   27 200000                  19 Female    2.222  0.036
## 74   81 115000                   7 Female    2.011  0.014
## 3     3 200000                  20 Female    1.835  0.028
## 125 133 100000                   6   Male    1.543  0.009
## 110 118 120000                   9 Female    1.520  0.006
## 146 155 120000                   9   Male    1.520  0.006
## 171 180 110000                   8 Female    1.337  0.005
## 89   97  95000                   6   Male    1.257  0.006
## 54   61 120000                  10 Female    1.133  0.003
## 99  107 120000                  10   Male    1.133  0.003

There are no problematic units left.

Since homoscedasticity is violated I will do White’s robust correction. First I will include only Years of Experience and then I will add Gender into the formula.

WHITE’S ROBUST TEST

library(estimatr)
## Warning: package 'estimatr' was built under R version 4.3.2
fitA <- lm_robust(Salary ~ Years.of.Experience,  
                  se_type = "HC1",
                  data = mydata)


summary(fitA)
## 
## Call:
## lm_robust(formula = Salary ~ Years.of.Experience, data = mydata, 
##     se_type = "HC1")
## 
## Standard error type:  HC1 
## 
## Coefficients:
##                     Estimate Std. Error t value   Pr(>|t|) CI Lower CI Upper
## (Intercept)            31721     1622.0   19.56  1.326e-46    28521    34921
## Years.of.Experience     6938      153.4   45.23 6.403e-101     6635     7241
##                      DF
## (Intercept)         182
## Years.of.Experience 182
## 
## Multiple R-squared:  0.908 , Adjusted R-squared:  0.9075 
## F-statistic:  2045 on 1 and 182 DF,  p-value: < 2.2e-16
sqrt(summary(fitA)$r.squared)
## [1] 0.9528773

H0: Ro^2 is equal to 0. H1: Ro^2 is greater than 0.

I reject H0 at P value 2.2e-16. Analysis shows that Years of Experience has some effect on the Salary.

Now I will explain some coefficients. Estimated coefficient is 6938. That means that for 1 additional year of Experience, salary on average increases by 6938 dollars annual. Multiple R-Squared is 0.908. That means that 90.08 % of variability of the Salary can be explained by variability of Years of Experience.

Now I will include also gender as a categorical variable.

fitB <- lm_robust(Salary ~ Years.of.Experience + Gender,
           se_type = "HC1",
           data = mydata)

summary(fitB)
## 
## Call:
## lm_robust(formula = Salary ~ Years.of.Experience + Gender, data = mydata, 
##     se_type = "HC1")
## 
## Standard error type:  HC1 
## 
## Coefficients:
##                     Estimate Std. Error t value   Pr(>|t|) CI Lower CI Upper
## (Intercept)            28424     1915.3  14.841  4.087e-33    24645    32203
## Years.of.Experience     6908      149.3  46.255 3.340e-102     6613     7203
## GenderFemale            8390     1956.0   4.289  2.912e-05     4530    12249
##                      DF
## (Intercept)         181
## Years.of.Experience 181
## GenderFemale        181
## 
## Multiple R-squared:  0.916 , Adjusted R-squared:  0.9151 
## F-statistic:  1088 on 2 and 181 DF,  p-value: < 2.2e-16

H0: Ro^2 is equal to 0. H1: Ro^2 is greater than 0.

I reject H0 at p value 2.2e-16. I assume that Years.of.Experience and Gender of an employee has some effect on the Salary of an employee.

I will explain some coefficients: Estimated coefficient for Years.of.Experience is 6908 that means that for every additioanl year of experience, salary on average increases by 6908 dollars annually. Estimated coefficient for GenderMale is -8390, that means given years of Experiences, employees that are males on average earn 8390 less compared to Females in one year. Multiple R squared is 0.916, that means that 91.6 % of varaibility can be explained by the variability of years of experiences and gender.

COMPARING TWO MODELS

library(lmtest)
## Warning: package 'lmtest' was built under R version 4.3.2
## Loading required package: zoo
## Warning: package 'zoo' was built under R version 4.3.2
## 
## Attaching package: 'zoo'
## The following objects are masked from 'package:base':
## 
##     as.Date, as.Date.numeric
waldtest(fitA, fitB, test = "F")
## Wald test
## 
## Model 1: Salary ~ Years.of.Experience
## Model 2: Salary ~ Years.of.Experience + Gender
##   Res.Df Df      F    Pr(>F)    
## 1    182                        
## 2    181  1 18.398 2.912e-05 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

H0: Delta Ro^2 is equal to 0. H1: Delta Ro^2 is greater than 0.

Based on calculated P-Value (2.912e-05) we reject the null hypothesis. I can conclude that model with Years of Experience and Gender is better than model that includes only Years of Experience.

CONCLUSION

After conducting the analysis I can assume that years of experience and Gender can explain the differences in the salary. Calculated P value was less than 0.01% (2.2e-16) so we reject H0 (Ro^2 is equal to 0). The comparison between two models (one including additional categorical variable Gender) has shown that including both variables, years of experience and gender is better than only including years of experience. The calculated P value was 2.912e-05, so we rejected H0 stating that Delta Ro^2 is equal to 0.