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 modify variables or add new variables:
## let(mtcars, new_var = 42, new_var2 = new_var*hp) %>% head()
##
## 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
# 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/eammi2_finalREAL.csv", header=T)
# for the HW, you may or may not need to use the code below this comment
# check to see if you have a variable called 'X' or 'ResponseId' in your data
names(d)
## [1] "gender" "marriage5" "support" "belong" "moa_role"
## [6] "moa_maturity"
# if you do have 'X' or 'ResponseId' (aka your ID variable) then DELETE THE CODE BELOW
# if you don't have those variables, keep the code and run it
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.
Two-Way: We predict that there will significant effects of gender identity (gender) and parent’s marriage status (marriage5) on percieved social support (support). We also predict that gender and parent’s marriage status will interact and that women with parents who are anything except currently married will report significantly lower levels of percieved social support.
# 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': 3075 obs. of 7 variables:
## $ gender : chr "f" "m" "m" "f" ...
## $ 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" ...
## $ support : num 6 6.75 5.17 5.58 6 ...
## $ belong : num 2.8 4.2 3.6 4 3.4 4.2 3.9 3.6 2.9 2.5 ...
## $ moa_role : num 3 2.67 2.5 2 2.67 ...
## $ 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
#recoding our variables as factors overriding current one withg
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$gender <- as.factor(d$gender)
d$marriage5 <- as.factor(d$marriage5)
# check your categorical variables
#table(d$VARIABLE)
table(d$gender)
##
## f m nb
## 2252 770 53
table(d$marriage, useNA = "always")
##
## are currently divorced from one another
## 712
## are currently married to one another
## 2084
## never married each other and are not together
## 235
## never married each other but are currently together
## 44
## <NA>
## 0
# also use histograms to examine your continuous variable
#hist(YOUR DV)
hist(d$support)
Assumptions checked below:
If you have confirmed everything else…
# check your categorical variables and make sure they have decent cell sizes
# they should have at least 5 participants in each cell
# but larger numbers are always better
#table(IV1)
#table(IV2)
#cross_cases(d, IV1, IV2)
table(d$gender)
##
## f m nb
## 2252 770 53
table(d$marriage5)
##
## are currently divorced from one another
## 712
## are currently married to one another
## 2084
## never married each other and are not together
## 235
## never married each other but are currently together
## 44
cross_cases(d, gender, marriage5)
| Â marriage5Â | ||||
|---|---|---|---|---|
|  are currently divorced from one another |  are currently married to one another |  never married each other and are not together |  never married each other but are currently together | |
|  gender | ||||
|    f | 529 | 1518 | 173 | 32 |
|    m | 170 | 532 | 56 | 12 |
|    nb | 13 | 34 | 6 | |
|    #Total cases | 712 | 2084 | 235 | 44 |
# we're going to recode our race/ethnicity variable into two groups: poc and white
# you may or may not need to combine and/or drop groups for the HW
table(d$marriage5)
##
## are currently divorced from one another
## 712
## are currently married to one another
## 2084
## never married each other and are not together
## 235
## never married each other but are currently together
## 44
d$marriage_rc[d$marriage5 == "are currently divorced from one another"] <- "not married"
d$marriage_rc[d$marriage5 == "never married each other and are not together"] <- "not married"
d$marriage_rc[d$marriage5 == "never married each other but are currently together"] <- "not married"
d$marriage_rc[d$marriage5 == "prefer_not"] <- NA
d$marriage_rc[d$marriage5 == "are currently married to one another"] <- "married"
table(d$marriage_rc)
##
## married not married
## 2084 991
d$marriage_rc <- as.factor(d$marriage_rc)
# 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 == "m" | gender == "f")
d2$gender <- droplevels(d2$gender)
# to double-check any changes we made
#table(IV1)
#table(IV2)
#cross_cases(d2, IV1, IV2)
table(d2$gender)
##
## f m
## 2252 770
table(d2$marriage_rc)
##
## married not married
## 2050 972
cross_cases(d2, gender, marriage_rc)
|  marriage_rc | ||
|---|---|---|
|  married |  not married | |
|  gender | ||
|    f | 1518 | 734 |
|    m | 532 | 238 |
|    #Total cases | 2050 | 972 |
# you can use the describe() command on an entire dataframe (d) or just on a single variable
#describe(YOUR DV)
describe(d$support)
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 3075 5.54 1.13 5.75 5.66 0.99 0 7 7 -1.11 1.48 0.02
# we'll use the describeBy() command to view skew and kurtosis across our IVs
#describeBy(YOUR DV, group = YOUR IV 1)
describeBy(d2$support, group = d2$gender)
##
## Descriptive statistics by group
## group: f
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 2252 5.6 1.11 5.83 5.72 0.99 0 7 7 -1.18 1.74 0.02
## ------------------------------------------------------------
## group: m
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 770 5.41 1.16 5.58 5.52 1.11 1 7 6 -0.98 1.06 0.04
#doing the two way anova
describeBy(d2$support, group = d2$gender)
##
## Descriptive statistics by group
## group: f
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 2252 5.6 1.11 5.83 5.72 0.99 0 7 7 -1.18 1.74 0.02
## ------------------------------------------------------------
## group: m
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 770 5.41 1.16 5.58 5.52 1.11 1 7 6 -0.98 1.06 0.04
#also do across levels of second IV which is race
#for the two anova
describeBy(d2$support, group = d2$marriage_rc)
##
## Descriptive statistics by group
## group: married
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 2050 5.62 1.1 5.83 5.75 0.99 0 7 7 -1.22 1.92 0.02
## ------------------------------------------------------------
## group: not married
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 972 5.4 1.16 5.58 5.51 1.11 1 7 6 -0.96 0.94 0.04
# 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(DV~IV1, data = d)
#leveneTest(DV~IV1*IV2, data = d2)
leveneTest(support~gender*marriage_rc, data = d2)
## Levene's Test for Homogeneity of Variance (center = median)
## Df F value Pr(>F)
## group 3 2.3846 0.06737 .
## 3018
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
#this one came out to not significant !
# use the lm() command to run the regression
# formula is y~x1*x2, where y is our DV, x1 is our first IV and x2 is our second IV
reg_model2 <- lm(support~gender*marriage_rc, data = d2) #for two-way
# Cook's distance
plot(reg_model2, 4)
# Residuals vs Leverage
plot(reg_model2, 5)
We are way below the 0.5 cutoff for the cook’s distance test.
leveneTest came out as not significant so that’s all good.
In the residual vs leverage the red line hugged the dotted line well.
Our cell sizes are very unbalanced especially for gender. A small sample size for one of the levels of our variable limits our power and increases our Type II error rate.
#two way anova
aov_model2 <- aov_ez(data = d2,
id = "row_id",
between = c("gender","marriage_rc"),
dv = "support",
anova_table = list(es = "pes"))
## Contrasts set to contr.sum for the following variables: gender, marriage_rc
Effect size cutoffs from Cohen (1988):
nice(aov_model2)
## Anova Table (Type 3 tests)
##
## Response: support
## Effect df MSE F pes p.value
## 1 gender 1, 3018 1.25 15.45 *** .005 <.001
## 2 marriage_rc 1, 3018 1.25 22.34 *** .007 <.001
## 3 gender:marriage_rc 1, 3018 1.25 0.24 <.001 .627
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '+' 0.1 ' ' 1
#need to use post hoc test to figure out whats significantly diff
#for the two way anova
#afex_plot(aov_model2, x = "IV1", trace = "IV2")
#swap order of variable
#afex_plot(aov_model2, x = "IV2", trace = "IV1")
afex_plot(aov_model2, x = "gender", trace = "marriage_rc")
afex_plot(aov_model2, x = "marriage_rc", trace = "gender")
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_model2, specs="IV1", adjust="tukey")
#pairs(emmeans(aov_model2, specs="IV1", adjust="tukey"))
#pairs sees if comparison is signif, get p value
emmeans(aov_model2, specs="gender", adjust="tukey")
## NOTE: Results may be misleading due to involvement in interactions
## Note: adjust = "tukey" was changed to "sidak"
## because "tukey" is only appropriate for one set of pairwise comparisons
## gender emmean SE df lower.CL upper.CL
## f 5.56 0.0251 3018 5.50 5.61
## m 5.36 0.0436 3018 5.26 5.46
##
## Results are averaged over the levels of: marriage_rc
## Confidence level used: 0.95
## Conf-level adjustment: sidak method for 2 estimates
pairs(emmeans(aov_model2, specs="gender", adjust="tukey"))
## NOTE: Results may be misleading due to involvement in interactions
## contrast estimate SE df t.ratio p.value
## f - m 0.198 0.0503 3018 3.931 0.0001
##
## Results are averaged over the levels of: marriage_rc
#emmeans(aov_model2, specs="IV2", adjust="tukey")
#pairs(emmeans(aov_model2, specs="IV2", adjust="tukey"))
emmeans(aov_model2, specs="marriage_rc", adjust="tukey")
## NOTE: Results may be misleading due to involvement in interactions
## Note: adjust = "tukey" was changed to "sidak"
## because "tukey" is only appropriate for one set of pairwise comparisons
## marriage_rc emmean SE df lower.CL upper.CL
## married 5.58 0.0282 3018 5.52 5.64
## not married 5.34 0.0417 3018 5.25 5.43
##
## Results are averaged over the levels of: gender
## Confidence level used: 0.95
## Conf-level adjustment: sidak method for 2 estimates
pairs(emmeans(aov_model2, specs="marriage_rc", adjust="tukey"))
## NOTE: Results may be misleading due to involvement in interactions
## contrast estimate SE df t.ratio p.value
## married - not married 0.238 0.0503 3018 4.727 <.0001
##
## Results are averaged over the levels of: gender
#emmeans(aov_model2, specs="IV1", by="IV2", adjust="sidak")
#pairs(emmeans(aov_model2, specs="IV2", by="IV1", adjust="sidak"))
#looking at interaction
emmeans(aov_model2, specs="gender", by="marriage_rc", adjust="sidak")
## marriage_rc = married:
## gender emmean SE df lower.CL upper.CL
## f 5.66 0.0287 3018 5.60 5.73
## m 5.49 0.0485 3018 5.38 5.60
##
## marriage_rc = not married:
## gender emmean SE df lower.CL upper.CL
## f 5.45 0.0413 3018 5.36 5.54
## m 5.23 0.0725 3018 5.07 5.39
##
## Confidence level used: 0.95
## Conf-level adjustment: sidak method for 2 estimates
pairs(emmeans(aov_model2, specs="marriage_rc", by="gender", adjust="sidak"))
## gender = f:
## contrast estimate SE df t.ratio p.value
## married - not married 0.214 0.0503 3018 4.246 <.0001
##
## gender = m:
## contrast estimate SE df t.ratio p.value
## married - not married 0.262 0.0872 3018 3.008 0.0026
emmeans(aov_model2, specs="marriage_rc", by="gender", adjust="sidak")
## gender = f:
## marriage_rc emmean SE df lower.CL upper.CL
## married 5.66 0.0287 3018 5.60 5.73
## not married 5.45 0.0413 3018 5.36 5.54
##
## gender = m:
## marriage_rc emmean SE df lower.CL upper.CL
## married 5.49 0.0485 3018 5.38 5.60
## not married 5.23 0.0725 3018 5.07 5.39
##
## Confidence level used: 0.95
## Conf-level adjustment: sidak method for 2 estimates
pairs(emmeans(aov_model2, specs="marriage_rc", by="gender", adjust="sidak"))
## gender = f:
## contrast estimate SE df t.ratio p.value
## married - not married 0.214 0.0503 3018 4.246 <.0001
##
## gender = m:
## contrast estimate SE df t.ratio p.value
## married - not married 0.262 0.0872 3018 3.008 0.0026
```
#summerize hypothesis To test our hypothesis that gender and parent’s marriage status would impact support 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). There were no noticable outliers on the residuals vs leverage plots.
As predicted, we found a significant main effect for gender, REPORT STUFF FROM ANOVA- F(1, 3018) = 15.45, p < .001, ηp2 .01 (small effect size; Cohen, 1988). As predicted, women reported significantly more support than men. As predicted, people with married parents reported significantly more support than people with not married parents- F(1, 3018) = 22.34, p < .001, ηp2 .01 (small effect size; Cohen, 1988)
Lastly, we did not find a significant interaction between gender and parent’s marriage (p=.627).
References
Cohen J. (1988). Statistical Power Analysis for the Behavioral Sciences. New York, NY: Routledge Academic.