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 data: take(mtcars, mean_mpg = mean(mpg), 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 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
# 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/arc_data_final.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)
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 depressive symptoms on partciapnst who live in urban or rural environments, as measured by Percieved Stress Scale (PSS).
# 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': 2073 obs. of 42 variables:
## $ X : int 1 20 30 31 32 33 48 49 57 58 ...
## $ gender : chr "female" "male" "female" "female" ...
## $ trans : chr "no" "no" "no" "no" ...
## $ sexual_orientation : chr "Heterosexual/Straight" "Heterosexual/Straight" "Heterosexual/Straight" "Heterosexual/Straight" ...
## $ ethnicity : chr "White - British, Irish, other" "White - British, Irish, other" "White - British, Irish, other" "White - British, Irish, other" ...
## $ relationship_status : chr "In a relationship/married and cohabiting" "Prefer not to say" "Prefer not to say" "In a relationship/married and cohabiting" ...
## $ age : chr NA "1 under 18" "1 under 18" "4 between 36 and 45" ...
## $ urban_rural : chr "city" "city" "city" "town" ...
## $ income : chr "3 high" NA NA "2 middle" ...
## $ education : chr "6 graduate degree or higher" "prefer not to say" "2 equivalent to high school completion" "5 undergraduate degree" ...
## $ employment : chr "3 employed" "1 high school equivalent" "1 high school equivalent" "3 employed" ...
## $ treatment : chr "no psychological disorders" "in treatment" "not in treatment" "no psychological disorders" ...
## $ health : chr "something else or not applicable" "something else or not applicable" "something else or not applicable" "two conditions" ...
## $ mhealth : chr "none or NA" "anxiety disorder" "none or NA" "none or NA" ...
## $ sleep_hours : chr "3 7-8 hours" "2 5-6 hours" "3 7-8 hours" "2 5-6 hours" ...
## $ exercise : num 0 2 3 1.5 NA 1 NA 2 2 1.7 ...
## $ pet : chr "cat" "cat" "dog" "no pets" ...
## $ covid_pos : int 0 0 0 0 0 0 0 0 0 0 ...
## $ covid_neg : int 0 0 0 0 0 0 0 0 0 0 ...
## $ big5_open : num 5.33 5.33 5 6 NA ...
## $ big5_con : num 6 3.33 5.33 5.67 NA ...
## $ big5_agr : num 4.33 4.33 6.67 4.67 NA ...
## $ big5_neu : num 6 6.67 4 4 NA ...
## $ big5_ext : num 2 1.67 6 5 NA ...
## $ pswq : num 4.94 3.36 1.86 3.94 NA ...
## $ iou : num 3.19 4 1.59 3.37 NA ...
## $ mfq_26 : num 4.2 3.35 4.65 4.65 NA 4.5 NA 4.3 5.25 4.45 ...
## $ mfq_state : num 3.62 3 5.88 4 NA ...
## $ rse : num 2.3 1.6 3.9 1.7 NA 3.9 NA 2.4 1.8 NA ...
## $ school_covid_support: num NA NA NA NA NA NA NA NA NA NA ...
## $ school_att : num NA NA NA NA NA NA NA NA NA NA ...
## $ pas_covid : num 3.22 4.56 3.33 4.22 NA ...
## $ pss : num 3.25 3.75 1 3.25 NA 2 NA 2 4 1.25 ...
## $ phq : num 1.33 3.33 1 2.33 NA ...
## $ gad : num 1.86 3.86 1.14 2 NA ...
## $ edeq12 : num 1.58 1.83 1 1.67 NA ...
## $ brs : num NA NA NA NA NA NA NA NA NA NA ...
## $ swemws : num 2.86 2.29 4.29 3.29 NA ...
## $ isolation_a : num 2.25 NA NA 2.5 NA 1.75 NA 2 1.25 NA ...
## $ isolation_c : num NA 3.5 1 NA NA NA NA NA NA 1 ...
## $ support : num 2.5 2.17 5 2.5 NA ...
## $ row_id : int 1 2 3 4 5 6 7 8 9 10 ...
# make our categorical variables factors
d$X<- as.factor (d$X)
d$urban_rural <- as.factor(d$urban_rural)
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
# you can use the describe() command on an entire dataframe (d) or just on a single variable
describe(d$pss)
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 1356 2.94 0.94 3 2.94 1.11 1 5 4 0.07 -0.73 0.03
# we'll use the describeBy() command to view skew and kurtosis across our IVs
describeBy(d$pss, group = d$urban_rural)
##
## Descriptive statistics by group
## group: city
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 315 3.06 0.95 3 3.07 1.11 1 5 4 -0.08 -0.64 0.05
## ------------------------------------------------------------
## group: isolated dwelling
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 32 2.91 0.9 2.88 2.91 1.11 1.5 4.5 3 -0.14 -1.24 0.16
## ------------------------------------------------------------
## group: town
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 599 2.96 0.95 3 2.95 1.11 1 5 4 0.02 -0.79 0.04
## ------------------------------------------------------------
## group: village
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 393 2.83 0.91 2.75 2.8 1.11 1 5 4 0.26 -0.59 0.05
# also use histograms to examine your continuous variable
hist(d$pss)
# and cross_cases() to examine your categorical variables
cross_cases(d, urban_rural)
|  #Total | |
|---|---|
|  urban_rural | |
|    city | 453 |
|    isolated dwelling | 41 |
|    town | 841 |
|    village | 501 |
|    #Total cases | 1836 |
table(d$urban_rural)
##
## city isolated dwelling town village
## 453 41 841 501
# 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
# to double-check any changes we made
# 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(pss~urban_rural, data = d)
## Levene's Test for Homogeneity of Variance (center = median)
## Df F value Pr(>F)
## group 3 0.7104 0.5458
## 1335
# 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!=(843))
# to drop multiple outliers, remove the # at the beginning of the line and use this code:
#d <- subset(d, row_id!=c(1640) & row_id!=c(843)
# 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(pss ~ urban_rural, data = d ) #for one-way
# Cook's distance
plot(reg_model, 4)
# Residuals vs Leverage
plot(reg_model, 5)
# Cook's distance
# Residuals vs Leverage
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 five-level Age 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 one outlier.
aov_model <- aov_ez(data = d,
id = "X",
between = c("urban_rural"),
dv = "pss",
anova_table = list(es = "pes"))
## Warning: Missing values for 734 ID(s), which were removed before analysis:
## 32, 48, 67, 69, 79, 80, 85, 103, 116, 142, ... [showing first 10 only]
## Below the first few rows (in wide format) of the removed cases with missing data.
## X urban_rural .
## # 5 32 <NA> NA
## # 7 48 town NA
## # 11 67 town NA
## # 13 69 town NA
## # 14 79 city NA
## # 15 80 <NA> NA
## Contrasts set to contr.sum for the following variables: urban_rural
Effect size cutoffs from Cohen (1988):
nice(aov_model)
## Anova Table (Type 3 tests)
##
## Response: pss
## Effect df MSE F pes p.value
## 1 urban_rural 3, 1335 0.88 3.75 * .008 .011
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '+' 0.1 ' ' 1
afex_plot(aov_model, x = "urban_rural")
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="urban_rural", adjust="tukey")
## Note: adjust = "tukey" was changed to "sidak"
## because "tukey" is only appropriate for one set of pairwise comparisons
## urban_rural emmean SE df lower.CL upper.CL
## city 3.06 0.0529 1335 2.93 3.19
## isolated dwelling 2.91 0.1661 1335 2.50 3.33
## town 2.96 0.0384 1335 2.86 3.05
## village 2.83 0.0474 1335 2.71 2.94
##
## Confidence level used: 0.95
## Conf-level adjustment: sidak method for 4 estimates
pairs(emmeans(aov_model, specs="urban_rural", adjust="tukey"))
## contrast estimate SE df t.ratio p.value
## city - isolated dwelling 0.1478 0.1743 1335 0.848 0.8314
## city - town 0.1053 0.0654 1335 1.610 0.3730
## city - village 0.2356 0.0711 1335 3.315 0.0052
## isolated dwelling - town -0.0425 0.1705 1335 -0.250 0.9945
## isolated dwelling - village 0.0877 0.1727 1335 0.508 0.9572
## town - village 0.1303 0.0610 1335 2.136 0.1424
##
## P value adjustment: tukey method for comparing a family of 4 estimates
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.
To test our hypothesis that there would be a significant effect of depressive symtpoms on particapnst who reside in rural/urban areas , we used a one-way ANOVA. Our data was unbalanced, with many more particapnts from city(n = 453), town (n = 841), than particapants in isolated dwellings (n = 41) or and Village (n = 501) . 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 = .5) 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 urban/rural, F(1334) = 3.75, p < .001, ηp2 = .008 (large effect size; Cohen, 1988). Posthoc tests using sidaks test revealed that particpants in villages reported more depressive symptoms than particpants in the city but less stress than isolated dwellings. (see Figure 1 for a comparison).
References
Cohen J. (1988). Statistical Power Analysis for the Behavioral Sciences. New York, NY: Routledge Academic.