#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 aggregate several columns with one summary: take(mtcars, mpg, hp, fun = 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'
d <- read.csv(file="Data/projectdata.csv", header=T)
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 neuroticism by people’s mental health disorders, between individuals with depression, individuals with anxiety disorders, and individuals with eating disorders
# you only need to check the variables you're using in the current analysis
str(d)
## 'data.frame': 380 obs. of 8 variables:
## $ X : int 6350 6608 6612 6715 6726 6778 6859 6860 6929 6980 ...
## $ relationship_status: chr "Single, never married" "Single, never married" "Single, never married" "Single, never married" ...
## $ mhealth : chr "none or NA" "obsessive compulsive disorder" "none or NA" "none or NA" ...
## $ big5_neu : num 6 3.33 4.67 3 5 ...
## $ edeq12 : num 1.83 2.75 1.25 1.92 1.67 ...
## $ brs : num 2.17 3.17 3.17 3 2 ...
## $ isolation_c : num 2.5 3.25 2.75 3 3.5 3.25 1.75 2 3 3.5 ...
## $ 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': 380 obs. of 8 variables:
## $ X : int 6350 6608 6612 6715 6726 6778 6859 6860 6929 6980 ...
## $ relationship_status: chr "Single, never married" "Single, never married" "Single, never married" "Single, never married" ...
## $ mhealth : Factor w/ 8 levels "anxiety disorder",..: 5 6 5 5 1 1 5 6 5 5 ...
## $ big5_neu : num 6 3.33 4.67 3 5 ...
## $ edeq12 : num 1.83 2.75 1.25 1.92 1.67 ...
## $ brs : num 2.17 3.17 3.17 3 2 ...
## $ isolation_c : num 2.5 3.25 2.75 3 3.5 3.25 1.75 2 3 3.5 ...
## $ row_id : Factor w/ 380 levels "1","2","3","4",..: 1 2 3 4 5 6 7 8 9 10 ...
d <- subset(d, mhealth != "bipolar")
table(d$mhealth, useNA = "always")
##
## anxiety disorder bipolar
## 52 0
## depression eating disorders
## 9 19
## none or NA obsessive compulsive disorder
## 251 15
## other ptsd
## 19 12
## <NA>
## 0
d$mhealth <- droplevels(d$mhealth)
table(d$mhealth, useNA = "always")
##
## anxiety disorder depression
## 52 9
## eating disorders none or NA
## 19 251
## obsessive compulsive disorder other
## 15 19
## ptsd <NA>
## 12 0
d <- subset(d, mhealth != "ptsd")
table(d$mhealth, useNA = "always")
##
## anxiety disorder depression
## 52 9
## eating disorders none or NA
## 19 251
## obsessive compulsive disorder other
## 15 19
## ptsd <NA>
## 0 0
d$mhealth <- droplevels(d$mhealth)
table(d$mhealth, useNA = "always")
##
## anxiety disorder depression
## 52 9
## eating disorders none or NA
## 19 251
## obsessive compulsive disorder other
## 15 19
## <NA>
## 0
d <- subset(d, mhealth != "obsessive compulsive disorder")
table(d$mhealth, useNA = "always")
##
## anxiety disorder depression
## 52 9
## eating disorders none or NA
## 19 251
## obsessive compulsive disorder other
## 0 19
## <NA>
## 0
d$mhealth <- droplevels(d$mhealth)
table(d$mhealth, useNA = "always")
##
## anxiety disorder depression eating disorders none or NA
## 52 9 19 251
## other <NA>
## 19 0
d <- subset(d, mhealth != "other")
table(d$mhealth, useNA = "always")
##
## anxiety disorder depression eating disorders none or NA
## 52 9 19 251
## other <NA>
## 0 0
d$mhealth <- droplevels(d$mhealth)
table(d$mhealth, useNA = "always")
##
## anxiety disorder depression eating disorders none or NA
## 52 9 19 251
## <NA>
## 0
d <- subset(d, mhealth != "none or NA")
table(d$mhealth, useNA = "always")
##
## anxiety disorder depression eating disorders none or NA
## 52 9 19 0
## <NA>
## 0
d$mhealth <- droplevels(d$mhealth)
table(d$mhealth, useNA = "always")
##
## anxiety disorder depression eating disorders <NA>
## 52 9 19 0
# check our DV skew and kurtosis
describe(d$big5_neu)
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 80 5.31 1.19 5.33 5.45 0.99 1.67 7 5.33 -1 0.96 0.13
# we'll use the describeBy() command to view our DV's skew and kurtosis across our IVs' levels
describeBy(d$big5_neu, 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 52 5.44 1.09 5.33 5.55 0.99 1.67 7 5.33 -1 1.54 0.15
## ------------------------------------------------------------
## group: depression
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 9 4.89 1.54 4.67 4.89 2.47 2.67 6.67 4 -0.24 -1.54 0.51
## ------------------------------------------------------------
## group: eating disorders
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 19 5.18 1.29 5.33 5.27 0.49 1.67 7 5.33 -1.12 0.77 0.3
# also use histograms to examine your continuous variable
hist(d$big5_neu)
# REMEMBER your test's level of POWER is determined by your SMALLEST subsample
# One-Way
table(d$mhealth)
##
## anxiety disorder depression eating disorders
## 52 9 19
# 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(big5_neu~mhealth, data = d)
## Levene's Test for Homogeneity of Variance (center = median)
## Df F value Pr(>F)
## group 2 0.7955 0.455
## 77
# 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(big5_neu~mhealth, data = d)
# Cook's distance
plot(reg_model, 4)
# Residuals VS Leverage
plot(reg_model, 5)
# Issues with My Data
Our cell sizes are unbalanced between the mental health disorder type group levels. A small sample size for 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 disorder type variable with the One-Way ANOVA and we will be continuing with the analysis.
We have not identified or removed any outliers for the One-Way ANOVA.
# Run an ANOVA
``` r
# One-Way
aov_model <- aov_ez(data = d,
id = "X",
between = c("mhealth"),
dv = "big5_neu",
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: big5_neu
## Effect df MSE F pes p.value
## 1 mhealth 2, 77 1.43 0.97 .025 .384
## ---
## 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")
To test our hypothesis that there will be a significant difference in neuroticism by people’s mental health disorders (depression, anxiety disorders, and eating disorders), we used a one-way ANOVA. Our data was unbalanced, with many more people who have anxiety disorders participating in our survey (n = 52) than who have eating disorders (n = 19) or depression (n = 9). This significantly reduces the power of our test and increases the chances of a Type II error. We have not identified or removed any outliers following visual analysis of Cook’s Distance and Residuals VS Leverage plots. A non-significant Levene’s test (p > .05) indicates that our data does not violate the assumption of homogeneity of variance.
Contrary to our prediction, we did not find a significant effect of type of mental health disorder on neuroticism, F(2, 77) = 0.97, p = .38, ns. (see Figure 1 for a comparison).
References
Cohen J. (1988). Statistical Power Analysis for the Behavioral Sciences. New York, NY: Routledge Academic.