#install.packages("afex")
#install.packages("emmeans")
#install.packages("ggbeeswarm")
#install.packages("expss")
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 select rows from data: rows(mtcars, am==0)
##
## 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
## Loading required package: lme4
## Loading required package: Matrix
## Registered S3 method overwritten by 'lme4':
## method from
## na.action.merMod car
##
## 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(ggbeeswarm) # to run plot results
library(emmeans) # for posthoc tests
## Welcome to emmeans.
## Caution: You lose important information if you filter this package's results.
## See '? untidy'
# For HW, import the project dataset you cleaned previously this will be the dataset you'll use throughout the rest of the semester
d <- read.csv(file="Data/projectdata.csv", header=T)
# new code! this adds a column with a number for each row. It will make it easier if we need to drop outliers later
d$row_id <- 1:nrow(d)
Note: For your HW, you will choose 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 with 2 or 3 levels each). You will need to specify your hypothesis and customize your code based on the choice you make. We will run BOTH versions of the test in the lab for illustrative purposes.
One Way: We predict that there will be a significant difference in Eating Disorder Symptoms by people’s mental health disorders (bipolar, depression, ocd).
# you only need to check the variables you're using in the current analysis
str(d)
## 'data.frame': 675 obs. of 8 variables:
## $ X : int 520 2814 3146 3295 717 6056 4753 5365 2044 1965 ...
## $ age : chr "1 under 18" "1 under 18" "1 under 18" "1 under 18" ...
## $ mhealth: chr "none or NA" "none or NA" "none or NA" "none or NA" ...
## $ pss : num 2.75 2.25 3 2 1.75 2 1 1.25 3 1.25 ...
## $ phq : num 1.56 1.44 1.11 1.33 1.44 ...
## $ gad : num 1.14 1.29 1 1 1.14 ...
## $ edeq12 : num 1.33 1.08 1 1 1.17 ...
## $ row_id : int 1 2 3 4 5 6 7 8 9 10 ...
# make our categorical variables of interest "factors"
# because we'll use our newly created row ID variable for this analysis, so make sure it's coded as a factor, too.
d$mhealth <- as.factor(d$mhealth)
d$row_id <- as.factor(d$row_id)
table(d$mhealth)
##
## anxiety disorder bipolar
## 75 3
## depression eating disorders
## 12 19
## none or NA obsessive compulsive disorder
## 519 15
## other ptsd
## 18 14
# check that all our categorical variables of interest are now factors
str(d)
## 'data.frame': 675 obs. of 8 variables:
## $ X : int 520 2814 3146 3295 717 6056 4753 5365 2044 1965 ...
## $ age : chr "1 under 18" "1 under 18" "1 under 18" "1 under 18" ...
## $ mhealth: Factor w/ 8 levels "anxiety disorder",..: 5 5 5 5 5 5 5 5 5 5 ...
## $ pss : num 2.75 2.25 3 2 1.75 2 1 1.25 3 1.25 ...
## $ phq : num 1.56 1.44 1.11 1.33 1.44 ...
## $ gad : num 1.14 1.29 1 1 1.14 ...
## $ edeq12 : num 1.33 1.08 1 1 1.17 ...
## $ row_id : Factor w/ 675 levels "1","2","3","4",..: 1 2 3 4 5 6 7 8 9 10 ...
# check our DV skew and kurtosis
describe(d$edeq12)
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 675 1.95 0.77 1.83 1.88 0.86 1 4 3 0.54 -0.83 0.03
# we'll use the describeBy() command to view our DV's skew and kurtosis across our IVs' levels
describeBy(d$edeq12, 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 75 14.04 8.11 14 13.95 10.38 1 29 28 0.08 -1.18 0.94
## ------------------------------------------------------------
## group: bipolar
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1* 1 3 1.33 0.58 1 1.33 0 1 2 1 0.38 -2.33 0.33
## ------------------------------------------------------------
## group: depression
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1* 1 12 5.75 2.83 6 5.8 2.97 1 10 9 -0.19 -1.36 0.82
## ------------------------------------------------------------
## group: eating disorders
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1* 1 19 7.84 3.96 8 7.88 4.45 1 14 13 -0.1 -1.33 0.91
## ------------------------------------------------------------
## group: none or NA
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1* 1 519 10.95 8.86 8 10.01 8.9 1 35 34 0.74 -0.53 0.39
## ------------------------------------------------------------
## group: obsessive compulsive disorder
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1* 1 15 7.33 3.72 8 7.38 4.45 1 13 12 -0.21 -1.35 0.96
## ------------------------------------------------------------
## group: other
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1* 1 18 8.06 4.73 7.5 8 5.93 1 16 15 0.12 -1.38 1.12
## ------------------------------------------------------------
## group: ptsd
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1* 1 14 7 3.55 7.5 7.08 3.71 1 12 11 -0.26 -1.47 0.95
# also use histograms to examine your continuous variable
hist(d$edeq12)
# REMEMBER your test's level of POWER is determined by your SMALLEST subsample
# One-Way
table(d$mhealth)
##
## anxiety disorder bipolar
## 75 3
## depression eating disorders
## 12 19
## none or NA obsessive compulsive disorder
## 519 15
## other ptsd
## 18 14
# 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
# One-Way
leveneTest(edeq12~mhealth, data = d)
## Levene's Test for Homogeneity of Variance (center = median)
## Df F value Pr(>F)
## group 7 1.3054 0.2449
## 667
# use this commented out section below ONLY IF if you need to remove outliers
# to drop a single outlier, use this code:
# d <- subset(d, row_id!=c(1108))
# to drop multiple outliers, 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.
# One-Way
reg_model <- lm(edeq12~mhealth, data = d)
# Cook's distance
plot(reg_model, 4)
# Residuals VS Leverage
plot(reg_model, 5)
Our cell sizes are unbalanced between the mental health group levels. Larger sample sizes for some of the levels of our variable limits our power and increases our Type II error rate.
Levene’s test was significant for our three-level mental health variable with the One-Way ANOVA. We are ignoring this and continuing with the analysis anyway for this class.
We identified some outliers, but nothing extreme, so proceeding with this caution would be important.
[UPDATE this section in your HW.]
# One-Way
aov_model <- aov_ez(data = d,
id = "X",
between = c("mhealth"),
dv = "edeq12",
anova_table = list(es = "pes"))
## Contrasts set to contr.sum for the following variables: mhealth
# One-Way
nice(aov_model)
## Anova Table (Type 3 tests)
##
## Response: edeq12
## Effect df MSE F pes p.value
## 1 mhealth 7, 667 0.55 9.18 *** .088 <.001
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '+' 0.1 ' ' 1
ANOVA Effect Size [partial eta-squared] cutoffs from Cohen (1988): * η^2 < 0.01 indicates a trivial effect * η^2 >= 0.01 indicates a small effect * η^2 >= 0.06 indicates a medium effect * η^2 >= 0.14 indicates a large effect
# One-Way
afex_plot(aov_model, x = "mhealth")
ONLY run posthoc IF the ANOVA test is SIGNIFICANT! E.g., only run the posthoc tests on pet type if there is a main effect for pet type
emmeans(aov_model, specs="mhealth", adjust="sidak")
## mhealth emmean SE df lower.CL upper.CL
## anxiety disorder 2.25 0.0855 667 2.01 2.48
## bipolar 2.69 0.4270 667 1.53 3.86
## depression 1.98 0.2140 667 1.39 2.56
## eating disorders 2.65 0.1700 667 2.19 3.12
## none or NA 1.83 0.0325 667 1.74 1.92
## obsessive compulsive disorder 2.40 0.1910 667 1.88 2.92
## other 2.36 0.1740 667 1.88 2.83
## ptsd 2.49 0.1980 667 1.95 3.03
##
## Confidence level used: 0.95
## Conf-level adjustment: sidak method for 8 estimates
pairs(emmeans(aov_model, specs="mhealth", adjust="sidak"))
## contrast estimate SE df t.ratio
## anxiety disorder - bipolar -0.4489 0.4360 667 -1.030
## anxiety disorder - depression 0.2664 0.2300 667 1.157
## anxiety disorder - eating disorders -0.4080 0.1900 667 -2.146
## anxiety disorder - none or NA 0.4161 0.0914 667 4.550
## anxiety disorder - obsessive compulsive disorder -0.1544 0.2090 667 -0.738
## anxiety disorder - other -0.1109 0.1940 667 -0.571
## anxiety disorder - ptsd -0.2425 0.2160 667 -1.125
## bipolar - depression 0.7153 0.4780 667 1.497
## bipolar - eating disorders 0.0409 0.4600 667 0.089
## bipolar - none or NA 0.8650 0.4290 667 2.018
## bipolar - obsessive compulsive disorder 0.2944 0.4680 667 0.629
## bipolar - other 0.3380 0.4620 667 0.732
## bipolar - ptsd 0.2063 0.4710 667 0.438
## depression - eating disorders -0.6743 0.2730 667 -2.470
## depression - none or NA 0.1497 0.2160 667 0.692
## depression - obsessive compulsive disorder -0.4208 0.2870 667 -1.468
## depression - other -0.3773 0.2760 667 -1.368
## depression - ptsd -0.5089 0.2910 667 -1.748
## eating disorders - none or NA 0.8240 0.1730 667 4.766
## eating disorders - obsessive compulsive disorder 0.2535 0.2560 667 0.991
## eating disorders - other 0.2970 0.2430 667 1.220
## eating disorders - ptsd 0.1654 0.2610 667 0.634
## none or NA - obsessive compulsive disorder -0.5705 0.1940 667 -2.943
## none or NA - other -0.5270 0.1770 667 -2.969
## none or NA - ptsd -0.6586 0.2000 667 -3.285
## obsessive compulsive disorder - other 0.0435 0.2590 667 0.168
## obsessive compulsive disorder - ptsd -0.0881 0.2750 667 -0.320
## other - ptsd -0.1316 0.2640 667 -0.499
## p.value
## 0.9698
## 0.9434
## 0.3866
## 0.0002
## 0.9958
## 0.9992
## 0.9512
## 0.8092
## 1.0000
## 0.4704
## 0.9985
## 0.9960
## 0.9999
## 0.2100
## 0.9972
## 0.8244
## 0.8717
## 0.6558
## <0.0001
## 0.9756
## 0.9260
## 0.9984
## 0.0660
## 0.0613
## 0.0237
## 1.0000
## 1.0000
## 0.9997
##
## P value adjustment: tukey method for comparing a family of 8 estimates
To test our hypothesis that there will be a significant difference in people’s eating disorder symptoms based on the mental health disorders (bipolar, depression, ocd), we used a one-way ANOVA. Our data was unbalanced, with more people with ocd in our survey (n = 15) than who had bipolar disorder (n = 3) or depression (n = 12). This significantly reduce the power of our test and increases the chances of a Type II error. We also identified some, but not extreme outliers, following visual analysis of Cook’s Distance and Residuals VS Leverage plots. A non-significant Levene’s test (p = .245) also indicates that our data does not violate the assumption of homogeneity of variance. This suggests that there is not 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 disorders, F(7, 667) = 9.18, p < .001, ηp2 = .88 (large effect size; Cohen, 1988). Posthoc tests using Sidak’s adjustment revealed that participants with depression (M = 1.98, SE = 0.21) reported lower eating disorders symptoms than those who have bipolar disorder (M = 2.69, SE = 0.43) and less eating disorder symptoms than those who have ocd (M = 2.40, SE = 0.19); participants who had bipolar disorder reported the highest amount of eating disorder symptoms overall (see Figure 1 for a comparison).
References
Cohen J. (1988). Statistical Power Analysis for the Behavioral Sciences. New York, NY: Routledge Academic.