1 Install Packages

#install.packages("afex")
#install.packages("emmeans")
#install.packages("ggbeeswarm")

2 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
## 
## Use magrittr pipe '%>%' to chain several operations:
##              mtcars %>%
##                  let(mpg_hp = mpg/hp) %>%
##                  take(mean(mpg_hp), by = am)
## 
## 
## Attaching package: 'maditr'
## The following object is masked from 'package:base':
## 
##     sort_by
## 
## 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'
library(ggbeeswarm)

3 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/eammi2_data_final.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)

4 State Your Hypothesis

Note: You can chose to run either a one-way ANOVA (a single IV with more than 3 levels) or a two-way/factorial ANOVA (at least two IVs) for the homework. You will need to specify your hypothesis and customize your code based on the choice you make. I will run both versions of the test here for illustrative purposes.

One-Way: We predict that there will be a significant effect of disability on the need to belong.

5 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':    3182 obs. of  28 variables:
##  $ ResponseId      : chr  "R_BJN3bQqi1zUMid3" "R_2TGbiBXmAtxywsD" "R_12G7bIqN2wB2N65" "R_39pldNoon8CePfP" ...
##  $ gender          : chr  "f" "m" "m" "f" ...
##  $ race_rc         : chr  "white" "white" "white" "other" ...
##  $ 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" ...
##  $ edu             : chr  "2 Currently in college" "5 Completed Bachelors Degree" "2 Currently in college" "2 Currently in college" ...
##  $ sibling         : chr  "at least one sibling" "at least one sibling" "at least one sibling" "at least one sibling" ...
##  $ party_rc        : chr  "democrat" "independent" "apolitical" "apolitical" ...
##  $ disability      : chr  NA NA "psychiatric" NA ...
##  $ marriage5       : chr  "are currently divorced from one another" "are currently married to one another" "are currently married to one another" "are currently married to one another" ...
##  $ phys_sym        : chr  "high number of symptoms" "high number of symptoms" "high number of symptoms" "high number of symptoms" ...
##  $ pipwd           : num  NA NA 2.33 NA NA ...
##  $ 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 ...
##  $ idea            : num  3.75 3.88 3.75 3.75 3.5 ...
##  $ swb             : num  4.33 4.17 1.83 5.17 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 ...
##  $ efficacy        : num  3.4 3.4 2.2 2.8 3 2.4 2.3 3 3 3.7 ...
##  $ support         : num  6 6.75 5.17 5.58 6 ...
##  $ socmeduse       : int  47 23 34 35 37 13 37 43 37 29 ...
##  $ usdream         : chr  "american dream is important and achievable for me" "american dream is important and achievable for me" "american dream is not important and maybe not achievable for me" "american dream is not important and maybe not achievable for me" ...
##  $ npi             : num  0.6923 0.1538 0.0769 0.0769 0.7692 ...
##  $ exploit         : num  2 3.67 4.33 1.67 4 ...
##  $ stress          : num  3.3 3.3 4 3.2 3.1 3.5 3.3 2.4 2.9 2.7 ...
##  $ 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$disability <- as.factor(d$disability)
d$row_id <- as.factor(d$row_id)

# 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 3175 3.23 0.6    3.3    3.25 0.59 1.3   5   3.7 -0.26    -0.12 0.01
# we'll use the describeBy() command to view skew and kurtosis across our IVs
describeBy(d$belong, group = d$disability)
## 
##  Descriptive statistics by group 
## group: chronic health
##    vars   n mean   sd median trimmed  mad min max range  skew kurtosis   se
## X1    1 148 3.23 0.62    3.3    3.26 0.59 1.5 4.6   3.1 -0.38    -0.25 0.05
## ------------------------------------------------------------ 
## group: learning
##    vars   n mean   sd median trimmed  mad min max range skew kurtosis   se
## X1    1 124 3.34 0.54    3.3    3.34 0.59 1.9   5   3.1 0.14     0.14 0.05
## ------------------------------------------------------------ 
## group: other
##    vars  n mean   sd median trimmed  mad min max range skew kurtosis   se
## X1    1 87 3.13 0.63    3.1    3.13 0.59 1.6 4.6     3    0    -0.49 0.07
## ------------------------------------------------------------ 
## group: physical
##    vars  n mean   sd median trimmed  mad min max range  skew kurtosis   se
## X1    1 50 3.22 0.62    3.3    3.24 0.59 1.4 4.3   2.9 -0.39     0.11 0.09
## ------------------------------------------------------------ 
## group: psychiatric
##    vars   n mean   sd median trimmed  mad min max range  skew kurtosis   se
## X1    1 383  3.4 0.58    3.4    3.42 0.59 1.7 4.6   2.9 -0.21    -0.39 0.03
## ------------------------------------------------------------ 
## group: sensory
##    vars  n mean   sd median trimmed  mad min max range  skew kurtosis   se
## X1    1 70 3.11 0.65    3.2    3.13 0.74 1.7 4.5   2.8 -0.22    -0.71 0.08
# also use histograms to examine your continuous variable
hist(d$belong)

# and cross_cases() to examine your categorical variables
cross_cases(d, disability)
 #Total 
 disability 
   chronic health  148
   learning  124
   other  87
   physical  50
   psychiatric  385
   sensory  70
   #Total cases  864

6 Check Your Assumptions

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

6.1.1 Check levels of IVs

