#data(package = .packages(all.available = TRUE))
#install.packages("carData")
library(carData)
mydata <- 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
* rank:role of the individual in the college (Assistant Professor, Associate Professor or Professor) (categorical, ordinal)
* discipline: A = 'theoretical' departments; B = 'applied' departments (categorical, binomial)
* yrs.since.phd: years since PhD (numerical, ratio)
* yrs.service: years of service (numerical, ratio)
* sex: sex of the individual (Male or Female) (categorical)
* salary: nine-month salary, in dollars (numerical, ratio)
nrow(mydata)
## [1] 397
colnames(mydata)[1] <- "Status" #changing the name of the first column from 'rank' to 'Status'
class(mydata$Status)
## [1] "factor"
summary(mydata$Status)
## AsstProf AssocProf Prof
## 67 64 266
mydata$disciplineF <- factor(mydata$discipline,
levels = c("A", "B"),
labels = c("theoretical", "applied"))
#Creating a new variable, disciplineF, and assigning values for 'A' and 'B'
class(mydata$disciplineF)
## [1] "factor"
summary(mydata)[ , -2 ]
## Status yrs.since.phd yrs.service sex salary
## AsstProf : 67 Min. : 1.00 Min. : 0.00 Female: 39 Min. : 57800
## AssocProf: 64 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
## disciplineF
## theoretical:181
## applied :216
##
##
##
##
#install.packages("pastecs")
library(pastecs)
round(stat.desc(mydata)[ , c(-1,-2,-5,-7)], 2)
## yrs.since.phd yrs.service salary
## nbr.val 397.00 397.00 397.00
## nbr.null 0.00 11.00 0.00
## nbr.na 0.00 0.00 0.00
## min 1.00 0.00 57800.00
## max 56.00 60.00 231545.00
## range 55.00 60.00 173745.00
## sum 8859.00 6993.00 45141464.00
## median 21.00 16.00 107300.00
## mean 22.31 17.61 113706.46
## SE.mean 0.65 0.65 1520.16
## CI.mean 1.27 1.28 2988.60
## var 166.07 169.16 917425865.05
## std.dev 12.89 13.01 30289.04
## coef.var 0.58 0.74 0.27
#install.packages("psych")
library(psych)
describe(mydata$salary)
## vars n mean sd median trimmed mad min max range skew
## X1 1 397 113706.5 30289.04 107300 111401.6 29355.48 57800 231545 173745 0.71
## kurtosis se
## X1 0.18 1520.16
head(mydata)
## Status discipline yrs.since.phd yrs.service sex salary disciplineF
## 1 Prof B 19 18 Male 139750 applied
## 2 Prof B 20 16 Male 173200 applied
## 3 AsstProf B 4 3 Male 79750 applied
## 4 Prof B 45 39 Male 115000 applied
## 5 Prof B 40 41 Male 141500 applied
## 6 AssocProf B 6 6 Male 97000 applied
describeBy(x = mydata$salary,group = mydata$Status)
##
## Descriptive statistics by group
## group: AsstProf
## vars n mean sd median trimmed mad min max range skew
## X1 1 67 80775.99 8174.11 79800 80825.6 9340.38 63100 97032 33932 0.08
## kurtosis se
## X1 -1 998.63
## ------------------------------------------------------------
## group: AssocProf
## vars n mean sd median trimmed mad min max range skew
## X1 1 64 93876.44 13831.7 95626.5 93937.38 14624.37 62884 126431 63547 -0.08
## kurtosis se
## X1 -0.71 1728.96
## ------------------------------------------------------------
## group: Prof
## vars n mean sd median trimmed mad min max range
## X1 1 266 126772.1 27718.67 123321.5 125080.8 28409.58 57800 231545 173745
## skew kurtosis se
## X1 0.58 0.32 1699.54
tapply(mydata$salary, mydata$Status, summary)
## $AsstProf
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 63100 74000 79800 80776 88598 97032
##
## $AssocProf
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 62884 82475 95626 93876 104226 126431
##
## $Prof
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 57800 105975 123322 126772 145080 231545
# Using tapply to compute summary statistics for 'salary' by each group in 'Status'
# (describeBy above was showing descriptive statistics only for one group of Status, even though it recognizes that there are 3 different groups and the variable is categorical, so I found alternative way using the function tapply.)
#install.packages("car")
library(car)
##
## Attaching package: 'car'
## The following object is masked from 'package:psych':
##
## logit
#install.packages("dplyr")
library(dplyr)
##
## Attaching package: 'dplyr'
## The following object is masked from 'package:car':
##
## recode
## The following objects are masked from 'package:pastecs':
##
## first, last
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
#install.packages("rstatix")
library(rstatix)
##
## Attaching package: 'rstatix'
## The following object is masked from 'package:stats':
##
## filter
mydata %>%
group_by(Status) %>%
shapiro_test(salary)
## # A tibble: 3 × 4
## Status variable statistic p
## <fct> <chr> <dbl> <dbl>
## 1 AsstProf salary 0.960 0.0301
## 2 AssocProf salary 0.975 0.229
## 3 Prof salary 0.976 0.000183
#install.packages("ggplot2")
library(ggplot2)
##
## Attaching package: 'ggplot2'
## The following objects are masked from 'package:psych':
##
## %+%, alpha
#install.packages("ggpubr")
library(ggpubr)
library(ggplot2)
AssistProf_salary <- ggplot(mydata[mydata$Status == "AsstProf", ], aes(x = salary)) +
theme_linedraw() +
geom_histogram() +
ggtitle("AssistProf Salary")
AssocProf_salary <- ggplot(mydata[mydata$Status == "AssocProf", ], aes(x = salary)) +
theme_linedraw() +
geom_histogram() +
ggtitle("AssocProf Salary")
Prof_salary <- ggplot(mydata[mydata$Status == "Prof", ], aes(x = salary)) +
theme_linedraw() +
geom_histogram() +
ggtitle("Prof Salary")
library(ggpubr)
ggarrange(AssistProf_salary, AssocProf_salary, Prof_salary,
ncol = 3, nrow = 1)
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
kruskal.test(salary ~ Status,
data = mydata)
##
## Kruskal-Wallis rank sum test
##
## data: salary by Status
## Kruskal-Wallis chi-squared = 194.01, df = 2, p-value < 2.2e-16
kruskal_effsize(salary ~ Status,
data = mydata)
## # A tibble: 1 × 5
## .y. n effsize method magnitude
## * <chr> <int> <dbl> <chr> <ord>
## 1 salary 397 0.487 eta2[H] large
library(rstatix)
groups_nonpar <- wilcox_test(salary ~ Status,
paired = FALSE,
p.adjust.method = "bonferroni",
data = mydata)
groups_nonpar
## # A tibble: 3 × 9
## .y. group1 group2 n1 n2 statistic p p.adj p.adj.signif
## * <chr> <chr> <chr> <int> <int> <dbl> <dbl> <dbl> <chr>
## 1 salary AsstProf AssocPr… 67 64 949 3.79e- 8 1.14e- 7 ****
## 2 salary AsstProf Prof 67 266 536. 1.32e-32 3.96e-32 ****
## 3 salary AssocProf Prof 64 266 2322 1.68e-19 5.04e-19 ****
Based on the sample data, we reject H0 (p.adj < 0.001 for all combinations status-salary) for all pairwise comparisons, implying that there are statistically significant differences in salaries between the groups represented by “Status” (AsstProf, AssocProf, Prof).
ANOVA_results <- aov(salary ~ Status,
data = mydata)
summary(ANOVA_results)
## Df Sum Sq Mean Sq F value Pr(>F)
## Status 2 1.432e+11 7.162e+10 128.2 <2e-16 ***
## Residuals 394 2.201e+11 5.586e+08
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
p-value = <2e-16 We reject H0 (p<0.001)
#install.packages("effectsize")
library(effectsize)
##
## Attaching package: 'effectsize'
## The following objects are masked from 'package:rstatix':
##
## cohens_d, eta_squared
## The following object is masked from 'package:psych':
##
## phi
eta_squared(ANOVA_results)
## For one-way between subjects designs, partial eta squared is equivalent
## to eta squared. Returning eta squared.
## # Effect Size for ANOVA
##
## Parameter | Eta2 | 95% CI
## -------------------------------
## Status | 0.39 | [0.33, 1.00]
##
## - One-sided CIs: upper bound fixed at [1.00].
interpret_eta_squared(0.39, rules = "cohen1992")
## [1] "large"
## (Rules: cohen1992)
#install.packages('car')
library(car)
leveneTest(mydata$salary, group = mydata$Status)
## Levene's Test for Homogeneity of Variance (center = median)
## Df F value Pr(>F)
## group 2 38.711 4.477e-16 ***
## 394
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
#install.packages("onewaytests")
library(onewaytests)
##
## Attaching package: 'onewaytests'
## The following object is masked from 'package:psych':
##
## describe
welch.test(salary ~ Status,
data = mydata)
##
## Welch's Heteroscedastic F Test (alpha = 0.05)
## -------------------------------------------------------------
## data : salary and Status
##
## statistic : 271.4386
## num df : 2
## denom df : 177.1872
## p.value : 1.126951e-54
##
## Result : Difference is statistically significant.
## -------------------------------------------------------------
We reject H0 at p<0.001.