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 select columns from data: columns(mtcars, mpg, vs:carb)
## 
## 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/mydatafinal.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 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 and race/ethnicity on maturity, as measured by the perceived maturity scale (MoA). We also predict that race/ethnicity and gender will interact and that women of color will report significantly higher maturity than men of color or white men and women.

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':    3182 obs. of  8 variables:
##  $ ResponseId  : chr  "R_BJN3bQqi1zUMid3" "R_2TGbiBXmAtxywsD" "R_12G7bIqN2wB2N65" "R_39pldNoon8CePfP" ...
##  $ gender      : chr  "f" "m" "m" "f" ...
##  $ race_rc     : chr  "white" "white" "white" "other" ...
##  $ moa_maturity: num  3.67 3.33 3.67 3 3.67 ...
##  $ idea        : num  3.75 3.88 3.75 3.75 3.5 ...
##  $ belong      : num  2.8 4.2 3.6 4 3.4 4.2 3.9 3.6 2.9 2.5 ...
##  $ 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 ...
d$ResponseId <- as.factor(d$ResponseId)
# make our categorical variables factors
d$gender <- as.factor(d$gender)
d$race_rc <- as.factor(d$race_rc)
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$race_rc)
## 
##       asian       black    hispanic multiracial  nativeamer       other 
##         210         249         286         293          12          97 
##       white 
##        2026
d$poc[d$race_rc == "asian"] <- "poc"
d$poc[d$race_rc == "black"] <- "poc"
d$poc[d$race_rc == "mideast"] <- "poc"
d$poc[d$race_rc == "multiracial"] <- "poc"
d$poc[d$race_rc == "other"] <- "poc"
d$poc[d$race_rc == "prefer_not"] <- NA
d$poc[d$race_rc == "white"] <- "white"
table(d$poc)
## 
##   poc white 
##   849  2026
d$poc <- as.factor(d$poc)

# 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 3146 3.59 0.43   3.67    3.65 0.49   1   4     3 -1.2     1.87 0.01
# we'll use the describeBy() command to view skew and kurtosis across our IVs
describeBy(d$moa_maturity, group = d$gender)
## 
##  Descriptive statistics by group 
## group: f
##    vars    n mean   sd median trimmed  mad min max range  skew kurtosis   se
## X1    1 2303 3.61 0.42   3.67    3.67 0.49   1   4     3 -1.22     1.94 0.01
## ------------------------------------------------------------ 
## group: m
##    vars   n mean   sd median trimmed  mad  min max range  skew kurtosis   se
## X1    1 785 3.54 0.46   3.67     3.6 0.49 1.33   4  2.67 -1.16     1.65 0.02
## ------------------------------------------------------------ 
## group: nb
##    vars  n mean  sd median trimmed  mad  min max range skew kurtosis   se
## X1    1 54  3.5 0.4   3.33    3.53 0.49 2.33   4  1.67 -0.5    -0.13 0.05
describeBy(d$moa_maturity, group = d$poc)
## 
##  Descriptive statistics by group 
## group: poc
##    vars   n mean   sd median trimmed  mad  min max range  skew kurtosis   se
## X1    1 833  3.6 0.44   3.67    3.67 0.49 1.33   4  2.67 -1.25     1.72 0.02
## ------------------------------------------------------------ 
## group: white
##    vars    n mean   sd median trimmed  mad min max range  skew kurtosis   se
## X1    1 2014 3.58 0.42   3.67    3.63 0.49   1   4     3 -1.15        2 0.01
describeBy(d$moa_maturity, group = d$race_rc)
## 
##  Descriptive statistics by group 
## group: asian
##    vars   n mean   sd median trimmed  mad  min max range  skew kurtosis   se
## X1    1 207 3.61 0.44   3.67    3.66 0.49 1.33   4  2.67 -1.29     2.58 0.03
## ------------------------------------------------------------ 
## group: black
##    vars   n mean   sd median trimmed  mad  min max range skew kurtosis   se
## X1    1 242 3.63 0.46   3.67     3.7 0.49 1.67   4  2.33 -1.5     2.42 0.03
## ------------------------------------------------------------ 
## group: hispanic
##    vars   n mean   sd median trimmed  mad  min max range  skew kurtosis   se
## X1    1 281 3.63 0.45   3.67    3.71 0.49 1.67   4  2.33 -1.39     1.87 0.03
## ------------------------------------------------------------ 
## group: multiracial
##    vars   n mean   sd median trimmed  mad min max range  skew kurtosis   se
## X1    1 288 3.59 0.44   3.67    3.66 0.49   2   4     2 -1.11      0.9 0.03
## ------------------------------------------------------------ 
## group: nativeamer
##    vars  n mean   sd median trimmed mad  min max range  skew kurtosis  se
## X1    1 10  3.6 0.62      4    3.71   0 2.33   4  1.67 -1.02    -0.71 0.2
## ------------------------------------------------------------ 
## group: other
##    vars  n mean   sd median trimmed  mad min max range  skew kurtosis   se
## X1    1 96 3.58 0.44   3.67    3.62 0.49   2   4     2 -0.86     0.27 0.04
## ------------------------------------------------------------ 
## group: white
##    vars    n mean   sd median trimmed  mad min max range  skew kurtosis   se
## X1    1 2014 3.58 0.42   3.67    3.63 0.49   1   4     3 -1.15        2 0.01
# also use histograms to examine your continuous variable
hist(d$moa_maturity)

