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 modify variables or add new variables:
##              let(mtcars, new_var = 42, new_var2 = new_var*hp) %>% head()
## 
## Use 'expss_output_viewer()' to display tables in the RStudio Viewer.
##  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

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/final copy.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

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 gender on stress, as measured by the perceived stress scale (PSS-4).

Two-Way: We predict that there will significant effects of income and race/ethnicity on belonging, as measured by the perceived NTBS scale. We also predict that race/ethnicity and income will interact and that higher income white people will report significantly higher belonging than low income people of color and lowe income white people.

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':    3182 obs. of  7 variables:
##  $ efficacy          : num  3.4 3.4 2.2 2.8 3 2.4 2.3 3 3 3.7 ...
##  $ belong            : int  4 4 2 4 4 3 4 4 4 3 ...
##  $ marriageimportance: int  2 3 2 1 2 3 4 3 4 2 ...
##  $ race_rc           : chr  "white" "white" "white" "other" ...
##  $ politicalviews    : num  2.5 2.5 5 8 4.5 8 4 1.5 5.5 6 ...
##  $ income            : int  3 3 1 1 6 1 2 3 7 1 ...
##  $ row_id            : int  1 2 3 4 5 6 7 8 9 10 ...
# make our categorical variables factors
d$race_rc <- as.factor(d$race_rc)
d$income <- as.factor(d$income)
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

# we're going to recode our race/ethnicity variable into two groups: poc and white
table(d$race_rc)
## 
##      asian      black   hispanic nativeamer      other      white 
##        210        249        286         12         97       2026
d$poc[d$race_rc == "asian"] <- "poc"
d$poc[d$race_rc == "black"] <- "poc"
d$poc[d$race_rc == "mideast"] <- "poc"
d$poc[d$race_rc == "multiracial"] <- "poc"
d$poc[d$race_rc == "other"] <- "poc"
d$poc[d$race_rc == "prefer_not"] <- NA
d$poc[d$race_rc == "white"] <- "white"
table(d$poc)
## 
##   poc white 
##   556  2026
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$belong)
##    vars    n mean sd median trimmed  mad min max range  skew kurtosis   se
## X1    1 3178 3.61  1      4    3.68 1.48   1   5     4 -0.62     0.04 0.02
# we'll use the describeBy() command to view skew and kurtosis across our IVs
describeBy(d$belong, group = d$poc)
## 
##  Descriptive statistics by group 
## group: poc
##    vars   n mean   sd median trimmed  mad min max range  skew kurtosis   se
## X1    1 555 3.52 1.05      4     3.6 1.48   1   5     4 -0.56    -0.09 0.04
## ------------------------------------------------------------ 
## group: white
##    vars    n mean   sd median trimmed  mad min max range  skew kurtosis   se
## X1    1 2025 3.65 0.98      4    3.72 1.48   1   5     4 -0.67     0.12 0.02
describeBy(d$belong, group = d$income)
## 
##  Descriptive statistics by group 
## group: 1
##    vars   n mean   sd median trimmed  mad min max range  skew kurtosis   se
## X1    1 857 3.53 0.99      4    3.58 1.48   1   5     4 -0.49    -0.15 0.03
## ------------------------------------------------------------ 
## group: 2
##    vars   n mean   sd median trimmed  mad min max range  skew kurtosis   se
## X1    1 518 3.48 1.03      4    3.54 1.48   1   5     4 -0.62    -0.04 0.05
## ------------------------------------------------------------ 
## group: 3
##    vars   n mean   sd median trimmed  mad min max range  skew kurtosis   se
## X1    1 361 3.59 1.01      4    3.66 1.48   1   5     4 -0.55    -0.08 0.05
## ------------------------------------------------------------ 
## group: 4
##    vars   n mean   sd median trimmed  mad min max range  skew kurtosis   se
## X1    1 344 3.67 1.01      4    3.75 1.48   1   5     4 -0.57    -0.05 0.05
## ------------------------------------------------------------ 
## group: 5
##    vars   n mean   sd median trimmed  mad min max range  skew kurtosis   se
## X1    1 302 3.73 0.93      4    3.81 1.48   1   5     4 -0.62     0.06 0.05
## ------------------------------------------------------------ 
## group: 6
##    vars   n mean   sd median trimmed  mad min max range  skew kurtosis   se
## X1    1 236 3.76 0.92      4    3.84 0.74   1   5     4 -0.89     1.06 0.06
## ------------------------------------------------------------ 
## group: 7
##    vars   n mean   sd median trimmed  mad min max range  skew kurtosis   se
## X1    1 389 3.76 0.95      4    3.84 1.48   1   5     4 -0.71     0.19 0.05
## ------------------------------------------------------------ 
## group: 8
##    vars   n mean   sd median trimmed  mad min max range  skew kurtosis  se
## X1    1 140 3.61 1.14      4    3.73 1.48   1   5     4 -0.78    -0.03 0.1
## ------------------------------------------------------------ 
## group: 9
##    vars n mean   sd median trimmed mad min max range skew kurtosis   se
## X1    1 7 3.86 0.69      4    3.86   0   3   5     2 0.11    -1.24 0.26
# also use histograms to examine your continuous variable
hist(d$belong)

