1 Loading Libraries

#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

2 Importing Data

# 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.

3 One-Way ANOVA

3.1 State Your Hypothesis

We predict that there will be a significant difference in people’s Stress levels by people’s level of Income between (Low-Up to 30,000 Dollars, Middle-40,000-99,999 Dollars and High-100,000 Dollars or Higher).

3.2 Check Your Variables

# you only need to check the variables you're using in the current analysis

str(d)
## 'data.frame':    2092 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" ...
##  $ swb             : num  4.33 4.17 1.83 5.17 3.67 ...
##  $ stress          : num  3.3 3.3 4 3.2 3.1 3.5 3.3 2.4 2.9 2.7 ...
##  $ exploit         : num  2 3.67 4.33 1.67 4 ...
##  $ moa_independence: num  3.67 3.67 3.5 3 3.83 ...
##  $ 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 <- subset(d, income != "rather not say")
table(d$income, useNA = "always")
## 
##    1 low 2 middle   3 high     <NA> 
##      582      604      339        0
d$income <- as.factor(d$income) 
d$row_id <- as.factor(d$row_id)


# check that our categorical variables of interest are now factors
str(d)
## 'data.frame':    1525 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 ...
##  $ swb             : num  4.33 4.17 3.67 3.67 5.5 ...
##  $ stress          : num  3.3 3.3 3.1 3.3 2.4 2.9 3.5 4.4 2.8 3.3 ...
##  $ exploit         : num  2 3.67 4 5.33 1 ...
##  $ moa_independence: num  3.67 3.67 3.83 4 4 ...
##  $ row_id          : Factor w/ 1525 levels "1","2","5","7",..: 1 2 3 4 5 6 7 8 9 10 ...
# check our DV skew and kurtosis
describe(d$stress)
##    vars    n mean  sd median trimmed  mad min max range  skew kurtosis   se
## X1    1 1525 3.04 0.6    3.1    3.04 0.59 1.3 4.6   3.3 -0.01    -0.19 0.02
# we'll use the describeBy() command to view our DV's skew and kurtosis across our IVs' levels
describeBy(d$stress, 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 582 3.05 0.59    3.1    3.05 0.59 1.5 4.6   3.1 -0.02    -0.14 0.02
## ------------------------------------------------------------ 
## group: 2 middle
##    vars   n mean   sd median trimmed  mad min max range  skew kurtosis   se
## X1    1 604 3.04 0.61    3.1    3.04 0.59 1.3 4.6   3.3 -0.01    -0.21 0.02
## ------------------------------------------------------------ 
## group: 3 high
##    vars   n mean   sd median trimmed  mad min max range skew kurtosis   se
## X1    1 339 3.03 0.63      3    3.03 0.59 1.3 4.6   3.3 0.03     -0.3 0.03
# also use histograms to examine your continuous variable
hist(d$stress)

3.3 Check Your Assumptions

3.3.1 ANOVA Assumptions

  • DV should be normally distributed across levels of the IV (we checked previously using “describeBy” function)
  • All levels of the IVs should have an equal number of cases and there should be no empty cells. Cells with low numbers decrease the power of the test (which increases chance of Type II error)
  • Homogeneity of variance should be confirmed (using Levene’s Test)
  • Outliers should be identified and removed – we will actually remove them this time!
  • If you have confirmed everything above, the sampling distribution should be normal.

3.3.2 Check levels of IVs

table(d$income)
## 
##    1 low 2 middle   3 high 
##      582      604      339
# REMEMBER your test's level of POWER is determined by your SMALLEST subsample

3.3.3 Check for outliers using Cook’s distance and Residuals VS Leverage plot

3.3.3.1 Run a Regression to get both outlier plots

# 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))  # NOTE: Participant 1108 is not truly probleamtic in the Lab, but we are going to removed them for demonstration.

# 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~x, where y is our DV, x is our IV

reg_model <- lm(stress~income, data = d)

3.3.3.2 Check for outliers

# 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

3.3.4 Check homogeneity of variance

# 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(stress~income, data = d)
## Levene's Test for Homogeneity of Variance (center = median)
##         Df F value Pr(>F)
## group    2  1.1851  0.306
##       1522

3.3.5 Issues with My Data

We removed a level from our income variable “rather not say” as that is not part of our hypothesis.

3.4 Run a One-Way ANOVA

aov_model <- aov_ez(data = d,
                    id = "row_id",
                    between = c("income"),
                    dv = "stress",
                    anova_table = list(es = "pes"))
## Contrasts set to contr.sum for the following variables: income

3.5 View One-Way Output

nice(aov_model)
## Anova Table (Type 3 tests)
## 
## Response: stress
##   Effect      df  MSE    F   pes p.value
## 1 income 2, 1522 0.37 0.20 <.001    .822
## ---
## 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

3.6 Visualize One-Way Results

afex_plot(aov_model, x = "income")

3.7 Run One-Way Posthoc Tests

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="pet")
#pairs(emmeans(aov_model, specs="pet", adjust="tukey"))

3.8 Write Up One-Way ANOVA Results

To test our hypothesis that there will be a significant difference in people’s level of stress based on their income levels (Low-Up to 30,000 Dollars, Middle-40,000-99,999 Dollars and High-100,000 Dollars or Higher), we used a one-way ANOVA. Our data was balanced and which meant our power was as well. We found no outliers in our following visual analysis of Cook’s Distance and Residual’s VS Leverage plots. A non- significant Levene’s test (p < 0.05.) also indicates that our data met the assumption of homogeneity of variance. We continued with our analysis to find support for our hypothesis.

Our hypothesis was not supported. We found a non-significant effect of income type, F(2, 1522) = 0.20, p = 0.822, ηp2 <.001 (negligible effect size; Cohen, 1988). We did not conduct posthoc tests using Tukey’s HSD as we did not get significant results (see Figure 1 for a comparison).

References

Cohen J. (1988). Statistical Power Analysis for the Behavioral Sciences. New York, NY: Routledge Academic.