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
## Warning: package 'ggplot2' was built under R version 4.5.2
## 
## Attaching package: 'ggplot2'
## The following objects are masked from 'package:psych':
## 
##     %+%, alpha
library(expss) # for the cross_cases() command
## Warning: package 'expss' was built under R version 4.5.2
## Loading required package: maditr
## 
## To drop variable use NULL: let(mtcars, am = NULL) %>% 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 
## Warning: package 'afex' was built under R version 4.5.2
## 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
## Warning: package 'ggbeeswarm' was built under R version 4.5.2
library(emmeans) # for posthoc tests
## Warning: package 'emmeans' was built under R version 4.5.2
## 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 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 life satisfaction by people’s level of age, between ages 18-25, 36-45, and 45 and older.

4 Check Your Variables

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

str(d)
## 'data.frame':    2155 obs. of  8 variables:
##  $ ResponseID: chr  "R_BJN3bQqi1zUMid3" "R_2TGbiBXmAtxywsD" "R_12G7bIqN2wB2N65" "R_39pldNoon8CePfP" ...
##  $ stress    : num  3.3 3.3 4 3.2 3.1 3.5 3.3 2.4 2.9 2.7 ...
##  $ swb       : num  4.33 4.17 1.83 5.17 3.67 ...
##  $ mindful   : num  2.4 1.8 2.2 2.2 3.2 ...
##  $ socmeduse : int  47 23 34 35 37 13 37 43 37 29 ...
##  $ age       : chr  "1 between 18 and 25" "1 between 18 and 25" "1 between 18 and 25" "1 between 18 and 25" ...
##  $ edu       : chr  "2 Currently in college" "5 Completed Bachelors Degree" "2 Currently in college" "2 Currently in college" ...
##  $ 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':    2155 obs. of  8 variables:
##  $ ResponseID: chr  "R_BJN3bQqi1zUMid3" "R_2TGbiBXmAtxywsD" "R_12G7bIqN2wB2N65" "R_39pldNoon8CePfP" ...
##  $ stress    : num  3.3 3.3 4 3.2 3.1 3.5 3.3 2.4 2.9 2.7 ...
##  $ swb       : num  4.33 4.17 1.83 5.17 3.67 ...
##  $ mindful   : num  2.4 1.8 2.2 2.2 3.2 ...
##  $ socmeduse : int  47 23 34 35 37 13 37 43 37 29 ...
##  $ age       : Factor w/ 4 levels "1 between 18 and 25",..: 1 1 1 1 1 1 1 1 1 1 ...
##  $ edu       : chr  "2 Currently in college" "5 Completed Bachelors Degree" "2 Currently in college" "2 Currently in college" ...
##  $ row_id    : Factor w/ 2155 levels "1","2","3","4",..: 1 2 3 4 5 6 7 8 9 10 ...
# check our DV skew and kurtosis
describe(d$swb)
##    vars    n mean   sd median trimmed  mad min max range  skew kurtosis   se
## X1    1 2155 4.43 1.33    4.5    4.49 1.48   1   7     6 -0.35    -0.49 0.03
# we'll use the describeBy() command to view our DV's skew and kurtosis across our IVs' levels
describeBy(d$swb, 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 1984 4.44 1.32   4.58     4.5 1.36   1   7     6 -0.38    -0.48 0.03
## ------------------------------------------------------------ 
## group: 2 between 26 and 35
##    vars   n mean   sd median trimmed  mad min max range  skew kurtosis   se
## X1    1 115 4.27 1.32   4.17    4.27 1.48   1   7     6 -0.01    -0.52 0.12
## ------------------------------------------------------------ 
## group: 3 between 36 and 45
##    vars  n mean   sd median trimmed  mad min max range  skew kurtosis   se
## X1    1 38 4.68 1.33   4.83    4.68 1.24   2   7     5 -0.12    -0.81 0.22
## ------------------------------------------------------------ 
## group: 4 over 45
##    vars  n mean   sd median trimmed  mad  min max range skew kurtosis   se
## X1    1 18 4.46 1.45   4.33    4.48 1.36 1.67   7  5.33 0.01    -0.81 0.34
# also use histograms to examine your continuous variable
hist(d$swb)

# REMEMBER your test's level of POWER is determined by your SMALLEST subsample
d <- subset(d, age != "2 between 26 and 35")
d$age <- droplevels(d$age)

table(d$age)
## 
## 1 between 18 and 25 3 between 36 and 45           4 over 45 
##                1984                  38                  18

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

5.1.1 Check levels of IVs

# One-Way
table(d$age)
## 
## 1 between 18 and 25 3 between 36 and 45           4 over 45 
##                1984                  38                  18

5.1.2 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(swb~age, data = d)
## Levene's Test for Homogeneity of Variance (center = median)
##         Df F value Pr(>F)
## group    2  0.0305 0.9699
##       2037

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

5.1.3.1 Run a Regression to get both outlier plots

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

# One-Way
reg_model <- lm(swb~age, data = d) 

5.1.3.2 Check for outliers (One-Way)

# Cook's distance
plot(reg_model, 4)

# Residuals VS Leverage
plot(reg_model, 5)


## Issues with My Data

Our cell sizes are very unbalanced between the age group levels. A small sample 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 age group 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.]

# Run an ANOVA


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

6 View Output

# One-Way
nice(aov_model)
## Anova Table (Type 3 tests)
## 
## Response: swb
##   Effect      df  MSE    F   pes p.value
## 1    age 2, 2037 1.76 0.59 <.001    .554
## ---
## 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

7 Visualize Results

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

8 Write Up Results

8.1 One-Way ANOVA

To test our hypothesis that there will be a significant difference in people’s level of life satisfaction based on age (18 to 25, 36-45, over 45), we used a one-way ANOVA. Our data was unbalanced, with many more people who were 18-25 participating in our survey (n = 1984) than who are 36-45 (n = 38) or over 45 (n = 18). This significantly reduces the power of our test and increases the chances of a Type II error. We did not need to remove outliers following visual analysis of Cook’s Distance and Residuals VS Leverage plots. A non-significant Levene’s test (p = .97) also indicates that our data does not violate the assumption of homogeneity of variance. This suggests that there is not an increased chance of Type I error.

We did not find a significant effect of age, F(2, 2037) = 0.59, p = .55, ηp2 < .001 (trivial effect size; Cohen, 1988). Age did not have a significant impact on participant’s life satisfaction.

References

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