1 Loading Libraries

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'

2 Importing Data

# import the dataset you cleaned previously
# this will be the dataset you'll use throughout the rest of the semester
# use ARC data
Data <- read.csv(file="~/Applications/R Studio P421/Homework/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
Data$row_id <- 1:nrow(Data)

3 State Your Hypothesis

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 disorders on intolerance of uncertainty, as measured by the intolerance of uncertainty scale.

4 Check Your Variables

# 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(Data)
## 'data.frame':    1030 obs. of  7 variables:
##  $ 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 ...
##  $ mfq_state: num  3.62 3 5.88 4 4.62 ...
##  $ edeq12   : num  1.58 1.83 1 1.67 1.42 ...
##  $ mhealth  : chr  "none or NA" "anxiety disorder" "none or NA" "none or NA" ...
##  $ treatment: chr  "no psychological disorders" "in treatment" "not in treatment" "no psychological disorders" ...
##  $ 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
Data$X<-(Data$row_id)
Data$mhealth <- as.factor(Data$mhealth)
Data$treatment <- as.factor(Data$treatment)

#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 mental health disorders variable into two groups: other disorders and ocd
table(Data$mhealth)
## 
##              anxiety disorder                       bipolar 
##                           121                             5 
##                    depression              eating disorders 
##                            29                            29 
##                    none or NA obsessive compulsive disorder 
##                           768                            25 
##                         other                          ptsd 
##                            33                            20
Data$mentalhealth[Data$mhealth == "anxiety disorder"] <- "others"
Data$mentalhealth[Data$mhealth == "bipolar"] <- "others"
Data$mentalhealth[Data$mhealth == "ptsd"] <- "others"
Data$mentalhealth[Data$mhealth == "depression"] <- "others"
Data$mentalhealth[Data$mhealth == "none or NA"] <- "NA"
Data$mentalhealth[Data$mhealth == "eating disorders"] <- "others"
Data$mentalhealth[Data$mhealth == "other"] <- "others"
Data$mentalhealth[Data$mhealth == "obsessive compulsive disorder"] <- "obsessive compulsive disorder"
table(Data$mentalhealth)
## 
##                            NA obsessive compulsive disorder 
##                           768                            25 
##                        others 
##                           237
Data$mentalhealth <- as.factor(Data$mentalhealth)

# you can use the describe() command on an entire dataframe (d) or just on a single variable
describe(Data)
##               vars    n   mean     sd median trimmed    mad min  max  range
## iou              1 1030   2.60   0.91   2.44    2.54   0.99 1.0    5    4.0
## mfq_26           2 1030   4.30   0.67   4.35    4.31   0.67 1.8    6    4.2
## mfq_state        3 1030   4.07   1.01   4.12    4.12   0.93 1.0    6    5.0
## edeq12           4 1030   1.90   0.74   1.75    1.83   0.74 1.0    4    3.0
## mhealth*         5 1030   4.58   1.49   5.00    4.79   0.00 1.0    8    7.0
## treatment*       6 1030   2.70   1.08   3.00    2.57   1.48 1.0    6    5.0
## row_id           7 1030 515.50 297.48 515.50  515.50 381.77 1.0 1030 1029.0
## X                8 1030 515.50 297.48 515.50  515.50 381.77 1.0 1030 1029.0
## mentalhealth*    9 1030   1.48   0.84   1.00    1.36   0.00 1.0    3    2.0
##                skew kurtosis   se
## iou            0.45    -0.66 0.03
## mfq_26        -0.29     0.10 0.02
## mfq_state     -0.51     0.08 0.03
## edeq12         0.66    -0.58 0.02
## mhealth*      -1.28     1.80 0.05
## treatment*     1.35     2.50 0.03
## row_id         0.00    -1.20 9.27
## X              0.00    -1.20 9.27
## mentalhealth*  1.20    -0.51 0.03
describe(Data$iou)
##    vars    n mean   sd median trimmed  mad min max range skew kurtosis   se
## X1    1 1030  2.6 0.91   2.44    2.54 0.99   1   5     4 0.45    -0.66 0.03
# we'll use the describeBy() command to view skew and kurtosis across our IVs
describeBy(Data$iou, group = Data$mhealth)
## 
##  Descriptive statistics by group 
## group: anxiety disorder
##    vars   n mean   sd median trimmed  mad  min  max range skew kurtosis   se
## X1    1 121 3.16 0.86   3.15    3.15 0.93 1.11 4.93  3.81    0    -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 29  2.3 0.87   2.07    2.24 0.77 1.04 4.33   3.3 0.76    -0.25 0.16
## ------------------------------------------------------------ 
## group: eating disorders
##    vars  n mean   sd median trimmed  mad min  max range  skew kurtosis   se
## X1    1 29 3.41 0.67   3.52    3.45 0.77   2 4.22  2.22 -0.65    -0.76 0.12
## ------------------------------------------------------------ 
## group: none or NA
##    vars   n mean   sd median trimmed  mad min max range skew kurtosis   se
## X1    1 768 2.42 0.83    2.3    2.35 0.88   1   5     4 0.61     -0.3 0.03
## ------------------------------------------------------------ 
## group: obsessive compulsive disorder
##    vars  n mean   sd median trimmed  mad  min  max range skew kurtosis   se
## X1    1 25  3.2 0.91   3.07     3.2 1.04 1.59 4.78  3.19 0.06    -1.08 0.18
## ------------------------------------------------------------ 
## group: other
##    vars  n mean   sd median trimmed  mad min max range skew kurtosis   se
## X1    1 33  3.4 1.02   3.41    3.48 1.04 1.3 4.7  3.41 -0.5    -0.76 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
# also use histograms to examine your continuous variable
hist(Data$iou)

# and cross_cases() to examine your categorical variables
cross_cases(Data, Data$mhealth, Data$treatment)
 Data$treatment 
 in treatment   no psychological disorders   not in treatment   other   seeking treatment   treatment disrupted by COVID-19 
 Data$mhealth 
   anxiety disorder  22 2 68 2 9 18
   bipolar  1 2 2
   depression  5 1 16 1 5 1
   eating disorders  11 9 9
   none or NA  10 379 346 7 16 10
   obsessive compulsive disorder  5 1 16 3
   other  17 2 7 1 2 4
   ptsd  4 10 1 2 3
   #Total cases  75 385 474 12 34 50

5 Check Your Assumptions

5.1 ANOVA Assumptions

  • DV should be normally distributed across levels of the IV
  • All levels of the IVs should have equal number of cases and there should be no empty cells. Cells with low numbers decrease the power of the test (increase change of Type II error)
  • Homogeneity of variance should be assured
  • Outliers should be identified and removed
  • If you have confirmed everything about, the sampling distribution should be normal. (For a demonstration of what the sampling distribution is, go here.)

5.1.1 Check levels of IVs

table(Data$mhealth)
## 
##              anxiety disorder                       bipolar 
##                           121                             5 
##                    depression              eating disorders 
##                            29                            29 
##                    none or NA obsessive compulsive disorder 
##                           768                            25 
##                         other                          ptsd 
##                            33                            20
cross_cases(Data, Data$mhealth, Data$treatment)
 Data$treatment 
 in treatment   no psychological disorders   not in treatment   other   seeking treatment   treatment disrupted by COVID-19 
 Data$mhealth 
   anxiety disorder  22 2 68 2 9 18
   bipolar  1 2 2
   depression  5 1 16 1 5 1
   eating disorders  11 9 9
   none or NA  10 379 346 7 16 10
   obsessive compulsive disorder  5 1 16 3
   other  17 2 7 1 2 4
   ptsd  4 10 1 2 3
   #Total cases  75 385 474 12 34 50
# 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(Data, mhealth != "anxiety disorder, bipolar, depression, eating disorders, none or NA, other, ptsd")
d2$mhealth <- droplevels(d2$mhealth)

# to double-check any changes we made
cross_cases(d2, mhealth, treatment)
 treatment 
 in treatment   no psychological disorders   not in treatment   other   seeking treatment   treatment disrupted by COVID-19 
 mhealth 
   anxiety disorder  22 2 68 2 9 18
   bipolar  1 2 2
   depression  5 1 16 1 5 1
   eating disorders  11 9 9
   none or NA  10 379 346 7 16 10
   obsessive compulsive disorder  5 1 16 3
   other  17 2 7 1 2 4
   ptsd  4 10 1 2 3
   #Total cases  75 385 474 12 34 50

5.1.2 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(iou~mhealth, data = Data)
## Levene's Test for Homogeneity of Variance (center = median)
##         Df F value Pr(>F)
## group    7  1.1237 0.3456
##       1022
leveneTest(iou~mhealth*treatment, data = d2)
## Levene's Test for Homogeneity of Variance (center = median)
##        Df F value Pr(>F)
## group  38  1.2528 0.1425
##       991

5.1.3 Check for outliers using Cook’s distance and Residuals vs Leverage plot

5.1.3.1 Run a Regression

# 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:
# Data <- subset(Data, 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))

# 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 = Data) #for one-way
reg_model2 <- lm(iou ~ mhealth*treatment, data = d2) #for two-way

5.1.3.2 Check for outliers (One-Way)

# Cook's distance
plot(reg_model, 4)

# Residuals vs Leverage
plot(reg_model, 5)

5.1.3.3 Check for outliers (Two-Way)

# Cook's distance
plot(reg_model2, 4)

# Residuals vs Leverage
plot(reg_model2, 5)
## Warning: not plotting observations with leverage one:
##   91, 566, 668, 901
## Warning in sqrt(crit * p * (1 - hh)/hh): NaNs produced
## Warning in sqrt(crit * p * (1 - hh)/hh): NaNs produced

5.2 Issues with My Data

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.

6 Run an ANOVA

aov_model <- aov_ez(data = Data,
                    id = "row_id",
                    between = c("mhealth"),
                    dv = "iou",
                    anova_table = list(es = "pes"))
## Contrasts set to contr.sum for the following variables: mhealth

7 View Output

Effect size cutoffs from Cohen (1988):

  • η2 = 0.01 indicates a small effect
  • η2 = 0.06 indicates a medium effect
  • η2 = 0.14 indicates a large effect
nice(aov_model)
## Anova Table (Type 3 tests)
## 
## Response: iou
##    Effect      df  MSE         F  pes p.value
## 1 mhealth 7, 1022 0.71 25.02 *** .146   <.001
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '+' 0.1 ' ' 1

8 Visualize Results

afex_plot(aov_model, x = "mhealth")

9 Run Posthoc Tests (One-Way)

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.16 0.0767 1022     2.95     3.37
##  bipolar                         2.59 0.3773 1022     1.56     3.62
##  depression                      2.30 0.1567 1022     1.87     2.73
##  eating disorders                3.41 0.1567 1022     2.98     3.84
##  none or NA                      2.42 0.0304 1022     2.33     2.50
##  obsessive compulsive disorder   3.20 0.1687 1022     2.74     3.66
##  other                           3.40 0.1468 1022     3.00     3.80
##  ptsd                            3.27 0.1886 1022     2.76     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                        0.56627 0.3850 1022   1.471
##  anxiety disorder - depression                     0.86129 0.1744 1022   4.938
##  anxiety disorder - eating disorders              -0.24855 0.1744 1022  -1.425
##  anxiety disorder - none or NA                     0.74321 0.0825 1022   9.007
##  anxiety disorder - obsessive compulsive disorder -0.04114 0.1853 1022  -0.222
##  anxiety disorder - other                         -0.24069 0.1657 1022  -1.453
##  anxiety disorder - ptsd                          -0.11521 0.2036 1022  -0.566
##  bipolar - depression                              0.29502 0.4085 1022   0.722
##  bipolar - eating disorders                       -0.81481 0.4085 1022  -1.995
##  bipolar - none or NA                              0.17694 0.3785 1022   0.467
##  bipolar - obsessive compulsive disorder          -0.60741 0.4133 1022  -1.470
##  bipolar - other                                  -0.80696 0.4048 1022  -1.993
##  bipolar - ptsd                                   -0.68148 0.4218 1022  -1.616
##  depression - eating disorders                    -1.10983 0.2215 1022  -5.010
##  depression - none or NA                          -0.11808 0.1596 1022  -0.740
##  depression - obsessive compulsive disorder       -0.90243 0.2302 1022  -3.920
##  depression - other                               -1.10198 0.2147 1022  -5.132
##  depression - ptsd                                -0.97650 0.2452 1022  -3.983
##  eating disorders - none or NA                     0.99175 0.1596 1022   6.215
##  eating disorders - obsessive compulsive disorder  0.20741 0.2302 1022   0.901
##  eating disorders - other                          0.00786 0.2147 1022   0.037
##  eating disorders - ptsd                           0.13333 0.2452 1022   0.544
##  none or NA - obsessive compulsive disorder       -0.78435 0.1714 1022  -4.575
##  none or NA - other                               -0.98390 0.1500 1022  -6.561
##  none or NA - ptsd                                -0.85842 0.1911 1022  -4.493
##  obsessive compulsive disorder - other            -0.19955 0.2237 1022  -0.892
##  obsessive compulsive disorder - ptsd             -0.07407 0.2531 1022  -0.293
##  other - ptsd                                      0.12548 0.2391 1022   0.525
##  p.value
##   0.8229
##   <.0001
##   0.8457
##   <.0001
##   1.0000
##   0.8321
##   0.9992
##   0.9963
##   0.4859
##   0.9998
##   0.8235
##   0.4869
##   0.7409
##   <.0001
##   0.9958
##   0.0024
##   <.0001
##   0.0019
##   <.0001
##   0.9860
##   1.0000
##   0.9994
##   0.0001
##   <.0001
##   0.0002
##   0.9868
##   1.0000
##   0.9995
## 
## P value adjustment: tukey method for comparing a family of 8 estimates

10 Write Up Results

10.1 One-Way ANOVA

To test our hypothesis that there would be a significant effect of mental health disorders on intolerance of uncertainty, we used a one-way ANOVA. Our data was unbalanced, with many more participants who had no mental health disorders in our survey (n = 768) than participants with mental health disorders (n = 141). 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 = .002) 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 disorders, F(6,902) = 19.07, p < .001, ηp2 = .11 (large effect size; Cohen, 1988). Posthoc tests using Tukey’s HSD revealed that participants with mental health disorders reported more intolerance to uncertainty than participants who reported none or NA for mental health disorders(see Figure 1 for a comparison).

References

Cohen J. (1988). Statistical Power Analysis for the Behavioral Sciences. New York, NY: Routledge Academic.