#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
# 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.
There will be a significant difference in perceived social support by people’s gender, between male, female, and other.
# you only need to check the variables you're using in the current analysis
str(d)
## 'data.frame': 773 obs. of 8 variables:
## $ ResponseID : chr "R_12G7bIqN2wB2N65" "R_3lLnoV2mYVYHFvf" "R_1gTNDGWsqikPuEX" "R_2QLjdu3yoxqQ21c" ...
## $ gender : chr "m" "f" "f" "f" ...
## $ disability : chr "psychiatric" "other" "learning" "psychiatric" ...
## $ pipwd : num 2.33 2.33 3 2.93 3.33 ...
## $ moa_independence: num 3.5 2.83 3.67 3.17 3.5 ...
## $ belong : num 3.6 2.4 3 3.3 2.7 3.8 2.6 3.8 2.4 3.1 ...
## $ support : num 5.17 2.67 4.5 3.92 5.67 ...
## $ 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)
# check that our categorical variables of interest are now factors
str(d)
## 'data.frame': 773 obs. of 8 variables:
## $ ResponseID : chr "R_12G7bIqN2wB2N65" "R_3lLnoV2mYVYHFvf" "R_1gTNDGWsqikPuEX" "R_2QLjdu3yoxqQ21c" ...
## $ gender : Factor w/ 3 levels "f","m","nb": 2 1 1 1 1 1 1 1 1 1 ...
## $ disability : chr "psychiatric" "other" "learning" "psychiatric" ...
## $ pipwd : num 2.33 2.33 3 2.93 3.33 ...
## $ moa_independence: num 3.5 2.83 3.67 3.17 3.5 ...
## $ belong : num 3.6 2.4 3 3.3 2.7 3.8 2.6 3.8 2.4 3.1 ...
## $ support : num 5.17 2.67 4.5 3.92 5.67 ...
## $ row_id : Factor w/ 773 levels "1","2","3","4",..: 1 2 3 4 5 6 7 8 9 10 ...
# check our DV skew and kurtosis
describe(d$support)
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 773 5.31 1.23 5.58 5.44 1.11 0 7 7 -0.95 0.75 0.04
# we'll use the describeBy() command to view our DV's skew and kurtosis across our IVs' levels
describeBy(d$support, group = d$gender)
##
## Descriptive statistics by group
## group: f
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 575 5.35 1.24 5.67 5.48 1.11 0 7 7 -1.03 0.95 0.05
## ------------------------------------------------------------
## group: m
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 162 5.26 1.18 5.29 5.36 1.3 1.5 7 5.5 -0.74 0.28 0.09
## ------------------------------------------------------------
## group: nb
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 36 4.96 1.22 5.04 5.05 1.3 1.83 6.83 5 -0.58 -0.34 0.2
# also use histograms to examine your continuous variable
hist(d$support)
table(d$gender)
##
## f m nb
## 575 162 36
# REMEMBER your test's level of POWER is determined by your SMALLEST subsample
# use the lm() command to run the regression
# formula is y~x, where y is our DV, x is our IV
reg_model <- lm(support~gender, data = d)
# 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
# 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(support~gender, data = d)
## Levene's Test for Homogeneity of Variance (center = median)
## Df F value Pr(>F)
## group 2 0.0689 0.9335
## 770
Our cell sizes are very unbalanced between the gender groups. A small sample size for one of the levels of our variable (nb, n = 36) limits our power and increases our type II error rate.
Levene’s test was significant for our three-level pet type variable with the One-Way ANOVA. We are ignoring this and continuing with the analysis for the purpose of this class.
We identified 0 outliers using Cook’s Distance & Residuals v Leverage.
aov_model <- aov_ez(data = d,
id = "ResponseID",
between = c("gender"),
dv = "support",
anova_table = list(es = "pes"))
## Contrasts set to contr.sum for the following variables: gender
nice(aov_model)
## Anova Table (Type 3 tests)
##
## Response: support
## Effect df MSE F pes p.value
## 1 gender 2, 770 1.50 1.90 .005 .150
## ---
## 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
afex_plot(aov_model, x = "gender")
To test our hypothesis that there will be a significant difference in people’s level of perceived social support based on their gender (female, male, nonbinary), we used a one-way ANOVA. Our data was unbalanced, with many more people who identify as female (n = 575) than who identify as male (n = 162) or nonbinary (n = 36). This significantly reduces the power of our test and increases the chances of a Type II error. We identified and removed 0 outliers following visual analysis of Cook’s Distance and Residuals vs Leverage plots plots. We did have a non-significant Levene’s test (p < .934) also indicates that our data meets the assumption of homogeneity of variance.
We found a non-significant effect of gender identity, F(2, 770) = 1.90, p <.150, ηp2 = .005 (small effect; Cohen, 1988) and therefore didn’t run any post-hoc tests.
References
Cohen J. (1988). Statistical Power Analysis for the Behavioral Sciences. New York, NY: Routledge Academic.