1 Loading Libraries

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
## 
## 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'

2 Importing Data

# 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)

3 State Your Hypothesis

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.

We predict that there will significant effects of gender and marriage status of parents on satisfaction with life. We also predict that marriage status of parents and gender will interact and that men and women from married parent families will report significantly higher life satisfaction than men and women from non-married parent families.

4 Check Your Variables

# 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" ...
##  $ moa_role    : num  3 2.67 2.5 2 2.67 ...
##  $ moa_maturity: num  3.67 3.33 3.67 3 3.67 ...
##  $ swb         : num  4.33 4.17 1.83 5.17 3.67 ...
##  $ belong      : num  2.8 4.2 3.6 4 3.4 4.2 3.9 3.6 2.9 2.5 ...
##  $ row_id      : int  1 2 3 4 5 6 7 8 9 10 ...
# make our categorical variables factors
d$marriage5 <- as.factor(d$marriage5) #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$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 
##                                                 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$msp[d$marriage5 == "are currently divorced from one another"] <- "not_married"
d$msp[d$marriage5 == "never married each other and are not together"] <- "not_married"
d$msp[d$marriage5 == "never married each other but are currently together"] <- "not_married"
d$msp[d$marriage5 == "are currently married to one another"] <- "married"
table(d$msp)
## 
##     married not_married 
##        2084         991
d$msp <- as.factor(d$msp)

# 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 3075 4.47 1.32   4.67    4.53 1.48   1   7     6 -0.37    -0.45 0.02
# we'll use the describeBy() command to view skew and kurtosis across our IVs
describeBy(d$swb, group = d$gender)
## 
##  Descriptive statistics by group 
## group: f
##    vars    n mean  sd median trimmed  mad min max range  skew kurtosis   se
## X1    1 2252 4.47 1.3    4.5    4.54 1.48   1   7     6 -0.39    -0.44 0.03
## ------------------------------------------------------------ 
## group: m
##    vars   n mean   sd median trimmed  mad min max range  skew kurtosis   se
## X1    1 770 4.52 1.37   4.67    4.57 1.48   1   7     6 -0.36    -0.46 0.05
## ------------------------------------------------------------ 
## group: nb
##    vars  n mean  sd median trimmed  mad min  max range  skew kurtosis   se
## X1    1 53 3.72 1.3   3.67    3.74 1.48   1 6.83  5.83 -0.02    -0.66 0.18
describeBy(d$swb, group = d$msp)
## 
##  Descriptive statistics by group 
## group: married
##    vars    n mean  sd median trimmed  mad min max range  skew kurtosis   se
## X1    1 2084 4.59 1.3   4.83    4.66 1.24   1   7     6 -0.43    -0.39 0.03
## ------------------------------------------------------------ 
## group: not_married
##    vars   n mean   sd median trimmed  mad min max range  skew kurtosis   se
## X1    1 991 4.22 1.33   4.33    4.26 1.48   1   7     6 -0.25    -0.49 0.04
# also use histograms to examine your continuous variable
hist(d$swb)

# and cross_cases() to examine your categorical variables
cross_cases(d, gender, msp)
 msp 
 married   not_married 
 gender 
   f  1518 734
   m  532 238
   nb  34 19
   #Total cases  2084 991

5 Check Your Assumptions

5.1 ANOVA Assumptions

  • DV should be normally distributed across levels of the IV
  • All levels of the IVs should have equal number of cases and there should be no empty cells. Cells with low numbers decrease the power of the test (increase change of Type II error)
  • Homogeneity of variance should be assured
  • Outliers should be identified and removed
  • If you have confirmed everything about, the sampling distribution should be normal. (For a demonstration of what the sampling distribution is, go here.)

5.1.1 Check levels of IVs

#two way
cross_cases(d, gender, msp)
 msp 
 married   not_married 
 gender 
   f  1518 734
   m  532 238
   nb  34 19
   #Total cases  2084 991
# 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 != "nb")
d2$gender <- droplevels(d2$gender)

# to double-check any changes we made
cross_cases(d2, gender, msp)
 msp 
 married   not_married 
 gender 
   f  1518 734
   m  532 238
   #Total cases  2050 972

5.1.2 Check homogeneity of variance

# 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~gender*msp, data = d2)
## Levene's Test for Homogeneity of Variance (center = median)
##         Df F value  Pr(>F)  
## group    3   2.913 0.03314 *
##       3018                  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

5.1.3 Check for outliers using Cook’s distance and Residuals vs Leverage plot

5.1.3.1 Run a Regression

# 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_model2 <- lm(swb ~ gender*msp, data = d2) #for two-way

