1 Loading Libraries

#install.packages("afex")
#install.packages("emmeans")
#install.packages("ggbeeswarm")
#install.packages("expss")

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 
library(ggbeeswarm) # to run plot results
library(emmeans) # for posthoc tests

2 Importing Data

# For HW, import the project dataset you cleaned previously this will be the dataset you'll use throughout the rest of the semester

d <- read.csv(file="Data/projectdata.csv", header=T)


# new code! this adds a column with a number for each row. It will make it easier if we need to drop outliers later
d$row_id <- 1:nrow(d)

Note: For your HW, you will choose to run EITHER a one-way ANOVA (a single IV with 3 or more levels) OR a two-way/factorial ANOVA (two IVs with 2 or 3 levels each). You will need to specify your hypothesis and customize your code based on the choice you make. We will run BOTH versions of the test in the lab for illustrative purposes.

3 One-Way ANOVA

3.1 State Your Hypothesis

We predict that there will be a significant difference in neuroticism by people’s level of gender (male, female, I use another term).

3.2 Check Your Variables

# you only need to check the variables you're using in the current analysis

str(d)
## 'data.frame':    679 obs. of  8 variables:
##  $ X        : int  520 2814 3146 3295 717 6056 4753 2044 1965 1246 ...
##  $ gender   : chr  "female" "male" "female" "male" ...
##  $ age      : chr  "1 under 18" "1 under 18" "1 under 18" "1 under 18" ...
##  $ big5_neu : num  5.33 2.67 1 3.67 4.33 ...
##  $ iou      : num  2.22 1.52 1.78 1.85 2.22 ...
##  $ pas_covid: num  3 3.44 4.67 2.44 1.56 ...
##  $ gad      : num  1.14 1.29 1 1 1.14 ...
##  $ row_id   : int  1 2 3 4 5 6 7 8 9 10 ...
# make our categorical variables of interest "factors"
# because we'll use our newly created row ID variable for this analysis, so make sure it's coded as a factor, too.
d$gender <- as.factor(d$gender) 
d$row_id <- as.factor(d$row_id)

d <- subset(d, gender != "Prefer not to say") # use subset() to remove all participants from the additional level

table(d$gender, useNA = "always") # verify that now there are ZERO participants in the additional level
## 
##             female I use another term               male  Prefer not to say 
##                535                 19                115                  0 
##               <NA> 
##                  0
 d$gender <- droplevels(d$gender) # use droplevels() to drop the empty factor

table(d$gender, useNA = "always") # verify that now the entire factor level is removed 
## 
##             female I use another term               male               <NA> 
##                535                 19                115                  0
# check that our categorical variables of interest are now factors
str(d)
## 'data.frame':    669 obs. of  8 variables:
##  $ X        : int  520 2814 3146 3295 717 6056 4753 2044 1965 1246 ...
##  $ gender   : Factor w/ 3 levels "female","I use another term",..: 1 3 1 3 1 1 1 3 3 3 ...
##  $ age      : chr  "1 under 18" "1 under 18" "1 under 18" "1 under 18" ...
##  $ big5_neu : num  5.33 2.67 1 3.67 4.33 ...
##  $ iou      : num  2.22 1.52 1.78 1.85 2.22 ...
##  $ pas_covid: num  3 3.44 4.67 2.44 1.56 ...
##  $ gad      : num  1.14 1.29 1 1 1.14 ...
##  $ row_id   : Factor w/ 679 levels "1","2","3","4",..: 1 2 3 4 5 6 7 8 9 10 ...
# check our DV skew and kurtosis
describe(d$big5_neu)
##    vars   n mean   sd median trimmed  mad min max range  skew kurtosis   se
## X1    1 669 4.64 1.45      5    4.73 1.48   1   7     6 -0.52    -0.42 0.06
# we'll use the describeBy() command to view our DV's skew and kurtosis across our IVs' levels
describeBy(d$big5_neu, group = d$gender)
## 
##  Descriptive statistics by group 
## group: female
##    vars   n mean   sd median trimmed  mad min max range  skew kurtosis   se
## X1    1 535 4.79 1.36      5    4.89 1.48   1   7     6 -0.61    -0.12 0.06
## ------------------------------------------------------------ 
## group: I use another term
##    vars  n mean   sd median trimmed  mad  min max range  skew kurtosis   se
## X1    1 19 5.74 0.98      6    5.78 0.99 3.67   7  3.33 -0.37    -1.15 0.22
## ------------------------------------------------------------ 
## group: male
##    vars   n mean   sd median trimmed  mad min max range skew kurtosis   se
## X1    1 115  3.8 1.59      4    3.75 1.98   1   7     6 0.14    -0.91 0.15
# also use histograms to examine your continuous variable
hist(d$big5_neu)

3.3 Check Your Assumptions

