Author: Edi Oblak
How nine-month salary of professors is affected by rank, discipline, years since PhD, years of service and gender.
#data() # Use to find data
#data(package = .packages(all.available = TRUE))
#install.packages("carData")
data("Salaries", package="carData")
mydata <- force(Salaries) # Importing data from data().
mydata$ID <- seq(1, nrow(mydata))# We add ID number for each unit
head(mydata[ , -7])
## rank discipline yrs.since.phd yrs.service sex salary
## 1 Prof B 19 18 Male 139750
## 2 Prof B 20 16 Male 173200
## 3 AsstProf B 4 3 Male 79750
## 4 Prof B 45 39 Male 115000
## 5 Prof B 40 41 Male 141500
## 6 AssocProf B 6 6 Male 97000
Unit of observation in our data is professor in a college in the U.S. Sample size is 397, which is appropriate for our analysis. In this data, there is no missing information.
Source of data: Fox J. and Weisberg, S. (2019) An R Companion to Applied Regression, Third Edition, Sage.
summary(mydata[ ,-7])
## rank discipline yrs.since.phd yrs.service sex salary
## AsstProf : 67 A:181 Min. : 1.00 Min. : 0.00 Female: 39 Min. : 57800
## AssocProf: 64 B:216 1st Qu.:12.00 1st Qu.: 7.00 Male :358 1st Qu.: 91000
## Prof :266 Median :21.00 Median :16.00 Median :107300
## Mean :22.31 Mean :17.61 Mean :113706
## 3rd Qu.:32.00 3rd Qu.:27.00 3rd Qu.:134185
## Max. :56.00 Max. :60.00 Max. :231545
mydata$RankF <- factor(mydata$rank,
levels = c ("Prof","AsstProf","AssocProf"),
labels = c ("P", "AssistP","AssocP"))
mydata$DisciplineF <- factor(mydata$discipline,
levels = c ("B","A"),
labels = c ("applied", "teoretical"))
mydata$GenderF <- factor(mydata$sex,
levels = c ("Male","Female"),
labels = c ("M", "F"))
head(mydata)
## rank discipline yrs.since.phd yrs.service sex salary ID RankF DisciplineF GenderF
## 1 Prof B 19 18 Male 139750 1 P applied M
## 2 Prof B 20 16 Male 173200 2 P applied M
## 3 AsstProf B 4 3 Male 79750 3 AssistP applied M
## 4 Prof B 45 39 Male 115000 4 P applied M
## 5 Prof B 40 41 Male 141500 5 P applied M
## 6 AssocProf B 6 6 Male 97000 6 AssocP applied M
In our regression model we are explaining how much effect does years since Phd, years of service, rank, discipline and gender have on nine-month salary of professors. We expect that professors who have PhD longer and are longer in service to have higher salary. About effect of discipline and gender it is hard to say how it affect salary, so we won’t make any statements about that yet.
We can check for correlation between chosen variables.
library(car)
## Loading required package: carData
## Warning: package 'carData' was built under R version 4.3.2
scatterplotMatrix(mydata[ , c(-1, -2, -5, -7, -8, -9, -10)], #Scatterplot matrix
smooth = FALSE)
From scatterplotMatrix we can see that there is increasing linear correlation between salary and years since Phd and also between salary and years of service. We will have to check this two explanatory variables for multicolinearity! But first, we have to set the regression model. Howewer in each case we can conclude that linearity is fulfiled.
fit1 <- lm(salary ~ yrs.since.phd + yrs.service + RankF + DisciplineF + GenderF, #Dependent and explanatory variables
data = mydata) #Name of the data frame
summary(fit1)
##
## Call:
## lm(formula = salary ~ yrs.since.phd + yrs.service + RankF + DisciplineF +
## GenderF, data = mydata)
##
## Residuals:
## Min 1Q Median 3Q Max
## -65248 -13211 -1775 10384 99592
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 130222.3 3907.3 33.328 < 2e-16 ***
## yrs.since.phd 535.1 241.0 2.220 0.0270 *
## yrs.service -489.5 211.9 -2.310 0.0214 *
## RankFAssistP -45066.0 4237.5 -10.635 < 2e-16 ***
## RankFAssocP -32158.4 3540.6 -9.083 < 2e-16 ***
## DisciplineFteoretical -14417.6 2342.9 -6.154 1.88e-09 ***
## GenderFF -4783.5 3858.7 -1.240 0.2158
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 22540 on 390 degrees of freedom
## Multiple R-squared: 0.4547, Adjusted R-squared: 0.4463
## F-statistic: 54.2 on 6 and 390 DF, p-value: < 2.2e-16
vif(fit1) #Multicolinearity
## GVIF Df GVIF^(1/(2*Df))
## yrs.since.phd 7.518936 1 2.742068
## yrs.service 5.923038 1 2.433729
## RankF 2.013193 2 1.191163
## DisciplineF 1.064105 1 1.031555
## GenderF 1.030805 1 1.015285
mean(vif(fit1))
## [1] 2.130925
From VIF (Variance Inflation Factor) we can see that there is too strong correlation between years since Phd and years of services. The values which we are expecting for VIF should be as close to 1, but surely all should be below 2.24 (we are looking at the right column of results). To solve this problem, we will adjust our regression model, from which we will exclude years since PhD.
fit2 <- lm(salary ~ yrs.service + RankF + DisciplineF + GenderF, #Dependent and explanatory variables
data = mydata) #Name of the data frame
summary(fit2)
##
## Call:
## lm(formula = salary ~ yrs.service + RankF + DisciplineF + GenderF,
## data = mydata)
##
## Residuals:
## Min 1Q Median 3Q Max
## -64202 -14255 -1533 10571 99163
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 135755.93 3024.29 44.889 < 2e-16 ***
## yrs.service -88.78 111.64 -0.795 0.427
## RankFAssistP -49159.64 3834.49 -12.820 < 2e-16 ***
## RankFAssocP -34599.24 3382.52 -10.229 < 2e-16 ***
## DisciplineFteoretical -13473.38 2315.50 -5.819 1.24e-08 ***
## GenderFF -4771.25 3878.00 -1.230 0.219
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 22650 on 391 degrees of freedom
## Multiple R-squared: 0.4478, Adjusted R-squared: 0.4407
## F-statistic: 63.41 on 5 and 391 DF, p-value: < 2.2e-16
We check new regression model for multicolinearity.
vif(fit2) #Multicolinearity
## GVIF Df GVIF^(1/(2*Df))
## yrs.service 1.627110 1 1.275582
## RankF 1.597441 2 1.124233
## DisciplineF 1.029040 1 1.014416
## GenderF 1.030803 1 1.015284
mean(vif(fit2))
## [1] 1.226159
When checking VIF in a new regression model, all values are below 2.24 with average value of 1.226, which is close enough to value 1. We can conclude that there is no too strong multicolinearity in our model.
mydata$StdResid <- round(rstandard(fit2), 3)
mydata$CooksD <- round(cooks.distance(fit2), 3)
hist(mydata$StdResid,
xlab = "Standardized residuals",
ylab = "Frequency",
main = "Histogram of standardized residuals")
From histogram of standardized residuals we can see that there are some units in our sample which should be removed as outliers (value of their std.res. is more than 3).
hist(mydata$CooksD,
xlab = "Cooks distance",
ylab = "Frequency",
main = "Histrogram of Cooks distances")
From histogram of Cooks distances we can see that there are some gaps on a graph. Even though there are all values lower than 1, we will remove all the values which have Cooks distances value more than 0.02.
head(mydata[order(- mydata$StdResid),], 10) #Ten units with highest value of stand. residuals
## rank discipline yrs.since.phd yrs.service sex salary ID RankF DisciplineF GenderF StdResid CooksD
## 44 Prof B 38 38 Male 231545 44 P applied M 4.406 0.042
## 365 Prof A 43 43 Male 205500 365 P teoretical M 3.871 0.038
## 250 Prof A 29 7 Male 204000 250 P teoretical M 3.661 0.031
## 272 Prof A 42 18 Male 194800 272 P teoretical M 3.284 0.014
## 390 Prof A 33 18 Male 186023 390 P teoretical M 2.895 0.011
## 331 Prof B 49 60 Male 192253 331 P applied M 2.788 0.057
## 293 Prof A 39 9 Male 183800 293 P teoretical M 2.768 0.016
## 78 Prof B 26 19 Male 193000 78 P applied M 2.610 0.007
## 199 Prof B 34 33 Male 189409 199 P applied M 2.510 0.010
## 351 Prof B 56 49 Male 186960 351 P applied M 2.483 0.026
From values of standardized residuals we can see that units with ID 44, 365, 250 and 272 has to be removed.
head(mydata[order(-mydata$CooksD),], 10) #Six units with highest value of Cooks distance
## rank discipline yrs.since.phd yrs.service sex salary ID RankF DisciplineF GenderF StdResid CooksD
## 331 Prof B 49 60 Male 192253 331 P applied M 2.788 0.057
## 44 Prof B 38 38 Male 231545 44 P applied M 4.406 0.042
## 365 Prof A 43 43 Male 205500 365 P teoretical M 3.871 0.038
## 250 Prof A 29 7 Male 204000 250 P teoretical M 3.661 0.031
## 283 Prof A 51 51 Male 57800 283 P teoretical M -2.679 0.029
## 318 Prof B 46 45 Male 67559 318 P applied M -2.862 0.027
## 351 Prof B 56 49 Male 186960 351 P applied M 2.483 0.026
## 132 Prof A 56 57 Male 76840 132 P teoretical M -1.812 0.018
## 293 Prof A 39 9 Male 183800 293 P teoretical M 2.768 0.016
## 272 Prof A 42 18 Male 194800 272 P teoretical M 3.284 0.014
From values of Cooks distances we can se that units with ID 331, 44, 365, 250, 283, 318 and 351 has to be removed.
Removing the units:
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(44, 250, 272, 283, 318, 331, 351, 365)) #Removing units
With Shapiro-Wilk test we can test standardized residuals for normal distribution.
shapiro.test(mydata$StdResid)
##
## Shapiro-Wilk normality test
##
## data: mydata$StdResid
## W = 0.98077, p-value = 4.777e-05
Based on sample data we reject the hypothesis that errors are normally distributed (p < 0.001). Since we have a sample of almost 400 units, we proceed with our analysis.
Now we have to re-fit the model, so that it will be done without excluded values.
fit3 <- lm(salary ~ yrs.service + RankF + DisciplineF + GenderF, #Dependent and explanatory variables
data = mydata) #Name of the data frame
To check the data for homoskedasticity we perform scatterplot between standardized residuals and standardized fitted values. To be sure about our assumptions we can also perform Breusch Pagan test.
mydata$StdFitted <- scale(fit3$fitted.values)
library(car)
scatterplot(y = mydata$StdResid, x = mydata$StdFitted,
ylab = "Standardized residuals",
xlab = "Standardized fitted values",
boxplots = FALSE,
regLine = FALSE,
smooth = FALSE)
library(olsrr)
##
## Attaching package: 'olsrr'
## 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 = 56.78473
## Prob > Chi2 = 4.862269e-14
It is seen from the graph that our data is heteroscedastic. Breuch Pagan test confirms that as we have to reject the null hypothesis which assumes homosedasticty (p < 0.001). We have to use robust standard errors.
library(estimatr)
fit_robust <- lm_robust(salary ~ yrs.service + RankF + DisciplineF + GenderF,
se_type = "HC1", #White's robust standard errors
data = mydata)
summary(fit_robust)
##
## Call:
## lm_robust(formula = salary ~ yrs.service + RankF + DisciplineF +
## GenderF, data = mydata, se_type = "HC1")
##
## Standard error type: HC1
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|) CI Lower CI Upper DF
## (Intercept) 135813.5 2985.1 45.497 1.598e-156 129944.2 141682.77 383
## yrs.service -151.1 117.9 -1.282 2.005e-01 -382.8 80.61 383
## RankFAssistP -49149.9 2788.7 -17.625 2.452e-51 -54632.9 -43666.85 383
## RankFAssocP -33988.0 2210.2 -15.378 6.727e-42 -38333.6 -29642.28 383
## DisciplineFteoretical -13480.8 2127.5 -6.336 6.612e-10 -17663.9 -9297.69 383
## GenderFF -4264.1 2439.4 -1.748 8.126e-02 -9060.3 532.16 383
##
## Multiple R-squared: 0.4961 , Adjusted R-squared: 0.4895
## F-statistic: 154.6 on 5 and 383 DF, p-value: < 2.2e-16
We found out that variability of nine-month salary of professors is affected by rank, discipline, years since PhD, years of service and gender (p < 0.001). The model is explaining at leas some of the variability (from F-statistic).
We can not assume that if years of service of professor increases by 1 year, then his average nine-month salary will decrease by 151.1 dollars (p = 0.201).
Given the values of other explanatory variables, Assistant Professor on average in nine-month period earns 49149.9 dollars less compared to Professor (p < 0.001).
Given the values of other explanatory variables, Associate Professor on average in nine-month period earns 33988 dollars less compared to Professor (p < 0.001).
Given the values of other explanatory variables, professors that works in theoretical department on average in nine-month period earns 13480.8 dollars less, compared to professor that works in applied department (p < 0.001).
Gender is statistically insignificant when addressing how nine-month salary of professors is affected (p = 0.082).
49.61 % of variability of nine-month salary of professors is explained by variability of rank, discipline, years since PhD, years of service and gender.