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
## 
## Use 'expss_output_viewer()' to display tables in the RStudio Viewer.
##  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'

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 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 race on stress, as measured by the perceived stress scale (PSS-4).

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':    2152 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" ...
##  $ belong : num  2.8 4.2 3.6 4 3.4 4.2 3.9 3.6 2.9 2.5 ...
##  $ npi    : num  0.6923 0.1538 0.0769 0.0769 0.7692 ...
##  $ stress : num  3.3 3.3 4 3.2 3.1 3.5 3.3 2.4 2.9 2.7 ...
##  $ exploit: num  2 3.67 4.33 1.67 4 ...
##  $ 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'll actually use our ID variable for this analysis, so make sure it's coded as a factor
d$race_rc <- as.factor(d$race_rc)
d$age <- as.factor(d$age)
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 
##         143         185         230         209           8          80 
##       white 
##        1297
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 
##   617  1297
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$stress)
##    vars    n mean  sd median trimmed  mad min max range  skew kurtosis   se
## X1    1 2152 3.07 0.6    3.1    3.07 0.59 1.3 4.6   3.3 -0.02    -0.15 0.01
# we'll use the describeBy() command to view skew and kurtosis across our IVs
describeBy(d$stress, 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 143 3.13 0.58    3.1    3.11 0.59 1.7 4.6   2.9 0.26    -0.07 0.05
## ------------------------------------------------------------ 
## group: black
##    vars   n mean   sd median trimmed  mad min max range skew kurtosis   se
## X1    1 185  3.1 0.53    3.1    3.09 0.44 1.8 4.6   2.8 0.17     0.25 0.04
## ------------------------------------------------------------ 
## group: hispanic
##    vars   n mean   sd median trimmed  mad min max range skew kurtosis   se
## X1    1 230 3.09 0.63    3.1    3.08 0.59 1.4 4.6   3.2 0.06    -0.05 0.04
## ------------------------------------------------------------ 
## group: multiracial
##    vars   n mean   sd median trimmed  mad min max range  skew kurtosis   se
## X1    1 209 3.09 0.58    3.1    3.09 0.59 1.6 4.4   2.8 -0.14    -0.23 0.04
## ------------------------------------------------------------ 
## group: nativeamer
##    vars n mean   sd median trimmed mad min max range skew kurtosis  se
## X1    1 8 3.01 0.56   2.85    3.01 0.3 2.5 4.2   1.7 1.05    -0.28 0.2
## ------------------------------------------------------------ 
## group: other
##    vars  n mean   sd median trimmed  mad min max range skew kurtosis   se
## X1    1 80    3 0.59      3       3 0.44 1.5 4.6   3.1  0.1     0.47 0.07
## ------------------------------------------------------------ 
## group: white
##    vars    n mean   sd median trimmed  mad min max range  skew kurtosis   se
## X1    1 1297 3.05 0.61    3.1    3.06 0.59 1.3 4.6   3.3 -0.06    -0.31 0.02
describeBy(d$stress, group = d$poc)
## 
##  Descriptive statistics by group 
## group: poc
##    vars   n mean   sd median trimmed  mad min max range skew kurtosis   se
## X1    1 617 3.09 0.57    3.1    3.09 0.44 1.5 4.6   3.1 0.06     0.09 0.02
## ------------------------------------------------------------ 
## group: white
##    vars    n mean   sd median trimmed  mad min max range  skew kurtosis   se
## X1    1 1297 3.05 0.61    3.1    3.06 0.59 1.3 4.6   3.3 -0.06    -0.31 0.02
# also use histograms to examine your continuous variable
hist(d$stress)

# and cross_cases() to examine your categorical variables
cross_cases(d, race_rc, poc)
 poc 
 poc   white 
 race_rc 
   asian  143
   black  185
   hispanic 
   multiracial  209
   nativeamer 
   other  80
   white  1297
   #Total cases  617 1297

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$race_rc)
## 
##       asian       black    hispanic multiracial  nativeamer       other 
##         143         185         230         209           8          80 
##       white 
##        1297
# 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, race_rc != "nativeamer")
d2$race_rc <- droplevels(d2$race_rc)

# to double-check any changes we made
cross_cases(d2, race_rc, poc)
 poc 
 poc   white 
 race_rc 
   asian  143
   black  185
   hispanic 
   multiracial  209
   other  80
   white  1297
   #Total cases  617 1297

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(stress~race_rc, data = d)
## Levene's Test for Homogeneity of Variance (center = median)
##         Df F value  Pr(>F)  
## group    6  1.9777 0.06553 .
##       2145                  
## ---
## 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(1530))

