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
##
## Use magrittr pipe '%>%' to chain several operations:
## mtcars %>%
## let(mpg_hp = mpg/hp) %>%
## take(mean(mpg_hp), by = am)
##
##
## Attaching package: 'maditr'
## The following object is masked from 'package:base':
##
## sort_by
##
## 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
## Welcome to emmeans.
## Caution: You lose important information if you filter this package's results.
## See '? untidy'
library (ggbeeswarm)
# 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/mydataa.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 income on stress, as measured by the perceived stress scale (PSS-4).
# 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': 364 obs. of 7 variables:
## $ pet : chr "cat" "no pets" "multiple types of pet" "cat" ...
## $ income : chr "3 high" "2 middle" "2 middle" "2 middle" ...
## $ pas_covid: num 3.22 4.22 3.22 3.56 4.56 ...
## $ pss : num 3.25 3.25 2 2 4 2.5 2 2.25 2.75 2.25 ...
## $ gad : num 1.86 2 1.43 1.57 2.86 ...
## $ rse : num 2.3 1.7 3.9 2.4 1.8 2.6 3.5 2.6 2.4 4 ...
## $ row_id : int 1 2 3 4 5 6 7 8 9 10 ...
# make our categorical variables factors
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
d$income <- as.factor(d$income)
d$row_id <- as.factor(d$row_id)
# we're going to recode our pet variable into two groups: pet owners and non pet owners
table(d$income)
##
## 1 low 2 middle 3 high prefer not to say
## 43 173 96 52
d$pet[d$pet == "cat"] <- "pet owner"
d$pet[d$pet == "dog"] <- "pet owner"
d$pet[d$pet == "multiple types of pet"] <- "pet owner"
d$pet[d$pet == "cat and dog"] <- "pet owner"
d$pet[d$pet == "bird"] <- "pet owner"
d$pet[d$pet == "other"] <- "pet owner"
d$pet[d$pet == "fish"] <- "pet owner"
d$pet[d$pet == "no pets"] <- "non pet owner"
table(d$pet)
##
## non pet owner pet owner
## 113 251
d$pet <- as.factor(d$pet)
# 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 364 2.39 0.72 2.25 2.37 0.74 1 4.5 3.5 0.34 -0.05 0.04
# we'll use the describeBy() command to view skew and kurtosis across our IVs
describeBy(d$pss, 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 43 2.54 0.67 2.5 2.54 0.74 1.25 4 2.75 0.13 -0.85 0.1
## ------------------------------------------------------------
## group: 2 middle
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 173 2.37 0.72 2.25 2.36 0.74 1 4.5 3.5 0.3 0 0.05
## ------------------------------------------------------------
## group: 3 high
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 96 2.3 0.7 2.25 2.27 0.74 1 4 3 0.36 -0.5 0.07
## ------------------------------------------------------------
## group: prefer not to say
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 52 2.51 0.8 2.5 2.47 0.56 1 4.5 3.5 0.48 0.26 0.11
describeBy(d$pss, group = d$pet)
##
## Descriptive statistics by group
## group: non pet owner
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 113 2.39 0.75 2.25 2.35 0.74 1 4.5 3.5 0.57 0.3 0.07
## ------------------------------------------------------------
## group: pet owner
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 251 2.39 0.71 2.25 2.38 0.74 1 4.5 3.5 0.23 -0.26 0.05
# also use histograms to examine your continuous variable
hist(d$pss)
# and cross_cases() to examine your categorical variables
cross_cases(d, income, pet)
| pet | ||
|---|---|---|
| non pet owner | pet owner | |
| income | ||
| 1 low | 12 | 31 |
| 2 middle | 61 | 112 |
| 3 high | 21 | 75 |
| prefer not to say | 19 | 33 |
| #Total cases | 113 | 251 |
table(d$income)
##
## 1 low 2 middle 3 high prefer not to say
## 43 173 96 52
# 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~income, data = d)
## Levene's Test for Homogeneity of Variance (center = median)
## Df F value Pr(>F)
## group 3 0.0345 0.9914
## 360
# 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(18))
# 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
reg_model <- lm(pss ~ income, 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. 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 three-level gender 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 a single outlier.
aov_model <- aov_ez(data = d,
id = "row_id",
between = c("income"),
dv = "pss",
anova_table = list(es = "pes"))
## Contrasts set to contr.sum for the following variables: income
Effect size cutoffs from Cohen (1988):
nice(aov_model)
## Anova Table (Type 3 tests)
##
## Response: pss
## Effect df MSE F pes p.value
## 1 income 3, 359 0.51 1.44 .012 .232
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '+' 0.1 ' ' 1
afex_plot(aov_model, x = "income")
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="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 2.54 0.1090 359 2.27 2.81
## 2 middle 2.37 0.0543 359 2.23 2.51
## 3 high 2.30 0.0729 359 2.12 2.48
## prefer not to say 2.48 0.1001 359 2.22 2.73
##
## 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.1708 0.1218 359 1.402 0.4986
## 1 low - 3 high 0.2412 0.1311 359 1.840 0.2565
## 1 low - prefer not to say 0.0652 0.1479 359 0.441 0.9713
## 2 middle - 3 high 0.0705 0.0909 359 0.775 0.8658
## 2 middle - prefer not to say -0.1055 0.1139 359 -0.927 0.7904
## 3 high - prefer not to say -0.1760 0.1238 359 -1.422 0.4867
##
## P value adjustment: tukey method for comparing a family of 4 estimates
To test our hypothesis that there would be a significant effect of income on stress, we used a one-way ANOVA. Our data was unbalanced, with many more pet owners participating in our survey (n = 251) than non pet owners (n = 113). This can reduce 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 = .9914) 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 an effect of income, F(2,1246) = 27.54, p < .001, ηp2 = .042 (large effect size; Cohen, 1988). Posthoc tests using Tukey’s HSD revealed that low income reported more stress than high income but less stress than middle income, while middle income reported the highest amount of stress overall (see Figure 1 for a comparison).
References
Cohen J. (1988). Statistical Power Analysis for the Behavioral Sciences. New York, NY: Routledge Academic.