library(psych) # for the describe() command
library(ggplot2) # to visualize our results
##
## Attaching package: 'ggplot2'
## The following objects are masked from 'package:psych':
##
## %+%, alpha
library(expss) # for the cross_cases() command
## Loading required package: maditr
##
## To aggregate several columns with one summary: take(mtcars, mpg, hp, fun = mean, by = am)
##
## Attaching package: 'expss'
## The following object is masked from 'package:ggplot2':
##
## vars
library(car) # for the leveneTest() command
## Loading required package: carData
##
## Attaching package: 'car'
## The following object is masked from 'package:expss':
##
## recode
## The following object is masked from 'package:psych':
##
## logit
library(afex) # to run the ANOVA and plot results
## Loading required package: lme4
## Loading required package: Matrix
##
## Attaching package: 'lme4'
## The following object is masked from 'package:expss':
##
## dummy
## ************
## Welcome to afex. For support visit: http://afex.singmann.science/
## - Functions for ANOVAs: aov_car(), aov_ez(), and aov_4()
## - Methods for calculating p-values with mixed(): 'S', 'KR', 'LRT', and 'PB'
## - 'afex_aov' and 'mixed' objects can be passed to emmeans() for follow-up tests
## - Get and set global package options with: afex_options()
## - Set sum-to-zero contrasts globally: set_sum_contrasts()
## - For example analyses see: browseVignettes("afex")
## ************
##
## Attaching package: 'afex'
## The following object is masked from 'package:lme4':
##
## lmer
library(emmeans) # for posthoc tests
# import the dataset you cleaned previously
# this will be the dataset you'll use throughout the rest of the semester
# use ARC data
d <- read.csv(file="Data/eammi2data_final.csv", header=T)
# for the HW, you may or may not need to use the code below this comment
# check to see if you have a variable called 'X' or 'ResponseId' in your data
names(d)
## [1] "gender" "income" "efficacy" "support" "stress" "swb"
# if you do have 'X' or 'ResponseId' (aka your ID variable) then DELETE THE CODE BELOW
# if you don't have those variables, keep the code and run it
d$row_id <- 1:nrow(d)
Note: You can chose to run either a one-way ANOVA (a single IV with 3 or more levels) or a two-way/factorial ANOVA (at least two IVs) for the homework. You will need to specify your hypothesis and customize your code based on the choice you make. I will run both versions of the test here for illustrative purposes.
One-Way: I predict that there will be a significant effect of gender on stress.
# you only need to check the variables you're using in the current analysis
# although you checked them previously, it's always a good idea to look them over again and be sure that everything is correct
str(d)
## 'data.frame': 3149 obs. of 7 variables:
## $ gender : chr "f" "m" "m" "f" ...
## $ income : chr "1 low" "1 low" "rather not say" "rather not say" ...
## $ efficacy: num 3.4 3.4 2.2 2.8 3 2.4 2.3 3 3 3.7 ...
## $ support : num 6 6.75 5.17 5.58 6 ...
## $ stress : num 3.3 3.3 4 3.2 3.1 3.5 3.3 2.4 2.9 2.7 ...
## $ swb : num 4.33 4.17 1.83 5.17 3.67 ...
## $ row_id : int 1 2 3 4 5 6 7 8 9 10 ...
# make our categorical variables factors
d$row_id <- as.factor(d$row_id)#we'll actually use our ID variable for this analysis, so make sure it's coded as a factor
d$gender <- as.factor(d$gender)
# check your categorical variables
table(d$gender)
##
## f m nb
## 2307 789 53
# also use histograms to examine your continuous variable
hist(d$stress)
Assumptions checked below:
If you have confirmed everything else…
# check your categorical variables and make sure they have decent cell sizes
# they should have at least 5 participants in each cell
# but larger numbers are always better
table(d$gender)
##
## f m nb
## 2307 789 53
# you can use the describe() command on an entire dataframe (d) or just on a single variable
describe(d$stress)
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 3149 3.05 0.6 3 3.05 0.59 1.3 4.7 3.4 0.03 -0.16 0.01
# we'll use the describeBy() command to view skew and kurtosis across our IVs
describeBy(d$stress, group =d$gender)
##
## Descriptive statistics by group
## group: f
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 2307 3.1 0.59 3.1 3.1 0.59 1.3 4.7 3.4 0.03 -0.12 0.01
## ------------------------------------------------------------
## group: m
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 789 2.88 0.6 2.9 2.87 0.59 1.3 4.5 3.2 0.1 -0.27 0.02
## ------------------------------------------------------------
## group: nb
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 53 3.35 0.59 3.3 3.33 0.59 2 4.6 2.6 0.15 -0.59 0.08
# use the leveneTest() command from the car package to test homogeneity of variance
# uses the 'formula' setup: formula is y~x1*x2, where y is our DV and x1 is our first IV and x2 is our second IV
leveneTest(stress~gender, data = d)
## Levene's Test for Homogeneity of Variance (center = median)
## Df F value Pr(>F)
## group 2 0.9931 0.3706
## 3146
# use the lm() command to run the regression
# formula is y~x1*x2, where y is our DV, x1 is our first IV and x2 is our second IV
reg_model <- lm(stress~gender, data = d) #for one-way
# Cook's distance
plot(reg_model, 4)
# Residuals vs Leverage
plot(reg_model, 5)
Levene’s test is significant for our three-level gender variable. We are ignoring this and continuing with the analysis anyway, but in the real world this is something we would have to correct for.
aov_model <- aov_ez(data = d,
id = "row_id",
between = c("gender"),
dv = "stress",
anova_table = list(es = "pes"))
## Contrasts set to contr.sum for the following variables: gender
Effect size cutoffs from Cohen (1988):
nice(aov_model)
## Anova Table (Type 3 tests)
##
## Response: stress
## Effect df MSE F pes p.value
## 1 gender 2, 3146 0.35 50.39 *** .031 <.001
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '+' 0.1 ' ' 1
afex_plot(aov_model, x = "gender")
Only run posthocs if the test is significant! E.g., only run the posthoc tests on gender if there is a main effect for gender.
emmeans(aov_model, specs="gender", adjust="tukey")
## Note: adjust = "tukey" was changed to "sidak"
## because "tukey" is only appropriate for one set of pairwise comparisons
## gender emmean SE df lower.CL upper.CL
## f 3.10 0.0123 3146 3.07 3.13
## m 2.88 0.0210 3146 2.83 2.93
## nb 3.35 0.0811 3146 3.15 3.54
##
## Confidence level used: 0.95
## Conf-level adjustment: sidak method for 3 estimates
pairs(emmeans(aov_model, specs="gender", adjust="tukey"))
## contrast estimate SE df t.ratio p.value
## f - m 0.228 0.0243 3146 9.345 <.0001
## f - nb -0.242 0.0820 3146 -2.948 0.0090
## m - nb -0.469 0.0838 3146 -5.602 <.0001
##
## P value adjustment: tukey method for comparing a family of 3 estimates
To test our hypothesis that there would be a significant effect of gender on stress, we used a one-way ANOVA. Our data was unbalanced, with many more women participating in our survey (n = 2307) than men (n = 789) or non-binary and other gender participants (n = 53). This significantly reduces the power of our test and increases the chances of a Type II error. A significant Levene’s test (p = .002) also indicates that our data violates the assumption of homogeneity of variance. This suggests that there is an increased chance of Type I error. We continued with our analysis for the purpose of this class.
We found a significant effect of gender, F(2,3146) = 50.39, p < .001, ηp2 = .031 (large effect size; Cohen, 1988). Posthoc tests using Tukey’s HSD revealed that women reported more stress than men but less stress than non-binary and other gender participants, while non-binary and other gender participants reported the highest amount of stress overall (see Figure 1 for a comparison).
References
Cohen J. (1988). Statistical Power Analysis for the Behavioral Sciences. New York, NY: Routledge Academic.