# and cross_cases() to examine your categorical variables
cross_cases(d, gender, poc)
 poc 
 poc   white 
 gender 
   f  630 1480
   m  205 508
   nb  14 38
   #Total cases  849 2026

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

table(d$gender)
## 
##    f    m   nb 
## 2332  792   54
cross_cases(d, gender, poc)
 poc 
 poc   white 
 gender 
   f  630 1480
   m  205 508
   nb  14 38
   #Total cases  849 2026
# 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, poc)
 poc 
 poc   white 
 gender 
   f  630 1480
   m  205 508
   #Total cases  835 1988

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(moa_maturity~gender, data = d)
## Levene's Test for Homogeneity of Variance (center = median)
##         Df F value  Pr(>F)  
## group    2  3.9717 0.01894 *
##       3139                  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
leveneTest(moa_maturity~gender*poc, data = d2)
## Levene's Test for Homogeneity of Variance (center = median)
##         Df F value Pr(>F)  
## group    3  2.7238 0.0428 *
##       2791                 
## ---
## 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) & 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 ~ gender, data = d) #for one-way
reg_model2 <- lm(moa_maturity ~ gender*poc, data = d2) #for two-way

5.1.3.2 Check for outliers (One-Way)

# Cook's distance
plot(reg_model, 4)

# Residuals vs Leverage
plot(reg_model, 5)

5.1.3.3 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.

We identified and removed a single outlier.

6 Run an ANOVA

aov_model <- aov_ez(data = d,
                    id = "ResponseId",
                    between = c("gender"),
                    dv = "moa_maturity",
                    anova_table = list(es = "pes"))
## Warning: Missing values for 40 ID(s), which were removed before analysis:
## R_0dNdRm6ew3q0NTL, R_10AR1BhlKdqLzd9, R_10OTSb3qKpsUpgY, R_10u6G17JITY60xl, R_1Cl7ONhtvJCpssB, R_1EiEtm2ZNl7dQHY, R_1EihKePhUenEL9L, R_1F3eLUtnUqenxLi, R_1f9Fkx8HFLhhUaI, R_1n1I7EcP36r128r, ... [showing first 10 only]
## Below the first few rows (in wide format) of the removed cases with missing data.
##              ResponseId gender  .
## # 17  R_0dNdRm6ew3q0NTL      f NA
## # 45  R_10AR1BhlKdqLzd9      f NA
## # 55  R_10OTSb3qKpsUpgY      m NA
## # 63  R_10u6G17JITY60xl      f NA
## # 132 R_1Cl7ONhtvJCpssB      f NA
## # 213 R_1EiEtm2ZNl7dQHY      f NA
## Contrasts set to contr.sum for the following variables: gender
aov_model2 <- aov_ez(data = d2,
                    id = "ResponseId",
                    between = c("gender","poc"),
                    dv = "moa_maturity",
                    anova_table = list(es = "pes"))
## Warning: Missing values for 329 ID(s), which were removed before analysis:
## R_0AnPM5x7wfwX1bf, R_0Cj1m8a7TIGCjwl, R_0CJcqtXWnOo8uBz, R_0dNdRm6ew3q0NTL, R_10AR1BhlKdqLzd9, R_10OTSb3qKpsUpgY, R_10u6G17JITY60xl, R_10uS04Ubp4IwfYE, R_125z5zdnETjS0jd, R_126hlQCkLu4yrOw, ... [showing first 10 only]
## Below the first few rows (in wide format) of the removed cases with missing data.
##             ResponseId gender  poc        .
## # 7  R_0AnPM5x7wfwX1bf      m <NA> 4.000000
## # 11 R_0Cj1m8a7TIGCjwl      f <NA> 4.000000
## # 12 R_0CJcqtXWnOo8uBz      m <NA> 3.666667
## # 17 R_0dNdRm6ew3q0NTL      f <NA>       NA
## # 45 R_10AR1BhlKdqLzd9      f <NA>       NA
## # 54 R_10OTSb3qKpsUpgY      m <NA>       NA
## Contrasts set to contr.sum for the following variables: gender, poc

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_model)
## Anova Table (Type 3 tests)
## 
## Response: moa_maturity
##   Effect      df  MSE        F  pes p.value
## 1 gender 2, 3138 0.19 8.70 *** .006   <.001
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '+' 0.1 ' ' 1
nice(aov_model2)
## Anova Table (Type 3 tests)
## 
## Response: moa_maturity
##       Effect      df  MSE       F   pes p.value
## 1     gender 1, 2791 0.18 9.40 **  .003    .002
## 2        poc 1, 2791 0.18    1.47 <.001    .226
## 3 gender:poc 1, 2791 0.18    0.17 <.001    .684
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '+' 0.1 ' ' 1

