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 drop variable use NULL: let(mtcars, am = NULL) %>% head()
##
## Attaching package: 'maditr'
## The following object is masked from 'package:base':
##
## sort_by
##
## Use 'expss_output_rnotebook()' to display tables inside R Notebooks.
## To return to the console output, use 'expss_output_default()'.
##
## 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'
# 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/mydata.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 age on importance of maturity.
# 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': 2036 obs. of 7 variables:
## $ race_rc : chr "white" "white" "white" "other" ...
## $ age : chr "1 between 18 and 25" "1 between 18 and 25" "1 between 18 and 25" "1 between 18 and 25" ...
## $ moa_independence: num 3.67 3.67 3.5 3 3.83 ...
## $ moa_role : num 3 2.67 2.5 2 2.67 ...
## $ moa_safety : num 2.75 3.25 3 1.25 2.25 2.5 4 3.25 2.75 3.5 ...
## $ moa_maturity : num 3.67 3.33 3.67 3 3.67 ...
## $ 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're going to recode our age vaciable into two groups: younger and older
table(d$age)
##
## 1 between 18 and 25 2 between 26 and 35 3 between 36 and 45 4 over 45
## 1871 111 37 17
d$younger[d$age == "1 between 18 and 25"] <- "younger"
d$younger[d$age == "2 between 26 and 35"] <- "younger"
d$younger[d$age == "3 between 36 and 45"] <- "younger"
d$younger[d$age == "4 over 45"] <- "older"
table(d$younger)
##
## older younger
## 17 2019
d$younger <- (as.factor(d$younger))
# you can use the describe() command on an entire dataframe (d) or just on a single variable
describe(d$moa_maturity)
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 2036 3.61 0.43 3.67 3.67 0.49 1.33 4 2.67 -1.24 1.74 0.01
# we'll use the describeBy() command to view skew and kurtosis across our IVs
describeBy(d$moa_maturity, group = d$age)
##
## Descriptive statistics by group
## group: 1 between 18 and 25
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 1871 3.6 0.43 3.67 3.67 0.49 1.33 4 2.67 -1.22 1.72 0.01
## ------------------------------------------------------------
## group: 2 between 26 and 35
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 111 3.71 0.37 3.67 3.77 0.49 2.33 4 1.67 -1.27 1.11 0.04
## ------------------------------------------------------------
## group: 3 between 36 and 45
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 37 3.75 0.41 4 3.82 0 2 4 2 -2.27 6.36 0.07
## ------------------------------------------------------------
## group: 4 over 45
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 17 3.49 0.47 3.33 3.51 0.49 2.67 4 1.33 -0.31 -1.3 0.11
# also use histograms to examine your continuous variable
hist(d$moa_maturity)
# and cross_cases() to examine your categorical variables
cross_cases(d, age, younger)
|  younger | ||
|---|---|---|
|  older |  younger | |
|  age | ||
| Â Â Â 1 between 18 and 25Â | 1871 | |
| Â Â Â 2 between 26 and 35Â | 111 | |
| Â Â Â 3 between 36 and 45Â | 37 | |
| Â Â Â 4 over 45Â | 17 | |
|    #Total cases | 17 | 2019 |
table(d$age)
##
## 1 between 18 and 25 2 between 26 and 35 3 between 36 and 45 4 over 45
## 1871 111 37 17
cross_cases(d, age, younger)
|  younger | ||
|---|---|---|
|  older |  younger | |
|  age | ||
| Â Â Â 1 between 18 and 25Â | 1871 | |
| Â Â Â 2 between 26 and 35Â | 111 | |
| Â Â Â 3 between 36 and 45Â | 37 | |
| Â Â Â 4 over 45Â | 17 | |
|    #Total cases | 17 | 2019 |
# 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
d2 <- subset(d, age != "nb")
# to double-check any changes we made
cross_cases(d2, age, younger)
|  younger | ||
|---|---|---|
|  older |  younger | |
|  age | ||
| Â Â Â 1 between 18 and 25Â | 1871 | |
| Â Â Â 2 between 26 and 35Â | 111 | |
| Â Â Â 3 between 36 and 45Â | 37 | |
| Â Â Â 4 over 45Â | 17 | |
|    #Total cases | 17 | 2019 |
# 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(moa_maturity~age, data = d)
## Warning in leveneTest.default(y = y, group = group, ...): group coerced to
## factor.
## Levene's Test for Homogeneity of Variance (center = median)
## Df F value Pr(>F)
## group 3 1.373 0.2492
## 2032
# 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(2))
# 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)) & row_id!=c(220))
# 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(moa_maturity ~ age, 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("age"),
dv = "moa_maturity",
anova_table = list(es = "pes"))
## Converting to factor: age
## Contrasts set to contr.sum for the following variables: age
Effect size cutoffs from Cohen (1988):
nice(aov_model)
## Anova Table (Type 3 tests)
##
## Response: moa_maturity
## Effect df MSE F pes p.value
## 1 age 3, 2031 0.18 3.70 * .005 .011
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '+' 0.1 ' ' 1
afex_plot(aov_model, x = "age")
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="age", adjust="tukey")
## Note: adjust = "tukey" was changed to "sidak"
## because "tukey" is only appropriate for one set of pairwise comparisons
## age emmean SE df lower.CL upper.CL
## 1 between 18 and 25 3.60 0.00987 2031 3.58 3.63
## 2 between 26 and 35 3.71 0.04051 2031 3.60 3.81
## 3 between 36 and 45 3.75 0.07016 2031 3.57 3.92
## 4 over 45 3.49 0.10350 2031 3.23 3.75
##
## Confidence level used: 0.95
## Conf-level adjustment: sidak method for 4 estimates
pairs(emmeans(aov_model, specs="age", adjust="tukey"))
## contrast estimate SE df t.ratio p.value
## 1 between 18 and 25 - 2 between 26 and 35 -0.101 0.0417 2031 -2.433 0.0713
## 1 between 18 and 25 - 3 between 36 and 45 -0.143 0.0708 2031 -2.025 0.1791
## 1 between 18 and 25 - 4 over 45 0.114 0.1040 2031 1.097 0.6913
## 2 between 26 and 35 - 3 between 36 and 45 -0.042 0.0810 2031 -0.519 0.9546
## 2 between 26 and 35 - 4 over 45 0.216 0.1111 2031 1.939 0.2121
## 3 between 36 and 45 - 4 over 45 0.258 0.1250 2031 2.060 0.1668
##
## P value adjustment: tukey method for comparing a family of 4 estimates
emmeans(aov_model, specs="age", adjust="tukey")
## Note: adjust = "tukey" was changed to "sidak"
## because "tukey" is only appropriate for one set of pairwise comparisons
## age emmean SE df lower.CL upper.CL
## 1 between 18 and 25 3.60 0.00987 2031 3.58 3.63
## 2 between 26 and 35 3.71 0.04051 2031 3.60 3.81
## 3 between 36 and 45 3.75 0.07016 2031 3.57 3.92
## 4 over 45 3.49 0.10350 2031 3.23 3.75
##
## Confidence level used: 0.95
## Conf-level adjustment: sidak method for 4 estimates
pairs(emmeans(aov_model, specs="age", adjust="tukey"))
## contrast estimate SE df t.ratio p.value
## 1 between 18 and 25 - 2 between 26 and 35 -0.101 0.0417 2031 -2.433 0.0713
## 1 between 18 and 25 - 3 between 36 and 45 -0.143 0.0708 2031 -2.025 0.1791
## 1 between 18 and 25 - 4 over 45 0.114 0.1040 2031 1.097 0.6913
## 2 between 26 and 35 - 3 between 36 and 45 -0.042 0.0810 2031 -0.519 0.9546
## 2 between 26 and 35 - 4 over 45 0.216 0.1111 2031 1.939 0.2121
## 3 between 36 and 45 - 4 over 45 0.258 0.1250 2031 2.060 0.1668
##
## P value adjustment: tukey method for comparing a family of 4 estimates
To test our hypothesis that there will be a significant effect of age on importance of maturity, we used a one-way ANOVA. Our data was unbalanced, with many more younger participating in our survey (n = 2019) than older (n = 17). 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 = 0.2492) 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 age, F(3,2031) = 3.70, p < .001, ηp2 = .042 (large effect size; Cohen, 1988). Posthoc tests using Tukey’s HSD revealed that women reported more stress than men but less stress than non-binary and other gender participants, while non-binary and other gender participants 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.