table(d$disability)
## 
## chronic health       learning          other       physical    psychiatric 
##            148            124             87             50            385 
##        sensory 
##             70
cross_cases(d, disability)
 #Total 
 disability 
   chronic health  148
   learning  124
   other  87
   physical  50
   psychiatric  385
   sensory  70
   #Total cases  864

6.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~disability, data = d)
## Levene's Test for Homogeneity of Variance (center = median)
##        Df F value Pr(>F)
## group   5  1.0681 0.3766
##       856

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

6.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(871))

# 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 ~ disability, data = d) #for one-way

6.1.3.2 Check for outliers (One-Way)

# Cook's distance
plot(reg_model, 4)

# Residuals vs Leverage
plot(reg_model, 5)

6.2 Issues with My Data

I identified and removed a single outlier.

7 Run an ANOVA

aov_model <- aov_ez(data = d,
                    id = "ResponseId",
                    between = c("disability"),
                    dv = "belong",
                    anova_table = list(es = "pes"))
## Warning: Missing values for 2320 ID(s), which were removed before analysis:
## R_00aEuEBExladk8V, R_00cbdktvaY86PbL, R_08MSceiScQuWWMV, R_0AnPM5x7wfwX1bf, R_0B4gtR1WbBTM34Z, R_0BRR4WuOgQsX7Zr, R_0Cj1m8a7TIGCjwl, R_0CJcqtXWnOo8uBz, R_0cVogNKh17252Lf, R_0DHawufGaevNITD, ... [showing first 10 only]
## Below the first few rows (in wide format) of the removed cases with missing data.
##             ResponseId disability   .
## # 1  R_00aEuEBExladk8V       <NA> 3.7
## # 2  R_00cbdktvaY86PbL       <NA> 3.1
## # 6  R_08MSceiScQuWWMV       <NA> 2.6
## # 7  R_0AnPM5x7wfwX1bf       <NA> 2.6
## # 8  R_0B4gtR1WbBTM34Z       <NA> 2.6
## # 10 R_0BRR4WuOgQsX7Zr       <NA> 3.3
## Contrasts set to contr.sum for the following variables: disability

8 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 disability 5, 855 0.35 5.81 *** .033   <.001
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '+' 0.1 ' ' 1

9 Visualize Results

afex_plot(aov_model, x = "disability")

10 Run Posthoc Tests (One-Way)

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

emmeans(aov_model, specs="disability", adjust="tukey")
## Note: adjust = "tukey" was changed to "sidak"
## because "tukey" is only appropriate for one set of pairwise comparisons
##  disability     emmean     SE  df lower.CL upper.CL
##  chronic health   3.23 0.0488 855     3.11     3.36
##  learning         3.34 0.0533 855     3.20     3.48
##  other            3.13 0.0636 855     2.96     3.29
##  physical         3.26 0.0848 855     3.03     3.48
##  psychiatric      3.40 0.0303 855     3.32     3.48
##  sensory          3.11 0.0709 855     2.93     3.30
## 
## Confidence level used: 0.95 
## Conf-level adjustment: sidak method for 6 estimates
pairs(emmeans(aov_model, specs="disability", adjust="tukey"))
##  contrast                     estimate     SE  df t.ratio p.value
##  chronic health - learning     -0.1073 0.0722 855  -1.486  0.6735
##  chronic health - other         0.1085 0.0802 855   1.353  0.7549
##  chronic health - physical     -0.0213 0.0978 855  -0.218  0.9999
##  chronic health - psychiatric  -0.1709 0.0574 855  -2.976  0.0355
##  chronic health - sensory       0.1209 0.0861 855   1.405  0.7242
##  learning - other               0.2158 0.0830 855   2.601  0.0982
##  learning - physical            0.0860 0.1001 855   0.859  0.9560
##  learning - psychiatric        -0.0636 0.0613 855  -1.037  0.9055
##  learning - sensory             0.2283 0.0887 855   2.573  0.1051
##  other - physical              -0.1298 0.1060 855  -1.225  0.8248
##  other - psychiatric           -0.2794 0.0705 855  -3.964  0.0011
##  other - sensory                0.0124 0.0953 855   0.130  1.0000
##  physical - psychiatric        -0.1496 0.0900 855  -1.661  0.5578
##  physical - sensory             0.1422 0.1105 855   1.287  0.7923
##  psychiatric - sensory          0.2918 0.0771 855   3.783  0.0023
## 
## P value adjustment: tukey method for comparing a family of 6 estimates

11 Write Up Results

11.1 One-Way ANOVA

To test my hypothesis that there would be a significant effect of disability on the need to belong, I used a one-way ANOVA. My data was unbalanced, with more people with psychiatric disabilities participating in the survey (n = 385) than chronic health disabilities (n = 148), learning disabilities (n = 124), sensory disabilities (n = 70), physical disabilities (n = 50), and “other” disabilities (n = 87). This significantly reduces the power of my test and increases the chances of a Type II error. I also identified and removed a single outlier following visual analysis of a Residuals vs Leverage plot. I conducted a Levene’s test (p = .38) that was not significant and also indicates that my data meets the assumption of homogeneity of variance.

I found a significant effect of disability, F(5,855) = 5.81, p < .001, ηp2 = .033 (small effect size; Cohen, 1988). Posthoc tests using Tukey’s HSD revealed that people with psychiatric disabilities reported more need to belong than people with chronic health,learning,physical,sensory, and other disabilities. Although the difference may not be substantial, the higher estimated marginal mean value for people with psychiatric disabilities suggests that, on average, individuals in the psychiatric group report a slightly higher need to belong compared to people in the other disability groups.

References

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