library(afex) # to run the ANOVA and plot results
library(psych) # for the describe() command
library(ggplot2) # to visualize our results
library(expss) # for the cross_cases() command
library(car) # for the leveneTest() command
library(emmeans) # for posthoc testsANOVA HW
Loading Libraries
Importing Data
d <- read.csv(file="Data/mydata.csv", header=T)
#
# # new code! this adds a column with a number for each row. it acts as an identifier for our participations
d$row_id <- 1:nrow(d)State Your Hypothesis
I hypothesize that people with a low income will be higher in perceived stress than people with a middle or high income.
Check Your Assumptions
ANOVA Assumptions
- Independence of observations (confirmed by data report)
- All levels of the IVs should have equal number of cases (ideally; in the real world, this varies) and there should be no empty cells. Cells with low numbers increase chance of Type II error. (we will check this below)
- Homogeneity of variance should be assured (we will check this below)
- Outliers should be identified and removed (we will check this below)
- DV should be normally distributed for each level of the IV (we will check this below)
Check levels of IVs
# # for a one-way ANOVA
# # COMMENT THIS SECTION OUR FROM THE HW IF YOU DO NOT USE IT
table(d$income, useNA = "always")
1 low 2 middle 3 high rather not say <NA>
877 877 535 853 0
# # if you need to recode
# # to drop levels from your variable
# # this subsets the data and says that any participant who is coded as 'LEVEL BAD' should be removed
# # if you don't need this for the homework, comment it out (add a # at the beginning of the line)
d <- subset(d, income != "rather not say")
# # preview your changes and make sure everything is correct
table(d$income, useNA = "always")
1 low 2 middle 3 high <NA>
877 877 535 0
# # or
# # check your variable types
str(d)'data.frame': 2289 obs. of 7 variables:
$ sibling: chr "at least one sibling" "at least one sibling" "at least one sibling" "at least one sibling" ...
$ income : chr "1 low" "1 low" "2 middle" "1 low" ...
$ mindful: num 2.4 1.8 3.2 4.13 3 ...
$ swb : num 4.33 4.17 3.67 3.67 5.5 ...
$ stress : num 3.3 3.3 3.1 3.3 2.4 2.9 3.5 4.4 2.8 3.3 ...
$ belong : num 2.8 4.2 3.4 3.9 3.6 2.9 4.1 3 4.1 3.3 ...
$ row_id : int 1 2 5 7 8 9 11 16 17 18 ...
# # make sure that your IV is recognized as a factor by R
d$income <- as.factor(d$income)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
# # for a one-way ANOVA
# # COMMENT THIS SECTION OUR FROM THE HW IF YOU DO NOT USE IT
leveneTest(stress~income, data = d)Levene's Test for Homogeneity of Variance (center = median)
Df F value Pr(>F)
group 2 1.2004 0.3013
2286
Check for outliers using Cook’s distance and Residuals vs Leverage plot
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(1108))
#
# # to drop multiple outliers, remove the # at the beginning of the line and 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, and c is our covariate
# # for a one-way ANOVA
# # COMMENT THIS SECTION OUR FROM THE HW IF YOU DO NOT USE IT
reg_model <- lm(stress ~ income, data = d) #for one-wayCheck for outliers
# # for a one-way ANOVA
# # COMMENT THIS SECTION OUR FROM THE HW IF YOU DO NOT USE IT
# # Cook's distance - cutoff is .5, not .05
plot(reg_model, 4)# # Residuals vs Leverage - look for wobble in the red line or any participants outside of the dashed lines for Cook's distance
plot(reg_model, 5)Check Your Variables
# # we'll use the describeBy() command to view skew and kurtosis across our IVs and make sure the DV is normally distributed across all of the levels
describeBy(d$stress, group = d$income)
Descriptive statistics by group
group: 1 low
vars n mean sd median trimmed mad min max range skew kurtosis se
X1 1 877 3.04 0.6 3 3.04 0.59 1.4 4.6 3.2 0.04 -0.23 0.02
------------------------------------------------------------
group: 2 middle
vars n mean sd median trimmed mad min max range skew kurtosis se
X1 1 877 3 0.6 3 3 0.59 1.3 4.6 3.3 0.01 -0.16 0.02
------------------------------------------------------------
group: 3 high
vars n mean sd median trimmed mad min max range skew kurtosis se
X1 1 535 3.02 0.63 3 3.02 0.59 1.3 4.7 3.4 0.07 -0.27 0.03
Issues with My Data
I dropped participants who preferred not to say what their income was, dropping 853 participants. There was homogeneity of variance and it was good to proceed. There were no outliers. Skew and kurtosis were within guidelines of -2 to 2.
Run an ANOVA
# for a one-way ANOVA
# COMMENT THIS SECTION OUR FROM THE HW IF YOU DO NOT USE IT
aov_model <- aov_ez(data = d,
id = "row_id",
between = c("income"),
dv = "stress",
anova_table = list(es = "pes"))Contrasts set to contr.sum for the following variables: income
View Output
Effect size cutoffs from Cohen (1988):
- ηp2 = 0.01 indicates a small effect
- ηp2 = 0.06 indicates a medium effect
- ηp2 = 0.14 indicates a large effect
# for a one-way ANOVA
# COMMENT THIS SECTION OUR FROM THE HW IF YOU DO NOT USE IT
nice(aov_model)Anova Table (Type 3 tests)
Response: stress
Effect df MSE F pes p.value
1 income 2, 2286 0.37 1.08 <.001 .339
---
Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '+' 0.1 ' ' 1
Visualize Results
# for a one-way ANOVA
# COMMENT THIS SECTION OUR FROM THE HW IF YOU DO NOT USE IT
afex_plot(aov_model, x = "income")# Run Posthoc Tests
# 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.# for a one-way ANOVA
# COMMENT THIS SECTION OUR FROM THE HW IF YOU DO NOT USE IT
emmeans(aov_model, specs="income", adjust="tukey")Note: adjust = "tukey" was changed to "sidak"
because "tukey" is only appropriate for one set of pairwise comparisons
income emmean SE df lower.CL upper.CL
1 low 3.04 0.0205 2286 2.99 3.09
2 middle 3.00 0.0205 2286 2.95 3.05
3 high 3.02 0.0262 2286 2.96 3.09
Confidence level used: 0.95
Conf-level adjustment: sidak method for 3 estimates
pairs(emmeans(aov_model, specs="income", adjust="tukey")) contrast estimate SE df t.ratio p.value
1 low - 2 middle 0.0426 0.0290 2286 1.472 0.3048
1 low - 3 high 0.0202 0.0333 2286 0.608 0.8159
2 middle - 3 high -0.0224 0.0333 2286 -0.673 0.7791
P value adjustment: tukey method for comparing a family of 3 estimates
Write Up Results
One-Way ANOVA
I hypothesize that people with a low income will be higher in perceived stress than people with a middle or high income.I dropped participants who preferred not to say what their income was, dropping 853 participants. There was homogeneity of variance and it was good to proceed. There were no outliers. Skew and kurtosis were within guidelines of -2 to 2.
I did not find a significant difference of stress level between income groups (p = 0.339).
References
Cohen J. (1988). Statistical Power Analysis for the Behavioral Sciences. New York, NY: Routledge Academic.