# to drop multiple outliers, remove the # at the beginning of the line and use this code:
 d <- subset(d, row_id!=c(1530) & row_id!=c(70)) 

# 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(stress ~ race_rc, data = d) #for one-way
reg_model2 <- lm(stress ~ race_rc*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.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 two outliers.

6 Run an ANOVA

aov_model <- aov_ez(data = d,
                    id = "row_id",
                    between = c("race_rc"),
                    dv = "stress",
                    anova_table = list(es = "pes"))
## Contrasts set to contr.sum for the following variables: race_rc

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: stress
##    Effect      df  MSE    F  pes p.value
## 1 race_rc 6, 2143 0.36 1.03 .003    .402
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '+' 0.1 ' ' 1

8 Visualize Results

afex_plot(aov_model, x = "race_rc")

9 Run Posthoc Tests (One-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_model, specs="race_rc", adjust="tukey")
## Note: adjust = "tukey" was changed to "sidak"
## because "tukey" is only appropriate for one set of pairwise comparisons
##  race_rc     emmean     SE   df lower.CL upper.CL
##  asian         3.13 0.0499 2143     3.00     3.27
##  black         3.10 0.0439 2143     2.98     3.22
##  hispanic      3.09 0.0394 2143     2.98     3.19
##  multiracial   3.09 0.0413 2143     2.98     3.20
##  nativeamer    2.75 0.2438 2143     2.10     3.40
##  other         3.00 0.0668 2143     2.82     3.18
##  white         3.05 0.0166 2143     3.01     3.10
## 
## Confidence level used: 0.95 
## Conf-level adjustment: sidak method for 7 estimates
pairs(emmeans(aov_model, specs="race_rc", adjust="tukey"))
##  contrast                 estimate     SE   df t.ratio p.value
##  asian - black             0.03573 0.0665 2143   0.537  0.9983
##  asian - hispanic          0.04835 0.0636 2143   0.760  0.9885
##  asian - multiracial       0.04553 0.0648 2143   0.702  0.9925
##  asian - nativeamer        0.38357 0.2489 2143   1.541  0.7197
##  asian - other             0.13232 0.0834 2143   1.587  0.6908
##  asian - white             0.08137 0.0526 2143   1.546  0.7166
##  black - hispanic          0.01262 0.0590 2143   0.214  1.0000
##  black - multiracial       0.00980 0.0603 2143   0.163  1.0000
##  black - nativeamer        0.34784 0.2477 2143   1.404  0.7998
##  black - other             0.09659 0.0799 2143   1.209  0.8910
##  black - white             0.04564 0.0469 2143   0.972  0.9599
##  hispanic - multiracial   -0.00282 0.0571 2143  -0.049  1.0000
##  hispanic - nativeamer     0.33522 0.2470 2143   1.357  0.8243
##  hispanic - other          0.08397 0.0775 2143   1.083  0.9331
##  hispanic - white          0.03302 0.0427 2143   0.773  0.9875
##  multiracial - nativeamer  0.33804 0.2473 2143   1.367  0.8194
##  multiracial - other       0.08679 0.0785 2143   1.105  0.9266
##  multiracial - white       0.03584 0.0445 2143   0.805  0.9845
##  nativeamer - other       -0.25125 0.2528 2143  -0.994  0.9554
##  nativeamer - white       -0.30220 0.2444 2143  -1.237  0.8798
##  other - white            -0.05095 0.0688 2143  -0.741  0.9900
## 
## P value adjustment: tukey method for comparing a family of 7 estimates

10 Write Up Results

10.1 One-Way ANOVA

To test our hypothesis that there would be a significant effect of race on stress, we used a one-way ANOVA. Our data was unbalanced, with many more white participants in our survey (n = 1297) than asiaj (n = 143), hispanic (n = 230), black (n = 185), multiracial (n = 209), or other (n = 80). This significantly reduces the power of our test and increases the chances of a Type II error. We also identified and removed two outliers following visual analysis of a Residuals vs Leverage plot. A significant Levene’s test (p = .402) 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 race, F(2143) = 1.03, p .402, ηp2 = .042 (large effect size; Cohen, 1988). Posthoc tests using Tukey’s HSD revealed that white people reported more stress than other races, asians reported the least stress overall (see Figure 1 for a comparison).

References

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