1 Loading Libraries

#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 aggregate all non-grouping columns: take_all(mtcars, mean, by = am)
## 
## 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'

2 Importing Data

# 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)

3 State Your Hypothesis

Note: For your HW, you will choose to run EITHER a one-way ANOVA (a single IV with at least 3 levels) OR a two-way ANOVA (two IVs, each with 2 levels). You will need to specify your hypothesis and customize your code based on the choice you make (i.e., delete code that is not relevant). We will run BOTH versions in the lab for illustrative purposes.

One-Way Hypothesis: We predict that there will be a significant difference in social support based on people’s clinical diagnosis of a mental health disorder (anxiety, depression, no mental health disorder).

IV = mental health disorder DV = social support

4 Check Your Variables

# 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':    958 obs. of  8 variables:
##  $ X        : int  321 401 520 1390 1422 1849 2247 2526 2609 2689 ...
##  $ 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" ...
##  $ pas_covid: num  2.33 4 3 2.89 2.67 ...
##  $ phq      : num  1.89 2.44 1.56 1.22 4 ...
##  $ gad      : num  1 2.14 1.14 1 1.57 ...
##  $ support  : num  2.5 3.83 2.83 2.83 2.33 ...
##  $ 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)

# We're going to recode our race variable into two groups for the Two-Way ANOVA: poc and white
# in doing so, we are creating a new variable "poc" that has 2 levels

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 
##                            97                             0 
##                    depression              eating disorders 
##                            24                            18 
##                    none or NA obsessive compulsive disorder 
##                           759                            17 
##                         other                          ptsd 
##                            23                            16 
##                          <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 
##                            97                            24 
##              eating disorders                    none or NA 
##                            18                           759 
## obsessive compulsive disorder                         other 
##                            17                            23 
##                          ptsd                          <NA> 
##                            16                             0
d <- subset(d, mhealth != "eating disorders") # 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 
##                            97                            24 
##              eating disorders                    none or NA 
##                             0                           759 
## obsessive compulsive disorder                         other 
##                            17                            23 
##                          ptsd                          <NA> 
##                            16                             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 
##                            97                            24 
##                    none or NA obsessive compulsive disorder 
##                           759                            17 
##                         other                          ptsd 
##                            23                            16 
##                          <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 
##                            97                            24 
##                    none or NA obsessive compulsive disorder 
##                           759                             0 
##                         other                          ptsd 
##                            23                            16 
##                          <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       none or NA            other 
##               97               24              759               23 
##             ptsd             <NA> 
##               16                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       none or NA            other 
##               97               24              759                0 
##             ptsd             <NA> 
##               16                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       none or NA             ptsd 
##               97               24              759               16 
##             <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       none or NA             ptsd 
##               97               24              759                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       none or NA             <NA> 
##               97               24              759                0
# For your HW, you can choose to combine levels like we did here, OR you can simply choose which existing levels you want to compare/test -- to do this option, you'll need to copy/paste the "drop levels" code from the t-test lab/HW and delete the recoding code line above.


