#install.packages("afex")
#install.packages("emmeans")
#install.packages("ggbeeswarm")
library(psych) # for the describe() command
library(ggplot2) # to visualize our results
##
## Attaching package: 'ggplot2'
## The following objects are masked from 'package:psych':
##
## %+%, alpha
library(expss) # for the cross_cases() command
## Loading required package: maditr
##
## To drop variable use NULL: let(mtcars, am = NULL) %>% head()
##
## Attaching package: 'maditr'
## The following object is masked from 'package:base':
##
## sort_by
##
## Attaching package: 'expss'
## The following object is masked from 'package:ggplot2':
##
## vars
library(car) # for the leveneTest() command
## Loading required package: carData
##
## Attaching package: 'car'
## The following object is masked from 'package:expss':
##
## recode
## The following object is masked from 'package:psych':
##
## logit
library(afex) # to run the ANOVA
## Loading required package: lme4
## Loading required package: Matrix
##
## Attaching package: 'lme4'
## The following object is masked from 'package:expss':
##
## dummy
## ************
## Welcome to afex. For support visit: http://afex.singmann.science/
## - Functions for ANOVAs: aov_car(), aov_ez(), and aov_4()
## - Methods for calculating p-values with mixed(): 'S', 'KR', 'LRT', and 'PB'
## - 'afex_aov' and 'mixed' objects can be passed to emmeans() for follow-up tests
## - Get and set global package options with: afex_options()
## - Set sum-to-zero contrasts globally: set_sum_contrasts()
## - For example analyses see: browseVignettes("afex")
## ************
##
## Attaching package: 'afex'
## The following object is masked from 'package:lme4':
##
## lmer
library(ggbeeswarm) # to run plot results
library(emmeans) # for posthoc tests
## Welcome to emmeans.
## Caution: You lose important information if you filter this package's results.
## See '? untidy'
# 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 more than 2 levels) OR a two-way/factorial ANOVA (at least two IVs). 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.
One-Way: We predict that there will be a significant effect of age on social support.
# you only need to check the variables you're using in the current analysis
# even if you checked them previously, it's always a good idea to look them over again and be sure that everything is correct
str(d)
## 'data.frame': 917 obs. of 8 variables:
## $ X : int 20 30 31 33 57 58 81 104 113 117 ...
## $ age : chr "1 under 18" "1 under 18" "4 between 36 and 45" "4 between 36 and 45" ...
## $ urban_rural: chr "city" "city" "town" "city" ...
## $ big5_con : num 3.33 5.33 5.67 6 3.33 ...
## $ phq : num 3.33 1 2.33 1.11 2.33 ...
## $ support : num 2.17 5 2.5 3.67 4.17 ...
## $ swemws : num 2.29 4.29 3.29 4 3.29 ...
## $ 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$age <- as.factor(d$age)
d$row_id <- as.factor(d$row_id)
# we're going to recode our race variable into two groups: poc and white
# in doing so, we are creating a new variable "poc" that has 2 levels
table(d$age)
##
## 1 under 18 2 between 18 and 25 3 between 26 and 35 4 between 36 and 45
## 599 53 6 86
## 5 over 45
## 173
d$age_3g[d$age == "1 under 18"] <- "under 18"
d$age_3g[d$age == "2 between 18 and 25"] <- "18 to 45"
d$age_3g[d$age == "3 between 26 and 35"] <- "18 to 45"
d$age_3g[d$age == "4 between 36 and 45"] <- "18 to 45"
d$age_3g[d$age == "5 over 45"] <- "over 45"
table(d$age_3g)
##
## 18 to 45 over 45 under 18
## 145 173 599
d$age_3g <- as.factor(d$age_3g)
# check that all our categorical variables of interest are now factors
str(d)
## 'data.frame': 917 obs. of 9 variables:
## $ X : int 20 30 31 33 57 58 81 104 113 117 ...
## $ age : Factor w/ 5 levels "1 under 18","2 between 18 and 25",..: 1 1 4 4 4 1 1 1 4 1 ...
## $ urban_rural: chr "city" "city" "town" "city" ...
## $ big5_con : num 3.33 5.33 5.67 6 3.33 ...
## $ phq : num 3.33 1 2.33 1.11 2.33 ...
## $ support : num 2.17 5 2.5 3.67 4.17 ...
## $ swemws : num 2.29 4.29 3.29 4 3.29 ...
## $ row_id : Factor w/ 917 levels "1","2","3","4",..: 1 2 3 4 5 6 7 8 9 10 ...
## $ age_3g : Factor w/ 3 levels "18 to 45","over 45",..: 3 3 1 1 1 3 3 3 1 3 ...
# check our DV skew and kurtosis
describe(d$support)
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 917 3.61 0.94 3.67 3.66 0.99 1 5 4 -0.45 -0.56 0.03
# we'll use the describeBy() command to view our DV's skew and kurtosis across our IVs' levels
describeBy(d$support, group = d$age_3g)
##
## Descriptive statistics by group
## group: 18 to 45
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 145 3.7 0.94 3.83 3.75 0.99 1.17 5 3.83 -0.46 -0.83 0.08
## ------------------------------------------------------------
## group: over 45
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 173 3.95 0.84 4.17 4.04 0.74 1.5 5 3.5 -0.87 0.31 0.06
## ------------------------------------------------------------
## group: under 18
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 599 3.49 0.95 3.5 3.53 0.99 1 5 4 -0.34 -0.6 0.04
# also use histograms to examine your continuous variable
hist(d$support)
# and cross_cases() to examine your categorical variables' cell count
cross_cases(d, age_3g)
| #Total | |
|---|---|
| age_3g | |
| 18 to 45 | 145 |
| over 45 | 173 |
| under 18 | 599 |
| #Total cases | 917 |
# REMEMBER your test's level of power is determined by your SMALLEST subsample (i.e., people of color who own a rabbit = 6).
# One-Way
table(d$age_3g)
##
## 18 to 45 over 45 under 18
## 145 173 599
# our small number of participants owning rabbits is going to hurt us for the two-way anova, but it should be okay for the one-way anova
# so we'll create a new dataframe for the two-way analysis and call it d_tw
# 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
# One-Way
leveneTest(support~age_3g, data = d)
## Levene's Test for Homogeneity of Variance (center = median)
## Df F value Pr(>F)
## group 2 3.911 0.02036 *
## 914
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
# 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))
# to drop multiple outliers, use this code:
# d <- subset(d, row_id!=c(1108) & row_id!=c(602))
# if you remove an outlier, you have to go back and re-run the regression model.
# 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.
reg_model <- lm(support~age_3g, data = d) #for One-Way
# Cook's distance
plot(reg_model, 4)
# Residuals VS Leverage
plot(reg_model, 5)
Our cell sizes were very unbalanced between the group levels. A small sample size for one of the levels of our variable limits our power and increases our Type II error rate. We combined three of the five levels of age to create one larger level, 18 to 45, to account for this issue.
Levene’s test was significant for our three-level age type variable. We are ignoring this and continuing with the analysis anyway for this class.
We identified zero outliers using a Cook’s distance plot. Our data met all other assumptions of a One-Way ANOVA test.
# One-Way
aov_model <- aov_ez(data = d,
id = "X",
between = c("age_3g"),
dv = "support",
anova_table = list(es = "pes"))
## Contrasts set to contr.sum for the following variables: age_3g
nice(aov_model)
## Anova Table (Type 3 tests)
##
## Response: support
## Effect df MSE F pes p.value
## 1 age_3g 2, 914 0.86 17.14 *** .036 <.001
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '+' 0.1 ' ' 1
ANOVA Effect Size 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
# One-Way
afex_plot(aov_model, x = "age_3g")
# NOTE: for the Two-Way, you will need to decide which plot version makes the most sense based on your data / rationale when you make the nice Figure 2 at the end
Only run posthocs 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="age_3g", adjust="tukey")
## Note: adjust = "tukey" was changed to "sidak"
## because "tukey" is only appropriate for one set of pairwise comparisons
## age_3g emmean SE df lower.CL upper.CL
## 18 to 45 3.70 0.0770 914 3.52 3.89
## over 45 3.95 0.0705 914 3.78 4.11
## under 18 3.49 0.0379 914 3.40 3.58
##
## Confidence level used: 0.95
## Conf-level adjustment: sidak method for 3 estimates
pairs(emmeans(aov_model, specs="age_3g", adjust="tukey"))
## contrast estimate SE df t.ratio p.value
## 18 to 45 - over 45 -0.243 0.1040 914 -2.325 0.0529
## 18 to 45 - under 18 0.214 0.0858 914 2.490 0.0346
## over 45 - under 18 0.456 0.0800 914 5.703 <.0001
##
## P value adjustment: tukey method for comparing a family of 3 estimates
To test our hypothesis that there would be a significant effect of age on one’s social support, we used a One-Way ANOVA. Our data was unbalanced, with many more people participating in our survey who were under 18 (n = 599) and over 45 (n = 173) than people who were aged 26 to 35 (n = 6). This significantly reduces the power of our test and increases the chances of a Type II error. Therefore, we combined three of the levels of our age variable to create a larger level, people aged 18 to 45 (n = 145). We identified zero outliers following visual analysis of Cook’s Distance and Residuals VS Leverage plots. A significant Levene’s test (p = .02) indicates that our data violates 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 age, F(2, 914) = 17.14, p < .001, ηp2 = .036 (small effect size; Cohen, 1988). Posthoc tests using Sidak’s adjustment revealed that participants over 45 (M = 3.95, SE = 0.07) and participants aged 18 to 45 (M = 3.70, SE = 0.08) reported significantly greater social support than those under 18 (M = 3.49, SE = 0.04), p < .05. However, there was not a signficant difference in social support between those over 45 and those aged 18 to 45, p = .053 (see Figure 1 for a comparison).
References
Cohen J. (1988). Statistical Power Analysis for the Behavioral Sciences. New York, NY: Routledge Academic.