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 aggregate several columns with one summary: take(mtcars, mpg, hp, fun = mean, 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'
# 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 2 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 parental marriage status on satisfaction of life.
# 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': 2115 obs. of 7 variables:
## $ age : chr "1 between 18 and 25" "1 between 18 and 25" "1 between 18 and 25" "1 between 18 and 25" ...
## $ marriage5 : chr "are currently divorced from one another" "are currently married to one another" "are currently married to one another" "are currently married to one another" ...
## $ moa_safety: num 2.75 3.25 3 1.25 2.25 2.5 4 3.25 2.75 3.5 ...
## $ swb : num 4.33 4.17 1.83 5.17 3.67 ...
## $ support : num 6 6.75 5.17 5.58 6 ...
## $ socmeduse : int 47 23 34 35 37 13 37 43 37 29 ...
## $ row_id : int 1 2 3 4 5 6 7 8 9 10 ...
# make our categorical variables factors
#we'll actually use our ID variable for this analysis, so make sure it's coded as a factor
d$marriage5 <- as.factor(d$marriage5)
# d$swb <- as.factor(d$swb)
d$row_id <- as.factor(d$row_id)
# we're going to recode our race/ethnicity variable into two groups: poc and white
table(d$marriage5)
##
## are currently divorced from one another
## 509
## are currently married to one another
## 1401
## never married each other and are not together
## 166
## never married each other but are currently together
## 39
# d$lom[d$marriage5 == "never married each other and are not together"] <- "divorced"
# d$lom[d$marriage5 == "are currently divorced from one another"] <- "divorced"
# d$lom[d$marriage5 == "are currently married to one another"] <- "together"
# d$lom[d$marriage5 == "never married each other and currently together"] <- "together"
# table(d$lom)
# d$lom <- as.factor(d$lom)
# you can use the describe() command on an entire dataframe (d) or just on a single variable
describe(d$swb)
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 2115 4.44 1.33 4.5 4.5 1.48 1 7 6 -0.36 -0.48 0.03
# we'll use the describeBy() command to view skew and kurtosis across our IVs
describeBy(d$swb, group = d$marriage5)
##
## Descriptive statistics by group
## group: are currently divorced from one another
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 509 4.14 1.34 4.33 4.18 1.48 1 7 6 -0.22 -0.53 0.06
## ------------------------------------------------------------
## group: are currently married to one another
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 1401 4.59 1.29 4.83 4.66 1.24 1 7 6 -0.44 -0.43 0.03
## ------------------------------------------------------------
## group: never married each other and are not together
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 166 4.04 1.35 4 4.08 1.48 1 7 6 -0.19 -0.54 0.1
## ------------------------------------------------------------
## group: never married each other but are currently together
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 39 4.51 1.42 4.67 4.55 1.24 1.33 7 5.67 -0.21 -0.51 0.23
# also use histograms to examine your continuous variable
hist(d$swb)
# and cross_cases() to examine your categorical variables
cross_cases(d, marriage5, swb)
| swb | |||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 1 | 1.16666666666667 | 1.33333333333333 | 1.5 | 1.66666666666667 | 1.83333333333333 | 2 | 2.16666666666667 | 2.33333333333333 | 2.5 | 2.66666666666667 | 2.83333333333333 | 3 | 3.16666666666667 | 3.33333333333333 | 3.5 | 3.66666666666667 | 3.83333333333333 | 4 | 4.16666666666667 | 4.33333333333333 | 4.5 | 4.66666666666667 | 4.83333333333333 | 5 | 5.16666666666667 | 5.33333333333333 | 5.5 | 5.66666666666667 | 5.83333333333333 | 6 | 6.16666666666667 | 6.33333333333333 | 6.5 | 6.66666666666667 | 6.83333333333333 | 7 | |
| marriage5 | |||||||||||||||||||||||||||||||||||||
| are currently divorced from one another | 5 | 5 | 6 | 1 | 8 | 6 | 13 | 7 | 9 | 10 | 12 | 16 | 13 | 18 | 26 | 13 | 22 | 20 | 26 | 18 | 27 | 23 | 26 | 20 | 26 | 22 | 21 | 15 | 16 | 15 | 12 | 7 | 11 | 2 | 4 | 3 | 5 |
| are currently married to one another | 5 | 2 | 5 | 9 | 11 | 8 | 13 | 25 | 16 | 22 | 20 | 37 | 34 | 35 | 36 | 41 | 39 | 41 | 61 | 57 | 60 | 55 | 65 | 67 | 67 | 78 | 78 | 68 | 67 | 65 | 63 | 42 | 32 | 29 | 14 | 11 | 23 |
| never married each other and are not together | 3 | 1 | 3 | 1 | 1 | 5 | 1 | 1 | 5 | 2 | 8 | 6 | 3 | 4 | 5 | 8 | 9 | 8 | 10 | 9 | 9 | 7 | 3 | 7 | 8 | 7 | 1 | 6 | 5 | 4 | 9 | 1 | 2 | 1 | 2 | 1 | |
| never married each other but are currently together | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 4 | 3 | 1 | 1 | 2 | 1 | 2 | 3 | 1 | 2 | 3 | 1 | 2 | 1 | 1 | 1 | 1 | 2 | ||||||||||||
| #Total cases | 13 | 8 | 15 | 11 | 20 | 20 | 28 | 33 | 31 | 35 | 40 | 59 | 51 | 57 | 68 | 62 | 74 | 72 | 98 | 85 | 98 | 86 | 96 | 97 | 102 | 109 | 103 | 90 | 88 | 86 | 85 | 50 | 46 | 32 | 21 | 15 | 31 |
table(d$marriage5)
##
## are currently divorced from one another
## 509
## are currently married to one another
## 1401
## never married each other and are not together
## 166
## never married each other but are currently together
## 39
# 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, gender_rc != "nb")
# d2$gender_rc <- droplevels(d2$gender_rc)
# to double-check any changes we made
# cross_cases(d2, gender_rc, poc)
# 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(swb~marriage5, data = d)
## Levene's Test for Homogeneity of Variance (center = median)
## Df F value Pr(>F)
## group 3 0.4368 0.7267
## 2111
# leveneTest(pss~gender_rc*poc, data = d2)
# 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
reg_model <- lm(swb ~ marriage5, data = d) #for one-way
# reg_model2 <- lm(pss ~ gender_rc*poc, data = d2) #for two-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 4 level marriage 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("marriage5"),
dv = "swb",
anova_table = list(es = "pes"))
## Contrasts set to contr.sum for the following variables: marriage5
Effect size cutoffs from Cohen (1988):
nice(aov_model)
## Anova Table (Type 3 tests)
##
## Response: swb
## Effect df MSE F pes p.value
## 1 marriage5 3, 2111 1.72 19.81 *** .027 <.001
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '+' 0.1 ' ' 1
afex_plot(aov_model, x = "marriage5")
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="marriage5", adjust="tukey")
## Note: adjust = "tukey" was changed to "sidak"
## because "tukey" is only appropriate for one set of pairwise comparisons
## marriage5 emmean SE df
## are currently divorced from one another 4.14 0.0581 2111
## are currently married to one another 4.59 0.0350 2111
## never married each other and are not together 4.04 0.1017 2111
## never married each other but are currently together 4.51 0.2099 2111
## lower.CL upper.CL
## 4.00 4.29
## 4.50 4.68
## 3.79 4.30
## 3.99 5.04
##
## Confidence level used: 0.95
## Conf-level adjustment: sidak method for 4 estimates
pairs(emmeans(aov_model, specs="marriage5", adjust="tukey"))
## contrast
## are currently divorced from one another - are currently married to one another
## are currently divorced from one another - never married each other and are not together
## are currently divorced from one another - never married each other but are currently together
## are currently married to one another - never married each other and are not together
## are currently married to one another - never married each other but are currently together
## never married each other and are not together - never married each other but are currently together
## estimate SE df t.ratio p.value
## -0.4460 0.0678 2111 -6.574 <.0001
## 0.0983 0.1172 2111 0.839 0.8360
## -0.3704 0.2178 2111 -1.701 0.3236
## 0.5442 0.1076 2111 5.058 <.0001
## 0.0756 0.2128 2111 0.355 0.9847
## -0.4686 0.2333 2111 -2.009 0.1849
##
## P value adjustment: tukey method for comparing a family of 4 estimates
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.
To test our hypothesis that there would be a significant effect of parental marital status on life satisfaction, we used a one-way ANOVA. Our unevel levels of participants with the different parental marital status significantly reduces the power of our test and increases the chances of a Type II error. A significant Levene’s test (p = .73) 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 marital status, F(3,2111) = 19.81, p < .001, ηp2 = .072 (large effect size; Cohen, 1988). Posthoc tests using Sidak’s test revealed that those with seperated parents reported lower life satisfaction than those with parents that were still together, while those with parents who were married and still together, showed the highest life satisfaction. (see Figure 1 for a comparison).
To test our hypothesis that gender and race would impact stress and would interact significantly, we used a two-way/factorial ANOVA. Our data met most of the assumptions of the test, although our data was unbalanced, with many more women participating in our survey (n = 1004) than men (n = 195). We identified and removed a single outlier following visual analysis of a Residuals vs Leverage plot.
As predicted, we found a significant main effect for gender, F(1,1195) = 28.31, p < .001, ηp2 .023 (small effect size; Cohen, 1988). As predicted, women reported significantly more stress than men. Contrary to our expectations, we did not find a significant main effect for race (p = .453).
Lastly, we found a significant interaction between gender and race (see Figure 2), F(1,1195) = 3.43, p = .064, ηp2 = .003 (trivial effect size; Cohen, 1988). When comparing by race, women of color (M = 3.16, SE = .07) reported significantly more stress than men of color (M = 2.52, SE = .14; p < .001), as did white women (M = 2.93, SE = .03) compared to white men (M = 2.62, SE = .08; p < .001). When comparing by gender, women of color reported significantly more stress than white women (p = .002), while men of color and white men reported similar levels of stress (p = .546).
References
Cohen J. (1988). Statistical Power Analysis for the Behavioral Sciences. New York, NY: Routledge Academic.