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
## 
## 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
## Registered S3 method overwritten by 'lme4':
##   method           from
##   na.action.merMod car
## 
## 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 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 reported social support based on gender identity (female, male, prefer not to say, other).

4 Check Your Variables

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

str(d)
## 'data.frame':    716 obs. of  8 variables:
##  $ X          : int  520 2814 3146 3295 717 6056 4753 5365 2044 1965 ...
##  $ gender     : chr  "female" "male" "female" "male" ...
##  $ mhealth    : chr  "none or NA" "none or NA" "none or NA" "none or NA" ...
##  $ covid_pos  : int  0 0 0 0 0 0 0 0 0 0 ...
##  $ covid_neg  : int  0 0 0 0 0 0 0 0 0 0 ...
##  $ isolation_c: num  1 1 1 1 1 1 1 1 1 1 ...
##  $ support    : num  2.83 3 4 4 3.67 ...
##  $ 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$pet <- as.factor(d$gender) 
d$row_id <- as.factor(d$row_id)

# we're going to recode our race variable into two groups: poc and white
# in doing so, we are creating a new variable "poc" that has 2 levels
table(d$gender)
## 
##             female I use another term               male  Prefer not to say 
##                562                 20                124                 10
d$gender[d$gender == "female"] <- "female"
d$gender[d$gender == "male"] <- "male"
d$gender[d$gender == "I use another term"] <- "other"
d$gender[d$gender == "Prefer not to say"] <- "NA"

table(d$gender)
## 
## female   male     NA  other 
##    562    124     10     20
d$gender <- as.factor(d$gender)

# check that all our categorical variables of interest are now factors
str(d)
## 'data.frame':    716 obs. of  9 variables:
##  $ X          : int  520 2814 3146 3295 717 6056 4753 5365 2044 1965 ...
##  $ gender     : Factor w/ 4 levels "female","male",..: 1 2 1 2 1 1 1 1 2 2 ...
##  $ mhealth    : chr  "none or NA" "none or NA" "none or NA" "none or NA" ...
##  $ covid_pos  : int  0 0 0 0 0 0 0 0 0 0 ...
##  $ covid_neg  : int  0 0 0 0 0 0 0 0 0 0 ...
##  $ isolation_c: num  1 1 1 1 1 1 1 1 1 1 ...
##  $ support    : num  2.83 3 4 4 3.67 ...
##  $ row_id     : Factor w/ 716 levels "1","2","3","4",..: 1 2 3 4 5 6 7 8 9 10 ...
##  $ pet        : Factor w/ 4 levels "female","I use another term",..: 1 3 1 3 1 1 1 1 3 3 ...
# check our DV skew and kurtosis
describe(d$support)
##    vars   n mean   sd median trimmed  mad min max range skew kurtosis   se
## X1    1 716 3.48 0.95    3.5    3.51 0.99   1   5     4 -0.3    -0.65 0.04
# we'll use the describeBy() command to view our DV's skew and kurtosis across our IVs' levels
describeBy(d$support, group = d$gender)
## 
##  Descriptive statistics by group 
## group: female
##    vars   n mean   sd median trimmed  mad min max range  skew kurtosis   se
## X1    1 562 3.48 0.94    3.5    3.52 0.99   1   5     4 -0.28    -0.68 0.04
## ------------------------------------------------------------ 
## group: male
##    vars   n mean   sd median trimmed  mad min max range  skew kurtosis   se
## X1    1 124 3.61 0.91   3.67    3.66 0.99   1   5     4 -0.45    -0.35 0.08
## ------------------------------------------------------------ 
## group: NA
##    vars  n mean   sd median trimmed  mad  min  max range skew kurtosis   se
## X1    1 10 2.68 1.21   2.25    2.58 0.99 1.33 4.83   3.5 0.56    -1.34 0.38
## ------------------------------------------------------------ 
## group: other
##    vars  n mean   sd median trimmed  mad  min  max range  skew kurtosis   se
## X1    1 20 2.81 0.83   2.92    2.83 0.86 1.17 4.17     3 -0.29    -0.79 0.19
# also use histograms to examine your continuous variable
hist(d$support)

# REMEMBER your test's level of POWER is determined by your SMALLEST subsample

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$gender)
## 
## female   male     NA  other 
##    562    124     10     20

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(support~gender, data = d)
## Levene's Test for Homogeneity of Variance (center = median)
##        Df F value Pr(>F)
## group   3   0.997 0.3937
##       712

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

# One-Way
reg_model <- lm(support~gender, 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 gender 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 four-level gender type variable with the One-Way ANOVA. We are ignoring this and continuing with the analysis anyway for this class.

We identified no outliers for the One-Way ANOVA.


# Run an ANOVA


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

6 View Output

# One-Way
nice(aov_model)
## Anova Table (Type 3 tests)
## 
## Response: support
##   Effect     df  MSE        F  pes p.value
## 1 gender 3, 712 0.87 6.69 *** .027   <.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

7 Visualize Results

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

8 Run Posthoc Tests (One-Way)

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="gender", adjust="sidak")
##  gender emmean     SE  df lower.CL upper.CL
##  female   3.48 0.0394 712     3.38     3.58
##  male     3.61 0.0839 712     3.40     3.82
##  NA       2.68 0.2950 712     1.95     3.42
##  other    2.81 0.2090 712     2.29     3.33
## 
## Confidence level used: 0.95 
## Conf-level adjustment: sidak method for 4 estimates
pairs(emmeans(aov_model, specs="gender", adjust="sidak"))
##  contrast       estimate     SE  df t.ratio p.value
##  female - male    -0.128 0.0927 712  -1.386  0.5083
##  female - NA       0.800 0.2980 712   2.684  0.0373
##  female - other    0.675 0.2130 712   3.175  0.0085
##  male - NA         0.928 0.3070 712   3.023  0.0138
##  male - other      0.803 0.2250 712   3.569  0.0022
##  NA - other       -0.125 0.3620 712  -0.346  0.9858
## 
## P value adjustment: tukey method for comparing a family of 4 estimates

```

9 Write Up Results

9.1 One-Way ANOVA

To test our hypothesis that there will be a significant difference in people’s level of reported social support based on their gender identity (female, male, other, or prefer not to say), we used a one-way ANOVA. Our data was unbalanced, with many more people who identify as female participating in our survey (n = 562) than who identify as male (n = 124) other (n = 20), or prefer not to say (n = 10). This significantly reduces the power of our test and increases the chances of a Type II error. We did not itentify any outliers following visual analysis of Cook’s Distance and Residuals VS Leverage plots. An insignificant Levene’s test (p = .39) also indicates that our data meets the assumption of homogeneity of variance.

We found a significant effect of gender, F(3, 712) = 6.69, p < .001, ηp2 = .027 (small effect size; Cohen, 1988). Posthoc tests using Tukey’s HSD adjustment revealed that participants who identify as female (M = 3.48, SE = .04) and participants who identify as male (M = 3.61, SE = .08) reported more support than those who identify as something other than male or female (M = 2.81, SE = ..21) and those who preferred not to specify their gender (M = 2.68, SE = .21); participants who identify as male reported the highest amount of social support overall (see Figure 1 for a comparison).

References

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