# and cross_cases() to examine your categorical variables
cross_cases(d, poc, income)
 income 
 1   2   3   4   5   6   7   8   9 
 poc 
   poc  205 100 61 72 26 25 41 17 1
   white  519 319 196 173 225 174 301 104 6
   #Total cases  724 419 257 245 251 199 342 121 7

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$poc)
## 
##   poc white 
##   556  2026
cross_cases(d, poc, income)
 income 
 1   2   3   4   5   6   7   8   9 
 poc 
   poc  205 100 61 72 26 25 41 17 1
   white  519 319 196 173 225 174 301 104 6
   #Total cases  724 419 257 245 251 199 342 121 7
d <- na.omit(d)

d$income <- d$income[1:length(d$race)] 

d$income<- droplevels(d$income, exclude = c("6", "7", "8", "9", "NA"))

# to double-check any changes we made
cross_cases(d, poc, income)
 income 
 1   2   3   4   5 
 poc 
   poc  199 97 61 72 26
   white  513 319 195 172 222
   #Total cases  712 416 256 244 248

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~poc*income, data = d)
## Levene's Test for Homogeneity of Variance (center = median)
##         Df F value Pr(>F)
## group    9  1.0945 0.3633
##       1866

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(1108))

# 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_model2 <- lm(d$belong ~ d$income*d$poc, data = d) #for two-way

5.1.3.2 Check for outliers (Two-Way)

# Cook's distance
plot(reg_model2, 4)

# Residuals vs Leverage
plot(reg_model2, 5)

5.2 Issues with My Data

I had to remove levels 6-9 of my “income” variable, because when I ran the Levene’s Test at first, there was no homogenity. After I removed those levels, there was homogenity. There are far more white people in my dataframe than people of color, which throws off the significance and generalizability of my data, but for this purpose, there isnt anything I can do. I did not have to remove any outliers, and my residuals vs leverage graph looked very good.

6 Run an ANOVA

aov_model2 <- aov_ez(data = d,
                    id = "row_id",
                    between = c("poc","income"),
                    dv = "belong",
                    anova_table = list(es = "pes"))
## Warning: Missing values for 667 ID(s), which were removed before analysis:
## 5, 57, 67, 72, 86, 89, 90, 103, 119, 128, ... [showing first 10 only]
## Below the first few rows (in wide format) of the removed cases with missing data.
##      row_id   poc income .
## # 5       5 white   <NA> 4
## # 55     57 white   <NA> 3
## # 64     67 white   <NA> 4
## # 68     72 white   <NA> 5
## # 78     86 white   <NA> 3
## # 80     89 white   <NA> 3
## Contrasts set to contr.sum for the following variables: poc, income

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_model2)
## Anova Table (Type 3 tests)
## 
## Response: belong
##       Effect      df  MSE      F  pes p.value
## 1        poc 1, 1866 0.98 3.00 + .002    .083
## 2     income 4, 1866 0.98 3.11 * .007    .015
## 3 poc:income 4, 1866 0.98   1.55 .003    .184
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '+' 0.1 ' ' 1

8 Visualize Results

d <- na.omit(d)


afex_plot(aov_model2, x = "income", trace = "poc")

