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 aggregate data: take(mtcars, mean_mpg = mean(mpg), by = am)
##
## 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'
# 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/mydata.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)
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 race/ethnicity on perceived social support, as measured by the multidimensional scale of perceived social support.
# 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': 3082 obs. of 7 variables:
## $ race_rc : chr "white" "white" "white" "other" ...
## $ 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" ...
## $ moa_independence: num 3.67 3.67 3.5 3 3.83 ...
## $ idea : num 3.75 3.88 3.75 3.75 3.5 ...
## $ support : num 6 6.75 5.17 5.58 6 ...
## $ socmeduse : int 47 23 34 35 37 13 37 43 37 29 ...
## $ row_id : int 1 2 3 4 5 6 7 8 9 10 ...
# make our categorical variables factors
d$row_id <- as.factor(d$row_id) #we'll actually use our ID variable for this analysis, so make sure it's coded as a factor
d$race_rc <- as.factor(d$race_rc)
d$usdream <- as.factor(d$usdream)
d$row_id <- as.factor(d$row_id)
# we're going to recode our race/ethnicity variable into two groups: poc and white
table(d$race_rc)
##
## asian black hispanic multiracial nativeamer other
## 201 234 277 285 10 91
## white
## 1984
d$poc[d$race_rc == "asian"] <- "poc"
d$poc[d$race_rc == "black"] <- "poc"
d$poc[d$race_rc == "hispanic"] <- "poc"
d$poc[d$race_rc == "multiracial"] <- "poc"
d$poc[d$race_rc == "other"] <- "poc"
d$poc[d$race_rc == "nativeamer"] <- "poc"
d$poc[d$race_rc == "prefer_not"] <- NA
d$poc[d$race_rc == "white"] <- "white"
table(d$poc)
##
## poc white
## 1098 1984
d$poc <- as.factor(d$poc)
# you can use the describe() command on an entire dataframe (d) or just on a single variable
describe(d$support)
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 3082 5.53 1.13 5.75 5.66 0.99 0 7 7 -1.1 1.43 0.02
# we'll use the describeBy() command to view skew and kurtosis across our IVs
describeBy(d$support, group = d$race_rc)
##
## Descriptive statistics by group
## group: asian
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 201 5.34 1.24 5.58 5.48 1.24 1.42 7 5.58 -0.99 0.66 0.09
## ------------------------------------------------------------
## group: black
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 234 5.14 1.31 5.33 5.23 1.24 1 7 6 -0.67 -0.11 0.09
## ------------------------------------------------------------
## group: hispanic
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 277 5.41 1.19 5.58 5.53 1.24 1 7 6 -0.95 0.97 0.07
## ------------------------------------------------------------
## group: multiracial
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 285 5.39 1.17 5.67 5.51 1.11 0 7 7 -1.12 1.85 0.07
## ------------------------------------------------------------
## group: nativeamer
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 10 5.71 1.41 6.21 5.9 0.74 2.92 7 4.08 -1.03 -0.63 0.45
## ------------------------------------------------------------
## group: other
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 91 5.45 0.95 5.67 5.49 0.99 2.83 7 4.17 -0.44 -0.41 0.1
## ------------------------------------------------------------
## group: white
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 1984 5.64 1.06 5.83 5.76 0.99 1 7 6 -1.18 1.85 0.02
# also use histograms to examine your continuous variable
hist(d$support)
# and cross_cases() to examine your categorical variables
cross_cases(d, race_rc, poc)
| poc | ||
|---|---|---|
| poc | white | |
| race_rc | ||
| asian | 201 | |
| black | 234 | |
| hispanic | 277 | |
| multiracial | 285 | |
| nativeamer | 10 | |
| other | 91 | |
| white | 1984 | |
| #Total cases | 1098 | 1984 |
table(d$race_rc)
##
## asian black hispanic multiracial nativeamer other
## 201 234 277 285 10 91
## white
## 1984
# 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(support~race_rc, data = d)
## Levene's Test for Homogeneity of Variance (center = median)
## Df F value Pr(>F)
## group 6 5.3969 1.472e-05 ***
## 3075
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
# 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(1750))
# to drop multiple outliers, remove the # at the beginning of the line and use this code:
d <- subset(d, row_id!=c(1750) & row_id!=c(664) & row_id!=c(72))
# 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(support ~ race_rc, data = d) #for one-way
# Cook's distance
plot(reg_model, 4)
# Residuals vs Leverage
plot(reg_model, 5)
Our cell sizes are very unbalanced. 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 is significant for our seven-level race/ethnicity 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 three outliers.
aov_model <- aov_ez(data = d,
id = "row_id",
between = c("race_rc"),
dv = "support",
anova_table = list(es = "pes"))
## Contrasts set to contr.sum for the following variables: race_rc
Effect size cutoffs from Cohen (1988):
nice(aov_model)
## Anova Table (Type 3 tests)
##
## Response: support
## Effect df MSE F pes p.value
## 1 race_rc 6, 3072 1.24 10.93 *** .021 <.001
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '+' 0.1 ' ' 1
afex_plot(aov_model, x = "race_rc")
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="race_rc", adjust="tukey")
## Note: adjust = "tukey" was changed to "sidak"
## because "tukey" is only appropriate for one set of pairwise comparisons
## race_rc emmean SE df lower.CL upper.CL
## asian 5.34 0.0786 3072 5.12 5.55
## black 5.14 0.0728 3072 4.94 5.34
## hispanic 5.41 0.0669 3072 5.23 5.59
## multiracial 5.39 0.0660 3072 5.22 5.57
## nativeamer 6.25 0.4209 3072 5.12 7.38
## other 5.45 0.1167 3072 5.14 5.76
## white 5.64 0.0250 3072 5.58 5.71
##
## Confidence level used: 0.95
## Conf-level adjustment: sidak method for 7 estimates
pairs(emmeans(aov_model, specs="race_rc", adjust="tukey"))
## contrast estimate SE df t.ratio p.value
## asian - black 0.1954 0.1071 3072 1.825 0.5312
## asian - hispanic -0.0770 0.1032 3072 -0.747 0.9896
## asian - multiracial -0.0590 0.1026 3072 -0.576 0.9975
## asian - nativeamer -0.9146 0.4282 3072 -2.136 0.3317
## asian - other -0.1133 0.1407 3072 -0.805 0.9845
## asian - white -0.3068 0.0824 3072 -3.721 0.0038
## black - hispanic -0.2725 0.0989 3072 -2.756 0.0853
## black - multiracial -0.2545 0.0982 3072 -2.590 0.1292
## black - nativeamer -1.1100 0.4272 3072 -2.598 0.1267
## black - other -0.3088 0.1376 3072 -2.244 0.2722
## black - white -0.5022 0.0770 3072 -6.524 <.0001
## hispanic - multiracial 0.0180 0.0940 3072 0.192 1.0000
## hispanic - nativeamer -0.8375 0.4262 3072 -1.965 0.4374
## hispanic - other -0.0363 0.1346 3072 -0.269 1.0000
## hispanic - white -0.2297 0.0714 3072 -3.216 0.0223
## multiracial - nativeamer -0.8556 0.4261 3072 -2.008 0.4097
## multiracial - other -0.0543 0.1341 3072 -0.405 0.9997
## multiracial - white -0.2477 0.0705 3072 -3.511 0.0082
## nativeamer - other 0.8013 0.4368 3072 1.834 0.5248
## nativeamer - white 0.6078 0.4217 3072 1.441 0.7792
## other - white -0.1935 0.1194 3072 -1.620 0.6692
##
## P value adjustment: tukey method for comparing a family of 7 estimates
To test our hypothesis that there would be a significant effect of race/ethnicity on perceive social support, we used a one-way ANOVA. Our data was unbalanced, with many more white participantsin our survey (n = 1984) than participants from other race/ethnicity groups (n = 1098). This significantly reduces the power of our test and increases the chances of a Type II error. We also identified and removed three outliers following visual analysis of a Residuals vs Leverage plot. A significant Levene’s test (p = .001) 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 race/ethnicity, F(6,3072) = 10.93, p < .001, ηp2 = .021 (small to medium effect size; Cohen, 1988). Posthoc tests using Tukey’s HSD revealed that white participants reported higher perceived social support than asian, black, hispanic, and multiracial participants. Native American participants, though not significantly different from other groups, reported the highest mean support overall (see Figure 1 for a comparison).
References
Cohen J. (1988). Statistical Power Analysis for the Behavioral Sciences. New York, NY: Routledge Academic.