#install.packages("afex")
#install.packages("emmeans")
#install.packages("ggbeeswarm")
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 drop variable use NULL: let(mtcars, am = NULL) %>% head()
##
## 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
## 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(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 participant’s intolerance of uncertainty by their mental health diagnosis of either anxiety disorder, depression, and eating disorders.
# you only need to check the variables you're using in the current analysis
# even if 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': 931 obs. of 8 variables:
## $ X : int 321 401 520 1390 1422 2247 2526 2609 2689 2752 ...
## $ age : chr "1 under 18" "4 between 36 and 45" "1 under 18" "5 over 45" ...
## $ mhealth : chr "none or NA" "obsessive compulsive disorder" "none or NA" "none or NA" ...
## $ big5_open: num 4 6 3.67 3 2 ...
## $ iou : num 2.48 2.81 2.22 1.48 1.74 ...
## $ mfq_state: num 6 5 3 3.5 3.25 ...
## $ pas_covid: num 2.33 4 3 2.89 2.67 ...
## $ 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)
# check that all our categorical variables of interest are now factors
str(d)
## 'data.frame': 931 obs. of 8 variables:
## $ X : int 321 401 520 1390 1422 2247 2526 2609 2689 2752 ...
## $ age : chr "1 under 18" "4 between 36 and 45" "1 under 18" "5 over 45" ...
## $ mhealth : Factor w/ 8 levels "anxiety disorder",..: 5 6 5 5 5 5 5 5 7 5 ...
## $ big5_open: num 4 6 3.67 3 2 ...
## $ iou : num 2.48 2.81 2.22 1.48 1.74 ...
## $ mfq_state: num 6 5 3 3.5 3.25 ...
## $ pas_covid: num 2.33 4 3 2.89 2.67 ...
## $ row_id : Factor w/ 931 levels "1","2","3","4",..: 1 2 3 4 5 6 7 8 9 10 ...
# check our DV skew and kurtosis
describe(d$iou)
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 931 2.53 0.89 2.41 2.47 0.99 1 4.78 3.78 0.49 -0.63 0.03
# we'll use the describeBy() command to view our DV's skew and kurtosis across our IVs' levels
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 95 3.08 0.87 3.11 3.07 0.93 1.11 4.78 3.67 -0.02 -0.82 0.09
## ------------------------------------------------------------
## group: bipolar
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 3 3.07 1.31 2.85 3.07 1.43 1.89 4.48 2.59 0.16 -2.33 0.76
## ------------------------------------------------------------
## group: depression
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 24 2.39 0.84 2.22 2.31 0.71 1.15 4.33 3.19 0.77 -0.21 0.17
## ------------------------------------------------------------
## group: eating disorders
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 18 3.34 0.77 3.56 3.37 0.77 2 4.22 2.22 -0.5 -1.29 0.18
## ------------------------------------------------------------
## group: none or NA
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 739 2.37 0.82 2.22 2.31 0.88 1 4.78 3.78 0.62 -0.34 0.03
## ------------------------------------------------------------
## group: obsessive compulsive disorder
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 17 3.35 0.86 3.52 3.36 0.82 1.7 4.78 3.07 -0.21 -1.04 0.21
## ------------------------------------------------------------
## group: other
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 23 3.66 0.83 3.63 3.74 0.71 1.37 4.7 3.33 -0.77 0.35 0.17
## ------------------------------------------------------------
## group: ptsd
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 12 3.3 0.99 3.37 3.37 0.82 1.41 4.52 3.11 -0.72 -0.73 0.29
# also use histograms to examine your continuous variable
hist(d$iou)
# remove additional levels not in use
d <- subset(d, mhealth != "bipolar") # use subset() to remove all participants from the additional level
table(d$mhealth, useNA = "always") # verify that now there are ZERO participants in the additional level
##
## anxiety disorder bipolar
## 95 0
## depression eating disorders
## 24 18
## none or NA obsessive compulsive disorder
## 739 17
## other ptsd
## 23 12
## <NA>
## 0
d$mhealth <- droplevels(d$mhealth) # use droplevels() to drop the empty factor
table(d$mhealth, useNA = "always") # verify that now the entire factor level is removed
##
## anxiety disorder depression
## 95 24
## eating disorders none or NA
## 18 739
## obsessive compulsive disorder other
## 17 23
## ptsd <NA>
## 12 0
d <- subset(d, mhealth != "none or NA") # use subset() to remove all participants from the additional level
table(d$mhealth, useNA = "always") # verify that now there are ZERO participants in the additional level
##
## anxiety disorder depression
## 95 24
## eating disorders none or NA
## 18 0
## obsessive compulsive disorder other
## 17 23
## ptsd <NA>
## 12 0
d$mhealth <- droplevels(d$mhealth) # use droplevels() to drop the empty factor
table(d$mhealth, useNA = "always") # verify that now the entire factor level is removed
##
## anxiety disorder depression
## 95 24
## eating disorders obsessive compulsive disorder
## 18 17
## other ptsd
## 23 12
## <NA>
## 0
d <- subset(d, mhealth != "obsessive compulsive disorder") # use subset() to remove all participants from the additional level
table(d$mhealth, useNA = "always") # verify that now there are ZERO participants in the additional level
##
## anxiety disorder depression
## 95 24
## eating disorders obsessive compulsive disorder
## 18 0
## other ptsd
## 23 12
## <NA>
## 0
d$mhealth <- droplevels(d$mhealth) # use droplevels() to drop the empty factor
table(d$mhealth, useNA = "always") # verify that now the entire factor level is removed
##
## anxiety disorder depression eating disorders other
## 95 24 18 23
## ptsd <NA>
## 12 0
d <- subset(d, mhealth != "other") # use subset() to remove all participants from the additional level
table(d$mhealth, useNA = "always") # verify that now there are ZERO participants in the additional level
##
## anxiety disorder depression eating disorders other
## 95 24 18 0
## ptsd <NA>
## 12 0
d$mhealth <- droplevels(d$mhealth) # use droplevels() to drop the empty factor
table(d$mhealth, useNA = "always") # verify that now the entire factor level is removed
##
## anxiety disorder depression eating disorders ptsd
## 95 24 18 12
## <NA>
## 0
d <- subset(d, mhealth != "ptsd") # use subset() to remove all participants from the additional level
table(d$mhealth, useNA = "always") # verify that now there are ZERO participants in the additional level
##
## anxiety disorder depression eating disorders ptsd
## 95 24 18 0
## <NA>
## 0
d$mhealth <- droplevels(d$mhealth) # use droplevels() to drop the empty factor
table(d$mhealth, useNA = "always") # verify that now the entire factor level is removed
##
## anxiety disorder depression eating disorders <NA>
## 95 24 18 0
# we'll use the describeBy() command to view our DV's skew and kurtosis across our IVs' levels
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 95 3.08 0.87 3.11 3.07 0.93 1.11 4.78 3.67 -0.02 -0.82 0.09
## ------------------------------------------------------------
## group: depression
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 24 2.39 0.84 2.22 2.31 0.71 1.15 4.33 3.19 0.77 -0.21 0.17
## ------------------------------------------------------------
## group: eating disorders
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 18 3.34 0.77 3.56 3.37 0.77 2 4.22 2.22 -0.5 -1.29 0.18
# and cross_cases() to examine your categorical variables' cell count
cross_cases(d, mhealth)
#Total | |
---|---|
mhealth | |
anxiety disorder | 95 |
depression | 24 |
eating disorders | 18 |
#Total cases | 137 |
# REMEMBER your test's level of POWER is determined by your SMALLEST subsample
# One-Way
table(d$mhealth)
##
## anxiety disorder depression eating disorders
## 95 24 18
# 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(iou~mhealth, data = d)
## Levene's Test for Homogeneity of Variance (center = median)
## Df F value Pr(>F)
## group 2 0.3307 0.719
## 134
# 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.
reg_model <- lm(iou~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 between the diagnosis types. A small size for one of the levels of our variable limits our power and increases our Type II error rate.
Levene’s test was not significant for our three-level mental health type variable with the One-Way ANOVA. We are going to proceed as normal.
We did not need to remove any outliers.
# One-Way
aov_model <- aov_ez(data = d,
id = "X",
between = c("mhealth"),
dv = "iou",
anova_table = list(es = "pes"))
## Contrasts set to contr.sum for the following variables: mhealth
nice(aov_model)
## Anova Table (Type 3 tests)
##
## Response: iou
## Effect df MSE F pes p.value
## 1 mhealth 2, 134 0.73 7.94 *** .106 <.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 3.08 0.0878 134 2.86 3.29
## depression 2.39 0.1750 134 1.96 2.81
## eating disorders 3.34 0.2020 134 2.85 3.83
##
## Confidence level used: 0.95
## Conf-level adjustment: sidak method for 3 estimates
pairs(emmeans(aov_model, specs="mhealth", adjust="sidak"))
## contrast estimate SE df t.ratio p.value
## anxiety disorder - depression 0.690 0.196 134 3.528 0.0017
## anxiety disorder - eating disorders -0.262 0.220 134 -1.192 0.4599
## depression - eating disorders -0.952 0.267 134 -3.568 0.0014
##
## P value adjustment: tukey method for comparing a family of 3 estimates
To test our hypothesis that there will be a significant difference in people’s level of intolerance of uncertainty based on the type of mental health disorder (anxiety disorder, depression, eating disorders), we used a one-way ANOVA. Our data was unbalanced, with many more people who have an anxiety disorder participating in our survey (n = 95) than who have depression (n = 24) or eating disorders (n = 18). This significantly reduces the power of our test and increases the chances of a Type II error. We did not identify an outlier when following visual analysis of Cook’s Distance and Residuals VS Leverage plots. A non-significant Levene’s test (p = 0.719) also indicates that our data does not violate the assumption of homogeneity of variance. This does not suggest that there is an increased chance of Type II error.
We found a significant effect of mental health disorder type, F(2, 134) = 7.94, p < 0.001, ηp2 = .106 (medium effect size; Cohen, 1988). Posthoc tests using Sidak’s adjustment revealed that participants who have an anxiety disorder (M = 3.08, SE = 0.0878) reported more intolerance of uncertainty than those who have depression (M = 2.39, SE = 0.175) but less intolerance of uncertainty than those who have eating disorders (M = 3.34, SE = 0.202); participants who have eating disorders reported the highest amount of intolerance of uncertainty overall (see Figure 1 for a comparison).
References
Cohen J. (1988). Statistical Power Analysis for the Behavioral Sciences. New York, NY: Routledge Academic.