#install.packages("afex")
#install.packages("emmeans")
#install.packages("ggbeeswarm")
#install.packages("expss")
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(afex) # to run the ANOVA
library(ggbeeswarm) # to run plot results
library(emmeans) # for posthoc tests
# 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 3 or more levels) OR a two-way/factorial ANOVA (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.
We predict that there will be a significant different in people’s levels of stress based on their annual household income (low, middle, high)
# you only need to check the variables you're using in the current analysis
str(d)
## 'data.frame': 2065 obs. of 8 variables:
## $ ResponseID : chr "R_BJN3bQqi1zUMid3" "R_2TGbiBXmAtxywsD" "R_12G7bIqN2wB2N65" "R_39pldNoon8CePfP" ...
## $ age : chr "1 between 18 and 25" "1 between 18 and 25" "1 between 18 and 25" "1 between 18 and 25" ...
## $ income : chr "1 low" "1 low" "rather not say" "rather not say" ...
## $ moa_independence: num 3.67 3.67 3.5 3 3.83 ...
## $ moa_safety : num 2.75 3.25 3 1.25 2.25 2.5 4 3.25 2.75 3.5 ...
## $ 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$row_id <- as.factor(d$row_id)
# check that our categorical variables of interest are now factors
str(d)
## 'data.frame': 2065 obs. of 8 variables:
## $ ResponseID : chr "R_BJN3bQqi1zUMid3" "R_2TGbiBXmAtxywsD" "R_12G7bIqN2wB2N65" "R_39pldNoon8CePfP" ...
## $ age : chr "1 between 18 and 25" "1 between 18 and 25" "1 between 18 and 25" "1 between 18 and 25" ...
## $ income : Factor w/ 4 levels "1 low","2 middle",..: 1 1 4 4 2 4 1 1 3 4 ...
## $ moa_independence: num 3.67 3.67 3.5 3 3.83 ...
## $ moa_safety : num 2.75 3.25 3 1.25 2.25 2.5 4 3.25 2.75 3.5 ...
## $ 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 : Factor w/ 2065 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 2065 3.07 0.6 3.1 3.07 0.59 1.3 4.6 3.3 -0.02 -0.14 0.01
# 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 572 3.05 0.59 3.1 3.06 0.59 1.5 4.6 3.1 -0.03 -0.13 0.02
## ------------------------------------------------------------
## group: 2 middle
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 599 3.04 0.6 3.1 3.04 0.59 1.3 4.6 3.3 -0.01 -0.18 0.02
## ------------------------------------------------------------
## group: 3 high
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 335 3.03 0.63 3 3.03 0.59 1.3 4.6 3.3 0.02 -0.27 0.03
## ------------------------------------------------------------
## group: rather not say
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 559 3.13 0.57 3.1 3.13 0.59 1.4 4.6 3.2 -0.01 -0.08 0.02
# also use histograms to examine your continuous variable
hist(d$stress)
d <- subset(d, income != "rather not say") # use subset() to remove all participants from the additional level
table(d$income, useNA = "always") # verify that now there are ZERO participants in the additional level
##
## 1 low 2 middle 3 high rather not say <NA>
## 572 599 335 0 0
d$income <- droplevels(d$income) # use droplevels() to drop the empty factor
table(d$income, useNA = "always") # verify that now the entire factor level is removed
##
## 1 low 2 middle 3 high <NA>
## 572 599 335 0
table(d$income)
##
## 1 low 2 middle 3 high
## 572 599 335
# REMEMBER your test's level of POWER is determined by your SMALLEST subsample
# 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))
# NOTE: Participant 1108 is not truly problematic in the Lab, but we are going to removed them for demonstration.
# 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~x, where y is our DV, x is our IV
reg_model <- lm(stress~income, data =d )
# Cook's distance
plot(reg_model, 4)
# Residuals VS Leverage
plot(reg_model, 5)
# IF you find outliers, go back up to line 114 or 117 and remove it/them, then re-run the reg_model code
# use the leveneTest() command from the car package to test homogeneity of variance
# uses the 'formula' setup: formula is y~x, where y is our DV and x is our IV
leveneTest(stress~income, data = d)
## Levene's Test for Homogeneity of Variance (center = median)
## Df F value Pr(>F)
## group 2 0.9204 0.3986
## 1503
Our independent variable, annual household income, originally had four levels (low, middle, high, and rather not say). To proceed with our analysis, we removed participants who selected “rather not say” for their annual household income. Our cell sizes are unbalanced between the annual household income group levels. A smaller sample size for one of the levels of our variable (high, n = 335) increases our type II error rate.
[UPDATE this section in your HW.]
aov_model <- aov_ez(data = d,
id = "ResponseID",
between = c("income"),
dv = "stress",
anova_table = list(es = "pes"))
## Contrasts set to contr.sum for the following variables: income
nice(aov_model)
## Anova Table (Type 3 tests)
##
## Response: stress
## Effect df MSE F pes p.value
## 1 income 2, 1503 0.36 0.21 <.001 .810
## ---
## 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
afex_plot(aov_model, x ="income" )
Remember: We 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")
#pairs(emmeans(aov_model, specs="income", adjust="tukey"))
To test our hypothesis that there will be a significant difference in people’s level of stress based on their annual household income (low, middle, high), we used a one-way ANOVA. Before conducting our anlysis, we removed participants who selected “rather not say” for their annual household income. Our data was somewhat unbalanced, with more participants in the low-income (n = 572) and middle-income (n = 599) groups than in the high-income group (n = 335). This increases the chances of a Type II error.
Our hypothesis was not supported, as we did not find a significant effect of annual household income on stress, F(2, 2061) = 0.21, p = .810 , ηp2 = .001 (trivial effect; Cohen, 1988). These results indicate that levels of stress were not significantly different among participants in the low (M = 3.05, SD = 0.69), middle (M = 3.04, SD = 0.60), and high (M = 3.03, SD = 0.63) income groups (see Figure 1 for a comparison).
References
Cohen J. (1988). Statistical Power Analysis for the Behavioral Sciences. New York, NY: Routledge Academic.