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/arc_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" "age" "pas_covid" "pss" "support" "brs"
# 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 more than 3 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: We predict that there will be a significant effect of gender on support, as measured by the social support scale (Support). (one categorical)
# 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': 322 obs. of 7 variables:
## $ gender : chr "female" "female" "female" "female" ...
## $ age : chr "1 under 18" "1 under 18" "1 under 18" "2 between 18 and 25" ...
## $ pas_covid: num 2.33 3.11 3.78 2.56 3.89 ...
## $ pss : num 2.75 2.75 4 4.25 2.75 2.5 3.5 4 4.5 3 ...
## $ support : num 3.17 2.5 3 3.5 3.17 ...
## $ brs : num 4 2.5 2.33 2 2.83 ...
## $ 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)
##
## female I use another term male Prefer not to say
## 253 17 45 7
# also use histograms to examine your continuous variable
hist(d$support)
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_rc)
## < table of extent 0 >
# our number of small nb participants is going to hurt us for the two-way anova, but it should be okay for the one-way anova
# so we'll create a new dataframe for the two-way analysis and call it d2
# you can use the describe() command on an entire dataframe (d) or just on a single variable
describe(d$support)
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 322 3.27 0.99 3.33 3.28 0.99 1 5 4 -0.11 -0.8 0.06
# we'll use the describeBy() command to view skew and kurtosis across our IVs
describeBy(d$support, group = d$gender)
##
## Descriptive statistics by group
## group: female
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 253 3.28 0.99 3.33 3.3 0.99 1 5 4 -0.15 -0.78 0.06
## ------------------------------------------------------------
## group: I use another term
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 17 2.79 0.86 2.83 2.81 0.99 1.17 4.17 3 -0.24 -0.83 0.21
## ------------------------------------------------------------
## group: male
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 45 3.43 1 3.5 3.45 1.24 1.5 5 3.5 -0.15 -1 0.15
## ------------------------------------------------------------
## group: Prefer not to say
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 7 2.67 0.88 2.5 2.67 0.74 1.83 4.33 2.5 0.75 -0.96 0.33
# 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(support ~ gender, data = d)
## Levene's Test for Homogeneity of Variance (center = median)
## Df F value Pr(>F)
## group 3 0.5956 0.6183
## 318
# 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(support ~ gender, data = d) #for one-way
# Cook's distance
plot(reg_model, 4)
# Residuals vs Leverage
plot(reg_model, 5)
Our cell sizes are very unbalanced. A small sample size for one of the levels of our variable limits our power and increases our Type II error rate.
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. (if you have outliers add here)
aov_model <- aov_ez(data = d, id = "row_id", between = c("gender"), dv = "support", 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: support
## Effect df MSE F pes p.value
## 1 gender 3, 318 0.96 2.65 * .024 .049
## ---
## 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
## female 3.28 0.0616 318 3.13 3.44
## I use another term 2.79 0.2377 318 2.20 3.39
## male 3.43 0.1461 318 3.07 3.80
## Prefer not to say 2.67 0.3705 318 1.74 3.59
##
## Confidence level used: 0.95
## Conf-level adjustment: sidak method for 4 estimates
pairs(emmeans(aov_model, specs="gender", adjust="tukey"))
## contrast estimate SE df t.ratio p.value
## female - I use another term 0.490 0.246 318 1.994 0.1923
## female - male -0.149 0.159 318 -0.942 0.7822
## female - Prefer not to say 0.617 0.376 318 1.643 0.3558
## I use another term - male -0.639 0.279 318 -2.291 0.1024
## I use another term - Prefer not to say 0.127 0.440 318 0.290 0.9915
## male - Prefer not to say 0.767 0.398 318 1.925 0.2196
##
## P value adjustment: tukey method for comparing a family of 4 estimates
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.
To test our hypothesis that there would be a significant effect of gender on support, we used a one-way ANOVA. Our data was unbalanced, with many more women participating in our survey (n = 253) than men (n = 45) or non-binary and other gender participants (n = 24). This significantly reduces the power of our test and increases the chances of a Type II error. We did not identify any outliers following visual analysis of a Residuals vs Leverage plot. A significant Levene’s test (p = .619) 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(3,318) = 2.65, p =.024, ηp2 = .042 (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.