1 Loading Libraries

library(psych) # for the describe() command
## Warning: package 'psych' was built under R version 4.2.3
library(ggplot2) # to visualize our results
## Warning: package 'ggplot2' was built under R version 4.2.3
## 
## Attaching package: 'ggplot2'
## The following objects are masked from 'package:psych':
## 
##     %+%, alpha
library(expss) # for the cross_cases() command
## Warning: package 'expss' was built under R version 4.2.3
## Loading required package: maditr
## Warning: package 'maditr' was built under R version 4.2.3
## 
## To drop variable use NULL: let(mtcars, am = NULL) %>% head()
## 
## Attaching package: 'expss'
## The following object is masked from 'package:ggplot2':
## 
##     vars
library(car) # for the leveneTest() command
## Warning: package 'car' was built under R version 4.2.3
## Loading required package: carData
## Warning: package 'carData' was built under R version 4.2.3
## 
## 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
## Warning: package 'afex' was built under R version 4.2.3
## Loading required package: lme4
## Warning: package 'lme4' was built under R version 4.2.3
## Loading required package: Matrix
## Warning in check_dep_version(): ABI version mismatch: 
## lme4 was built with Matrix ABI version 1
## Current Matrix ABI version is 0
## Please re-install lme4 from source or restore original 'Matrix' package
## 
## 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
d <- read.csv(file="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
d$row_id <- 1:nrow(d)

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 on anxiety, as measured by the pandemic anxiety scale (pas_covid).

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(d)
## 'data.frame':    1207 obs. of  7 variables:
##  $ gender   : chr  "female" "male" "female" "female" ...
##  $ mhealth  : chr  "none or NA" "anxiety disorder" "none or NA" "none or NA" ...
##  $ big5_neu : num  6 6.67 4 4 2.67 ...
##  $ mfq_26   : num  4.2 3.35 4.65 4.65 4.5 4.3 5.25 4.45 5 4.7 ...
##  $ mfq_state: num  3.62 3 5.88 4 4.62 ...
##  $ pas_covid: num  3.22 4.56 3.33 4.22 3.22 ...
##  $ row_id   : int  1 2 3 4 5 6 7 8 9 10 ...
# make our categorical variables factors
d$gender <- as.factor(d$gender)
d$mhealth <- as.factor(d$mhealth)
d$row_id <- as.factor(d$row_id)
#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 race/ethnicity variable into two groups: poc and white
table(d$mhealth)
## 
##              anxiety disorder                       bipolar 
##                           122                             5 
##                    depression              eating disorders 
##                            29                            26 
##                    none or NA obsessive compulsive disorder 
##                           938                            26 
##                         other                          ptsd 
##                            37                            24
d$diagnosed[d$mhealth == "anxiety disorder"] <- "diagnosed"
d$diagnosed[d$mhealth == "bipolar"] <- "diagnosed"
d$diagnosed[d$mhealth == "depression"] <- "diagnosed"
d$diagnosed[d$mhealth == "eating disorder"] <- "diagnosed"
d$diagnosed[d$mhealth == "obsessive compulsive disorder"] <- "diagnosed"
d$diagnosed[d$mhealth == "ptsd"] <- "diagnosed"
d$diagnosed[d$mhealth == "other"] <- NA
d$diagnosed[d$mhealth == "none or NA"] <- "none"
table(d$diagnosed)
## 
## diagnosed      none 
##       206       938
d$diagnosed <- as.factor(d$diagnosed)

# you can use the describe() command on an entire dataframe (d) or just on a single variable
describe(d$pas_covid)
##    vars    n mean   sd median trimmed  mad min max range  skew kurtosis   se
## X1    1 1207 3.23 0.68   3.22    3.24 0.66   1   5     4 -0.18    -0.02 0.02
# we'll use the describeBy() command to view skew and kurtosis across our IVs
describeBy(d$pas_covid, group = d$diagnosed)
## 
##  Descriptive statistics by group 
## group: diagnosed
##    vars   n mean   sd median trimmed  mad  min max range  skew kurtosis   se
## X1    1 206 3.48 0.71   3.56     3.5 0.66 1.22   5  3.78 -0.29    -0.08 0.05
## ------------------------------------------------------------ 
## group: none
##    vars   n mean   sd median trimmed  mad min  max range  skew kurtosis   se
## X1    1 938 3.17 0.66   3.22    3.18 0.66   1 4.89  3.89 -0.23    -0.05 0.02
# also use histograms to examine your continuous variable
hist(d$pas_covid)

# and cross_cases() to examine your categorical variables
cross_cases(d, gender, diagnosed)
 diagnosed 
 diagnosed   none 
 gender 
   female  165 753
   I use another term  10 14
   male  29 155
   Prefer not to say  2 16
   #Total cases  206 938

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(d$gender)
## 
##             female I use another term               male  Prefer not to say 
##                969                 29                188                 21
cross_cases(d, gender, diagnosed)
 diagnosed 
 diagnosed   none 
 gender 
   female  165 753
   I use another term  10 14
   male  29 155
   Prefer not to say  2 16
   #Total cases  206 938
# 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

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(pas_covid~gender, data = d)
## Levene's Test for Homogeneity of Variance (center = median)
##         Df F value  Pr(>F)  
## group    3  2.5508 0.05428 .
##       1203                  
## ---
## 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

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

# to drop multiple outliers, remove the # at the beginning of the line and use this code:
d <- subset(d, row_id!=c(907) & row_id!=c(233))

# 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(pas_covid ~ gender, 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. 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 = d,
                    id = "row_id",
                    between = c("gender"),
                    dv = "pas_covid",
                    anova_table = list(es = "pes"))
