1 Loading Libraries

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: 'maditr'
## The following object is masked from 'package:base':
## 
##     sort_by
## 
## Use 'expss_output_rnotebook()' to display tables inside R Notebooks.
##  To return to the console output, use 'expss_output_default()'.
## 
## 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 and plot results
## 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(emmeans) # for posthoc tests
## Welcome to emmeans.
## Caution: You lose important information if you filter this package's results.
## See '? untidy'

2 Importing Data

# import the dataset you cleaned previously
# this will be the dataset you'll use throughout the rest of the semester
# use ARC data
d <- read.csv(file="Data/mydata2.csv", header=T)

# new code! this adds a column with a number for each row. it makes it easier when we drop outliers later
d$row_id <- 1:nrow(d)

3 State Your Hypothesis

One-Way: We predict that there will be a significant effect of gender on the need to belong, as measured by the need to belong scale (NBS).

4 Check Your Variables

# you only need to check the variables you're using in the current analysis
# although 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':    3166 obs. of  11 variables:
##  $ gender          : chr  "f" "m" "m" "f" ...
##  $ edu             : chr  "2 Currently in college" "5 Completed Bachelors Degree" "2 Currently in college" "2 Currently in college" ...
##  $ moa_independence: num  3.67 3.67 3.5 3 3.83 ...
##  $ moa_role        : num  3 2.67 2.5 2 2.67 ...
##  $ moa_safety      : num  2.75 3.25 3 1.25 2.25 2.5 4 3.25 2.75 3.5 ...
##  $ moa_maturity    : num  3.67 3.33 3.67 3 3.67 ...
##  $ mindful         : num  2.4 1.8 2.2 2.2 3.2 ...
##  $ belong          : num  2.8 4.2 3.6 4 3.4 4.2 3.9 3.6 2.9 2.5 ...
##  $ socmeduse       : int  47 23 34 35 37 13 37 43 37 29 ...
##  $ ResponseId      : chr  "R_BJN3bQqi1zUMid3" "R_2TGbiBXmAtxywsD" "R_12G7bIqN2wB2N65" "R_39pldNoon8CePfP" ...
##  $ row_id          : int  1 2 3 4 5 6 7 8 9 10 ...
# make our categorical variables factors
d$ResponseId <- as.factor(d$ResponseId) #we'll actually use our ID variable for this analysis, so make sure it's coded as a factor
d$gender <- as.factor(d$gender)
d$row_id <- as.factor(d$row_id)

levels(d$gender)
## [1] ""   "f"  "m"  "nb"
d$gender <- factor(d$gender, levels = c("f","m","nb"))

# you can use the describe() command on an entire dataframe (d) or just on a single variable
describe(d$belong)
##    vars    n mean   sd median trimmed  mad min max range  skew kurtosis   se
## X1    1 2995 3.23 0.61    3.3    3.25 0.59 1.3   5   3.7 -0.26    -0.13 0.01
# we'll use the describeBy() command to view skew and kurtosis across our IVs
describeBy(d$belong, group = d$gender)
## 
##  Descriptive statistics by group 
## group: f
##    vars    n mean   sd median trimmed  mad min max range  skew kurtosis   se
## X1    1 2195 3.28 0.59    3.3     3.3 0.59 1.3   5   3.7 -0.26    -0.18 0.01
## ------------------------------------------------------------ 
## group: m
##    vars   n mean   sd median trimmed  mad min max range  skew kurtosis   se
## X1    1 748 3.08 0.64    3.1    3.09 0.59 1.3 4.9   3.6 -0.16    -0.12 0.02
## ------------------------------------------------------------ 
## group: nb
##    vars  n mean  sd median trimmed  mad min max range skew kurtosis   se
## X1    1 52 3.39 0.5    3.3    3.38 0.59 2.3 4.6   2.3  0.1    -0.33 0.07
# also use histograms to examine your continuous variable
hist(d$belong)

# and cross_cases() to examine your categorical variables
cross_cases(d, gender)
 #Total 
 gender 
   f  2195
   m  748
   nb  52
   #Total cases  2995

5 Check Your Assumptions

5.1 ANOVA Assumptions

  • DV should be normally distributed across levels of the IV
  • All levels of the IVs should have equal number of cases and there should be no empty cells. Cells with low numbers decrease the power of the test (increase change of Type II error)
  • Homogeneity of variance should be assured
  • Outliers should be identified and removed
  • If you have confirmed everything about, the sampling distribution should be normal. (For a demonstration of what the sampling distribution is, go here.)

5.1.1 Check levels of IVs

table(d$gender)
## 
##    f    m   nb 
## 2195  748   52

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
leveneTest(belong~gender, data = d)
## Levene's Test for Homogeneity of Variance (center = median)
##         Df F value  Pr(>F)  
## group    2  3.7348 0.02399 *
##       2992                  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

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