9 Run Posthoc Tests (Two-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_model2, specs="poc", adjust="tukey")
## NOTE: Results may be misleading due to involvement in interactions
## Note: adjust = "tukey" was changed to "sidak"
## because "tukey" is only appropriate for one set of pairwise comparisons
##  poc   emmean     SE   df lower.CL upper.CL
##  poc     3.52 0.0575 1866     3.39     3.65
##  white   3.63 0.0284 1866     3.57     3.69
## 
## Results are averaged over the levels of: income 
## Confidence level used: 0.95 
## Conf-level adjustment: sidak method for 2 estimates
pairs(emmeans(aov_model2, specs="poc", adjust="tukey"))
## NOTE: Results may be misleading due to involvement in interactions
##  contrast    estimate     SE   df t.ratio p.value
##  poc - white   -0.111 0.0641 1866  -1.733  0.0833
## 
## Results are averaged over the levels of: income
emmeans(aov_model2, specs="income", adjust="tukey")
## NOTE: Results may be misleading due to involvement in interactions
## Note: adjust = "tukey" was changed to "sidak"
## because "tukey" is only appropriate for one set of pairwise comparisons
##  income emmean     SE   df lower.CL upper.CL
##  1        3.53 0.0414 1866     3.42     3.63
##  2        3.39 0.0574 1866     3.24     3.54
##  3        3.62 0.0727 1866     3.44     3.81
##  4        3.65 0.0695 1866     3.47     3.83
##  5        3.68 0.1027 1866     3.42     3.95
## 
## Results are averaged over the levels of: poc 
## Confidence level used: 0.95 
## Conf-level adjustment: sidak method for 5 estimates
pairs(emmeans(aov_model2, specs="income", adjust="tukey"))
## NOTE: Results may be misleading due to involvement in interactions
##  contrast          estimate     SE   df t.ratio p.value
##  income1 - income2   0.1336 0.0708 1866   1.887  0.3246
##  income1 - income3  -0.0968 0.0836 1866  -1.157  0.7758
##  income1 - income4  -0.1231 0.0809 1866  -1.521  0.5491
##  income1 - income5  -0.1578 0.1107 1866  -1.425  0.6113
##  income2 - income3  -0.2304 0.0926 1866  -2.487  0.0941
##  income2 - income4  -0.2567 0.0902 1866  -2.845  0.0362
##  income2 - income5  -0.2914 0.1177 1866  -2.476  0.0964
##  income3 - income4  -0.0263 0.1006 1866  -0.261  0.9990
##  income3 - income5  -0.0610 0.1258 1866  -0.485  0.9887
##  income4 - income5  -0.0348 0.1240 1866  -0.280  0.9987
## 
## Results are averaged over the levels of: poc 
## P value adjustment: tukey method for comparing a family of 5 estimates
emmeans(aov_model2, specs="poc", by="income", adjust="sidak")
## income = 1:
##  poc   emmean     SE   df lower.CL upper.CL
##  poc     3.53 0.0702 1866     3.37     3.68
##  white   3.52 0.0438 1866     3.43     3.62
## 
## income = 2:
##  poc   emmean     SE   df lower.CL upper.CL
##  poc     3.23 0.1006 1866     3.00     3.45
##  white   3.56 0.0555 1866     3.43     3.68
## 
## income = 3:
##  poc   emmean     SE   df lower.CL upper.CL
##  poc     3.57 0.1269 1866     3.29     3.86
##  white   3.67 0.0710 1866     3.51     3.83
## 
## income = 4:
##  poc   emmean     SE   df lower.CL upper.CL
##  poc     3.65 0.1168 1866     3.39     3.91
##  white   3.65 0.0756 1866     3.48     3.81
## 
## income = 5:
##  poc   emmean     SE   df lower.CL upper.CL
##  poc     3.62 0.1943 1866     3.18     4.05
##  white   3.75 0.0665 1866     3.60     3.90
## 
## Confidence level used: 0.95 
## Conf-level adjustment: sidak method for 2 estimates
pairs(emmeans(aov_model2, specs="income", by="poc", adjust="sidak"))
## poc = poc:
##  contrast          estimate     SE   df t.ratio p.value
##  income1 - income2   0.3008 0.1227 1866   2.452  0.1024
##  income1 - income3  -0.0461 0.1450 1866  -0.318  0.9978
##  income1 - income4  -0.1251 0.1363 1866  -0.918  0.8900
##  income1 - income5  -0.0877 0.2066 1866  -0.425  0.9932
##  income2 - income3  -0.3470 0.1619 1866  -2.143  0.2025
##  income2 - income4  -0.4260 0.1541 1866  -2.763  0.0457
##  income2 - income5  -0.3886 0.2188 1866  -1.776  0.3882
##  income3 - income4  -0.0790 0.1724 1866  -0.458  0.9909
##  income3 - income5  -0.0416 0.2321 1866  -0.179  0.9998
##  income4 - income5   0.0374 0.2267 1866   0.165  0.9998
## 
## poc = white:
##  contrast          estimate     SE   df t.ratio p.value
##  income1 - income2  -0.0336 0.0707 1866  -0.476  0.9895
##  income1 - income3  -0.1474 0.0834 1866  -1.768  0.3924
##  income1 - income4  -0.1210 0.0873 1866  -1.386  0.6370
##  income1 - income5  -0.2279 0.0796 1866  -2.863  0.0345
##  income2 - income3  -0.1138 0.0901 1866  -1.263  0.7138
##  income2 - income4  -0.0874 0.0937 1866  -0.932  0.8846
##  income2 - income5  -0.1943 0.0866 1866  -2.243  0.1645
##  income3 - income4   0.0264 0.1037 1866   0.255  0.9991
##  income3 - income5  -0.0805 0.0973 1866  -0.827  0.9222
##  income4 - income5  -0.1069 0.1007 1866  -1.062  0.8260
## 
## P value adjustment: tukey method for comparing a family of 5 estimates
emmeans(aov_model2, specs="income", by="poc", adjust="sidak")
## poc = poc:
##  income emmean     SE   df lower.CL upper.CL
##  1        3.53 0.0702 1866     3.35     3.71
##  2        3.23 0.1006 1866     2.97     3.49
##  3        3.57 0.1269 1866     3.25     3.90
##  4        3.65 0.1168 1866     3.35     3.95
##  5        3.62 0.1943 1866     3.12     4.12
## 
## poc = white:
##  income emmean     SE   df lower.CL upper.CL
##  1        3.52 0.0438 1866     3.41     3.64
##  2        3.56 0.0555 1866     3.42     3.70
##  3        3.67 0.0710 1866     3.49     3.85
##  4        3.65 0.0756 1866     3.45     3.84
##  5        3.75 0.0665 1866     3.58     3.92
## 
## Confidence level used: 0.95 
## Conf-level adjustment: sidak method for 5 estimates
pairs(emmeans(aov_model2, specs="income", by="poc", adjust="sidak"))
## poc = poc:
##  contrast          estimate     SE   df t.ratio p.value
##  income1 - income2   0.3008 0.1227 1866   2.452  0.1024
##  income1 - income3  -0.0461 0.1450 1866  -0.318  0.9978
##  income1 - income4  -0.1251 0.1363 1866  -0.918  0.8900
##  income1 - income5  -0.0877 0.2066 1866  -0.425  0.9932
##  income2 - income3  -0.3470 0.1619 1866  -2.143  0.2025
##  income2 - income4  -0.4260 0.1541 1866  -2.763  0.0457
##  income2 - income5  -0.3886 0.2188 1866  -1.776  0.3882
##  income3 - income4  -0.0790 0.1724 1866  -0.458  0.9909
##  income3 - income5  -0.0416 0.2321 1866  -0.179  0.9998
##  income4 - income5   0.0374 0.2267 1866   0.165  0.9998
## 
## poc = white:
##  contrast          estimate     SE   df t.ratio p.value
##  income1 - income2  -0.0336 0.0707 1866  -0.476  0.9895
##  income1 - income3  -0.1474 0.0834 1866  -1.768  0.3924
##  income1 - income4  -0.1210 0.0873 1866  -1.386  0.6370
##  income1 - income5  -0.2279 0.0796 1866  -2.863  0.0345
##  income2 - income3  -0.1138 0.0901 1866  -1.263  0.7138
##  income2 - income4  -0.0874 0.0937 1866  -0.932  0.8846
##  income2 - income5  -0.1943 0.0866 1866  -2.243  0.1645
##  income3 - income4   0.0264 0.1037 1866   0.255  0.9991
##  income3 - income5  -0.0805 0.0973 1866  -0.827  0.9222
##  income4 - income5  -0.1069 0.1007 1866  -1.062  0.8260
## 
## P value adjustment: tukey method for comparing a family of 5 estimates

10 Write Up Results

10.1 Two-Way ANOVA

To examine the potential impact of income and race on the sense of belonging, we conducted a two-way ANOVA using the variables income and race from our dataset. It is important to note that our data had an unequal distribution of participants across income levels and racial categories.

The analysis revealed a significant main effect of income on belonging, F(4, 1884) = 3.04, p = .016, indicating that there are differences in sense of belonging among different income groups. However, the effect size was relatively small (pes = 0.006), suggesting that income is one of the many other factors influencing belonging.

The main effect of race on belonging was not statistically significant, F(1, 1884) = 2.79, p = .094. This implies that there was no overall difference in the sense of belonging between different racial groups.

The interaction between income and race in relation to belonging was not statistically significant, F(4, 1884) = 1.64, p = .163. This indicates that the relationship between income and belonging does not significantly differ across racial categories.

References

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