1 Loading Libraries

#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 get total summary skip 'by' argument: take_all(mtcars, mean)
## 
## 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'

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)

3 State Your Hypothesis

Note: For your HW, you will choose to run EITHER a one-way ANOVA (a single IV with at least 3 levels) OR a two-way ANOVA (two IVs, each with 2 levels). You will need to specify your hypothesis and customize your code based on the choice you make (i.e., delete code that is not relevant). We will run BOTH versions in the lab for illustrative purposes.

One-Way Hypothesis: We predict that there will be a significant difference in people’s level of perceived stress based on their income level (low, middle, high).

IV = income level DV = stress

4 Check Your Variables

# 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':    3078 obs. of  8 variables:
##  $ ResponseID      : chr  "R_BJN3bQqi1zUMid3" "R_2TGbiBXmAtxywsD" "R_12G7bIqN2wB2N65" "R_39pldNoon8CePfP" ...
##  $ gender          : chr  "f" "m" "m" "f" ...
##  $ income          : chr  "1 low" "1 low" "rather not say" "rather not say" ...
##  $ efficacy        : num  3.4 3.4 2.2 2.8 3 2.4 2.3 3 3 3.7 ...
##  $ stress          : num  3.3 3.3 4 3.2 3.1 3.5 3.3 2.4 2.9 2.7 ...
##  $ moa_independence: num  3.67 3.67 3.5 3 3.83 ...
##  $ support         : num  6 6.75 5.17 5.58 6 ...
##  $ 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)


table(d$income)
## 
##          1 low       2 middle         3 high rather not say 
##            857            867            520            834
# check that all our categorical variables of interest are now factors
str(d)
## 'data.frame':    3078 obs. of  8 variables:
##  $ ResponseID      : chr  "R_BJN3bQqi1zUMid3" "R_2TGbiBXmAtxywsD" "R_12G7bIqN2wB2N65" "R_39pldNoon8CePfP" ...
##  $ gender          : chr  "f" "m" "m" "f" ...
##  $ income          : Factor w/ 4 levels "1 low","2 middle",..: 1 1 4 4 2 4 1 1 3 4 ...
##  $ efficacy        : num  3.4 3.4 2.2 2.8 3 2.4 2.3 3 3 3.7 ...
##  $ stress          : num  3.3 3.3 4 3.2 3.1 3.5 3.3 2.4 2.9 2.7 ...
##  $ moa_independence: num  3.67 3.67 3.5 3 3.83 ...
##  $ support         : num  6 6.75 5.17 5.58 6 ...
##  $ row_id          : Factor w/ 3078 levels "1","2","3","4",..: 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 3078 3.05 0.6      3    3.05 0.59 1.3 4.7   3.4 0.03    -0.17 0.01
# 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 857 3.04 0.6      3    3.04 0.59 1.4 4.6   3.2 0.04    -0.22 0.02
## ------------------------------------------------------------ 
## group: 2 middle
##    vars   n mean  sd median trimmed  mad min max range skew kurtosis   se
## X1    1 867    3 0.6      3       3 0.59 1.3 4.6   3.3 0.01    -0.17 0.02
## ------------------------------------------------------------ 
## group: 3 high
##    vars   n mean   sd median trimmed  mad min max range skew kurtosis   se
## X1    1 520 3.02 0.63      3    3.02 0.59 1.3 4.7   3.4 0.08    -0.26 0.03
## ------------------------------------------------------------ 
## group: rather not say
##    vars   n mean   sd median trimmed  mad min max range skew kurtosis   se
## X1    1 834 3.13 0.57    3.1    3.12 0.59 1.4 4.7   3.3 0.07     -0.1 0.02

5 Check Your Assumptions

5.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 decreases the power of the test (which increases chance of Type Type II error)
  • Homogeneity of variance should be assured (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.

5.1.1 Check levels of IVs

# One-Way
table(d$income)
## 
##          1 low       2 middle         3 high rather not say 
##            857            867            520            834
### Check homogeneity of variance
# 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(stress~income, data = d)
## Levene's Test for Homogeneity of Variance (center = median)
##         Df F value  Pr(>F)  
## group    3  2.4044 0.06562 .
##       3074                  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

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

5.1.2.1 Run a Regression to get these 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))

# 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(stress~income, data = d) #for One-Way

5.1.2.2 Check for outliers (One-Way)

# Cook's distance
plot(reg_model, 4)

# Residuals VS Leverage
plot(reg_model, 5)

5.2 Issues with My Data

Our cell sizes are somewhat unbalanced between the income group levels. A small size for one of the levels of our variable limits our power and increases our Type II error rate.

Levene’s test was not significant for our three-level income variable with the One-Way ANOVA. We are ignoring this and continuing with the analysis anyway for class purposes.

We did not identify or remove any outliers for the One-Way ANOVA.

6 Run an ANOVA

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

7 View Output

nice(aov_model)
## Anova Table (Type 3 tests)
## 
## Response: stress
##   Effect      df  MSE        F  pes p.value
## 1 income 3, 3074 0.36 7.16 *** .007   <.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

8 Visualize Results

# One-Way
afex_plot(aov_model, x = "income")

9 Run Posthoc Tests (One-Way)

emmeans(aov_model, specs="income", adjust="sidak")
##  income         emmean     SE   df lower.CL upper.CL
##  1 low            3.04 0.0205 3074     2.99     3.09
##  2 middle         3.00 0.0203 3074     2.95     3.05
##  3 high           3.02 0.0263 3074     2.96     3.09
##  rather not say   3.13 0.0207 3074     3.08     3.18
## 
## Confidence level used: 0.95 
## Conf-level adjustment: sidak method for 4 estimates
pairs(emmeans(aov_model, specs="income", adjust="sidak"))
##  contrast                  estimate     SE   df t.ratio p.value
##  1 low - 2 middle            0.0435 0.0288 3074   1.509  0.4321
##  1 low - 3 high              0.0212 0.0333 3074   0.636  0.9203
##  1 low - rather not say     -0.0847 0.0291 3074  -2.907  0.0193
##  2 middle - 3 high          -0.0223 0.0332 3074  -0.673  0.9074
##  2 middle - rather not say  -0.1282 0.0290 3074  -4.414  0.0001
##  3 high - rather not say    -0.1058 0.0335 3074  -3.164  0.0086
## 
## P value adjustment: tukey method for comparing a family of 4 estimates

```

10 Write Up Results

10.1 One-Way ANOVA

To test our hypothesis that there will be a significant difference in people’s level of percieved stress based on their income level (low, middle, high), we used a one-way ANOVA. Our data was unbalanced, with a higher number of participants in rather not say (n=834) than in the other income groups low (n=857) middle (n=867) and high (n=520). This significantly reduces the power of our test and increases the chances of a Type II error. We also didn’t identify and removed single outlier following visual analysis of Cook’s Distance and Residuals VS Leverage plots because there was no highly influential outlier.

We found a significant effect of income level, F(3, 3074) = 7.16, p < .001, ηp2 = 0.007 (trivial effect size; Cohen, 1988). Posthoc tests using Sidak’s adjustment revealed that participants who selected “prefer not to say” (M = 3.13, SE = 0.0207) reported more stress than those in the middle income group (M = 3.00, SE = 0.0203) and high income group (M = 3.04, SE = 0.0263, p<0.001). Additionally, participants in the low income group (M = 3.04, SE= 0.0205) reported significantly more stress than those in the high income group (p=0.0193). No other comparisons were statistically significant.

References

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