#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 modify variables or add new variables:
## let(mtcars, new_var = 42, new_var2 = new_var*hp) %>% head()
##
## 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 3 or more levels) OR a two-way/factorial ANOVA (at least 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.
One-Way: We predict that there will be a significant difference in people’s level of felt of belonging based on the age of the participant (between 18 and 25, between 26 and 35, between 36 and 45, and over 45).
# 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': 2093 obs. of 8 variables:
## $ ResponseID : chr "R_BJN3bQqi1zUMid3" "R_2TGbiBXmAtxywsD" "R_12G7bIqN2wB2N65" "R_39pldNoon8CePfP" ...
## $ gender : chr "f" "m" "m" "f" ...
## $ age : chr "1 between 18 and 25" "1 between 18 and 25" "1 between 18 and 25" "1 between 18 and 25" ...
## $ moa_independence: num 3.67 3.67 3.5 3 3.83 ...
## $ moa_maturity : num 3.67 3.33 3.67 3 3.67 ...
## $ belong : num 2.8 4.2 3.6 4 3.4 4.2 3.9 3.6 2.9 2.5 ...
## $ stress : num 3.3 3.3 4 3.2 3.1 3.5 3.3 2.4 2.9 2.7 ...
## $ 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)
# check that all our categorical variables of interest are now factors
str(d)
## 'data.frame': 2093 obs. of 8 variables:
## $ ResponseID : chr "R_BJN3bQqi1zUMid3" "R_2TGbiBXmAtxywsD" "R_12G7bIqN2wB2N65" "R_39pldNoon8CePfP" ...
## $ gender : chr "f" "m" "m" "f" ...
## $ age : Factor w/ 4 levels "1 between 18 and 25",..: 1 1 1 1 1 1 1 1 1 1 ...
## $ moa_independence: num 3.67 3.67 3.5 3 3.83 ...
## $ moa_maturity : num 3.67 3.33 3.67 3 3.67 ...
## $ belong : num 2.8 4.2 3.6 4 3.4 4.2 3.9 3.6 2.9 2.5 ...
## $ stress : num 3.3 3.3 4 3.2 3.1 3.5 3.3 2.4 2.9 2.7 ...
## $ row_id : Factor w/ 2093 levels "1","2","3","4",..: 1 2 3 4 5 6 7 8 9 10 ...
# check our DV skew and kurtosis
describe(d$belong)
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 2093 3.21 0.61 3.2 3.23 0.59 1.3 5 3.7 -0.27 -0.12 0.01
# we'll use the describeBy() command to view our DV's skew and kurtosis across our IVs' levels
describeBy(d$belong, group = d$age)
##
## Descriptive statistics by group
## group: 1 between 18 and 25
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 1926 3.25 0.6 3.3 3.26 0.59 1.3 5 3.7 -0.28 -0.03 0.01
## ------------------------------------------------------------
## group: 2 between 26 and 35
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 113 2.87 0.66 2.8 2.87 0.74 1.4 4.3 2.9 0.01 -0.84 0.06
## ------------------------------------------------------------
## group: 3 between 36 and 45
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 37 2.77 0.51 2.8 2.78 0.59 1.7 3.6 1.9 -0.2 -0.87 0.08
## ------------------------------------------------------------
## group: 4 over 45
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 17 2.87 0.59 2.9 2.87 0.44 1.8 4 2.2 -0.01 -0.7 0.14
# also use histograms to examine your continuous variable
hist(d$belong)
# and cross_cases() to examine your categorical variables' cell count
cross_cases(d, age)
| #Total | |
|---|---|
| age | |
| 1 between 18 and 25 | 1926 |
| 2 between 26 and 35 | 113 |
| 3 between 36 and 45 | 37 |
| 4 over 45 | 17 |
| #Total cases | 2093 |
# REMEMBER your test's level of POWER is determined by your SMALLEST subsample
# One-Way
table(d$age)
##
## 1 between 18 and 25 2 between 26 and 35 3 between 36 and 45 4 over 45
## 1926 113 37 17
# 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
# We will create a new dataframe for the two-way analysis and call it d_twoway and remove the pet owning Ps.
# 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(belong~age, data = d)
## Levene's Test for Homogeneity of Variance (center = median)
## Df F value Pr(>F)
## group 3 2.0065 0.111
## 2089
# 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))
# 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(belong~age, data = d) #for One-Way
# Cook's distance
plot(reg_model, 4)
# Residuals VS Leverage
plot(reg_model, 5)
Our cell sizes are very unbalanced between the age range group levels. A small sample size for two of the levels of our variable limits our power and increases our Type II error rate.
Levene’s test was significant for our four-level age variable with the One-Way ANOVA. We are ignoring this and continuing with the analysis anyway for this class.
We identified and removed a single outlier for the One-Way ANOVA.
[UPDATE this section in your HW.]
# One-Way
aov_model <- aov_ez(data = d,
id = "row_id",
between = c("age"),
dv = "belong",
anova_table = list(es = "pes"))
## Contrasts set to contr.sum for the following variables: age
nice(aov_model)
## Anova Table (Type 3 tests)
##
## Response: belong
## Effect df MSE F pes p.value
## 1 age 3, 2089 0.36 23.01 *** .032 <.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
# One-Way
afex_plot(aov_model, x = "age")
# 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 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="age", adjust="sidak")
## age emmean SE df lower.CL upper.CL
## 1 between 18 and 25 3.25 0.0137 2089 3.21 3.28
## 2 between 26 and 35 2.87 0.0564 2089 2.72 3.01
## 3 between 36 and 45 2.77 0.0985 2089 2.53 3.02
## 4 over 45 2.87 0.1450 2089 2.51 3.23
##
## Confidence level used: 0.95
## Conf-level adjustment: sidak method for 4 estimates
pairs(emmeans(aov_model, specs="age", adjust="sidak"))
## contrast estimate SE df t.ratio p.value
## 1 between 18 and 25 - 2 between 26 and 35 0.3796 0.0580 2089 6.545 <.0001
## 1 between 18 and 25 - 3 between 36 and 45 0.4721 0.0994 2089 4.747 <.0001
## 1 between 18 and 25 - 4 over 45 0.3745 0.1460 2089 2.566 0.0507
## 2 between 26 and 35 - 3 between 36 and 45 0.0925 0.1130 2089 0.815 0.8473
## 2 between 26 and 35 - 4 over 45 -0.0051 0.1560 2089 -0.033 1.0000
## 3 between 36 and 45 - 4 over 45 -0.0976 0.1760 2089 -0.556 0.9449
##
## P value adjustment: tukey method for comparing a family of 4 estimates
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.
```
To test our hypothesis that there will be a significant difference in people’s level of felt belonging based on the particpant’s age range (between 18 and 25, between 26 and 35, between 36 and 45, and over 45), we used a one-way ANOVA. Our data was unbalanced, with many more people who are between 18 and 15 participating in our survey (n = 1926) than who are between 26 and 35 (n = 113), between 36 and 45 (n = 37) and over 45 (n = 17. This significantly reduces the power of our test and increases the chances of a Type II error. We also identified and removed a single outlier following visual analysis of Cook’s Distance and Residuals VS Leverage plots. A non-significant Levene’s test (p = 0.111 ) indicates that our data does not violate the assumption of homogeniety of variance.
We found a significant effect of age, F(3,2088) = 22.99, p < .001, ηp2 = .032 (large effect size; Cohen, 1988). Posthoc tests using Sidak’s adjustment revealed that participants who are between ages of 18 and 25 (M = 3.24, SE = .01) reported more belonging than those who are between 26 and 35 (M = 2.87, SE = .06), those who are between 36 and 45 (M = 2.77, SE = .10) and those who are over 45. (M = 2.87, SE = .15); participants who are between 18 and 25 reported the highest amount of belonging overall (see Figure 1 for a comparison).
References
Cohen J. (1988). Statistical Power Analysis for the Behavioral Sciences. New York, NY: Routledge Academic.