library(expss) # for the cross_cases() command
## Loading required package: maditr
##
## To drop variable use NULL: let(mtcars, am = NULL) %>% head()
library(psych) # for the describe() command
library(car) # for the leveneTest() command
## Loading required package: carData
##
## Attaching package: 'car'
## The following object is masked from 'package:psych':
##
## logit
## The following object is masked from 'package:expss':
##
## recode
library(effsize) # for the cohen.d() command
##
## Attaching package: 'effsize'
## The following object is masked from 'package:psych':
##
## cohen.d
# import the dataset you cleaned previously
# this will be the dataset you'll use throughout the rest of the semester
d <- read.csv(file="Data/EAMMIfinal.csv", header=T)
There will be differences in income across racial/ethnicity origins. # Chi-square: 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:
## $ ResponseId: chr "R_BJN3bQqi1zUMid3" "R_2TGbiBXmAtxywsD" "R_12G7bIqN2wB2N65" "R_39pldNoon8CePfP" ...
## $ mindful : num 2.4 1.8 2.2 2.2 3.2 ...
## $ stress : num 3.1 3.8 4.3 3 3.3 3.7 3.4 2.2 2.9 2.6 ...
## $ support : num 6 6.75 5.17 5.58 6 ...
## $ swb : num 4.33 4.17 1.83 5.17 3.67 ...
## $ race_rc : chr "white" "white" "white" "other" ...
## $ income : int 3 3 1 1 6 1 2 3 7 1 ...
# we can see in the str() command that our categorical variables are being read as character or string variables
# to correct this, we'll use the as.factor() command
d$race_rc <- as.factor(d$race_rc)
table(d$race_rc, useNA = "always")
##
## asian black hispanic multiracial nativeamer other
## 210 249 286 293 12 97
## white <NA>
## 2026 9
cross_cases(d, income, race_rc)
| race_rc | |||||||
|---|---|---|---|---|---|---|---|
| asian | black | hispanic | multiracial | nativeamer | other | white | |
| income | |||||||
| 1 | 87 | 76 | 64 | 68 | 4 | 42 | 519 |
| 2 | 25 | 56 | 45 | 53 | 1 | 19 | 319 |
| 3 | 21 | 30 | 57 | 46 | 1 | 10 | 196 |
| 4 | 27 | 37 | 67 | 29 | 2 | 8 | 173 |
| 5 | 14 | 11 | 26 | 24 | 1 | 1 | 225 |
| 6 | 7 | 12 | 11 | 26 | 6 | 174 | |
| 7 | 19 | 18 | 12 | 33 | 2 | 4 | 301 |
| 8 | 10 | 3 | 4 | 14 | 1 | 4 | 104 |
| 9 | 1 | 6 | |||||
| #Total cases | 210 | 244 | 286 | 293 | 12 | 94 | 2017 |
While my data meets the first three assumptions, I don’t have at least 5 participants in all cells. The number of people in level 8+ in income is fairly small across all races. The number of Native American participants and “other” race participants is also small.
To proceed with this analysis, I will drop the Native American category and the “other category”, while adding levels combining levles 7,8,& 9 into 7+. Dropping participants is always a difficult choice, and has the potential to further marginalize already minoritized groups, but it’s a necessary compromise for my analysis. I will make a note to discuss this issue in my Method write-up and in my Discussion as a limitation of my study.
# second thing we"ll cover is how to combine categories
# we'll recode our race variable to combine our native american participants with our other participants
# create a new variable (race_rc2_ identical to current variable (race_rc)
d$race_rc2 <- d$race_rc
# we will use some of our previous code to recode our Native American participants
d$race_rc2[d$race_rc == "nativeamer"] <- "other"
d$income[d$income == "9"] <- 8
d$income[d$income == "8"] <- 7
table(d$race_rc2, useNA = "always")
##
## asian black hispanic multiracial nativeamer other
## 210 249 286 293 0 109
## white <NA>
## 2026 9
# once again, we need to use the droplevels() command
d$race_rc2 <- droplevels(d$race_rc2)
# since I made changes to my variables, I am going to re-run the cross_cases() command
cross_cases(d, income, race_rc2)
| race_rc2 | ||||||
|---|---|---|---|---|---|---|
| asian | black | hispanic | multiracial | other | white | |
| income | ||||||
| 1 | 87 | 76 | 64 | 68 | 46 | 519 |
| 2 | 25 | 56 | 45 | 53 | 20 | 319 |
| 3 | 21 | 30 | 57 | 46 | 11 | 196 |
| 4 | 27 | 37 | 67 | 29 | 10 | 173 |
| 5 | 14 | 11 | 26 | 24 | 2 | 225 |
| 6 | 7 | 12 | 11 | 26 | 6 | 174 |
| 7 | 29 | 22 | 16 | 47 | 11 | 411 |
| #Total cases | 210 | 244 | 286 | 293 | 106 | 2017 |
# we use the chisq.test() command to run our chi-square test
# the only arguments we need to specify are the variables we're using for the chi-square test
# we are saving the output from our chi-square test to the chi_output object so we can view it again later
chi_output <- chisq.test(d$income, d$race_rc2)
# to view the results of our chi-square test, we just have to call up the output we saved
chi_output
##
## Pearson's Chi-squared test
##
## data: d$income and d$race_rc2
## X-squared = 214.89, df = 30, p-value < 2.2e-16
# to view the standardized residuals, we use the $ operator to access the stdres element of the chi_output file that we created
chi_output$stdres
## d$race_rc2
## d$income asian black hispanic multiracial other white
## 1 4.7764560 1.4236248 -1.9405511 -1.6313051 3.7979820 -2.5494549
## 2 -1.8256638 2.8702739 -0.3250581 0.8129902 0.6940844 -1.2061873
## 3 -0.6779117 0.4376370 4.7313984 2.4060730 -0.3491801 -4.0426409
## 4 0.9584994 2.2444580 7.1556560 -0.5604367 -0.4826074 -5.5030993
## 5 -1.4798810 -2.7976935 -0.2882674 -0.8418431 -2.7350692 4.0308662
## 6 -2.3633176 -1.5825636 -2.4485335 0.9537522 -0.7236387 3.2652295
## 7 -1.2678638 -3.4504329 -5.3790379 -0.4511367 -1.8425737 6.7558880
To test our hypothesis that there will be differences in income across racial/ethnicity origins, we ran a Chi-square test of independence. Our variables met most of the criteria for running a chi-square test of analysis (it used frequences, the variables were independent, and there were two variables). However, we had a low number of Native American and “other” participants as well as low number in income levels 8 & 9 did not meet the criteria for at least five participants per cell. To proceed with this analysis, we dropped the Native American and “other” category and combined our income levels 8 & 9 with levels 7 (100,000+). The final sample for analysis can be seen in Table 1:
| race_rc2 | ||||||
|---|---|---|---|---|---|---|
| asian | black | hispanic | multiracial | other | white | |
| income | ||||||
| 1 | 87 | 76 | 64 | 68 | 46 | 519 |
| 2 | 25 | 56 | 45 | 53 | 20 | 319 |
| 3 | 21 | 30 | 57 | 46 | 11 | 196 |
| 4 | 27 | 37 | 67 | 29 | 10 | 173 |
| 5 | 14 | 11 | 26 | 24 | 2 | 225 |
| 6 | 7 | 12 | 11 | 26 | 6 | 174 |
| 7 | 29 | 22 | 16 | 47 | 11 | 411 |
As predicted, we did find a difference in income across the racial/ethnic categories, X^2(24, N = 3064) = 192.65, p = < 0.001.
We predict that minorities will report significantly more stress than white people, as measured by the perceived stress scale.
# 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 8 variables:
## $ ResponseId: chr "R_BJN3bQqi1zUMid3" "R_2TGbiBXmAtxywsD" "R_12G7bIqN2wB2N65" "R_39pldNoon8CePfP" ...
## $ mindful : num 2.4 1.8 2.2 2.2 3.2 ...
## $ stress : num 3.1 3.8 4.3 3 3.3 3.7 3.4 2.2 2.9 2.6 ...
## $ support : num 6 6.75 5.17 5.58 6 ...
## $ swb : num 4.33 4.17 1.83 5.17 3.67 ...
## $ race_rc : Factor w/ 7 levels "asian","black",..: 7 7 7 6 7 7 7 7 4 4 ...
## $ income : num 3 3 1 1 6 1 2 3 7 1 ...
## $ race_rc2 : Factor w/ 6 levels "asian","black",..: 6 6 6 5 6 6 6 6 4 4 ...
table(d$race_rc2, useNA = "always")
##
## asian black hispanic multiracial other white
## 210 249 286 293 109 2026
## <NA>
## 9
table(d$race_rc2)
##
## asian black hispanic multiracial other white
## 210 249 286 293 109 2026
d$poc[d$race_rc2 == "asian"] <- "poc"
d$poc[d$race_rc2 == "black"] <- "poc"
d$poc[d$race_rc2 == "mideast"] <- "poc"
d$poc[d$race_rc2 == "multiracial"] <- "poc"
d$poc[d$race_rc2 == "other"] <- "poc"
d$poc[d$race_rc2 == "prefer_not"] <- NA
d$poc[d$race_rc2 == "white"] <- "white"
table(d$poc)
##
## poc white
## 861 2026
d$poc <- as.factor(d$poc)
# you can use the describe() command on an entire dataframe (d) or just on a single variable (d$pss)
describe(d$stress)
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 3175 3.06 0.66 3.1 3.06 0.59 1 5 4 0.04 -0.04 0.01
# also use a histogram to examine your continuous variable
hist(d$stress)
# can use the describeBy() command to view the means and standard deviations by group
# it's very similar to the describe() command but splits the dataframe according to the 'group' variable
describeBy(d$stress, group=d$poc)
##
## Descriptive statistics by group
## group: poc
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 858 3.07 0.62 3.1 3.07 0.59 1 5 4 0.09 0.39 0.02
## ------------------------------------------------------------
## group: white
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 2025 3.05 0.67 3 3.05 0.59 1.2 5 3.8 0.03 -0.2 0.01
# last, use a boxplot to examine your continuous and categorical variables together
boxplot(d$stress~d$poc)
We can test whether the variances of our two groups are equal using Levene’s test. The null hypothesis is that the variance between the two groups is equal, which is the result we want. So when running Levene’s test we’re hoping for a non-significant result!
# use the leveneTest() command from the car package to test homogeneity of variance
# uses the same 'formula' setup that we'll use for our t-test: formula is y~x, where y is our DV and x is our IV
leveneTest(stress~poc, data = d)
## Levene's Test for Homogeneity of Variance (center = median)
## Df F value Pr(>F)
## group 1 10.697 0.001086 **
## 2881
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
As you can see, our data is significant. When running a t-test, we can account for heterogeneity in our variance by using Welch’s t-test, which does not have the same assumptions as Student’s t-test (the default type of t-test) about variance. R defaults to using Welch’s t-test so this doesn’t require any changes on our part! Even if your data has no issues with homogeneity of variance, you’ll still use Welch’s t-test – it handles the potential issues around variance well and there are no real downsides. We’re just using Levene’s test here to get into the habit of changing the homogeneity of our variance, even if we already have a solution for any potential problems.
My independent variable has more than two levels. To proceed with this analysis, I will combine the minority participants into a category of poc. I will make a note to discuss this issue in my Method write-up and in my Discussion as a limitation of my study.
# very simple! we specify the dataframe alongside the variables instead of having a separate argument for the dataframe like we did for leveneTest()
t_output <- t.test(d$stress~d$poc)
t_output
##
## Welch Two Sample t-test
##
## data: d$stress by d$poc
## t = 0.93372, df = 1732.3, p-value = 0.3506
## alternative hypothesis: true difference in means between group poc and group white is not equal to 0
## 95 percent confidence interval:
## -0.02671635 0.07526646
## sample estimates:
## mean in group poc mean in group white
## 3.074942 3.050667
# once again, we use our formula to calculate cohen's d
d_output <- cohen.d(d$stress~d$poc)
d_output
##
## Cohen's d
##
## d estimate: 0.03688533 (negligible)
## 95 percent confidence interval:
## lower upper
## -0.04299275 0.11676341
To test our hypothesis that minorities will report significantly more stress than white people, as measured by the perceived stress scale, we used an two-sample or independent t-test. This required us to combine our minority participants into one category from our sample, as we are limited to a two-group comparison when using this test. We tested the homogeneity of variance with Levene’s test and found significant homegeneity (p = .0006). Our data met all other assumptions of a t-test.
Unlike predicted, we found that poc (M = 3.08, SD = .61) reported slightly higher stress than white people (M = 3.05, SD = .67); t(1457.6) = 1.2, p = .229 (see Figure 1). The effect size was calculated using Cohen’s d, with a value of .05 (negligible; Cohen, 1988).
References
Cohen J. (1988). Statistical Power Analysis for the Behavioral Sciences. New York, NY: Routledge Academic.