#install.packages("afex")
#install.packages("emmeans")
#install.packages("ggbeeswarm")
#install.packages("expss")
library(psych) # for the describe() command
library(ggplot2) # to visualize our results
library(expss) # for the cross_cases() command
library(car) # for the leveneTest() command
library(afex) # to run the ANOVA
library(ggbeeswarm) # to run plot results
library(emmeans) # for posthoc tests
# For HW, import the project dataset you cleaned previously this will be the dataset you'll use throughout the rest of the semester
d <- read.csv(file="Data/projectdata.csv", header=T)
# new code! this adds a column with a number for each row. It will make it easier if we need to drop outliers later
d$row_id <- 1:nrow(d)
Note: For your HW, you will choose to run EITHER a one-way ANOVA (a single IV with 3 or more levels) OR a two-way/factorial ANOVA (two IVs with 2 or 3 levels each). You will need to specify your hypothesis and customize your code based on the choice you make. We will run BOTH versions of the test in the lab for illustrative purposes.
We predict that there will be a significant difference in negative effect of COVID-19 based on employment status.
# you only need to check the variables you're using in the current analysis
str(d)
## 'data.frame': 945 obs. of 8 variables:
## $ X : int 520 2814 3146 3295 717 6056 4753 5365 2044 1965 ...
## $ employment : chr "1 high school equivalent" "1 high school equivalent" "1 high school equivalent" "1 high school equivalent" ...
## $ relationship_status: chr "Single, never married" "Single, never married" "Prefer not to say" "Single, never married" ...
## $ covid_pos : int 0 0 0 0 0 0 0 0 0 0 ...
## $ covid_neg : int 0 0 0 0 0 0 0 0 0 0 ...
## $ big5_open : num 3.67 4.33 5.67 6 5.67 ...
## $ big5_agr : num 4.33 2.67 5.67 5.67 5 ...
## $ row_id : int 1 2 3 4 5 6 7 8 9 10 ...
# make our categorical variables of interest "factors"
# because we'll use our newly created row ID variable for this analysis, so make sure it's coded as a factor, too.
d$employment <- as.factor(d$employment)
d$row_id <- as.factor(d$row_id)
# check that our categorical variables of interest are now factors
str(d)
## 'data.frame': 945 obs. of 8 variables:
## $ X : int 520 2814 3146 3295 717 6056 4753 5365 2044 1965 ...
## $ employment : Factor w/ 5 levels "1 high school equivalent",..: 1 1 1 1 1 1 1 1 1 1 ...
## $ relationship_status: chr "Single, never married" "Single, never married" "Prefer not to say" "Single, never married" ...
## $ covid_pos : int 0 0 0 0 0 0 0 0 0 0 ...
## $ covid_neg : int 0 0 0 0 0 0 0 0 0 0 ...
## $ big5_open : num 3.67 4.33 5.67 6 5.67 ...
## $ big5_agr : num 4.33 2.67 5.67 5.67 5 ...
## $ row_id : Factor w/ 945 levels "1","2","3","4",..: 1 2 3 4 5 6 7 8 9 10 ...
# check our DV skew and kurtosis
describe(d$covid_neg)
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 945 1.61 2.02 0 1.32 0 0 8 8 0.86 -0.52 0.07
# we'll use the describeBy() command to view our DV's skew and kurtosis across our IVs' levels
describeBy(d$covid_neg, group = d$employment)
##
## Descriptive statistics by group
## group: 1 high school equivalent
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 887 1.53 1.98 0 1.23 0 0 8 8 0.92 -0.43 0.07
## ------------------------------------------------------------
## group: 2 college/university
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 23 3.35 2.12 4 3.26 1.48 0 8 8 0.05 -0.46 0.44
## ------------------------------------------------------------
## group: 3 employed
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 17 2 2.06 2 1.87 2.97 0 6 6 0.4 -1.37 0.5
## ------------------------------------------------------------
## group: 4 unemployed
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 9 3.11 2.15 3 3.11 2.97 0 6 6 -0.26 -1.51 0.72
## ------------------------------------------------------------
## group: prefer not to say
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 9 2.89 2.67 4 2.89 4.45 0 7 7 0.07 -1.79 0.89
# also use histograms to examine your continuous variable
hist(d$covid_neg)
table(d$employment)
##
## 1 high school equivalent 2 college/university 3 employed
## 887 23 17
## 4 unemployed prefer not to say
## 9 9
# REMEMBER your test's level of POWER is determined by your SMALLEST subsample
# use this commented out section below ONLY IF if you need to remove outliers
# to drop a single outlier, use this code:
#d <- subset(d, row_id!=c(1108)) # NOTE: Participant 1108 is not truly problamtic in the Lab, but we are going to removed them for demonstration.
# to drop multiple outliers, use this code:
# d <- subset(d, row_id!=c(1108) & row_id!=c(602))
# use the lm() command to run the regression
# formula is y~x, where y is our DV, x is our IV
reg_model <- lm(covid_neg~employment, data = d)
# Cook's distance
plot(reg_model, 4)
# Residuals VS Leverage
plot(reg_model, 5)
# IF you find outliers, go back up to line 114 or 117 and remove it/them, then re-run the reg_model code
# use the leveneTest() command from the car package to test homogeneity of variance
# uses the 'formula' setup: formula is y~x, where y is our DV and x is our IV
leveneTest(covid_neg~employment, data = d)
## Levene's Test for Homogeneity of Variance (center = median)
## Df F value Pr(>F)
## group 4 0.3579 0.8386
## 940
Our cell sizes are very unbalanced between the employment status groups. There are many more participants in the high scholl equivalent group (n = 887) than in the college/university (n = 23), employed (n = 17), unemployed (n = 9), and prefer not to say (n = 9) groups. The small sample size in some of the groups limit our power and increase the chance of a Type II error.
Levene’s test was significant (p = .839), which suggests that the assumption of homogeneity of variance was met. Therefore, we did not find evidence that the variances differed significantly across the employment status groups.
We examined potential outliers using Cook’s Distance and the Residuals vs. Leverage plots. We will determine whether any observations need to be removed before running the final ANOVA.
aov_model <- aov_ez(data = d,
id = "X",
between = c("employment"),
dv = "covid_neg",
anova_table = list(es = "pes"))
## Contrasts set to contr.sum for the following variables: employment
nice(aov_model)
## Anova Table (Type 3 tests)
##
## Response: covid_neg
## Effect df MSE F pes p.value
## 1 employment 4, 940 3.97 7.13 *** .029 <.001
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '+' 0.1 ' ' 1
ANOVA Effect Size [partial eta-squared] cutoffs from Cohen (1988): * η^2 < 0.01 indicates a trivial effect * η^2 >= 0.01 indicates a small effect * η^2 >= 0.06 indicates a medium effect * η^2 >= 0.14 indicates a large effect
afex_plot(aov_model, x = "employment")
Remember: We ONLY run posthoc IF the ANOVA test is SIGNIFICANT! E.g., only run the posthoc tests on pet type if there is a main effect for pet type
emmeans(aov_model, specs="employment")
## employment emmean SE df lower.CL upper.CL
## 1 high school equivalent 1.53 0.0669 940 1.40 1.66
## 2 college/university 3.35 0.4150 940 2.53 4.16
## 3 employed 2.00 0.4830 940 1.05 2.95
## 4 unemployed 3.11 0.6640 940 1.81 4.41
## prefer not to say 2.89 0.6640 940 1.59 4.19
##
## Confidence level used: 0.95
pairs(emmeans(aov_model, specs="employment", adjust="tukey"))
## contrast estimate SE df t.ratio
## 1 high school equivalent - (2 college/university) -1.820 0.421 940 -4.328
## 1 high school equivalent - 3 employed -0.472 0.488 940 -0.969
## 1 high school equivalent - 4 unemployed -1.583 0.667 940 -2.373
## 1 high school equivalent - prefer not to say -1.361 0.667 940 -2.040
## (2 college/university) - 3 employed 1.348 0.637 940 2.116
## (2 college/university) - 4 unemployed 0.237 0.783 940 0.302
## (2 college/university) - prefer not to say 0.459 0.783 940 0.586
## 3 employed - 4 unemployed -1.111 0.821 940 -1.353
## 3 employed - prefer not to say -0.889 0.821 940 -1.083
## 4 unemployed - prefer not to say 0.222 0.939 940 0.237
## p.value
## 0.0002
## 0.8692
## 0.1235
## 0.2476
## 0.2140
## 0.9982
## 0.9771
## 0.6577
## 0.8155
## 0.9993
##
## P value adjustment: tukey method for comparing a family of 5 estimates
To test our hypothesis that there will be a significant difference in negative effects of COVID-19 based on employment status, we used a one-way ANOVA. Our data was unbalanced, with many more people in the high school equivalent group participating in our survey (n = 887) than in the college/university (n = 23), employed (n = 17), unemployed (n = 9) or prefer not to say (n = 9) groups. The small sample sizes in some of the groups limit our power and increase the chance of a Type II error. Levene’s test was not significant (p = .839), which suggests that our data met the assumption of homogeneity of variance.
We found a significant effect of employment status on negative effects of COVID-19, F(4, 940) = 7.13, p< .001, ηp2 = .029 (small effect; Cohen, 1988). Posthoc tests using Tukey’s HSD adjustment revealed that participants in the high school equivalent group (M = 1.53, SE = .07) reported significantly fewer negative effects of COVID-19 than participants in the college/university group (M = 3.35, SE = .42), p = .0002. None of the other comparisons between employment-status groups were significant (see Figure 1 for a comparison).
References
Cohen J. (1988). Statistical Power Analysis for the Behavioral Sciences. New York, NY: Routledge Academic.