#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 select columns from data: columns(mtcars, mpg, vs:carb)
##
## 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 belonging based on their level of income. (Low, Middle, High).
# 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': 2146 obs. of 8 variables:
## $ ResponseID: chr "R_BJN3bQqi1zUMid3" "R_2TGbiBXmAtxywsD" "R_12G7bIqN2wB2N65" "R_39pldNoon8CePfP" ...
## $ age : chr "1 between 18 and 25" "1 between 18 and 25" "1 between 18 and 25" "1 between 18 and 25" ...
## $ income : chr "1 low" "1 low" "rather not say" "rather not say" ...
## $ stress : num 3.3 3.3 4 3.2 3.1 3.5 3.3 2.4 2.9 2.7 ...
## $ support : num 6 6.75 5.17 5.58 6 ...
## $ socmeduse : int 47 23 34 35 37 13 37 43 37 29 ...
## $ belong : num 2.8 4.2 3.6 4 3.4 4.2 3.9 3.6 2.9 2.5 ...
## $ 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$income <- as.factor(d$income)
d$row_id <- as.factor(d$row_id)
d <- subset(d, income != "rather not say") # use subset() to remove all participants from the additional level
table(d$income, useNA = "always") # verify that now there are ZERO participants in the additional level
##
## 1 low 2 middle 3 high rather not say <NA>
## 598 615 350 0 0
d$income <- droplevels(d$income) # use droplevels() to drop the empty factor
table(d$income, useNA = "always") # verify that now the entire factor level is removed
##
## 1 low 2 middle 3 high <NA>
## 598 615 350 0
# check that all our categorical variables of interest are now factors
str(d)
## 'data.frame': 1563 obs. of 8 variables:
## $ ResponseID: chr "R_BJN3bQqi1zUMid3" "R_2TGbiBXmAtxywsD" "R_1QiKb2LdJo1Bhvv" "R_2Quh0h3wxTjZjKP" ...
## $ age : chr "1 between 18 and 25" "1 between 18 and 25" "1 between 18 and 25" "1 between 18 and 25" ...
## $ income : Factor w/ 3 levels "1 low","2 middle",..: 1 1 2 1 1 3 1 1 1 1 ...
## $ stress : num 3.3 3.3 3.1 3.3 2.4 2.9 3.5 4.4 2.8 3.3 ...
## $ support : num 6 6.75 6 5 6.08 ...
## $ socmeduse : int 47 23 37 37 43 37 35 26 26 26 ...
## $ belong : num 2.8 4.2 3.4 3.9 3.6 2.9 4.1 3 4.1 3.3 ...
## $ row_id : Factor w/ 2146 levels "1","2","3","4",..: 1 2 5 7 8 9 11 16 17 18 ...
# check our DV skew and kurtosis
describe(d$belong)
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 1563 3.21 0.61 3.2 3.23 0.59 1.3 5 3.7 -0.3 -0.04 0.02
# we'll use the describeBy() command to view our DV's skew and kurtosis across our IVs' levels
describeBy(d$belong, group = d$income )
##
## Descriptive statistics by group
## group: 1 low
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 598 3.16 0.64 3.2 3.18 0.59 1.3 5 3.7 -0.31 -0.18 0.03
## ------------------------------------------------------------
## group: 2 middle
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 615 3.2 0.58 3.2 3.22 0.59 1.3 4.9 3.6 -0.2 0 0.02
## ------------------------------------------------------------
## group: 3 high
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 350 3.31 0.6 3.3 3.34 0.59 1.4 4.6 3.2 -0.38 0.05 0.03
# also use histograms to examine your continuous variable
hist(d$belong)
# REMEMBER your test's level of POWER is determined by your SMALLEST subsample
# One-Way
table(d$income)
##
## 1 low 2 middle 3 high
## 598 615 350
# 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
# 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~income, data = d)
## Levene's Test for Homogeneity of Variance (center = median)
## Df F value Pr(>F)
## group 2 3.2793 0.03792 *
## 1560
## ---
## 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))
# 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~income, data = d) #for One-Way
# Cook's distance
plot(reg_model, 4)
# Residuals VS Leverage
plot(reg_model, 5)
Our cell sizes are fairly balanced between levels.
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 anyway for this class. [UPDATE this section in your HW.]
# One-Way
aov_model <- aov_ez(data = d,
id = "ResponseID",
between = c("income"),
dv = "belong",
anova_table = list(es = "pes"))
## Contrasts set to contr.sum for the following variables: income
nice(aov_model)
## Anova Table (Type 3 tests)
##
## Response: belong
## Effect df MSE F pes p.value
## 1 income 2, 1560 0.37 7.07 *** .009 <.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 = "income")
# 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="income", adjust="sidak")
## income emmean SE df lower.CL upper.CL
## 1 low 3.16 0.0249 1560 3.10 3.22
## 2 middle 3.20 0.0245 1560 3.15 3.26
## 3 high 3.31 0.0325 1560 3.23 3.39
##
## Confidence level used: 0.95
## Conf-level adjustment: sidak method for 3 estimates
pairs(emmeans(aov_model, specs="income", adjust="sidak"))
## contrast estimate SE df t.ratio p.value
## 1 low - 2 middle -0.0447 0.0349 1560 -1.280 0.4065
## 1 low - 3 high -0.1530 0.0409 1560 -3.741 0.0006
## 2 middle - 3 high -0.1083 0.0407 1560 -2.662 0.0214
##
## 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 level of felt belonging based on their income (Low, Middle or High), we used a one-way ANOVA. Our data was fairly balanced. We also identified and removed no outliers following visual analysis of Cook’s Distance and Residuals VS Leverage plots.
We found a significant effect of income, F(2, 1560) = 7.07, p<.001, ηp2 = .009 (trivial effect size; Cohen, 1988). Posthoc tests using Sidak’s adjustment revealed that participants who reported high income (M = 3.31, SE = .03) reported more need to belong than than those who reported low income. (M = 3.16, SE = .02) but less companionship than those who reported middle income (M = 3.20, SE = .02); (see Figure 1 for a comparison).
References
Cohen J. (1988). Statistical Power Analysis for the Behavioral Sciences. New York, NY: Routledge Academic.