library(expss) # for the cross_cases() command
## Loading required package: maditr
##
## To select columns from data: columns(mtcars, mpg, vs:carb)
##
## Use 'expss_output_rnotebook()' to display tables inside R Notebooks.
## To return to the console output, use 'expss_output_default()'.
# 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/arc_clean.csv", header=T)
There will be no gender differences in participation across the racial/ethnic categories.
# 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': 1250 obs. of 6 variables:
## $ X : int 1 20 30 31 33 57 68 81 86 104 ...
## $ gender_rc : chr "f" "m" "f" "f" ...
## $ ethnicity_rc: chr "white" "white" "white" "white" ...
## $ pss : num 3.25 3.75 1 3.25 2 4 3.75 1.25 2.5 2.5 ...
## $ phq : num 1.33 3.33 1 2.33 1.11 ...
## $ rse : num 2.3 1.6 3.9 1.7 3.9 1.8 1.3 3.5 2.6 3 ...
# 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$gender <- as.factor(d$gender)
d$ethnicity_rc <- as.factor(d$ethnicity_rc)
table(d$gender, useNA = "always")
##
## f m nb <NA>
## 1020 197 33 0
table(d$ethnicity_rc, useNA = "always")
##
## asian black mideast multiracial other prefer_not
## 139 26 12 65 11 18
## white <NA>
## 979 0
cross_cases(d, gender, ethnicity_rc)
|  ethnicity_rc | |||||||
|---|---|---|---|---|---|---|---|
|  asian |  black |  mideast |  multiracial |  other |  prefer_not |  white | |
|  gender | |||||||
|    f | 110 | 24 | 9 | 54 | 8 | 16 | 799 |
|    m | 27 | 2 | 2 | 8 | 3 | 2 | 153 |
|    nb | 2 | 1 | 3 | 27 | |||
|    #Total cases | 139 | 26 | 12 | 65 | 11 | 18 | 979 |
My data consists of frequencies or counts. Both of my variables are independent. Those being gender and ethnicity. There are not at least 5 or more participants per cell. Only my female groups consists of 5+ participants for each ethnicity. To fix this, I will get rid of my non-binary group of participants and combine my ‘Mideast’ group, ‘Multiracial’ group, and ‘Other’ group together. Still, there are only 2 black men and 2 men who preferred to not say.
# we'll use the subset command to drop our non-binary participants
d <- subset(d, gender != "nb")
# using the '!=' sign here tells R to filter out the indicated criteria
# once we've dropped a level from our factor, we need to use the droplevels() command to remove it, or it will still show as 0
d$gender <- droplevels(d$gender)
table(d$gender, useNA = "always")
##
## f m <NA>
## 1020 197 0
# we'll recode our race variable to combine our native american participants with our other participants
d$ethnicity_rc2 <- d$ethnicity_rc
# create a new variable (race_rc2_ identical to current variable (race_rc)
d$ethnicity_rc2[d$ethnicity_rc == "mideast"] <- "other"
d$ethnicity_rc2[d$ethnicity_rc == "multiracial"] <- "other"
# we will use some of our previous code to recode our Native American participants
d$ethnicity_rc <- droplevels(d$ethnicity_rc)
# once again, we need to use the droplevels() command
table(d$ethnicity_rc2, useNA = "always")
##
## asian black mideast multiracial other prefer_not
## 137 26 0 0 84 18
## white <NA>
## 952 0
d$ethnicity_rc2 <- droplevels(d$ethnicity_rc2)
table(d$ethnicity_rc2, useNA = "always")
##
## asian black other prefer_not white <NA>
## 137 26 84 18 952 0
# since I made changes to my variables, I am going to re-run the cross_cases() command
cross_cases(d, gender, ethnicity_rc2)
| Â ethnicity_rc2Â | |||||
|---|---|---|---|---|---|
|  asian |  black |  other |  prefer_not |  white | |
|  gender | |||||
|    f | 110 | 24 | 71 | 16 | 799 |
|    m | 27 | 2 | 13 | 2 | 153 |
|    #Total cases | 137 | 26 | 84 | 18 | 952 |
# 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$gender, d$ethnicity_rc2)
## Warning in chisq.test(d$gender, d$ethnicity_rc2): Chi-squared approximation may
## be incorrect
# 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$gender and d$ethnicity_rc2
## X-squared = 3.0173, df = 4, p-value = 0.5549
# 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$ethnicity_rc2
## d$gender asian black other prefer_not white
## f -1.1876214 1.1887726 0.1833969 0.5890756 0.2080878
## m 1.1876214 -1.1887726 -0.1833969 -0.5890756 -0.2080878
# the apply_labels() command can make our levels long and difficult to work with, so we'll only set them at the end
d <- apply_labels (d,
gender = "Gender",
gender = c("Women" = "f", "Men" = "m"),
ethnicity_rc2 = "Ethnicity",
ethnicity_rc2 = c("Asian" = "asian",
"Black/African American" = "black",
"Small Groups Combined" = "other",
"White" = "white"))
## Warning in set_val_lab.default(data[[curr_name]], curr_lab): You are trying to
## put value labels on factor. It can lead to unexpected results. Factor will be
## converted to character.
## Warning in set_val_lab.default(data[[curr_name]], curr_lab): You are trying to
## put value labels on factor. It can lead to unexpected results. Factor will be
## converted to character.
To test my hypothesis that there would be no gender differences in participation across the ethnic categories, I ran a Chi-square test of Independence. My variables met most of the necessary criteria for the test, but some of my groups did not meet the criteria for at least five participants per cell. To proceed with this analysis, we dropped the non-binary group and combined multiracial and mideast ethnicity groups. The final sample for my analysis can be seen in Table 1:
|  Ethnicity | |||||
|---|---|---|---|---|---|
|  Asian |  Black/African American |  Small Groups Combined |  prefer_not |  White | |
|  Gender | |||||
|    Women | 110 | 24 | 71 | 16 | 799 |
|    Men | 27 | 2 | 13 | 2 | 153 |
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/arc_clean.csv", header=T)
I predict that women will report significantly more self-esteem issues than men, as measured by the Rosenberg Self Esteem Scale (RSE-10).
# 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': 1250 obs. of 6 variables:
## $ X : int 1 20 30 31 33 57 68 81 86 104 ...
## $ gender_rc : chr "f" "m" "f" "f" ...
## $ ethnicity_rc: chr "white" "white" "white" "white" ...
## $ pss : num 3.25 3.75 1 3.25 2 4 3.75 1.25 2.5 2.5 ...
## $ phq : num 1.33 3.33 1 2.33 1.11 ...
## $ rse : num 2.3 1.6 3.9 1.7 3.9 1.8 1.3 3.5 2.6 3 ...
d$gender_rc <- as.factor(d$gender_rc)
table(d$gender_rc, useNA = "always")
##
## f m nb <NA>
## 1020 197 33 0
# you can use the describe() command on an entire datafrom (d) or just on a single variable (d$pss)
describe(d$rse)
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 1250 2.63 0.71 2.7 2.65 0.74 1 4 3 -0.22 -0.7 0.02
# also use a histogram to examine your continuous variable
hist(d$rse)
# 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$rse, group=d$gender_rc)
##
## Descriptive statistics by group
## group: f
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 1020 2.61 0.7 2.7 2.63 0.74 1 4 3 -0.2 -0.67 0.02
## ------------------------------------------------------------
## group: m
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 197 2.86 0.7 3 2.9 0.74 1 4 3 -0.5 -0.43 0.05
## ------------------------------------------------------------
## group: nb
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 33 1.81 0.52 1.8 1.78 0.44 1 3.1 2.1 0.39 -0.5 0.09
# last, use a boxplot to examine your continuous and categorical variables together
boxplot(d$pss~d$gender_rc)
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!
d <- subset(d, gender_rc != "nb")
table(d$gender_rc, useNA = "always")
##
## f m nb <NA>
## 1020 197 0 0
d$gender_rc <- droplevels(d$gender_rc)
# 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(rse~gender_rc, data = d)
## Levene's Test for Homogeneity of Variance (center = median)
## Df F value Pr(>F)
## group 1 0.657 0.4178
## 1215
As you can see, our data is very close to 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 drop the non-binary participants from my sample. I will make a note to discuss this issue in my Method write-up and in my Discussion as a limitation of my study.
My data also has some potential issues regarding homogeneity of variance. Although Levene’s test was not significant, it was close to the significance threshold. To accommodate any potential heterogeneity of variance, I will use Welch’s t-test instead of Student’s t-test.
# once again, subetting to drop the nb group
d <- subset(d, gender_rc != "nb")
d$gender_rc <- droplevels(d$gender_rc) # using droplevels() to drop the empty factor
# 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$rse~d$gender_rc)
t_output
##
## Welch Two Sample t-test
##
## data: d$rse by d$gender_rc
## t = -4.5528, df = 277.36, p-value = 7.933e-06
## alternative hypothesis: true difference in means between group f and group m is not equal to 0
## 95 percent confidence interval:
## -0.3549365 -0.1406512
## sample estimates:
## mean in group f mean in group m
## 2.613627 2.861421
# once again, we use our formula to calculate cohen's d
d_output <- cohen.d(d$rse~d$gender_rc)
d_output
##
## Cohen's d
##
## d estimate: -0.3538613 (small)
## 95 percent confidence interval:
## lower upper
## -0.5071922 -0.2005304
To test our hypothesis that women in our sample would report significantly more stress than men, we used an two-sample or independent t-test. This required us to drop our non-binary and other gender participants 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 some signs of heterogeneity. This suggests that there is an increased chance of Type I error. To correct for this possible issue, we use Welch’s t-test, which does not assume homogeneity of variance. Our data met all other assumptions of a t-test.
References
Cohen J. (1988). Statistical Power Analysis for the Behavioral Sciences. New York, NY: Routledge Academic.