# check that all our categorical variables of interest are now factors
str(d)
## 'data.frame':    880 obs. of  8 variables:
##  $ X        : int  321 520 1390 1422 1849 2247 2526 2609 2752 2814 ...
##  $ age      : chr  "1 under 18" "1 under 18" "5 over 45" "1 under 18" ...
##  $ mhealth  : Factor w/ 3 levels "anxiety disorder",..: 3 3 3 3 1 3 3 3 3 3 ...
##  $ pas_covid: num  2.33 3 2.89 2.67 2.56 ...
##  $ phq      : num  1.89 1.56 1.22 4 3.67 ...
##  $ gad      : num  1 1.14 1 1.57 2.86 ...
##  $ support  : num  2.5 2.83 2.83 2.33 1.5 ...
##  $ row_id   : Factor w/ 958 levels "1","2","3","4",..: 1 3 4 5 6 7 8 9 11 12 ...
# check our DV skew and kurtosis
describe(d$support)
##    vars   n mean   sd median trimmed  mad min max range  skew kurtosis   se
## X1    1 880 3.63 0.94   3.83    3.69 0.99   1   5     4 -0.49     -0.5 0.03
# we'll use the describeBy() command to view our DV's skew and kurtosis across our IVs' levels
describeBy(d$support, 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 97 3.18 1.02   3.33     3.2 1.24 1.17   5  3.83 -0.15    -0.92 0.1
## ------------------------------------------------------------ 
## group: depression
##    vars  n mean   sd median trimmed  mad min max range  skew kurtosis   se
## X1    1 24 3.16 1.05   3.58    3.19 0.99 1.5 4.5     3 -0.34    -1.53 0.21
## ------------------------------------------------------------ 
## group: none or NA
##    vars   n mean   sd median trimmed  mad min max range skew kurtosis   se
## X1    1 759  3.7 0.91   3.83    3.76 0.99   1   5     4 -0.5    -0.44 0.03
# also use histograms to examine your continuous variable
hist(d$support)

# REMEMBER your test's level of POWER is determined by your SMALLEST subsample

5 Check Your Assumptions

5.1 ANOVA Assumptions

  • DV should be normally distributed across levels of the IV (we checked previously using “describeBy” function)
  • All levels of the IVs should have an equal number of cases and there should be no empty cells. Cells with low numbers decreases the power of the test (which increases chance of Type II error)
  • Homogeneity of variance should be assured (using Levene’s Test)
  • Outliers should be identified and removed – we will actually remove them this time!
  • If you have confirmed everything above, the sampling distribution should be normal.

5.1.1 Check levels of IVs

# One-Way
table(d$mhealth)
## 
## anxiety disorder       depression       none or NA 
##               97               24              759
# REMEMBER your test's level of POWER is determined by your SMALLEST subsample

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

# One-Way
leveneTest(support~mhealth, data = d)
## Levene's Test for Homogeneity of Variance (center = median)
##        Df F value  Pr(>F)  
## group   2  2.4645 0.08564 .
##       877                  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

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

5.1.3.1 Run a Regression to get these outlier plots

# 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(support~mhealth, data = d) #for One-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.2 Issues with My Data

Our cell sizes are very unbalanced between the mental health group levels. 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 was not significant for our three-level mental health variable with the One-Way ANOVA.)

(There were no identified outliers for the One-Way ANOVA.)

6 Run an ANOVA

# One-Way
aov_model <- aov_ez(data = d,
                    id = "X",
                    between = c("mhealth"),
                    dv = "support",
                    anova_table = list(es = "pes"))
## Contrasts set to contr.sum for the following variables: mhealth

7 View Output

nice(aov_model)
## Anova Table (Type 3 tests)
## 
## Response: support
##    Effect     df  MSE         F  pes p.value
## 1 mhealth 2, 877 0.86 16.94 *** .037   <.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

8 Visualize Results

# One-Way
afex_plot(aov_model, x = "mhealth")

9 Run Posthoc Tests (One-Way)

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.18 0.0940 877     2.95     3.40
##  depression         3.16 0.1890 877     2.71     3.61
##  none or NA         3.70 0.0336 877     3.62     3.78
## 
## 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.019 0.2110 877   0.090  0.9956
##  anxiety disorder - none or NA   -0.524 0.0998 877  -5.246  <.0001
##  depression - none or NA         -0.543 0.1920 877  -2.827  0.0133
## 
## P value adjustment: tukey method for comparing a family of 3 estimates

10 Write Up Results

10.1 One-Way ANOVA

To test our hypothesis that there will be a significant difference in social support based on people’s diagnosis of a mental health disorder (anxiety, depression, no mental health disorder), we used a one-way ANOVA. Our data was unbalanced, with many more people who are not clinically diagnosed with a mental health disorder participating in our survey (n = 759) than who are clinically diagnosed with anxiety (n = 97) or depression (n = 24). This significantly reduces the power of our test and increases the chances of a Type II error. There were no outliers identified following visual analysis of Cook’s Distance and Residuals VS Leverage plots. Levene’s test was not significant for our three-level mental health variable with this One-Way ANOVA. We continued with our analysis for the purpose of this class.

We found a significant effect of mental health disorder, F(2, 877) = 16.94, p < .001, ηp2 = .037 (small effect size; Cohen, 1988). Posthoc tests using Sidak’s adjustment revealed that participants who were not clinically diagnosed with a mental health disorder (M = 3.70, SE = 0.03) reported more social support than those who are clinically diagnosed with anxiety (M = 3.18, SE = 0.09) or depression (M = 3.16, SE = 0.19). Contrary to our expectations, people who are clinically diagnosed with anxiety and people who are clinically diagnosed with depression reported similar levels of social support (p = 0.996) (see Figure 1 for a comparison).

References

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