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)
##
## Use 'expss_output_rnotebook()' to display tables inside R Notebooks.
## To return to the console output, use 'expss_output_default()'.
##
## 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_data_finalclean.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)
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 mental health can predict intolerance of uncertainty, as measured by the intolerance of uncertainty scale.
# 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': 1182 obs. of 7 variables:
## $ sleep_hours: chr "3 7-8 hours" "2 5-6 hours" "3 7-8 hours" "2 5-6 hours" ...
## $ mhealth : chr "none or NA" "anxiety disorder" "none or NA" "none or NA" ...
## $ iou : num 3.19 4 1.59 3.37 1.7 ...
## $ mfq_26 : num 4.2 3.35 4.65 4.65 4.5 4.3 5.25 4.45 4.7 4.05 ...
## $ pas_covid : num 3.22 4.56 3.33 4.22 3.22 ...
## $ pss : num 3.25 3.75 1 3.25 2 2 4 1.25 1.25 2.5 ...
## $ row_id : int 1 2 3 4 5 6 7 8 9 10 ...
# make our categorical variables factors
d$sleep_hours <- as.factor(d$sleep_hours)
d$mhealth <- as.factor(d$mhealth)
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
# we're going to recode our race/ethnicity variable into two groups: poc and white
table(d$mhealth)
##
## anxiety disorder bipolar
## 123 5
## depression eating disorders
## 28 28
## none or NA obsessive compulsive disorder
## 916 27
## other ptsd
## 35 20
d$mhp[d$mhealth == "anxiety disorder"] <- "mhp"
d$mhp[d$mhealth == "bipolar"] <- "mhp"
d$mhp[d$mhealth == "depression"] <- "mhp"
d$mhp[d$mhealth == "eating disorders"] <- "mhp"
d$mhp[d$mhealth == "other"] <- "mhp"
d$mhp[d$mhealth == "obsessive compulsive disorder"] <- "mhp"
d$mhp[d$mhealth == "ptsd"] <- "mhp"
d$mhp[d$mhealth == "none or NA"] <- "nmhp"
table(d$mhp)
##
## mhp nmhp
## 266 916
d$mhp <- as.factor(d$mhp)
# you can use the describe() command on an entire dataframe (d) or just on a single variable
describe(d$iou)
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 1182 2.56 0.9 2.41 2.5 0.99 1 5 4 0.5 -0.59 0.03
# we'll use the describeBy() command to view skew and kurtosis across our IVs
describeBy(d$iou, group = d$mhealth)
##
## Descriptive statistics by group
## group: anxiety disorder
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 123 3.14 0.85 3.15 3.14 0.93 1.11 4.93 3.81 0.02 -0.79 0.08
## ------------------------------------------------------------
## group: bipolar
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 5 2.59 1.19 2.37 2.59 0.71 1.37 4.48 3.11 0.54 -1.49 0.53
## ------------------------------------------------------------
## group: depression
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 28 2.41 0.98 2.15 2.34 0.8 1.04 4.78 3.74 0.83 -0.27 0.19
## ------------------------------------------------------------
## group: eating disorders
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 28 3.41 0.69 3.59 3.45 0.71 2 4.33 2.33 -0.58 -0.87 0.13
## ------------------------------------------------------------
## group: none or NA
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 916 2.39 0.82 2.26 2.33 0.82 1 5 4 0.63 -0.24 0.03
## ------------------------------------------------------------
## group: obsessive compulsive disorder
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 27 3.26 0.91 3.11 3.27 0.99 1.59 4.78 3.19 -0.06 -1.11 0.17
## ------------------------------------------------------------
## group: other
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 35 3.37 1.05 3.41 3.44 1.04 1.3 4.7 3.41 -0.5 -0.87 0.18
## ------------------------------------------------------------
## group: ptsd
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 20 3.27 1.07 3.46 3.32 0.8 1.41 5 3.59 -0.51 -0.92 0.24
describeBy(d$iou, group = d$mhp)
##
## Descriptive statistics by group
## group: mhp
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 266 3.14 0.94 3.15 3.15 1.1 1.04 5 3.96 -0.14 -0.9 0.06
## ------------------------------------------------------------
## group: nmhp
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 916 2.39 0.82 2.26 2.33 0.82 1 5 4 0.63 -0.24 0.03
# also use histograms to examine your continuous variable
hist(d$iou)
# and cross_cases() to examine your categorical variables
cross_cases(d, mhealth, mhp)
| mhp | ||
|---|---|---|
| mhp | nmhp | |
| mhealth | ||
| anxiety disorder | 123 | |
| bipolar | 5 | |
| depression | 28 | |
| eating disorders | 28 | |
| none or NA | 916 | |
| obsessive compulsive disorder | 27 | |
| other | 35 | |
| ptsd | 20 | |
| #Total cases | 266 | 916 |
table(d$mhealth)
##
## anxiety disorder bipolar
## 123 5
## depression eating disorders
## 28 28
## none or NA obsessive compulsive disorder
## 916 27
## other ptsd
## 35 20
cross_cases(d, mhealth, mhp)
| mhp | ||
|---|---|---|
| mhp | nmhp | |
| mhealth | ||
| anxiety disorder | 123 | |
| bipolar | 5 | |
| depression | 28 | |
| eating disorders | 28 | |
| none or NA | 916 | |
| obsessive compulsive disorder | 27 | |
| other | 35 | |
| ptsd | 20 | |
| #Total cases | 266 | 916 |
# 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, mhealth != "mhp")
# d2$mhealth <- droplevels(d2$mhealth)
# to double-check any changes we made
# cross_cases(d2, IV1, IV2)
# 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(iou~mhealth, data = d)
## Levene's Test for Homogeneity of Variance (center = median)
## Df F value Pr(>F)
## group 7 1.4479 0.1823
## 1174
# leveneTest(iou~mhealth*hmp, data = d2)
# 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(824))
# 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(iou ~ mhealth, data = d) #for one-way
# reg_model2 <- lm(DV ~ IV1*IV2, data = d2) #for two-way
# Cook's distance
plot(reg_model, 4)
# Residuals vs Leverage
plot(reg_model, 5)
# 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 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.
We identified and removed a single outlier.
aov_model <- aov_ez(data = d,
id = "row_id",
between = c("mhealth"),
dv = "iou",
anova_table = list(es = "pes"))
## Contrasts set to contr.sum for the following variables: mhealth
# aov_model2 <- aov_ez(data = d2,
# id = "X",
# between = c("IV1","IV2"),
# dv = "pss",
# anova_table = list(es = "pes"))
Effect size cutoffs from Cohen (1988):
nice(aov_model)
## Anova Table (Type 3 tests)
##
## Response: iou
## Effect df MSE F pes p.value
## 1 mhealth 7, 1173 0.70 27.69 *** .142 <.001
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '+' 0.1 ' ' 1
# nice(aov_model2)
afex_plot(aov_model, x = "mhealth")
# afex_plot(aov_model2, x = "IV1", trace = "IV2")
# afex_plot(aov_model2, x = "IV2", trace = "IV1")
# p < 0.001, result is significant
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="mhealth", adjust="tukey")
## Note: adjust = "tukey" was changed to "sidak"
## because "tukey" is only appropriate for one set of pairwise comparisons
## mhealth emmean SE df lower.CL upper.CL
## anxiety disorder 3.14 0.0755 1173 2.937 3.35
## bipolar 2.12 0.4188 1173 0.976 3.26
## depression 2.41 0.1583 1173 1.980 2.85
## eating disorders 3.41 0.1583 1173 2.979 3.84
## none or NA 2.39 0.0277 1173 2.316 2.47
## obsessive compulsive disorder 3.26 0.1612 1173 2.820 3.70
## other 3.37 0.1416 1173 2.984 3.76
## ptsd 3.27 0.1873 1173 2.762 3.79
##
## Confidence level used: 0.95
## Conf-level adjustment: sidak method for 8 estimates
pairs(emmeans(aov_model, specs="mhealth", adjust="tukey"))
## contrast estimate SE df t.ratio
## anxiety disorder - bipolar 1.0230 0.4256 1173 2.404
## anxiety disorder - depression 0.7306 0.1754 1173 4.166
## anxiety disorder - eating disorders -0.2680 0.1754 1173 -1.528
## anxiety disorder - none or NA 0.7520 0.0804 1173 9.348
## anxiety disorder - obsessive compulsive disorder -0.1173 0.1780 1173 -0.659
## anxiety disorder - other -0.2270 0.1605 1173 -1.415
## anxiety disorder - ptsd -0.1307 0.2020 1173 -0.647
## bipolar - depression -0.2923 0.4477 1173 -0.653
## bipolar - eating disorders -1.2910 0.4477 1173 -2.883
## bipolar - none or NA -0.2710 0.4197 1173 -0.646
## bipolar - obsessive compulsive disorder -1.1403 0.4488 1173 -2.541
## bipolar - other -1.2500 0.4421 1173 -2.827
## bipolar - ptsd -1.1537 0.4588 1173 -2.515
## depression - eating disorders -0.9987 0.2239 1173 -4.461
## depression - none or NA 0.0213 0.1607 1173 0.133
## depression - obsessive compulsive disorder -0.8479 0.2259 1173 -3.753
## depression - other -0.9577 0.2124 1173 -4.509
## depression - ptsd -0.8614 0.2452 1173 -3.512
## eating disorders - none or NA 1.0200 0.1607 1173 6.347
## eating disorders - obsessive compulsive disorder 0.1507 0.2259 1173 0.667
## eating disorders - other 0.0410 0.2124 1173 0.193
## eating disorders - ptsd 0.1373 0.2452 1173 0.560
## none or NA - obsessive compulsive disorder -0.8693 0.1636 1173 -5.315
## none or NA - other -0.9790 0.1443 1173 -6.786
## none or NA - ptsd -0.8827 0.1893 1173 -4.662
## obsessive compulsive disorder - other -0.1097 0.2146 1173 -0.511
## obsessive compulsive disorder - ptsd -0.0134 0.2471 1173 -0.054
## other - ptsd 0.0963 0.2348 1173 0.410
## p.value
## 0.2404
## 0.0009
## 0.7922
## <.0001
## 0.9980
## 0.8506
## 0.9982
## 0.9981
## 0.0769
## 0.9982
## 0.1794
## 0.0893
## 0.1901
## 0.0002
## 1.0000
## 0.0045
## 0.0002
## 0.0109
## <.0001
## 0.9978
## 1.0000
## 0.9993
## <.0001
## <.0001
## 0.0001
## 0.9996
## 1.0000
## 0.9999
##
## P value adjustment: tukey method for comparing a family of 8 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.
#emmeans(aov_model, specs="IV1", adjust="tukey")
#pairs(emmeans(aov_model, specs="IV1", adjust="tukey"))
#emmeans(aov_model2, specs="IV2", adjust="tukey")
#pairs(emmeans(aov_model2, specs="IV2", adjust="tukey"))
#emmeans(aov_model2, specs="IV1", by="IV2", adjust="sidak")
#pairs(emmeans(aov_model2, specs="IV2", by="IV1", adjust="sidak"))
#emmeans(aov_model2, specs="IV2", by="IV1", adjust="sidak")
#pairs(emmeans(aov_model2, specs="IV2", by="IV1", adjust="sidak"))
To test our hypothesis that mental health predicts intolerance of uncertainty, we used a one-way ANOVA. Our data was unbalanced, with only a small portion of participants have mental health issues in our survey (n = 266), the majority of participants don’t have mental health issues (n - 916). This significantly reduces the power of our test and increases the chances of a Type II error. We also identified and removed a single outlier following visual analysis of a Residuals vs Leverage plot. A significant Levene’s test (p = 0.17) 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 mental health problems on intolerance of uncertanity, F(7,1173) = 27.69, p < .001, ηp2 = .142 (large effect size; Cohen, 1988). Posthoc tests using Tukey’s HSD revealed that different types of mental disorders predicts a different level of intolerance of uncertainty. (see Figure 1 for a comparison).
To test our hypothesis that gender and race 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 = 1004) than men (n = 195). We identified and removed a single outlier following visual analysis of a Residuals vs Leverage plot.
As predicted, we found a significant main effect for gender, F(1,1195) = 28.31, p < .001, ηp2 .023 (small effect size; Cohen, 1988). As predicted, women reported significantly more stress than men. Contrary to our expectations, we did not find a significant main effect for race (p = .453).
Lastly, we found a significant interaction between gender and race (see Figure 2), F(1,1195) = 3.43, p = .064, ηp2 = .003 (trivial effect size; Cohen, 1988). When comparing by race, women of color (M = 3.16, SE = .07) reported significantly more stress than men of color (M = 2.52, SE = .14; p < .001), as did white women (M = 2.93, SE = .03) compared to white men (M = 2.62, SE = .08; p < .001). When comparing by gender, women of color reported significantly more stress than white women (p = .002), while men of color and white men reported similar levels of stress (p = .546).
References
Cohen J. (1988). Statistical Power Analysis for the Behavioral Sciences. New York, NY: Routledge Academic.