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 all non-grouping columns: take_all(mtcars, mean, by = am)
##
## Attaching package: 'maditr'
## The following object is masked from 'package:base':
##
## sort_by
##
## 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
## Welcome to emmeans.
## Caution: You lose important information if you filter this package's results.
## See '? untidy'
# 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/mydata.csv", header=T)
# new code! this adds a column with a number for each row. it makes it easier when we drop outliers later
d$row_id <- 1:nrow(d)
Two-Way ANOVA: We predict that there will significant effects of gender and income on worry, as measured by the Penn State Worry Questionaire (PSWQ). We also predict that income and gender will interact and that women of low income will report significantly higher stress than men of low income, medium and high income men and women, and those who prefer not to say.
# 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': 329 obs. of 7 variables:
## $ gender : chr "female" "female" "female" "female" ...
## $ income : chr "3 high" "2 middle" "2 middle" "2 middle" ...
## $ covid_neg: int 0 0 0 0 0 0 0 0 0 0 ...
## $ pswq : num 4.94 3.94 2.62 2.94 2.81 ...
## $ iou : num 3.19 3.37 1.7 1.89 1.11 ...
## $ edeq12 : num 1.58 1.67 1.42 1.33 3.17 ...
## $ row_id : int 1 2 3 4 5 6 7 8 9 10 ...
# make our categorical variables factors
#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)
d$income <- as.factor(d$income)
d$row_id <- as.factor(d$row_id)
# you can use the describe() command on an entire dataframe (d) or just on a single variable
describe(d$pswq)
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 329 2.88 0.88 2.88 2.86 1.02 1.12 4.94 3.81 0.18 -0.82 0.05
# we'll use the describeBy() command to view skew and kurtosis across our IVs
describeBy(d$pswq, group = d$gender)
##
## Descriptive statistics by group
## group: female
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 285 2.94 0.87 2.94 2.92 0.93 1.19 4.94 3.75 0.14 -0.8 0.05
## ------------------------------------------------------------
## group: I use another term
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 1 4.44 NA 4.44 4.44 0 4.44 4.44 0 NA NA NA
## ------------------------------------------------------------
## group: male
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 39 2.4 0.86 2.25 2.35 0.65 1.12 4.19 3.06 0.66 -0.44 0.14
## ------------------------------------------------------------
## group: Prefer not to say
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 4 2.92 0.73 2.75 2.92 0.6 2.31 3.88 1.56 0.31 -2.1 0.37
describeBy(d$pswq, group = d$income)
##
## Descriptive statistics by group
## group: 1 low
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 38 3.31 0.88 3.38 3.31 0.97 1.56 4.94 3.38 -0.16 -0.95 0.14
## ------------------------------------------------------------
## group: 2 middle
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 157 2.82 0.89 2.81 2.8 1.02 1.12 4.75 3.62 0.24 -0.82 0.07
## ------------------------------------------------------------
## group: 3 high
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 86 2.8 0.86 2.69 2.77 0.93 1.19 4.94 3.75 0.26 -0.64 0.09
## ------------------------------------------------------------
## group: prefer not to say
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 48 2.87 0.85 2.91 2.86 0.97 1.25 4.62 3.38 0.07 -1.04 0.12
# also use histograms to examine your continuous variable
hist(d$pswq)
# and cross_cases() to examine your categorical variables
cross_cases(d, gender, income)
| income | ||||
|---|---|---|---|---|
| 1 low | 2 middle | 3 high | prefer not to say | |
| gender | ||||
| female | 34 | 139 | 71 | 41 |
| I use another term | 1 | |||
| male | 4 | 15 | 14 | 6 |
| Prefer not to say | 2 | 1 | 1 | |
| #Total cases | 38 | 157 | 86 | 48 |
table(d$gender)
##
## female I use another term male Prefer not to say
## 285 1 39 4
cross_cases(d, gender, income) #2 way anova
| income | ||||
|---|---|---|---|---|
| 1 low | 2 middle | 3 high | prefer not to say | |
| gender | ||||
| female | 34 | 139 | 71 | 41 |
| I use another term | 1 | |||
| male | 4 | 15 | 14 | 6 |
| Prefer not to say | 2 | 1 | 1 | |
| #Total cases | 38 | 157 | 86 | 48 |
# 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
d2 <- subset(d, gender != "I use another term")
d2$gender <- droplevels(d2$gender)
d3 <- subset(d2, gender != "Prefer not to say")
d3$gender <- droplevels(d3$gender)
# to double-check any changes we made
cross_cases(d3, gender, income)
| income | ||||
|---|---|---|---|---|
| 1 low | 2 middle | 3 high | prefer not to say | |
| gender | ||||
| female | 34 | 139 | 71 | 41 |
| male | 4 | 15 | 14 | 6 |
| #Total cases | 38 | 154 | 85 | 47 |
# 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
# significant means no homogeneity of variance, acknowledge issues for this class but don't change anything
leveneTest(pswq~gender*income, data = d3) # 2 way anova
## Levene's Test for Homogeneity of Variance (center = median)
## Df F value Pr(>F)
## group 7 0.4187 0.8905
## 316
# use this commented out section only if you need to remove outliers
# to drop a single outlier, remove the # at the beginning of the line and use this code:
# d <- subset(d, row_id!=c(296))
# to drop multiple outliers, remove the # at the beginning of the line and 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~x1*x2 + c, where y is our DV, x1 is our first IV, x2 is our second IV, and c is our covariate
# reg_model <- lm(pss ~ gender_rc, data = d) #for one-way
reg_model2 <- lm(pswq ~ gender*income, data = d3) #for two-way
# Cook's distance
plot(reg_model2, 4)
# Residuals vs Leverage
plot(reg_model2, 5)
Our cell sizes are very unbalanced. A small sample size for one of the levels of our variable (male low income) limits our power and increases our Type II error rate.
Levene’s test is was not significant, so we have met the assumptions for homogeneity of variance.
We checked for outliers and decided to not remove any, as there are none reaching Cook’s distance of 0.5. Although, there are two at or above 0.1.
aov_model2 <- aov_ez(data = d3,
id = "row_id",
between = c("gender","income"),
dv = "pswq",
anova_table = list(es = "pes"))
## Contrasts set to contr.sum for the following variables: gender, income
Effect size cutoffs from Cohen (1988):
nice(aov_model2)
## Anova Table (Type 3 tests)
##
## Response: pswq
## Effect df MSE F pes p.value
## 1 gender 1, 316 0.74 8.95 ** .028 .003
## 2 income 3, 316 0.74 2.08 .019 .102
## 3 gender:income 3, 316 0.74 0.85 .008 .467
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '+' 0.1 ' ' 1
afex_plot(aov_model2, x = "gender", trace = "income")
afex_plot(aov_model2, x = "income", trace = "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_model2, specs="gender", adjust="tukey") #removes effect of race
## NOTE: Results may be misleading due to involvement in interactions
## 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.01 0.0587 316 2.87 3.14
## male 2.50 0.1597 316 2.14 2.86
##
## Results are averaged over the levels of: income
## Confidence level used: 0.95
## Conf-level adjustment: sidak method for 2 estimates
pairs(emmeans(aov_model2, specs="gender", adjust="tukey"))
## NOTE: Results may be misleading due to involvement in interactions
## contrast estimate SE df t.ratio p.value
## female - male 0.509 0.17 316 2.991 0.0030
##
## Results are averaged over the levels of: income
# emmeans(aov_model2, specs="income", adjust="tukey")
# pairs(emmeans(aov_model2, specs="income", adjust="tukey"))
# emmeans(aov_model2, specs="gender", by="income", adjust="sidak")
# pairs(emmeans(aov_model2, specs="gender", by="income", adjust="sidak"))
# emmeans(aov_model2, specs="income", by="gender", adjust="sidak")
# pairs(emmeans(aov_model2, specs="income", by="gender", adjust="sidak"))
To test our hypothesis that gender and income would impact stress and would interact significantly, we used a two-way/factorial ANOVA. Our data met most of the assumptions of the test, although our data was unbalanced, with many more women participating in our survey (n = 285) than men (n = 39). We removed those who used another term or preferred not to specify their gender, due to the small number of participants. We did not remove any outliers from the data set.
As predicted, we found a significant main effect for gender, F(1,315) = 11.64, p < .001, ηp2 .036 (small to medium effect size; Cohen, 1988). As predicted, women reported significantly more stress (mean = 3.01) than men (mean = 2.39). Contrary to our expectations, we did not find a significant main effect for income (p = .376).
Lastly, we did not find a significant interaction between gender and income (p = .370) (see Figure 2).
References
Cohen J. (1988). Statistical Power Analysis for the Behavioral Sciences. New York, NY: Routledge Academic.