5.1.3.1 Run a Regression

# use this commented out section only if you need to remove outliers
# to drop a single outlier, remove the # at the beginning of the line and use this code:
d <- subset(d, row_id!=c(2235))

# to drop multiple outliers, remove the # at the beginning of the line and 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, and c is our covariate
reg_model <- lm(belong ~ gender, data = d) #for one-way

5.1.3.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 very unbalanced. A small sample size for one of the levels of our variable (nb) limits our power and increases our Type II error rate.

Levene’s test is significant for our three-level gender variable. We are ignoring this and continuing with the analysis anyway, but in the real world this is something we would have to correct for.

We identified and removed a single outlier.

6 Run an ANOVA

aov_model <- aov_ez(data = d,
                    id = "ResponseId",
                    between = c("gender"),
                    dv = "belong",
                    anova_table = list(es = "pes"))
## Warning: Missing values for 171 ID(s), which were removed before analysis:
## R_08MSceiScQuWWMV, R_0BRR4WuOgQsX7Zr, R_10HtfDDQWAF0TUu, R_10ID9IwsGTH8jKh, R_10qEDsOS2lof6ZX, R_12F64OvWX2h8al3, R_1AAIjIxYsrkDKF3, R_1CygJEsM94OqSPG, R_1dEQbcs91OVCSCh, R_1DRRPakJVtTAalx, ... [showing first 10 only]
## Below the first few rows (in wide format) of the removed cases with missing data.
##             ResponseId gender  .
## # 6  R_08MSceiScQuWWMV   <NA> NA
## # 10 R_0BRR4WuOgQsX7Zr   <NA> NA
## # 48 R_10HtfDDQWAF0TUu   <NA> NA
## # 49 R_10ID9IwsGTH8jKh   <NA> NA
## # 57 R_10qEDsOS2lof6ZX   <NA> NA
## # 86 R_12F64OvWX2h8al3   <NA> NA
## Contrasts set to contr.sum for the following variables: gender

7 View Output

Effect size cutoffs from Cohen (1988):

  • η2 = 0.01 indicates a small effect
  • η2 = 0.06 indicates a medium effect
  • η2 = 0.14 indicates a large effect
nice(aov_model)
## Anova Table (Type 3 tests)
## 
## Response: belong
##   Effect      df  MSE         F  pes p.value
## 1 gender 2, 2991 0.36 34.59 *** .023   <.001
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '+' 0.1 ' ' 1

8 Visualize Results

afex_plot(aov_model, x = "gender")

9 Run Posthoc Tests (One-Way)

Only run posthocs if the test is significant! E.g., only run the posthoc tests on gender if there is a main effect for gender.

emmeans(aov_model, specs="gender", adjust="tukey")
## Note: adjust = "tukey" was changed to "sidak"
## because "tukey" is only appropriate for one set of pairwise comparisons
##  gender emmean     SE   df lower.CL upper.CL
##  f        3.28 0.0128 2991     3.25     3.31
##  m        3.08 0.0219 2991     3.03     3.13
##  nb       3.41 0.0838 2991     3.21     3.61
## 
## Confidence level used: 0.95 
## Conf-level adjustment: sidak method for 3 estimates
pairs(emmeans(aov_model, specs="gender", adjust="tukey"))
##  contrast estimate     SE   df t.ratio p.value
##  f - m       0.204 0.0253 2991   8.049  <.0001
##  f - nb     -0.126 0.0848 2991  -1.482  0.2996
##  m - nb     -0.330 0.0866 2991  -3.805  0.0004
## 
## P value adjustment: tukey method for comparing a family of 3 estimates

10 Write Up Results

10.1 One-Way ANOVA

To test our hypothesis that there would be a significant effect of gender on the need to belong, we used a one-way ANOVA. Our data was unbalanced, with many more women participating in our survey (n = 2195) than men (n = 748) or non-binary and other gender participants (n = 52). This significantly reduces the power of our test and increases the chances of a Type II error. We also identified and removed one outlier following visual analysis of a Residuals vs Leverage plot. A significant Levene’s test (p = .024) also indicates that our data violates the assumption of homogeneity of variance. This suggests that there is an increased chance of Type I error. We continued with our analysis for the purpose of this class.

We found a significant effect of gender, F(2,2991) = 34.59, p < .001, ηp2 = .023 (medium effect size; Cohen, 1988). Posthoc tests using Sidak’s test revealed that women reported more of a need to belong than men but less of a need to belong than non-binary and other gender participants, while non-binary and other gender participants reported the highest amount of need to belong overall (see Figure 1 for a comparison).

References

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