## Contrasts set to contr.sum for the following variables: gender

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: pas_covid
##   Effect      df  MSE         F  pes p.value
## 1 gender 3, 1201 0.45 12.08 *** .029   <.001
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '+' 0.1 ' ' 1

8 Visualize Results

afex_plot(aov_model, x = "gender")

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="gender", adjust="tukey")
## Note: adjust = "tukey" was changed to "sidak"
## because "tukey" is only appropriate for one set of pairwise comparisons
##  gender             emmean     SE   df lower.CL upper.CL
##  female               3.27 0.0216 1201     3.21     3.32
##  I use another term   3.62 0.1269 1201     3.30     3.93
##  male                 2.99 0.0490 1201     2.87     3.11
##  Prefer not to say    3.29 0.1501 1201     2.92     3.67
## 
## Confidence level used: 0.95 
## Conf-level adjustment: sidak method for 4 estimates
pairs(emmeans(aov_model, specs="gender", adjust="tukey"))
##  contrast                               estimate     SE   df t.ratio p.value
##  female - I use another term             -0.3473 0.1287 1201  -2.699  0.0355
##  female - male                            0.2766 0.0535 1201   5.170  <.0001
##  female - Prefer not to say              -0.0267 0.1517 1201  -0.176  0.9981
##  I use another term - male                0.6239 0.1360 1201   4.588  <.0001
##  I use another term - Prefer not to say   0.3206 0.1966 1201   1.631  0.3614
##  male - Prefer not to say                -0.3033 0.1579 1201  -1.921  0.2198
## 
## P value adjustment: tukey method for comparing a family of 4 estimates

10 Write Up Results

10.1 One-Way ANOVA

To test our hypothesis that there would be a significant effect of mental health on anxiety, as measured by the pandemic anxiety scale, we used a one-way ANOVA. Our data was unbalanced, with many more women participating in our survey (n = 968) than men (n = 188) those who prefer to use another term (n = 29) and those who prefer not to say (n = 21). This significantly reduces the power of our test and increases the chances of a Type II error. We also identified and removed multiple outliers following visual analysis of a Residuals vs Leverage plot. A significant Levene’s test (p = .05) 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 gender, F(12.08) = .029, p < .001, ηp2 = .042 (large effect size; Cohen, 1988). Posthoc tests using Sidak’s HSD revealed that women reported more anxiety than men but less anxiety than those who prefer not to say participants, while those who use another term reported the highest amount of stress overall (see Figure 1 for a comparison).

References

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