ANOVA HW

Author

Riley Midkiff

Loading Libraries

library(afex) # to run the ANOVA and plot results
library(psych) # for the describe() command
library(ggplot2) # to visualize our results
library(expss) # for the cross_cases() command
library(car) # for the leveneTest() command
library(emmeans) # for posthoc tests

Importing Data

d <- read.csv(file="Data/mydata.csv", header=T)
# 
# # new code! this adds a column with a number for each row. it acts as an identifier for our participations
d$row_id <- 1:nrow(d)

State Your Hypothesis - PART OF YOUR WRITEUP

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 ANOVA: Females will be higher in worry than males or people that use another term.

Check Your Assumptions

ANOVA Assumptions

  • Independence of observations (confirmed by data report)
  • All levels of the IVs should have equal number of cases (ideally; in the real world, this varies) and there should be no empty cells. Cells with low numbers increase chance of Type II error. (we will check this below)
  • Homogeneity of variance should be assured (we will check this below)
  • Outliers should be identified and removed (we will check this below)
  • DV should be normally distributed for each level of the IV (we will check this below)

Check levels of IVs

# # for a one-way ANOVA
# # COMMENT THIS SECTION OUR FROM THE HW IF YOU DO NOT USE IT
table(d$gender, useNA = "always")

            female I use another term               male  Prefer not to say 
               319                 23                 56                 10 
              <NA> 
                 0 
# # if you need to recode
# # to drop levels from your variable
# # this subsets the data and says that any participant who is coded as 'LEVEL BAD' should be removed
# # if you don't need this for the homework, comment it out (add a # at the beginning of the line)
d <- subset(d, gender != "Prefer not to say")

 # to combine levels
 # this says that where any participant is coded as 'LEVEL BAD' it should be replaced by 'LEVEL GOOD'
 # you can repeat this as needed, changing 'LEVEL BAD' if you have multiple levels that you want to combine into a single level
 # if you don't need this for the homework, comment it out (add a # at the beginning of the line)

# preview your changes and make sure everything is correct
table(d$gender, useNA = "always")

            female I use another term               male               <NA> 
               319                 23                 56                  0 
# check your variable types
str(d)
'data.frame':   398 obs. of  7 variables:
 $ gender   : chr  "female" "female" "female" "female" ...
 $ mhealth  : chr  "none or NA" "depression" "anxiety disorder" "anxiety disorder" ...
 $ big5_open: num  5.67 6.67 5.67 6 3.67 ...
 $ pswq     : num  -1.124 -0.183 1.697 1.227 0.663 ...
 $ mfq_state: num  4 4 2.75 3.12 3.12 ...
 $ brs      : num  4 2.5 1.17 2.33 2 ...
 $ row_id   : int  1 2 3 4 5 6 7 9 10 11 ...
# make sure that your IV is recognized as a factor by R
d$gender <- as.factor(d$gender)

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

# for a one-way ANOVA
# COMMENT THIS SECTION OUR FROM THE HW IF YOU DO NOT USE IT
leveneTest(pswq~gender, data = d)
Levene's Test for Homogeneity of Variance (center = median)
       Df F value Pr(>F)
group   2  2.2641 0.1053
      395               

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

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

# for a one-way ANOVA
# COMMENT THIS SECTION OUR FROM THE HW IF YOU DO NOT USE IT
reg_model <- lm(pswq ~ gender, data = d) #for one-way

Check for outliers

# for a one-way ANOVA
# COMMENT THIS SECTION OUR FROM THE HW IF YOU DO NOT USE IT
# Cook's distance - cutoff is .5 not .05!!
plot(reg_model, 4)

# Residuals vs Leverage -- look for wobble in red line or any participants outside the dashed lines for Cook's distance
plot(reg_model, 5)

Check Your Variables

# we'll use the describeBy() command to view skew and kurtosis across our IVs and make sure the DV is normally distributed across all of the levels
describeBy(d$pswq, group = d$gender)

 Descriptive statistics by group 
group: female
   vars   n mean   sd median trimmed  mad   min  max range  skew kurtosis   se
X1    1 319 0.33 0.92   0.47    0.39 0.98 -2.25 2.02  4.27 -0.53    -0.42 0.05
------------------------------------------------------------ 
group: I use another term
   vars  n mean   sd median trimmed mad   min max range  skew kurtosis   se
X1    1 23 0.65 0.75   0.57    0.73 0.7 -1.41 1.6  3.01 -0.85     0.35 0.16
------------------------------------------------------------ 
group: male
   vars  n  mean   sd median trimmed  mad   min max range skew kurtosis   se
X1    1 56 -0.25 1.04  -0.23   -0.29 1.18 -1.92 1.7  3.62 0.32    -1.03 0.14

Issues with My Data - PART OF YOUR WRITEUP

One-way ANOVA We dropped everyone but females, males, and people that prefer not to say. There was homogeneity of variance. There were a few outliers found in the residuals vs. leverage plot. Skew and kurtosis are in the normal range.

Run an ANOVA

# for a one-way ANOVA
# COMMENT THIS SECTION OUR FROM THE HW IF YOU DO NOT USE IT
aov_model <- aov_ez(data = d,
                    id = "row_id",
                    between = c("gender"),
                    dv = "pswq",
                    anova_table = list(es = "pes"))
Contrasts set to contr.sum for the following variables: gender

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
# for a one-way ANOVA
# COMMENT THIS SECTION OUR FROM THE HW IF YOU DO NOT USE IT
nice(aov_model)
Anova Table (Type 3 tests)

Response: pswq
  Effect     df  MSE         F  pes p.value
1 gender 2, 395 0.87 11.16 *** .053   <.001
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '+' 0.1 ' ' 1

Visualize Results

# for a one-way ANOVA
# COMMENT THIS SECTION OUR FROM THE HW IF YOU DO NOT USE IT
afex_plot(aov_model, x = "gender")

Run Posthoc Tests

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.

# for a one-way ANOVA
# COMMENT THIS SECTION OUR FROM THE HW IF YOU DO NOT USE IT
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
 female              0.330 0.0522 395    0.205   0.4553
 I use another term  0.651 0.1950 395    0.184   1.1171
 male               -0.247 0.1250 395   -0.546   0.0523

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
 female - I use another term   -0.321 0.201 395  -1.592  0.2502
 female - male                  0.577 0.135 395   4.267  0.0001
 I use another term - male      0.897 0.231 395   3.884  0.0004

P value adjustment: tukey method for comparing a family of 3 estimates 

Write Up Results

One-Way ANOVA

Females will be higher in worry than males or people that use another term. We dropped everyone but females, males, and people that prefer not to say. There was homogeneity of variance. There were a few outliers found in the residuals vs. leverage plot. Skew and kurtosis are in the normal range.

We did find a significant effect for gender (see figure 1): F(2,395) = 11.16, p = < .001, η2 < .05. The effect size indicates that is a small effect (Cohen J. (1988). Statistical Power Analysis for the Behavioral Sciences. New York, NY: Routledge Academic.). We found a significant interaction between the female and male group (p = .0001). We also found another significant interaction between I use another term and male group (p = .0004).

References

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