#install.packages("carData")
library(carData)
mydata <- force(Salaries)
set.seed(1) #Random sample of 100 units
mydata <- mydata[sample(nrow(mydata), 100), ]
head(mydata)
## rank discipline yrs.since.phd yrs.service sex salary
## 324 Prof B 24 15 Female 161101
## 167 Prof B 16 16 Male 167284
## 129 Prof A 32 30 Male 113278
## 299 Prof A 49 43 Male 72300
## 270 Prof A 13 7 Male 103700
## 187 AssocProf B 13 10 Female 103750
Unit of observation is a college professor in the U.S.
Sample size is 100, there are 6 variables:
Source of data: R database - CarData Reference: Fox J. and Weisberg, S. (2019) An R Companion to Applied Regression, Third Edition, Sage.
#Renaming columns
colnames(mydata)[colnames(mydata) == "rank"] <- "Rank"
colnames(mydata)[colnames(mydata) == "discipline"] <- "Discipline"
colnames(mydata)[colnames(mydata) == "yrs.since.phd"] <- "Years of PhD"
colnames(mydata)[colnames(mydata) == "yrs.service"] <- "Years of service"
colnames(mydata)[colnames(mydata) == "sex"] <- "Gender"
colnames(mydata)[colnames(mydata) == "salary"] <- "Salary"
#Factoring the "discipline" variable
mydata$Discipline <- factor(mydata$Discipline,
levels = c("A", "B"),
labels =c("Theoretical", "Applied"))
library(psych)
library(ggplot2)
##
## Attaching package: 'ggplot2'
## The following objects are masked from 'package:psych':
##
## %+%, alpha
describe(mydata)
## vars n mean sd median trimmed mad min
## Rank* 1 100 2.63 0.69 3.0 2.79 0.00 1
## Discipline* 2 100 1.53 0.50 2.0 1.54 0.00 1
## Years of PhD 3 100 22.82 12.22 21.0 22.49 11.86 1
## Years of service 4 100 18.47 12.54 17.5 17.60 14.08 0
## Gender* 5 100 1.88 0.33 2.0 1.98 0.00 1
## Salary 6 100 116282.27 29217.89 111133.0 114207.43 26189.39 72300
## max range skew kurtosis se
## Rank* 3 2 -1.56 0.86 0.07
## Discipline* 2 1 -0.12 -2.01 0.05
## Years of PhD 52 51 0.30 -0.75 1.22
## Years of service 60 60 0.67 0.04 1.25
## Gender* 2 1 -2.30 3.34 0.03
## Salary 231545 159245 0.92 1.30 2921.79
#Describing by disciplines
describeBy(mydata$Salary, g = mydata$Discipline)
##
## Descriptive statistics by group
## group: Theoretical
## vars n mean sd median trimmed mad min max range skew
## X1 1 47 105302.9 20616.9 105260 104265.5 14904.58 72300 150564 78264 0.34
## kurtosis se
## X1 -0.31 3007.28
## ------------------------------------------------------------
## group: Applied
## vars n mean sd median trimmed mad min max range skew
## X1 1 53 126018.7 32306.31 126200 123915.4 37620.97 77000 231545 154545 0.68
## kurtosis se
## X1 0.52 4437.61
Average salary of professors was 116282.27 dollars, the median was 111133.0 dollars, whereas the highest salary accounted for 231545 dollars.
Professors were categorized into two groups, the ones from theoretical department and the ones from applied department. Average salary of theoretical department professors was 105302.9 dollars, while professors of applied department on average got 126018.7 dollars.
ggplot(mydata, aes(x = Salary, fill = factor(Discipline))) +
geom_histogram(position = "dodge", bins = 10) +
scale_fill_manual(values = c("blue", "lightblue"),
labels = c("Theoretical", "Applied")) +
labs(title = "Distribution of salaries by discipline",
x = "Salary", y = "Frequency", fill = "Discipline") +
theme_bw()
Research question: Do salaries among professors of different disciplines differ?
H0: Salaries of professors of different disciplines are the same.
H1: Salaries of professors of different disciplines are different.
t.test(mydata$Salary ~ mydata$Discipline,
paired = FALSE,
var.equal = FALSE, #Welch correction
alternative = "two.sided")
##
## Welch Two Sample t-test
##
## data: mydata$Salary by mydata$Discipline
## t = -3.8644, df = 89.412, p-value = 0.0002106
## alternative hypothesis: true difference in means between group Theoretical and group Applied is not equal to 0
## 95 percent confidence interval:
## -31366.51 -10065.02
## sample estimates:
## mean in group Theoretical mean in group Applied
## 105302.9 126018.7
We reject H0 at p<0.001, which means that salaries of professors of different disciplines are different.
# Filtering data for different disciplines
mydata_A <- mydata[mydata$Discipline == "Theoretical", ]
mydata_B <- mydata[mydata$Discipline == "Applied", ]
#Performing Shapiro-Wilk test for testing normality
shapiro.test(mydata_A$Salary)
##
## Shapiro-Wilk normality test
##
## data: mydata_A$Salary
## W = 0.94351, p-value = 0.02425
shapiro.test(mydata_B$Salary)
##
## Shapiro-Wilk normality test
##
## data: mydata_B$Salary
## W = 0.95683, p-value = 0.05327
Hypothesis for group A (salaries of professors of theoretical departments)
H0: Variable is normally distributed.
H1: Variable is not normally distributed.
We cannot reject H0 (p>0.005), which means that based on Shapiro-Wilk normality test, group A is normally distributed.
Hypothesis for group B (salaries of professors of applied departments)
H0: Variable is normally distributed.
H1: Variable is not normally distributed.
We cannot reject H0 (p>0.005), which means that based on Shapiro-Wilk normality test, group B is normally distributed.
Since the assumptions are met (numeric variable and normality) in both groups, we can use parametric test.
wilcox.test(mydata$Salary ~ mydata$Discipline,
paired = FALSE,
correct = FALSE,
exact = FALSE,
alternative = "two.sided")
##
## Wilcoxon rank sum test
##
## data: mydata$Salary by mydata$Discipline
## W = 764, p-value = 0.000883
## alternative hypothesis: true location shift is not equal to 0
H0: Location distribution is the same for professors of theoretical and applied departments.
H1: Location distribution is not the same for professors of theoretical and applied departments.
We reject H0 at p<0.001, location distribution is not the same. This means that based on the sample, salaries of professors among different disciplines differ.
library(effectsize)
##
## Attaching package: 'effectsize'
## The following object is masked from 'package:psych':
##
## phi
effectsize(wilcox.test(mydata$Salary ~ mydata$Discipline,
paired = FALSE,
correct = FALSE,
exact = FALSE,
alternative = "two.sided"))
## r (rank biserial) | 95% CI
## ----------------------------------
## -0.39 | [-0.56, -0.18]
interpret_rank_biserial(0.39)
## [1] "large"
## (Rules: funder2019)
Based on the sample data we find that the salary of professors of theoretical and applied departments differ (p<0.001). Professors of applied department have higher salaries and the difference in distribution is large (r=0.39).