#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.
We predict that there will be a significant difference in people’s perceived social support based on people’s level of disability (chronic health, learning, psychiatric).
# you only need to check the variables you're using in the current analysis
str(d)
## 'data.frame': 858 obs. of 8 variables:
## $ ResponseID: chr "R_12G7bIqN2wB2N65" "R_3lLnoV2mYVYHFvf" "R_1gTNDGWsqikPuEX" "R_3G1XvswZmPZTkMU" ...
## $ disability: chr "psychiatric" "other" "learning" "psychiatric" ...
## $ race_rc : chr "white" "white" "white" "white" ...
## $ belong : num 3.6 2.4 3 3.5 3.3 2.7 3.8 2.6 3.8 4 ...
## $ support : num 5.17 2.67 4.5 6.75 3.92 ...
## $ swb : num 1.83 5.83 5.33 5 2.67 ...
## $ npi : num 0.0769 0.7692 0 0 0.0769 ...
## $ 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$disability <- as.factor(d$disability)
d$row_id <- as.factor(d$row_id)
# check that our categorical variables of interest are now factors
str(d)
## 'data.frame': 858 obs. of 8 variables:
## $ ResponseID: chr "R_12G7bIqN2wB2N65" "R_3lLnoV2mYVYHFvf" "R_1gTNDGWsqikPuEX" "R_3G1XvswZmPZTkMU" ...
## $ disability: Factor w/ 6 levels "chronic health",..: 5 3 2 5 5 3 3 5 1 2 ...
## $ race_rc : chr "white" "white" "white" "white" ...
## $ belong : num 3.6 2.4 3 3.5 3.3 2.7 3.8 2.6 3.8 4 ...
## $ support : num 5.17 2.67 4.5 6.75 3.92 ...
## $ swb : num 1.83 5.83 5.33 5 2.67 ...
## $ npi : num 0.0769 0.7692 0 0 0.0769 ...
## $ row_id : Factor w/ 858 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 858 5.32 1.23 5.58 5.45 1.11 0 7 7 -0.96 0.76 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$disability)
##
## Descriptive statistics by group
## group: chronic health
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 148 5.19 1.32 5.42 5.33 1.36 1.42 7 5.58 -0.84 0.09 0.11
## ------------------------------------------------------------
## group: learning
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 123 5.54 1.3 5.83 5.71 1.11 1 7 6 -1.27 1.55 0.12
## ------------------------------------------------------------
## group: other
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 86 5.31 1.34 5.62 5.47 1.3 1.17 7 5.83 -0.98 0.32 0.14
## ------------------------------------------------------------
## group: physical
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 50 5.79 1.02 5.96 5.91 0.99 2.75 7 4.25 -1 0.75 0.14
## ------------------------------------------------------------
## group: psychiatric
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 381 5.22 1.15 5.42 5.32 1.11 0 7 7 -0.83 0.69 0.06
## ------------------------------------------------------------
## group: sensory
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 70 5.44 1.16 5.67 5.59 0.99 1 7 6 -1.26 1.81 0.14
# also use histograms to examine your continuous variable
hist(d$support)
d <- subset(d, disability != "physical")
table(d$disability, useNA = "always")
##
## chronic health learning other physical psychiatric
## 148 123 86 0 381
## sensory <NA>
## 70 0
d$disability <- droplevels(d$disability)
table(d$disability, useNA = "always")
##
## chronic health learning other psychiatric sensory
## 148 123 86 381 70
## <NA>
## 0
d <- subset(d, disability != "sensory")
table(d$disability, useNA = "always")
##
## chronic health learning other psychiatric sensory
## 148 123 86 381 0
## <NA>
## 0
d$disability <- droplevels(d$disability)
table(d$disability, useNA = "always")
##
## chronic health learning other psychiatric <NA>
## 148 123 86 381 0
d <- subset(d, disability != "other")
table(d$disability, useNA = "always")
##
## chronic health learning other psychiatric <NA>
## 148 123 0 381 0
d$disability <- droplevels(d$disability)
table(d$disability, useNA = "always")
##
## chronic health learning psychiatric <NA>
## 148 123 381 0
table(d$disability)
##
## chronic health learning psychiatric
## 148 123 381
# REMEMBER your test's level of POWER is determined by your SMALLEST subsample
reg_model <- lm(support~disability, 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~disability, data = d)
## Levene's Test for Homogeneity of Variance (center = median)
## Df F value Pr(>F)
## group 2 1.8443 0.159
## 649
Our cell sizes are a little unbalanced between the disability type group levels. A smaller sample size for one of the levels of our variable (learning, n = 123) limits our power and increases our Type II error rate.
Levene’s test was not significant for our three-level disability type variable with the One-Way ANOVA so we are able to continue.
There were not outliers detected
aov_model <- aov_ez(data = d,
id = "ResponseID",
between = c("disability"),
dv = "support",
anova_table = list(es = "pes"))
## Contrasts set to contr.sum for the following variables: disability
nice(aov_model)
## Anova Table (Type 3 tests)
##
## Response: support
## Effect df MSE F pes p.value
## 1 disability 2, 649 1.49 3.52 * .011 .030
## ---
## 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 = "disability")
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="disability")
## disability emmean SE df lower.CL upper.CL
## chronic health 5.19 0.1000 649 4.99 5.39
## learning 5.54 0.1100 649 5.32 5.75
## psychiatric 5.22 0.0625 649 5.10 5.35
##
## Confidence level used: 0.95
pairs(emmeans(aov_model, specs="disability", adjust="tukey"))
## contrast estimate SE df t.ratio p.value
## chronic health - learning -0.3475 0.149 649 -2.333 0.0521
## chronic health - psychiatric -0.0349 0.118 649 -0.295 0.9532
## learning - psychiatric 0.3126 0.127 649 2.469 0.0367
##
## P value adjustment: tukey method for comparing a family of 3 estimates
To test our hypothesis that there will be a significant difference in people’s perceived social support based on people’s level of disability (chronic health, learning, psychiatric), we used a one-way ANOVA. Our data was unbalanced, with many more people who have a psychiatric disability in our survey (n = 381) than who have a chronic health disability (n = 148) or learning disability (n = 123). This significantly reduces the power of our test and increases the chances of a Type II error. We did not identify any outliers following visual analysis of cooks distance and residual vs leverage plots. A non-significant Levene’s test (p= .16) indicates our data passes the assumption of homogeneity of variance.
We found a significant effect of disability type, F(2, 649) = 3.52, p = 0.03, ηp2 = .011 (small effect; Cohen, 1988). Posthoc tests using Tukey’s HSD adjustment revealed that participants who have a psychiatrtic disability (M = 5.22, SE = 0.06) reported slightly more support than those who have a chronic health disability (M = 5.19, SE = 0.10) but less support than those who have a learning disability (M = 5.54, SE = 0.11); participants who have a learning disability reported the highest amount of support overall (see Figure 2 for a comparison).
r