5.1.3.2 Check for outliers (Two-Way)

# Cook's distance
plot(reg_model2, 4)

# Residuals vs Leverage
plot(reg_model2, 5)

5.2 Issues with My Data

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.

6 Run an ANOVA

aov_model2 <- aov_ez(data = d2,
                    id = "row_id",
                    between = c("gender","msp"),
                    dv = "swb",
                    anova_table = list(es = "pes"))
## Contrasts set to contr.sum for the following variables: gender, msp

7 View Output

Effect size cutoffs from Cohen (1988):

  • η2 = 0.01 indicates a small effect
  • η2 = 0.06 indicates a medium effect
  • η2 = 0.14 indicates a large effect
nice(aov_model2)
## Anova Table (Type 3 tests)
## 
## Response: swb
##       Effect      df  MSE         F   pes p.value
## 1     gender 1, 3018 1.71      0.25 <.001    .619
## 2        msp 1, 3018 1.71 44.81 ***  .015   <.001
## 3 gender:msp 1, 3018 1.71      0.21 <.001    .647
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '+' 0.1 ' ' 1

8 Visualize Results

afex_plot(aov_model2, x = "gender", trace = "msp")

afex_plot(aov_model2, x = "msp", trace = "gender")

9 Run Posthoc Tests (Two-Way)

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="msp", 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
##  msp         emmean     SE   df lower.CL upper.CL
##  married       4.62 0.0329 3018     4.55     4.69
##  not_married   4.23 0.0488 3018     4.12     4.34
## 
## 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="msp", adjust="tukey"))
## NOTE: Results may be misleading due to involvement in interactions
##  contrast              estimate     SE   df t.ratio p.value
##  married - not_married    0.394 0.0588 3018   6.694  <.0001
## 
## Results are averaged over the levels of: gender
emmeans(aov_model2, specs="gender", by="msp", adjust="sidak")
## msp = married:
##  gender emmean     SE   df lower.CL upper.CL
##  f        4.59 0.0336 3018     4.52     4.67
##  m        4.65 0.0567 3018     4.52     4.78
## 
## msp = not_married:
##  gender emmean     SE   df lower.CL upper.CL
##  f        4.23 0.0483 3018     4.12     4.33
##  m        4.23 0.0848 3018     4.04     4.42
## 
## Confidence level used: 0.95 
## Conf-level adjustment: sidak method for 2 estimates
pairs(emmeans(aov_model2, specs="gender", by="msp", adjust="sidak"))
## msp = married:
##  contrast estimate     SE   df t.ratio p.value
##  f - m    -0.05627 0.0659 3018  -0.854  0.3931
## 
## msp = not_married:
##  contrast estimate     SE   df t.ratio p.value
##  f - m    -0.00234 0.0975 3018  -0.024  0.9809
emmeans(aov_model2, specs="msp", by="gender", adjust="sidak")
## gender = f:
##  msp         emmean     SE   df lower.CL upper.CL
##  married       4.59 0.0336 3018     4.52     4.67
##  not_married   4.23 0.0483 3018     4.12     4.33
## 
## gender = m:
##  msp         emmean     SE   df lower.CL upper.CL
##  married       4.65 0.0567 3018     4.52     4.78
##  not_married   4.23 0.0848 3018     4.04     4.42
## 
## Confidence level used: 0.95 
## Conf-level adjustment: sidak method for 2 estimates
pairs(emmeans(aov_model2, specs="msp", by="gender", adjust="sidak"))
## gender = f:
##  contrast              estimate     SE   df t.ratio p.value
##  married - not_married    0.367 0.0588 3018   6.243  <.0001
## 
## gender = m:
##  contrast              estimate     SE   df t.ratio p.value
##  married - not_married    0.421 0.1020 3018   4.128  <.0001

10 Write Up Results

10.1 Two-Way ANOVA

To test our hypothesis that gender and marriage status of parents would impact life satisfaction 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 = 2252) than men (n = 770).

As predicted, we found a significant main effect for marriage staus of parents, F(1,3018) = 44.81, p < .001, ηp2 .015 (small effect size; Cohen, 1988). As predicted, those with married parents reported significantly more life satisfaction than those with unmarried parents. Contrary to our expectations, we did not find a significant main effect for gender (p = .619).

Lastly, we did not find a significant interaction between gender and marriage status of parents (see Figure 2) (p = .647). Males and females reported similar life satisfaction from both married parent families (p = .393) or non-married parent families (p = .981).

References

Cohen J. (1988). Statistical Power Analysis for the Behavioral Sciences. New York, NY: Routledge Academic.