8 Visualize Results

afex_plot(aov_model, x = "gender")

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

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


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


```r
emmeans(aov_model, specs="gender", adjust="tukey")
## 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       3.610 0.008999 3138    3.588    3.631
##  m       3.541 0.015410 3138    3.504    3.577
##  nb      3.500 0.058755 3138    3.360    3.640
## 
## Confidence level used: 0.95 
## Conf-level adjustment: sidak method for 3 estimates
pairs(emmeans(aov_model, specs="gender", adjust="tukey"))
##  contrast estimate     SE   df t.ratio p.value
##  f - m      0.0691 0.0178 3138   3.870  0.0003
##  f - nb     0.1096 0.0594 3138   1.844  0.1555
##  m - nb     0.0406 0.0607 3138   0.668  0.7823
## 
## P value adjustment: tukey method for comparing a family of 3 estimates
emmeans(aov_model2, specs="poc", 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
##  poc   emmean     SE   df lower.CL upper.CL
##  poc     3.59 0.0174 2791     3.55     3.63
##  white   3.57 0.0111 2791     3.54     3.59
## 
## 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="poc", adjust="tukey"))
## NOTE: Results may be misleading due to involvement in interactions
##  contrast    estimate     SE   df t.ratio p.value
##  poc - white   0.0249 0.0206 2791   1.210  0.2262
## 
## Results are averaged over the levels of: gender
emmeans(aov_model2, specs="gender", by="poc", adjust="sidak")
## poc = poc:
##  gender emmean     SE   df lower.CL upper.CL
##  f        3.62 0.0173 2791     3.58     3.66
##  m        3.56 0.0302 2791     3.50     3.63
## 
## poc = white:
##  gender emmean     SE   df lower.CL upper.CL
##  f        3.60 0.0112 2791     3.58     3.63
##  m        3.53 0.0191 2791     3.49     3.57
## 
## Confidence level used: 0.95 
## Conf-level adjustment: sidak method for 2 estimates
pairs(emmeans(aov_model2, specs="poc", by="gender", adjust="sidak"))
## gender = f:
##  contrast    estimate     SE   df t.ratio p.value
##  poc - white   0.0166 0.0206 2791   0.804  0.4212
## 
## gender = m:
##  contrast    estimate     SE   df t.ratio p.value
##  poc - white   0.0333 0.0357 2791   0.933  0.3507
emmeans(aov_model2, specs="poc", by="gender", adjust="sidak")
## gender = f:
##  poc   emmean     SE   df lower.CL upper.CL
##  poc     3.62 0.0173 2791     3.58     3.66
##  white   3.60 0.0112 2791     3.58     3.63
## 
## gender = m:
##  poc   emmean     SE   df lower.CL upper.CL
##  poc     3.56 0.0302 2791     3.50     3.63
##  white   3.53 0.0191 2791     3.49     3.57
## 
## Confidence level used: 0.95 
## Conf-level adjustment: sidak method for 2 estimates
pairs(emmeans(aov_model2, specs="poc", by="gender", adjust="sidak"))
## gender = f:
##  contrast    estimate     SE   df t.ratio p.value
##  poc - white   0.0166 0.0206 2791   0.804  0.4212
## 
## gender = m:
##  contrast    estimate     SE   df t.ratio p.value
##  poc - white   0.0333 0.0357 2791   0.933  0.3507

9 Write Up Results

9.1 Two-Way ANOVA

To test our hypothesis that gender and race would impact maturity 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 = 2332) than men (n = 792).

As predicted, we found a significant main effect for gender, F(1,2791) = 9.40, p < .001, ηp2 .017 (small effect size; Cohen, 1988). As predicted, women reported significantly more maturity than men. Contrary to our expectations, we did not find a significant main effect for race (p = .226).

Lastly, we found a significant interaction between gender and race (see Figure 2), F(1,2791) = 0.17, p = .064, ηp2 < .001 (trivial effect size; Cohen, 1988). When comparing by race, women of color (M = 3.62, SE = .02) reported significantly more maturity than men of color (M = 3.56, SE = .03; p < .001), as did white women (M = 3.60, SE = .01) compared to white men (M = 3.53, SE = .02; p < .001). When comparing by gender, women of color reported significantly more maturity than white women (p = .002), while men of color and white men reported similar levels of maturity (p = .351).

References

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