#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 get total summary skip 'by' argument: take_all(mtcars, mean)
##
## Use 'expss_output_viewer()' to display tables in the RStudio Viewer.
## 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 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: There will be a significant difference in perceived loneliness by people’s type of mental health disorder, between anxiety, depression, and bipolar
IV = Mental health disorder DV = Perceived loneliness
# 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': 668 obs. of 8 variables:
## $ X : int 321 520 1422 1849 2247 2526 2609 2814 3053 3108 ...
## $ age : chr "1 under 18" "1 under 18" "1 under 18" "1 under 18" ...
## $ mhealth : chr "none or NA" "none or NA" "none or NA" "anxiety disorder" ...
## $ big5_neu : num 3.67 5.33 3.67 6 3.33 ...
## $ pas_covid : num 2.33 3 2.67 2.56 3 ...
## $ isolation_c: num 1.25 1 3.5 3.5 2 2.5 1.75 1 2 1.25 ...
## $ mfq_26 : num 4.3 2.7 2.95 2.4 4.7 4.35 4.25 4.55 4.95 4.95 ...
## $ 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
f_filtered <- d[d$mhealth %in% c("anxiety disorder", "depression", "bipolar"), ]
# 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': 668 obs. of 8 variables:
## $ X : int 321 520 1422 1849 2247 2526 2609 2814 3053 3108 ...
## $ age : chr "1 under 18" "1 under 18" "1 under 18" "1 under 18" ...
## $ mhealth : Factor w/ 8 levels "anxiety disorder",..: 5 5 5 1 5 5 5 5 5 5 ...
## $ big5_neu : num 3.67 5.33 3.67 6 3.33 ...
## $ pas_covid : num 2.33 3 2.67 2.56 3 ...
## $ isolation_c: num 1.25 1 3.5 3.5 2 2.5 1.75 1 2 1.25 ...
## $ mfq_26 : num 4.3 2.7 2.95 2.4 4.7 4.35 4.25 4.55 4.95 4.95 ...
## $ row_id : Factor w/ 668 levels "1","2","3","4",..: 1 2 3 4 5 6 7 8 9 10 ...
# check our DV skew and kurtosis
describe(d$isolation_c)
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 668 2.29 0.81 2.25 2.3 1.11 1 3.5 2.5 -0.02 -1.24 0.03
# we'll use the describeBy() command to view our DV's skew and kurtosis across our IVs' levels
describeBy(d$isolation_c, 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 76 2.67 0.77 2.88 2.74 0.93 1 3.5 2.5 -0.59 -0.92 0.09
## ------------------------------------------------------------
## group: bipolar
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 3 2.83 0.52 3 2.83 0.37 2.25 3.25 1 -0.29 -2.33 0.3
## ------------------------------------------------------------
## group: depression
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 10 2.78 0.78 2.88 2.88 0.93 1.25 3.5 2.25 -0.56 -1.09 0.25
## ------------------------------------------------------------
## group: eating disorders
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 17 2.59 0.73 2.75 2.62 0.74 1.25 3.5 2.25 -0.34 -1.35 0.18
## ------------------------------------------------------------
## group: none or NA
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 517 2.17 0.8 2 2.15 1.11 1 3.5 2.5 0.16 -1.18 0.04
## ------------------------------------------------------------
## group: obsessive compulsive disorder
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 15 2.62 0.6 2.5 2.63 0.74 1.5 3.5 2 0 -1.26 0.16
## ------------------------------------------------------------
## group: other
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 18 2.94 0.59 3.25 2.98 0.37 1.75 3.5 1.75 -0.61 -1.19 0.14
## ------------------------------------------------------------
## group: ptsd
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 12 2.77 0.74 2.88 2.88 0.93 1 3.5 2.5 -0.93 0.03 0.21
# also use histograms to examine your continuous variable
hist(d$isolation_c)
# and cross_cases() to examine your categorical variables' cell count
cross_cases(d, mhealth)
| #Total | |
|---|---|
| mhealth | |
| anxiety disorder | 76 |
| bipolar | 3 |
| depression | 10 |
| eating disorders | 17 |
| none or NA | 517 |
| obsessive compulsive disorder | 15 |
| other | 18 |
| ptsd | 12 |
| #Total cases | 668 |
# REMEMBER your test's level of POWER is determined by your SMALLEST subsample
# One-Way
table(d$mhealth)
##
## anxiety disorder bipolar
## 76 3
## depression eating disorders
## 10 17
## none or NA obsessive compulsive disorder
## 517 15
## other ptsd
## 18 12
# 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(isolation_c~mhealth, data = d)
## Levene's Test for Homogeneity of Variance (center = median)
## Df F value Pr(>F)
## group 7 1.471 0.1745
## 660
# 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(isolation_c~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 mental health group levels. A small sample size for one of the levels (bipolar) 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. We are ignoring this and continuing with the analysis anyway for class purposes.
# One-Way
aov_model_filtered <- aov_ez(data = f_filtered ,
id = "X",
between = c("mhealth"),
dv = "isolation_c",
anova_table = list(es = "pes"))
## Contrasts set to contr.sum for the following variables: mhealth
nice(aov_model_filtered)
## Anova Table (Type 3 tests)
##
## Response: isolation_c
## Effect df MSE F pes p.value
## 1 mhealth 2, 86 0.59 0.14 .003 .872
## ---
## 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_filtered, x = "mhealth")
# NOTE: for the Two-Way, we will decide which plot version makes the MOST SENSE based on the data / rationale when we make the nice Figure 2 at the end
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_filtered, specs="mhealth", adjust="sidak")
## mhealth emmean SE df lower.CL upper.CL
## anxiety disorder 2.67 0.0879 86 2.46 2.89
## bipolar 2.83 0.4420 86 1.76 3.91
## depression 2.77 0.2420 86 2.18 3.37
##
## Confidence level used: 0.95
## Conf-level adjustment: sidak method for 3 estimates
pairs(emmeans(aov_model_filtered, specs="mhealth", adjust="sidak"))
## contrast estimate SE df t.ratio p.value
## anxiety disorder - bipolar -0.1623 0.451 86 -0.360 0.9312
## anxiety disorder - depression -0.1039 0.258 86 -0.403 0.9144
## bipolar - depression 0.0583 0.504 86 0.116 0.9927
##
## P value adjustment: tukey method for comparing a family of 3 estimates
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.
```
To test our hypothesis that there will be a significant difference in people’s level of perceived loneliness based on the mental health disorder (anxiety, depression, bipolar), we used a one-way ANOVA. Our data was unbalanced, with many more people who are diagnosed with anxiety disorder (n = 76) than who have depression disorder (10) or bipolar disorder (n = 3). This significantly reduces the power of our test and increases the chances of a Type II error. We did not identify or remove a single outlier following visual analysis of Cook’s Distance and Residuals VS Leverage plots. A non-significant Levene’s test (p = 0.17) 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 disorder, F(7,660) = 8.22, p < 0.001, ηp2 = .080 (medium effect size; Cohen, 1988). Posthoc tests using Sidak’s adjustment revealed that participants who have anxiety disorder (M* = 2.67, SE = 0.09) reported less loneliness than those who have depression (M = 2.77, SE = 0.25) and less loneliness than those who have bipolar disorder (M = 2.83, SE = 0.45); participants who have bipolar disorder reported the highest amount of loneliness overall (see Figure 1 for a comparison).
References
Cohen J. (1988). Statistical Power Analysis for the Behavioral Sciences. New York, NY: Routledge Academic.