3.3.1 ANOVA Assumptions

  • DV should be normally distributed across levels of the IV (we checked previously using “describeBy” function)
  • All levels of the IVs should have an equal number of cases and there should be no empty cells. Cells with low numbers decrease the power of the test (which increases chance of Type II error)
  • Homogeneity of variance should be confirmed (using Levene’s Test)
  • Outliers should be identified and removed – we will actually remove them this time!
  • If you have confirmed everything above, the sampling distribution should be normal.

3.3.2 Check levels of IVs

table(d$gender)
## 
##             female I use another term               male 
##                535                 19                115
# REMEMBER your test's level of POWER is determined by your SMALLEST subsample

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

3.3.3.1 Run a Regression to get both outlier plots

# use this commented out section below ONLY IF if you need to remove outliers
# to drop a single outlier, use this code:
 d <- subset(d, row_id!=c(1108))  # NOTE: Participant 1108 is not truly problamtic in the Lab, but we are going to removed them for demonstration.

# to drop multiple outliers, 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~x, where y is our DV, x is our IV

reg_model <- lm(big5_neu~gender, data = d) 

3.3.3.2 Check for outliers

# Cook's distance
plot(reg_model, 4)

# Residuals VS Leverage
plot(reg_model, 5)

# IF you find outliers, go back up to line 114 or 117 and remove it/them, then re-run the reg_model code

3.3.4 Check homogeneity of variance

# use the leveneTest() command from the car package to test homogeneity of variance
# uses the 'formula' setup: formula is y~x, where y is our DV and x is our IV 
leveneTest(big5_neu~gender, data = d)
## Levene's Test for Homogeneity of Variance (center = median)
##        Df F value   Pr(>F)   
## group   2  5.3762 0.004828 **
##       666                    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

3.3.5 Issues with My Data

Our cell sizes are very unbalanced between the gender group levels. A small sample size for one of the levels of our variable (I use another term, n = 19) limits our power and increases our Type II error rate.

Levene’s test was significant for our three-level gender variable with the One-Way ANOVA. This means that issues with homogeneity of variance could increase chances of a Type I Error so evaluations of the data should be done carefully.

There were no outliers.

3.4 Run a One-Way ANOVA

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

3.5 View One-Way Output

nice(aov_model)
## Anova Table (Type 3 tests)
## 
## Response: big5_neu
##   Effect     df  MSE         F  pes p.value
## 1 gender 2, 666 1.94 29.94 *** .082   <.001
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '+' 0.1 ' ' 1

ANOVA Effect Size [partial eta-squared] cutoffs from Cohen (1988): * η^2 < 0.01 indicates a trivial effect * η^2 >= 0.01 indicates a small effect * η^2 >= 0.06 indicates a medium effect * η^2 >= 0.14 indicates a large effect

3.6 Visualize One-Way Results

afex_plot(aov_model, x = "gender")

3.7 Run One-Way Posthoc Tests

Remember: We ONLY run posthoc IF the ANOVA test is SIGNIFICANT! E.g., only run the posthoc tests on pet type if there is a main effect for pet type

emmeans(aov_model, specs="gender")
##  gender             emmean     SE  df lower.CL upper.CL
##  female               4.79 0.0602 666     4.67     4.90
##  I use another term   5.74 0.3190 666     5.11     6.36
##  male                 3.80 0.1300 666     3.54     4.05
## 
## Confidence level used: 0.95
pairs(emmeans(aov_model, specs="gender", adjust="tukey"))
##  contrast                    estimate    SE  df t.ratio p.value
##  female - I use another term   -0.951 0.325 666  -2.925  0.0099
##  female - male                  0.989 0.143 666   6.914 <0.0001
##  I use another term - male      1.940 0.345 666   5.627 <0.0001
## 
## P value adjustment: tukey method for comparing a family of 3 estimates

3.8 Write Up One-Way ANOVA Results

To test our hypothesis that there will be a significant difference in people’s level of neuroticism based on the gender (male, female, I use another term), we used a one-way ANOVA. Our data was unbalanced, with many more people identifying as female participating in our survey (n = 535) than who identify as male (n = 115) or who use another term (n = 19). This significantly reduces the power of our test and increases the chances of a Type II error. There were no outliers identified. A significant Levene’s test (p = .005) also indicates that our data fails 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 gender, F(2, 666) = 29.94, p < .001, ηp2 = .082 (medium effect; Cohen, 1988). Posthoc tests using Tukey’s HSD adjustment revealed that participants who identified as male (M = 3.80, SE = .13) reported more neuroticism than those who used another term (M = 5.74, SE = .32) but less neuroticism than those who identified as female (M = 4.79, SE = .06); participants who identified as female reported the highest amount of neuroticism overall (see Figure 1 for a comparison).

References

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