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(afex) # to run the ANOVA and plot results
library(emmeans) # for posthoc testsANOVA Lab
Loading Libraries
Importing Data
d <- read.csv(file="Data/labdata.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: Cat Owners will be significantly higher in depression (measured by the PHQ) when compared to dog owners and people without pets.
Two-way ANOVA: Pet owners will have significantly lower depression scores (measured by the PHQ) than non-pet owners. Participants without mental health diagnoses will have significantly lower depression scores than participants with mental health diagnoses. Pet owners with mental health diagnoses will report significantly lower depression scores than non-pet owners with mental health diagnoses.
State your hypotheses. Remember, your DV will be a continuous variable. For your IV, you need either one categorical variable with three levels, or two categorical variables with at least two levels each.
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$pet, useNA = "always")
bird cat cat and dog
5 211 136
dog fish multiple types of pet
246 35 104
no pets other <NA>
396 68 0
d2 <- d
# # for a two-way ANOVA
# # COMMENT THIS SECTION OUR FROM THE HW IF YOU DO NOT USE IT
cross_cases(d2, pet, mhealth)| mhealth | ||||||||
|---|---|---|---|---|---|---|---|---|
| anxiety disorder | bipolar | depression | eating disorders | none or NA | obsessive compulsive disorder | other | ptsd | |
| pet | ||||||||
| bird | 1 | 1 | 3 | |||||
| cat | 29 | 1 | 4 | 3 | 164 | 4 | 3 | 3 |
| cat and dog | 19 | 1 | 6 | 99 | 4 | 5 | 2 | |
| dog | 32 | 3 | 12 | 6 | 176 | 4 | 5 | 8 |
| fish | 3 | 1 | 30 | 1 | ||||
| multiple types of pet | 12 | 3 | 3 | 76 | 2 | 4 | 4 | |
| no pets | 25 | 1 | 8 | 9 | 326 | 9 | 14 | 4 |
| other | 7 | 2 | 53 | 2 | 3 | 1 | ||
| #Total cases | 128 | 5 | 31 | 28 | 927 | 26 | 34 | 22 |
#
# # 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)
# # 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$pet, useNA = "always")
bird cat cat and dog
5 211 136
dog fish multiple types of pet
246 35 104
no pets other <NA>
396 68 0
# # or
# cross_cases(d, IV1, IV2)
#
# # check your variable types
# str(d)
#
# # make sure that your IV is recognized as a factor by R
# VARIABLE <- as.factor(VARIABLE)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(DV~IV1, data = d)
#
# # for a two-way ANOVA
# # COMMENT THIS SECTION OUR FROM THE HW IF YOU DO NOT USE IT
# leveneTest(DV~IV1*IV2, data = d)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(DV ~ IV1, data = d) #for one-way
#
# # for a two-way ANOVA
# # COMMENT THIS SECTION OUR FROM THE HW IF YOU DO NOT USE IT
# reg_model2 <- lm(DV ~ IV1*IV2, data = d) #for two-wayCheck for outliers
# # for a one-way ANOVA
# # COMMENT THIS SECTION OUR FROM THE HW IF YOU DO NOT USE IT
# # Cook's distance
# plot(reg_model, 4)
#
# # Residuals vs Leverage
# plot(reg_model, 5)
#
# # for a two-way ANOVA
# # COMMENT THIS SECTION OUR FROM THE HW IF YOU DO NOT USE IT
# # Cook's distance
# plot(reg_model2, 4)
#
# # Residuals vs Leverage
# plot(reg_model2, 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(YOUR DV, group = YOUR IV 1)Issues with My Data - PART OF YOUR WRITEUP
Briefly describe any issues with your data and how they might impact the interpretation of your results. As usual, this should be written in an appropriate scientific tone.
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 = "X",
# between = c("IV1"),
# dv = "pss",
# anova_table = list(es = "pes"))
#
# # for a two-way ANOVA
# # COMMENT THIS SECTION OUR FROM THE HW IF YOU DO NOT USE IT
# aov_model2 <- aov_ez(data = d,
# id = "X",
# between = c("IV1","IV2"),
# dv = "pss",
# anova_table = list(es = "pes"))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)
#
# # for a two-way ANOVA
# # COMMENT THIS SECTION OUR FROM THE HW IF YOU DO NOT USE IT
# nice(aov_model2)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 = "IV1")
#
# # for a two-way ANOVA
# # COMMENT THIS SECTION OUR FROM THE HW IF YOU DO NOT USE IT
# afex_plot(aov_model2, x = "IV1", trace = "IV2")
# afex_plot(aov_model2, x = "IV2", trace = "IV1")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="IV1", adjust="tukey")
# pairs(emmeans(aov_model, specs="IV1", adjust="tukey"))
#
# # for a two-way ANOVA
# # COMMENT THIS SECTION OUR FROM THE HW IF YOU DO NOT USE IT
# emmeans(aov_model, specs="IV1", adjust="tukey")
# pairs(emmeans(aov_model, specs="IV1", adjust="tukey"))
#
# emmeans(aov_model2, specs="IV2", adjust="tukey")
# pairs(emmeans(aov_model2, specs="IV2", adjust="tukey"))
#
# emmeans(aov_model2, specs="IV1", by="IV2", adjust="sidak")
# pairs(emmeans(aov_model2, specs="IV2", by="IV1", adjust="sidak"))
#
# emmeans(aov_model2, specs="IV2", by="IV1", adjust="sidak")
# pairs(emmeans(aov_model2, specs="IV2", by="IV1", adjust="sidak"))Write Up Results
One-Way ANOVA
Write up your results. Again, make sure to maintain an appropriate tone, and follow APA guidelines for reporting statistical results. I recommend following the below outline:
- Briefly restate your hypothesis
- Describe any issues with your data (you can copy/paste from above, just make sure everything flows).
- Report your results. Make sure to include your F-value, degrees of freedom, p-value, and effect size. Since we are showing our means and standard deviations for the levels of our IV in our plot, you do NOT have to report them in the text (normally, you would report it like this: (M = #.##, SD = .##), repeating for each level).
- Specify where the differences occur. For instance, one level of the IV may be significantly different than the other two, or there may be multiple differences.
- Describe your interaction, if you ran a two-way ANOVA and there is one.
- Interpret your effect size (trivial, small, medium, or large) and include the citation.
- Make sure to include a reference to Figure 1 (created using the code below)
References
Cohen J. (1988). Statistical Power Analysis for the Behavioral Sciences. New York, NY: Routledge Academic.