#install.packages("carData")
library(carData)
mydata <- force(SLID)
head (mydata)
## wages education age sex language
## 1 10.56 15.0 40 Male English
## 2 11.00 13.2 19 Male English
## 3 NA 16.0 49 Male Other
## 4 17.76 14.0 46 Male Other
## 5 NA 8.0 71 Male English
## 6 14.00 16.0 50 Female English
Wage: Composite hourly wage rate from all jobs.
Education: Number of years of schooling.
Age: in years.
Sex: A factor with levels: Female, Male
Language: A factor with levels: English, French, Other.
The source of data is R-Studio data package named “carData”.
H0 (Null hypothesis): The mean of hourly rate wage of people with more than average years of education is the same as the mean of hourly rate wage of people with less than average years of education.
H1 (Alternative hypothesis):The mean of hourly rate wage of people with more than average years of education is not the same as the mean of hourly rate wage of people with less than average years of education.
#Cleaning the data, removing N/A's.
#install.packages("tidyr")
library(tidyr)
mydata2 <- drop_na(mydata)
head (mydata2)
## wages education age sex language
## 1 10.56 15.0 40 Male English
## 2 11.00 13.2 19 Male English
## 4 17.76 14.0 46 Male Other
## 6 14.00 16.0 50 Female English
## 9 8.20 15.0 31 Male English
## 12 16.97 13.5 30 Female English
set.seed(1)
mydata3 <- mydata2[sample(nrow(mydata2), 100), ] #Making the sample out of data.
mydata4 <- mydata3 [,c(-3,-4,-5,-6) ] #Deleting the irrelevant variables for hypothesis analysis.
head (mydata4)
## wages education
## 1870 6.63 12.5
## 7264 10.00 15.3
## 1276 11.78 13.0
## 4063 19.20 13.0
## 1714 10.26 10.0
## 2821 28.53 17.0
library(psych)
describe(mydata4)
## vars n mean sd median trimmed mad min max range skew kurtosis se
## wages 1 100 16.71 8.61 15.5 15.74 7.67 3.43 44.95 41.52 1.08 1.10 0.86
## education 2 100 13.27 2.78 13.0 13.22 2.97 6.00 20.00 14.00 0.15 -0.21 0.28
#Two independent groups are needed for the analysis. Here we transformed the years of education into a categorical variable. One group is for the ID's that have more than average years of education and the other for the ones that have less than average years of education.
average_years <- mean(mydata4$education)
mydata4$education_group <- ifelse(mydata4$education < average_years, "LessThanAverage", "MoreThanAverage")
mydata4$education_group <- factor(mydata4$education_group,
levels = c("LessThanAverage", "MoreThanAverage"),
labels=c("LessThanAverage", "MoreThanAverage"))
summary(mydata4)
## wages education education_group
## Min. : 3.43 Min. : 6.00 LessThanAverage:57
## 1st Qu.:10.24 1st Qu.:12.00 MoreThanAverage:43
## Median :15.51 Median :13.00
## Mean :16.71 Mean :13.27
## 3rd Qu.:19.99 3rd Qu.:15.07
## Max. :44.95 Max. :20.00
library(psych)
describeBy (x= mydata4$wages, group =mydata4$education_group)
##
## Descriptive statistics by group
## group: LessThanAverage
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 57 14.08 7.28 12.53 13.2 6.72 3.43 44.78 41.35 1.62 4.15 0.96
## ---------------------------------------------------------------------------
## group: MoreThanAverage
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 43 20.2 9.07 17.49 19.52 11.1 6.4 44.95 38.55 0.62 -0.18 1.38
We are interested if the differences in the mean are statistically significant. A two sample independent t-test could be used to compare the means of the hourly rate of wages between people with less than average years of education and more than average years of education. This research questions could also involve a comparison of a numeric variable (average hourly wage) between two groups based on a categorical variable (More than average or Less than average years of education), fulfilling the criteria of using at least one categorical and one numeric variable.
library(ggplot2)
##
## Attaching package: 'ggplot2'
## The following objects are masked from 'package:psych':
##
## %+%, alpha
ggplot(mydata4, aes(x = wages, fill = education_group)) +
geom_histogram(binwidth = 2, position = "dodge", alpha = 0.7) +
labs(title = "Hourly Wage Distribution by Education Group",
x = "Wages",
y = "Frequency") +
scale_fill_manual(values = c("LessThanAverage" = "blue", "MoreThanAverage" = "orange")) +
facet_wrap(~education_group, scales = "free") +
theme_minimal()
#Shapiro-Wilk test for Normality
#install.packages("rstatix")
library (rstatix)
##
## Attaching package: 'rstatix'
## The following object is masked from 'package:stats':
##
## filter
mydata4 %>%
group_by(education_group) %>%
shapiro_test(wages)
## # A tibble: 2 × 4
## education_group variable statistic p
## <fct> <chr> <dbl> <dbl>
## 1 LessThanAverage wages 0.871 0.0000205
## 2 MoreThanAverage wages 0.950 0.0604
We reject H0 for “LessThanAverage” at p-value<0.001. This means that the hourly rate wages in the group Less than average years of education are not normally distributed. The assumptions for performing a parametric test are not met, therefore a non-parametric test has to be performed.
#Wilcoxon rank sum test
wilcox.test(mydata4$wages~mydata4$education_group,
paired=FALSE,
correct=FALSE,
exact=FALSE,
alternative= "two.sided")
##
## Wilcoxon rank sum test
##
## data: mydata4$wages by mydata4$education_group
## W = 733, p-value = 0.0006054
## alternative hypothesis: true location shift is not equal to 0
We reject H0 at p-value < 0.001. The distribution of locations of hourly wages are not the same.
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
effectsize(wilcox.test(mydata4$wages~mydata4$education_group,
paired=FALSE,
correct=FALSE,
exact=FALSE,
alternative= "two.sided"))
## r (rank biserial) | 95% CI
## ----------------------------------
## -0.40 | [-0.58, -0.19]
interpret_rank_biserial(0.40)
## [1] "very large"
## (Rules: funder2019)
Based on sample data, we find that the hourly rate of wages in the group of people with Less than average years of education and More than average years of education differ, the difference is very large (r=0,40).
#Welch two sample t-test
t.test(wages ~ education_group,
data = mydata4,
subset = education_group %in% c("LessThanAverage", "MoreThanAverage"))
##
## Welch Two Sample t-test
##
## data: wages by education_group
## t = -3.6295, df = 78.811, p-value = 0.0005027
## alternative hypothesis: true difference in means between group LessThanAverage and group MoreThanAverage is not equal to 0
## 95 percent confidence interval:
## -9.476184 -2.763612
## sample estimates:
## mean in group LessThanAverage mean in group MoreThanAverage
## 14.07754 20.19744
The Welch Two Sample t-test results show a p-value < 0.001, so I can reject the null hypotheses. The means of hourly wage in MoreThanAverage and LessThanAverage differ significantly.
#install.packages("effectsize")
library(effectsize)
effectsize::cohens_d(mydata4$wages ~ mydata4$education_group,
pooled_sd = FALSE)
## Cohen's d | 95% CI
## --------------------------
## -0.74 | [-1.16, -0.32]
##
## - Estimated using un-pooled SD.
#install.packages("effectsize")
library(effectsize)
interpret_cohens_d(0.74, rules = "sawilowsky2009")
## [1] "medium"
## (Rules: sawilowsky2009)
Using the sample data, we can see a medium effect difference between the means of hourly wage in MoreThanAverage and LessThanAverage (d=0.74).
In the analysis, focusing on whether the average hourly rate of wages are the same, the data supported that we can reject the null hypothesis (H0) at p-value < 0.001, which proposes a difference in average hourly rate of wage . The t-test and Wilcoxon test, both support this finding with their respective p-values < 0.001, suggest that the variations are significant. The very large effect size of Wilcoxon rank sum test, points difference in hourly rates of wages between people with less than average and more than average years of education .