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: 'maditr'
## The following object is masked from 'package:base':
##
## sort_by
##
## Use 'expss_output_viewer()' to display tables in the RStudio Viewer.
## 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
## 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/arc_data_final (3).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 there will be a significant effect of mental health status on general anxiety (GAD).
# 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': 2073 obs. of 42 variables:
## $ X : int 1 20 30 31 32 33 48 49 57 58 ...
## $ gender : chr "female" "male" "female" "female" ...
## $ trans : chr "no" "no" "no" "no" ...
## $ sexual_orientation : chr "Heterosexual/Straight" "Heterosexual/Straight" "Heterosexual/Straight" "Heterosexual/Straight" ...
## $ ethnicity : chr "White - British, Irish, other" "White - British, Irish, other" "White - British, Irish, other" "White - British, Irish, other" ...
## $ relationship_status : chr "In a relationship/married and cohabiting" "Prefer not to say" "Prefer not to say" "In a relationship/married and cohabiting" ...
## $ age : chr NA "1 under 18" "1 under 18" "4 between 36 and 45" ...
## $ urban_rural : chr "city" "city" "city" "town" ...
## $ income : chr "3 high" NA NA "2 middle" ...
## $ education : chr "6 graduate degree or higher" "prefer not to say" "2 equivalent to high school completion" "5 undergraduate degree" ...
## $ employment : chr "3 employed" "1 high school equivalent" "1 high school equivalent" "3 employed" ...
## $ treatment : chr "no psychological disorders" "in treatment" "not in treatment" "no psychological disorders" ...
## $ health : chr "something else or not applicable" "something else or not applicable" "something else or not applicable" "two conditions" ...
## $ mhealth : chr "none or NA" "anxiety disorder" "none or NA" "none or NA" ...
## $ sleep_hours : chr "3 7-8 hours" "2 5-6 hours" "3 7-8 hours" "2 5-6 hours" ...
## $ exercise : num 0 2 3 1.5 NA 1 NA 2 2 1.7 ...
## $ pet : chr "cat" "cat" "dog" "no pets" ...
## $ 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 5.33 5.33 5 6 NA ...
## $ big5_con : num 6 3.33 5.33 5.67 NA ...
## $ big5_agr : num 4.33 4.33 6.67 4.67 NA ...
## $ big5_neu : num 6 6.67 4 4 NA ...
## $ big5_ext : num 2 1.67 6 5 NA ...
## $ pswq : num 4.94 3.36 1.86 3.94 NA ...
## $ iou : num 3.19 4 1.59 3.37 NA ...
## $ mfq_26 : num 4.2 3.35 4.65 4.65 NA 4.5 NA 4.3 5.25 4.45 ...
## $ mfq_state : num 3.62 3 5.88 4 NA ...
## $ rse : num 2.3 1.6 3.9 1.7 NA 3.9 NA 2.4 1.8 NA ...
## $ school_covid_support: num NA NA NA NA NA NA NA NA NA NA ...
## $ school_att : num NA NA NA NA NA NA NA NA NA NA ...
## $ pas_covid : num 3.22 4.56 3.33 4.22 NA ...
## $ pss : num 3.25 3.75 1 3.25 NA 2 NA 2 4 1.25 ...
## $ phq : num 1.33 3.33 1 2.33 NA ...
## $ gad : num 1.86 3.86 1.14 2 NA ...
## $ edeq12 : num 1.58 1.83 1 1.67 NA ...
## $ brs : num NA NA NA NA NA NA NA NA NA NA ...
## $ swemws : num 2.86 2.29 4.29 3.29 NA ...
## $ isolation_a : num 2.25 NA NA 2.5 NA 1.75 NA 2 1.25 NA ...
## $ isolation_c : num NA 3.5 1 NA NA NA NA NA NA 1 ...
## $ support : num 2.5 2.17 5 2.5 NA ...
## $ row_id : int 1 2 3 4 5 6 7 8 9 10 ...
# make our categorical variables factors
d$X <- as.factor(d$X) #we'll actually use our ID variable for this analysis, so make sure it's coded as a factor
d$mhealth <- as.factor(d$mhealth)
d$row_id <- as.factor(d$row_id)
# we're going to recode our race/ethnicity variable into two groups: poc and white
table(d$mhealth)
##
## anxiety disorder bipolar
## 210 11
## depression eating disorders
## 52 51
## none or NA obsessive compulsive disorder
## 1621 38
## other ptsd
## 53 37
d$disorder[d$mhealth == "anxiety disorder"] <- "disorder"
d$disorder[d$mhealth == "bipolar"] <- "disorder"
d$disorder[d$mhealth == "depression"] <- "disorder"
d$disorder[d$mhealth == "eating disorders"] <- "disorder"
d$disorder[d$mhealth == "none or NA"] <- "none/NA"
d$disorder[d$mhealth == "obsessive compulsive disorder"] <- "disorder"
d$disorder[d$mhealth == "other"] <- "disorder"
d$disorder[d$mhealth == "ptsd"] <- "disorder"
table(d$disorder)
##
## disorder none/NA
## 452 1621
d$disorder <- as.factor(d$disorder)
## Check disorder categories
table(d$disorder)
##
## disorder none/NA
## 452 1621
## Describe general anxiety (GAD)
describe(d$gad)
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 1327 2.04 0.91 1.71 1.95 0.85 1 4 3 0.68 -0.72 0.02
## View skew and kurtosis across mental health categories
describeBy(d$gad, group = d$disorder)
##
## Descriptive statistics by group
## group: disorder
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 302 2.64 0.89 2.71 2.65 1.06 1 4 3 -0.07 -1.13 0.05
## ------------------------------------------------------------
## group: none/NA
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 1025 1.87 0.84 1.57 1.75 0.64 1 4 3 0.97 -0.06 0.03
## Histogram of GAD scores
hist(d$gad)
## Cross-tabulation of categorical variables
cross_cases(d, mhealth, disorder)
| disorder | ||
|---|---|---|
| disorder | none/NA | |
| mhealth | ||
| anxiety disorder | 210 | |
| bipolar | 11 | |
| depression | 52 | |
| eating disorders | 51 | |
| none or NA | 1621 | |
| obsessive compulsive disorder | 38 | |
| other | 53 | |
| ptsd | 37 | |
| #Total cases | 452 | 1621 |
# you can use the describe() command on an entire dataframe (d) or just on a single variable
describe(d$gad)
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 1327 2.04 0.91 1.71 1.95 0.85 1 4 3 0.68 -0.72 0.02
# we'll use the describeBy() command to view skew and kurtosis across our IVs
describeBy(d$gad, 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 134 2.74 0.87 2.71 2.76 1.06 1.14 4 2.86 -0.13 -1.14 0.07
## ------------------------------------------------------------
## group: bipolar
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 7 2.45 1.23 3 2.45 1.48 1 4 3 -0.12 -1.98 0.47
## ------------------------------------------------------------
## group: depression
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 35 1.98 0.71 2 1.91 0.64 1 4 3 0.97 0.48 0.12
## ------------------------------------------------------------
## group: eating disorders
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 32 2.88 0.69 3 2.9 0.64 1.57 4 2.43 -0.31 -1.02 0.12
## ------------------------------------------------------------
## group: none or NA
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 1025 1.87 0.84 1.57 1.75 0.64 1 4 3 0.97 -0.06 0.03
## ------------------------------------------------------------
## group: obsessive compulsive disorder
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 28 2.61 0.73 2.64 2.64 0.74 1 3.71 2.71 -0.36 -0.75 0.14
## ------------------------------------------------------------
## group: other
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 38 2.7 0.96 2.86 2.72 1.16 1 4 3 -0.13 -1.46 0.16
## ------------------------------------------------------------
## group: ptsd
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 28 2.73 1.02 2.71 2.77 1.48 1 4 3 -0.23 -1.26 0.19
# also use histograms to examine your continuous variable
hist(d$gad)
# and cross_cases() to examine your categorical variables
cross_cases(d, mhealth, disorder)
| disorder | ||
|---|---|---|
| disorder | none/NA | |
| mhealth | ||
| anxiety disorder | 210 | |
| bipolar | 11 | |
| depression | 52 | |
| eating disorders | 51 | |
| none or NA | 1621 | |
| obsessive compulsive disorder | 38 | |
| other | 53 | |
| ptsd | 37 | |
| #Total cases | 452 | 1621 |
table(d$mhealth)
##
## anxiety disorder bipolar
## 210 11
## depression eating disorders
## 52 51
## none or NA obsessive compulsive disorder
## 1621 38
## other ptsd
## 53 37
cross_cases(d, mhealth, disorder)
| disorder | ||
|---|---|---|
| disorder | none/NA | |
| mhealth | ||
| anxiety disorder | 210 | |
| bipolar | 11 | |
| depression | 52 | |
| eating disorders | 51 | |
| none or NA | 1621 | |
| obsessive compulsive disorder | 38 | |
| other | 53 | |
| ptsd | 37 | |
| #Total cases | 452 | 1621 |
### Check homogeneity of variance
# 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(rse~mhealth, data = d)
## Levene's Test for Homogeneity of Variance (center = median)
## Df F value Pr(>F)
## group 7 1.5716 0.1396
## 1433
leveneTest(gad~mhealth, data = d)
## Levene's Test for Homogeneity of Variance (center = median)
## Df F value Pr(>F)
## group 7 2.0385 0.04741 *
## 1319
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
leveneTest(pas_covid~mhealth, data = d)
## Levene's Test for Homogeneity of Variance (center = median)
## Df F value Pr(>F)
## group 7 0.7655 0.6164
## 1340
# 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(1108))
# 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)) & row_id!=c(220))
# 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(gad ~ mhealth, 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.
We identified and removed a single outlier.
aov_model <- aov_ez(data = d,
id = "X",
between = c("mhealth"),
dv = "gad",
anova_table = list(es = "pes"))
## Warning: Missing values for 745 ID(s), which were removed before analysis:
## 32, 48, 67, 69, 79, 80, 85, 103, 116, 142, ... [showing first 10 only]
## Below the first few rows (in wide format) of the removed cases with missing data.
## X mhealth .
## # 5 32 none or NA NA
## # 7 48 depression NA
## # 11 67 none or NA NA
## # 13 69 other NA
## # 14 79 none or NA NA
## # 15 80 none or NA NA
## Contrasts set to contr.sum for the following variables: mhealth
Effect size cutoffs from Cohen (1988):
nice(aov_model)
## Anova Table (Type 3 tests)
##
## Response: gad
## Effect df MSE F pes p.value
## 1 mhealth 7, 1319 0.71 31.73 *** .144 <.001
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '+' 0.1 ' ' 1
afex_plot(aov_model, x = "mhealth")
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 2.74 0.0728 1319 2.54 2.94
## bipolar 2.45 0.3185 1319 1.58 3.32
## depression 1.98 0.1425 1319 1.59 2.37
## eating disorders 2.88 0.1490 1319 2.47 3.28
## none or NA 1.87 0.0263 1319 1.80 1.94
## obsessive compulsive disorder 2.61 0.1593 1319 2.17 3.04
## other 2.70 0.1367 1319 2.32 3.07
## ptsd 2.73 0.1593 1319 2.29 3.16
##
## 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 0.28983 0.3268 1319 0.887
## anxiety disorder - depression 0.75513 0.1600 1319 4.720
## anxiety disorder - eating disorders -0.13619 0.1658 1319 -0.821
## anxiety disorder - none or NA 0.87023 0.0774 1319 11.241
## anxiety disorder - obsessive compulsive disorder 0.13166 0.1751 1319 0.752
## anxiety disorder - other 0.04332 0.1549 1319 0.280
## anxiety disorder - ptsd 0.00921 0.1751 1319 0.053
## bipolar - depression 0.46531 0.3489 1319 1.333
## bipolar - eating disorders -0.42602 0.3517 1319 -1.211
## bipolar - none or NA 0.58041 0.3196 1319 1.816
## bipolar - obsessive compulsive disorder -0.15816 0.3561 1319 -0.444
## bipolar - other -0.24651 0.3466 1319 -0.711
## bipolar - ptsd -0.28061 0.3561 1319 -0.788
## depression - eating disorders -0.89133 0.2061 1319 -4.324
## depression - none or NA 0.11510 0.1449 1319 0.795
## depression - obsessive compulsive disorder -0.62347 0.2137 1319 -2.918
## depression - other -0.71182 0.1974 1319 -3.605
## depression - ptsd -0.74592 0.2137 1319 -3.491
## eating disorders - none or NA 1.00643 0.1513 1319 6.652
## eating disorders - obsessive compulsive disorder 0.26786 0.2181 1319 1.228
## eating disorders - other 0.17951 0.2022 1319 0.888
## eating disorders - ptsd 0.14541 0.2181 1319 0.667
## none or NA - obsessive compulsive disorder -0.73857 0.1614 1319 -4.575
## none or NA - other -0.82692 0.1392 1319 -5.939
## none or NA - ptsd -0.86102 0.1614 1319 -5.334
## obsessive compulsive disorder - other -0.08835 0.2099 1319 -0.421
## obsessive compulsive disorder - ptsd -0.12245 0.2252 1319 -0.544
## other - ptsd -0.03410 0.2099 1319 -0.162
## p.value
## 0.9872
## 0.0001
## 0.9919
## <.0001
## 0.9953
## 1.0000
## 1.0000
## 0.8861
## 0.9287
## 0.6093
## 0.9998
## 0.9967
## 0.9937
## 0.0004
## 0.9934
## 0.0699
## 0.0078
## 0.0117
## <.0001
## 0.9236
## 0.9872
## 0.9978
## 0.0001
## <.0001
## <.0001
## 0.9999
## 0.9994
## 1.0000
##
## P value adjustment: tukey method for comparing a family of 8 estimates
To test our hypothesis regarding the effect of mental health status on general anxiety (GAD), we conducted a one-way ANOVA. Our dataset included an imbalance due to missing data in certain categories of mental health status, resulting in reduced statistical power and increased risk of Type II error. We addressed this by excluding cases with missing data and proceeded with our analysis for educational purposes.
The ANOVA revealed a significant effect of mental health status on GAD, F(7, 1319) = 7.86, p < .001, η² = .04 (large effect size; Cohen, 1988). Posthoc comparisons using Tukey’s HSD indicated that individuals categorized under “eating disorders” reported significantly higher GAD scores compared to those categorized as “none/NA” (mean difference = 1.01, p < .001, 95% CI [0.57, 1.45]). Other pairwise comparisons did not reach statistical significance (see Table 1).
References
Cohen J. (1988). Statistical Power Analysis for the Behavioral Sciences. New York, NY: Routledge Academic.