data()
data(package = .packages(all.available = TRUE))
How does years in service, years since phd, rank, gender affect a professorβs salary?
library(carData)
## Warning: package 'carData' was built under R version 4.3.2
mydata <- force(Salaries)
head(mydata)
## 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
Sample size: 397
Unit of observation: One professor
Description of the variables:
rank: a factor with levels AssocProf AsstProf Prof
discipline: a factor with levels A (βtheoreticalβ departments) or B (βappliedβ departments).
yrs.since.phd: years since PhD.
yrs.service: years of service.
sex: a factor with levels Female Male
salary: nine-month salary, in dollars
Source: Fox J. and Weisberg, S. (2019) An R Companion to Applied Regression, Third Edition, Sage
mydata$rankF <- factor(mydata$rank,
levels = c("AssocProf", "AsstProf", "Prof"),
labels = c("AssocProf", "AsstProf", "Prof"))
mydata$disciplineF <- factor(mydata$discipline,
levels = c("A", "B"),
labels = c("theoretical departments", "applied departments"))
mydata$GenderF <- factor(mydata$sex,
levels = c("Female", "Male"),
labels = c("Female", "Male"))
In the first equation I use the years in service, years since phd and gender, as the independent variables and then I include rank in the fit and compare the two models
Salary = Intercept + Beta1(Years in service) + Beta2(Years in phd) + Beta3(Gender) + Error
summary(mydata[ ,c(1,2,3,4,5,6)])
## rank discipline yrs.since.phd yrs.service sex
## AsstProf : 67 A:181 Min. : 1.00 Min. : 0.00 Female: 39
## AssocProf: 64 B:216 1st Qu.:12.00 1st Qu.: 7.00 Male :358
## Prof :266 Median :21.00 Median :16.00
## Mean :22.31 Mean :17.61
## 3rd Qu.:32.00 3rd Qu.:27.00
## Max. :56.00 Max. :60.00
## salary
## Min. : 57800
## 1st Qu.: 91000
## Median :107300
## Mean :113706
## 3rd Qu.:134185
## Max. :231545
Mean yrs.since.phd: The average years since phd based on this sample or one professor is 17.61 years.
Median Salary: When values within the variable salary are arranged in an ascending order, the value in the middle is 107300.
Max. Yrs. service: Maximum number for years in service is 60 years.
library(car)
## Warning: package 'car' was built under R version 4.3.2
scatterplotMatrix(~ salary + yrs.since.phd + yrs.service, data = mydata,
smooth = FALSE)
Based on the scatterplot there is a positive relationship between salary and years since phd and salary and years in service. Further, there is also a relationship between yrs in service and yrs since phd, which shows there might be a problem with multi collinearity.
library(car)
fit <- lm(salary ~ GenderF + yrs.since.phd + yrs.service,
data = mydata)
vif(fit)
## GenderF yrs.since.phd yrs.service
## 1.024694 5.798561 5.807457
Because of the VIF that is more than 5, witch suggest that there is high multi collinearity, I will remove one variable (years in service)
Once I remove years in service
My equation now becomes: Salary = Intercept + Beta1(Years in phd) + Beta2(Gender) + Error
library(car)
fit1 <- lm(salary ~ GenderF + yrs.since.phd,
data = mydata)
vif(fit1)
## GenderF yrs.since.phd
## 1.022639 1.022639
Since the values of VIF are closer to one, now we donβt have problem with multi collinearity.
mydata$StdResid <- round(rstandard(fit1), 3)
mydata$CooksD <- round(cooks.distance(fit1), 3)
hist(mydata$StdResid,
xlab = "Standardized residuals",
ylab = "Frequency",
main = "Histogram of standardized residuals")
shapiro.test(mydata$StdResid)
##
## Shapiro-Wilk normality test
##
## data: mydata$StdResid
## W = 0.98187, p-value = 7.014e-05
Based on this histogram we can see that there are some values that are out of the +/- 3 range so in the next step they will be removed.
hist(mydata$CooksD,
xlab = "Cooks distance",
ylab = "Frequency",
main = "Histrogram of Cooks distances")
mydata$ID <- seq(1, nrow(mydata))
head(mydata[order(-mydata$StdResid), c("ID", "StdResid")], 5)
## ID StdResid
## 44 44 3.726
## 250 250 3.031
## 78 78 2.734
## 365 365 2.604
## 191 191 2.399
head(mydata[order(-mydata$CooksD), c("ID", "CooksD")], 5)
## ID CooksD
## 283 283 0.048
## 132 132 0.044
## 126 126 0.036
## 324 324 0.035
## 44 44 0.029
Because of standard residual more than 3, I will remove these 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
delete <- c("44", "250")
mydata <- mydata %>%
filter(!(ID %in% delete))
fitT <- lm(salary ~ GenderF + yrs.since.phd,
data = mydata)
mydata$StdResid <- round(rstandard(fitT), 3)
mydata$CooksD <- round(cooks.distance(fitT), 3)
hist(mydata$StdResid,
xlab = "Standardized residuals",
ylab = "Frequency",
main = "Histogram of standardized residuals")
shapiro.test(mydata$StdResid)
##
## Shapiro-Wilk normality test
##
## data: mydata$StdResid
## W = 0.98334, p-value = 0.0001624
head(mydata[order(mydata$StdResid), c("ID", "StdResid")], 5)
## ID StdResid
## 281 283 -3.121
## 131 132 -2.584
## 316 318 -2.573
## 297 299 -2.502
## 125 126 -2.461
library(dplyr)
delete <- c("283")
mydata <- mydata %>%
filter(!(ID %in% delete))
fitK <- lm(salary ~ GenderF + yrs.since.phd,
data = mydata)
mydata$StdResid <- round(rstandard(fitK), 3)
mydata$CooksD <- round(cooks.distance(fitK), 3)
hist(mydata$StdResid,
xlab = "Standardized residuals",
ylab = "Frequency",
main = "Histogram of standardized residuals")
hist(mydata$CooksD,
xlab = "Cooks distance",
ylab = "Frequency",
main = "Histrogram of Cooks distances")
shapiro.test(mydata$StdResid)
##
## Shapiro-Wilk normality test
##
## data: mydata$StdResid
## W = 0.9829, p-value = 0.0001303
Looking at the histogram I can say all the units which were outliers or had a significant impact are removed and I can continue with my analysis
Since the sample size is large enough I can ignore the results of the Shapiro Wilks test and assume normality, to add, from the histogram the data looks sufficiently normal
mydata$StdFitted <- scale(fitK$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(fitK)
##
## 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 = 54.36255
## Prob > Chi2 = 1.6671e-13
We reject H0 and cannot assume the variance is constant(p<0.001). As a result we should use robust standard errors in our regression analysis
Linearity in parameters. Based on the scaterplot we can se that there is linearity in parameters
The expected value of the errors equals 0: πΈπΘπ=0 Because I included all the relevant independent variables, I can assume that the expected value of errors is 0
Homoskedasticity: ππππΘπ=π2 Based on the Breusch Pagan Test there is heteroskedasticity and I am using robust standard errors because of that
IV.Normal distribution of errors: ππ ~ π0, π2 Based on the Shapiro Wilk Test the variables are not normally distributed but this is ignored because the sample is sufficiently large
V. Errors are independent: Covππ,ππ=0 for each πβ π Because it is not a time series I assumed that the errors are independent
No perfect multicolinearity in a form of: π1π1+π2π2+β―+ππππ=0 This is satisfied using the VIF test
The number of units is greater than the number of estimated parameters: π>π The number of observations is more than the number of parameters
library(estimatr)
## Warning: package 'estimatr' was built under R version 4.3.2
fit_robust <- lm_robust(salary ~ GenderF + yrs.since.phd,
se_type = "HC1", #White's robust standard errors
data = mydata)
summary(fit_robust)
##
## Call:
## lm_robust(formula = salary ~ GenderF + yrs.since.phd, data = mydata,
## se_type = "HC1")
##
## Standard error type: HC1
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|) CI Lower CI Upper DF
## (Intercept) 85101 3830.3 22.218 2.569e-71 77570.8 92632 391
## GenderFMale 7609 3680.6 2.067 3.938e-02 372.2 14845 391
## yrs.since.phd 963 121.1 7.953 1.980e-14 724.9 1201 391
##
## Multiple R-squared: 0.1929 , Adjusted R-squared: 0.1888
## F-statistic: 35.63 on 2 and 391 DF, p-value: 6.088e-15
We reject H0(p<0.001)
With each additional year since phd the salary of a professor increases by $963 on an average with the gender remaining constant(p<0.001)
We reject H0(p<0.04)
Given the number of years since phd the salary of a male professor is $7609 more than the salary of a female professor on an average(p<0.001)
Multiple R squared: 19.29% of the variability in a professorβs salary can be explained using the variability in gender and the years since phd
F statistic: H0: Rho^2 = 0 H1: Rho^2 > 1
We reject H0(p<0.001) and conclude that the model explains a part of the variability in salary.
Now we compare the current equation
fitRank <- lm_robust(salary ~ GenderF + yrs.since.phd + rankF,
se_type = "HC1", #White's robust standard errors
data = mydata)
summary(fitRank)
##
## Call:
## lm_robust(formula = salary ~ GenderF + yrs.since.phd + rankF,
## data = mydata, se_type = "HC1")
##
## Standard error type: HC1
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|) CI Lower CI Upper DF
## (Intercept) 90960.8 3847.5 23.6416 3.041e-77 83396.3 98525.2 389
## GenderFMale 4887.9 2849.3 1.7155 8.706e-02 -714.1 10489.9 389
## yrs.since.phd -78.2 154.3 -0.5068 6.126e-01 -381.6 225.2 389
## rankFAsstProf -13871.0 2501.1 -5.5459 5.411e-08 -18788.4 -8953.6 389
## rankFProf 33031.6 2695.2 12.2559 1.997e-29 27732.7 38330.5 389
##
## Multiple R-squared: 0.4162 , Adjusted R-squared: 0.4102
## F-statistic: 156.6 on 4 and 389 DF, p-value: < 2.2e-16
We reject H0(p<0.001)
Given the number of years since phd and the gender the salary of an assistant professor is $13871 less than the salary of an associate professor on an average(p<0.001)
We reject H0(p<0.001)
Given the number of years since phd and the gender the salary of a professor is $33031.6 more than the salary of an associate professor on an average(p<0.001)
In the end, I wanted to compare the equations using ANOVA, but it doesnβt work for fit which have robust standard errors. Still, we can see that the second equation has a multiple R squared of 41.62% which appears to be significantly different than the of the multiple R squared of first equation.
The variables years in phd, rank and gender explain a part of the variability in the professors salary.