#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 select rows from data: rows(mtcars, am==0)
##
## 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
## Registered S3 method overwritten by 'lme4':
## method from
## na.action.merMod car
##
## 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
setwd("~/Library/CloudStorage/OneDrive-IndianaUniversity/421 Lab in Social Psychology/Research/Final Paper/")
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 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: There will be a significant difference in perceived stress by people’s level of yearly income, between low income, middle income, and high income groups.
# you only need to check the variables you're using in the current analysis
str(d)
## 'data.frame': 3146 obs. of 8 variables:
## $ ResponseID: chr "R_BJN3bQqi1zUMid3" "R_2TGbiBXmAtxywsD" "R_12G7bIqN2wB2N65" "R_39pldNoon8CePfP" ...
## $ income : chr "1 low" "1 low" "rather not say" "rather not say" ...
## $ edu : chr "2 Currently in college" "5 Completed Bachelors Degree" "2 Currently in college" "2 Currently in college" ...
## $ idea : num 3.75 3.88 3.75 3.75 3.5 ...
## $ swb : num 4.33 4.17 1.83 5.17 3.67 ...
## $ efficacy : num 3.4 3.4 2.2 2.8 3 2.4 2.3 3 3 3.7 ...
## $ stress : num 3.3 3.3 4 3.2 3.1 3.5 3.3 2.4 2.9 2.7 ...
## $ 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$income <- as.factor(d$income)
d$stress <- as.factor(d$stress)
d$row_id <- as.factor(d$row_id)
# we're going to recode our race variable into two groups: poc and white
# in doing so, we are creating a new variable "poc" that has 2 levels
table(d$income)
##
## 1 low 2 middle 3 high rather not say
## 878 879 534 855
d$income <- as.factor(d$income)
# check that all our categorical variables of interest are now factors
str(d)
## 'data.frame': 3146 obs. of 8 variables:
## $ ResponseID: chr "R_BJN3bQqi1zUMid3" "R_2TGbiBXmAtxywsD" "R_12G7bIqN2wB2N65" "R_39pldNoon8CePfP" ...
## $ income : Factor w/ 4 levels "1 low","2 middle",..: 1 1 4 4 2 4 1 1 3 4 ...
## $ edu : chr "2 Currently in college" "5 Completed Bachelors Degree" "2 Currently in college" "2 Currently in college" ...
## $ idea : num 3.75 3.88 3.75 3.75 3.5 ...
## $ swb : num 4.33 4.17 1.83 5.17 3.67 ...
## $ efficacy : num 3.4 3.4 2.2 2.8 3 2.4 2.3 3 3 3.7 ...
## $ stress : Factor w/ 35 levels "1.3","1.4","1.5",..: 21 21 28 20 19 23 21 12 17 15 ...
## $ row_id : Factor w/ 3146 levels "1","2","3","4",..: 1 2 3 4 5 6 7 8 9 10 ...
# check our DV skew and kurtosis
describe(d$stress)
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1* 1 3146 18.5 6 18 18.47 5.93 1 35 34 0.03 -0.16 0.11
# fix stress variable
d$stress <- as.numeric(as.character(d$stress))
# remove missing values
d <- subset(d, !is.na(stress))
# histogram
hist(d$stress)
# we'll use the describeBy() command to view our DV's skew and kurtosis across our IVs' 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 878 3.04 0.6 3 3.04 0.59 1.4 4.6 3.2 0.05 -0.23 0.02
## ------------------------------------------------------------
## group: 2 middle
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 879 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 534 3.02 0.63 3 3.02 0.59 1.3 4.7 3.4 0.07 -0.28 0.03
## ------------------------------------------------------------
## group: rather not say
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 855 3.12 0.57 3.1 3.12 0.59 1.4 4.7 3.3 0.07 -0.08 0.02
# also use histograms to examine your continuous variable
hist(d$stress)
# REMEMBER your test's level of POWER is determined by your SMALLEST subsample
# One-Way
table(d$income)
##
## 1 low 2 middle 3 high rather not say
## 878 879 534 855
## If cross_cases() doesn't work for you, then use xtabs() instead. Fill in the code below and remove the "#" to run. Then hashtag out the cross_cases() line above.
#xtabs(~ V1 + V2, data=d)
# our small number of participants owning rabbits is going to hurt us for the two-way anova, but it should be okay for the one-way anova
# 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(stress~income, data = d)
## Levene's Test for Homogeneity of Variance (center = median)
## Df F value Pr(>F)
## group 3 2.5748 0.05225 .
## 3142
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
# use this commented out section below ONLY IF if you need to remove outliers
# to drop a single outlier, use this code:
# d <- subset(d, row_id!=c(1108))
# 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.
# One-Way
reg_model <- lm(stress~income, data = d)
# Cook's distance
plot(reg_model, 4)
# Residuals VS Leverage
plot(reg_model, 5)
My cell sizes were somewhat unbalanced across the three income groups, with more participants in the low- and middle-income groups (n = 878 and n = 879, respectively) compared to the high-income group (n = 534). Additionally, a large number of participants selected “rather not say” (n = 855), which was excluded from the analysis. This imbalance may reduce the power of the analysis and increase the likelihood of a Type II error.
Levene’s test was not significant, F(3, 3142) = 2.57, p = .052, indicating that the assumption of equal variances was met. Therefore, we proceeded with the one-way ANOVA.
We examined potential outliers using Cook’s Distance and Residuals vs. Leverage plots and did not identify any influential outliers. No cases were removed prior to analysis.
# One-Way
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
# One-Way
nice(aov_model)
## Anova Table (Type 3 tests)
##
## Response: stress
## Effect df MSE F pes p.value
## 1 income 3, 3142 0.36 6.78 *** .006 <.001
## ---
## 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 = "income")
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, 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.0202 3142 2.99 3.09
## 2 middle 3.00 0.0202 3142 2.95 3.05
## 3 high 3.02 0.0259 3142 2.96 3.09
## rather not say 3.12 0.0204 3142 3.07 3.18
##
## Confidence level used: 0.95
## Conf-level adjustment: sidak method for 4 estimates
pairs(emmeans(aov_model, specs="income", adjust="tukey"))
## contrast estimate SE df t.ratio p.value
## 1 low - 2 middle 0.0426 0.0285 3142 1.493 0.4418
## 1 low - 3 high 0.0204 0.0328 3142 0.623 0.9248
## 1 low - rather not say -0.0808 0.0287 3142 -2.813 0.0254
## 2 middle - 3 high -0.0222 0.0328 3142 -0.675 0.9064
## 2 middle - rather not say -0.1234 0.0287 3142 -4.297 0.0001
## 3 high - rather not say -0.1013 0.0330 3142 -3.071 0.0116
##
## P value adjustment: tukey method for comparing a family of 4 estimates
To test our hypothesis that perceived stress differs by income level (low, middle, high, and “rather not say”), we conducted a one-way ANOVA. The data were somewhat unbalanced across groups, with similar sample sizes for low-income (n = 878) and middle-income participants (n = 879), fewer participants in the high-income group (n = 534), and a substantial number of participants selecting “rather not say” (n = 855). This imbalance may reduce statistical power and increase the risk of Type II error.
Levene’s test for homogeneity of variance was not significant, F(3, 3142) = 2.57, p = .052, indicating that the assumption of equal variances was met. No influential outliers were identified using Cook’s Distance and Residuals vs. Leverage plots, so no cases were removed prior to analysis.
The results of the ANOVA indicated a significant effect of income on perceived stress, F(3, 3142) = 6.78, p < .001, ηp² = .006 (trivial effect size; Cohen, 1988).
Post hoc comparisons using Tukey-adjusted pairwise tests revealed that participants who selected “rather not say” reported significantly higher stress than those in the low-income (p = .025), middle-income (p < .001), and high-income groups (p = .012). There were no significant differences between the low-, middle-, and high-income groups (p > .05).
Overall, these results suggest that stress levels are relatively similar across reported income groups, but individuals who chose not to report their income experienced slightly higher levels of perceived stress.
References
Cohen J. (1988). Statistical Power Analysis for the Behavioral Sciences. New York, NY: Routledge Academic.