library(expss) # for the cross_cases() command
## Loading required package: maditr
##
## To select rows from data: rows(mtcars, am==0)
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/eammi2_clean.csv", header=T)
There will be no gender differences in participation across the income categories (in other words, men (M), women (F), and non-binary (nb) participants will be evenly distributed across the income 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': 3143 obs. of 7 variables:
## $ ResponseId: chr "R_BJN3bQqi1zUMid3" "R_2TGbiBXmAtxywsD" "R_12G7bIqN2wB2N65" "R_39pldNoon8CePfP" ...
## $ gender : chr "f" "m" "m" "f" ...
## $ income_rc : chr "20,000 - 39,999" "20,000 - 39,999" "Rather not say" "Rather not say" ...
## $ stress : num 3.3 3.3 4 3.2 3.1 3.5 3.3 2.4 2.9 2.7 ...
## $ swb : num 4.33 4.17 1.83 5.17 3.67 ...
## $ socmeduse : int 47 23 34 35 37 13 37 43 37 29 ...
## $ belong : num 2.8 4.2 3.6 4 3.4 4.2 3.9 3.6 2.9 2.5 ...
# 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$income_rc <- as.factor(d$income_rc)
table(d$gender, useNA = "always")
##
## f m nb <NA>
## 2303 786 54 0
table(d$income_rc, useNA = "always")
##
## 100,000 - 199,999 20,000 - 39,999 200,000 - 1 million 40,000 - 59,999
## 388 360 139 343
## 60,000 - 79,999 80,000 - 99,999 Over 1 million Rather not say
## 301 236 7 852
## Under 20,000 <NA>
## 517 0
cross_cases(d, gender , income_rc)
|  income_rc | |||||||||
|---|---|---|---|---|---|---|---|---|---|
|  100,000 - 199,999 |  20,000 - 39,999 |  200,000 - 1 million |  40,000 - 59,999 |  60,000 - 79,999 |  80,000 - 99,999 |  Over 1 million |  Rather not say |  Under 20,000 | |
|  gender | |||||||||
|    f | 270 | 256 | 99 | 260 | 233 | 170 | 5 | 624 | 386 |
|    m | 117 | 100 | 37 | 78 | 65 | 61 | 2 | 208 | 118 |
|    nb | 1 | 4 | 3 | 5 | 3 | 5 | 20 | 13 | |
|    #Total cases | 388 | 360 | 139 | 343 | 301 | 236 | 7 | 852 | 517 |
While my data meets the first three assumptions, I don’t have at least 5 participants in all cells. The number of non-binary participants is pretty small, and for one of the income groups it is less than five. The number of participants whose income is over 1 million is also small, with only 5 females and 2 males.
To proceed with this analysis, I will drop the non-binary participants from my sample and I will also drop the participants whose income is over 1 million. 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.
# 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>
## 2303 786 0
d <- subset(d, income_rc != "Over 1 million")
d$income_rc <- droplevels(d$income_rc)
table(d$income_rc, useNA = "always")
##
## 100,000 - 199,999 20,000 - 39,999 200,000 - 1 million 40,000 - 59,999
## 387 356 136 338
## 60,000 - 79,999 80,000 - 99,999 Rather not say Under 20,000
## 298 231 832 504
## <NA>
## 0
# since I made changes to my variables, I am going to re-run the cross_cases() command
cross_cases(d, gender, income_rc)
|  income_rc | ||||||||
|---|---|---|---|---|---|---|---|---|
|  100,000 - 199,999 |  20,000 - 39,999 |  200,000 - 1 million |  40,000 - 59,999 |  60,000 - 79,999 |  80,000 - 99,999 |  Rather not say |  Under 20,000 | |
|  gender | ||||||||
|    f | 270 | 256 | 99 | 260 | 233 | 170 | 624 | 386 |
|    m | 117 | 100 | 37 | 78 | 65 | 61 | 208 | 118 |
|    #Total cases | 387 | 356 | 136 | 338 | 298 | 231 | 832 | 504 |
# 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$income_rc)
# 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$income_rc
## X-squared = 10.582, df = 7, p-value = 0.1579
# 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$income_rc
## d$gender 100,000 - 199,999 20,000 - 39,999 200,000 - 1 million 40,000 - 59,999
## f -2.3159977 -1.2215989 -0.4841898 1.0563245
## m 2.3159977 1.2215989 0.4841898 -1.0563245
## d$income_rc
## d$gender 60,000 - 79,999 80,000 - 99,999 Rather not say Under 20,000
## f 1.5122056 -0.3515620 0.3395373 1.1415246
## m -1.5122056 0.3515620 -0.3395373 -1.1415246
To test my hypothesis that there would be no gender differences in participation across the income categories, I ran a Chi-square test of independence. My 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, I had a low number of non-binary participants and the above 1 million category did not meet the criteria for at least five participants per cell. To proceed with this analysis, I dropped the non-binary and above 1 million category from our sample. The final sample for my analysis can be seen in Table 1:
|  income_rc | ||||||||
|---|---|---|---|---|---|---|---|---|
|  100,000 - 199,999 |  20,000 - 39,999 |  200,000 - 1 million |  40,000 - 59,999 |  60,000 - 79,999 |  80,000 - 99,999 |  Rather not say |  Under 20,000 | |
|  gender | ||||||||
|    f | 270 | 256 | 99 | 260 | 233 | 170 | 624 | 386 |
|    m | 117 | 100 | 37 | 78 | 65 | 61 | 208 | 118 |
As predicted, we did not find a gender difference in participation across the income categories, χ2(7, N = 3082) = 3.38, p = .1579.
–alternative–
As predicted, we did not find a gender difference in participation across the income categories, X^2(7, N = 3082) = 3.38, p = .1579.
I predict that women will report significantly more feelings of needing to belong than men, as measured by the need to belong scale.
str(d)
## 'data.frame': 3082 obs. of 7 variables:
## $ ResponseId: chr "R_BJN3bQqi1zUMid3" "R_2TGbiBXmAtxywsD" "R_12G7bIqN2wB2N65" "R_39pldNoon8CePfP" ...
## $ gender : Factor w/ 2 levels "f","m": 1 2 2 1 2 1 1 1 1 1 ...
## $ income_rc : Factor w/ 8 levels "100,000 - 199,999",..: 2 2 7 7 6 7 8 2 1 7 ...
## $ stress : num 3.3 3.3 4 3.2 3.1 3.5 3.3 2.4 2.9 2.7 ...
## $ swb : num 4.33 4.17 1.83 5.17 3.67 ...
## $ socmeduse : int 47 23 34 35 37 13 37 43 37 29 ...
## $ belong : num 2.8 4.2 3.6 4 3.4 4.2 3.9 3.6 2.9 2.5 ...
d$gender <- as.factor(d$gender)
table(d$gender, useNA = "always")
##
## f m <NA>
## 2298 784 0
# you can use the describe() command on an entire dataframe (d) or just on a single variable (d$pss)
describe(d$belong)
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 3082 3.23 0.6 3.3 3.25 0.59 1.3 5 3.7 -0.26 -0.13 0.01
# also use a histogram to examine your continuous variable
hist(d$belong)
# 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$belong, group=d$gender)
##
## Descriptive statistics by group
## group: f
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 2298 3.28 0.59 3.3 3.3 0.59 1.3 5 3.7 -0.26 -0.17 0.01
## ------------------------------------------------------------
## group: m
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 784 3.08 0.63 3.1 3.1 0.59 1.3 4.9 3.6 -0.16 -0.07 0.02
# last, use a boxplot to examine your continuous and categorical variables together
boxplot(d$belong~d$gender)
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
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.
leveneTest(belong~gender, data = d)
## Levene's Test for Homogeneity of Variance (center = median)
## Df F value Pr(>F)
## group 1 2.747 0.09754 .
## 3080
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
# 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$belong~d$gender)
t_output
##
## Welch Two Sample t-test
##
## data: d$belong by d$gender
## t = 7.8245, df = 1283.6, p-value = 1.058e-14
## alternative hypothesis: true difference in means between group f and group m is not equal to 0
## 95 percent confidence interval:
## 0.1498013 0.2500558
## sample estimates:
## mean in group f mean in group m
## 3.282071 3.082143
# once again, we use our formula to calculate cohen's d
d_output <- cohen.d(d$belong~d$gender)
d_output
##
## Cohen's d
##
## d estimate: 0.334042 (small)
## 95 percent confidence interval:
## lower upper
## 0.2525175 0.4155664
To test my hypothesis that women in our sample would report significantly more feeling of needing to belong than men, I used an two-sample or independent t-test. This required me to drop our non-binary 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 (p = .097). 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. My data met all other assumptions of a t-test.
As predicted, we found that women (M = 3.28, SD = .59) reported significantly higher stress than men (M = 3.08, SD = .63); t(1283.6) = 7.82, p < .001 (see Figure 1). The effect size was calculated using Cohen’s d, with a value of .33 (small effect; Cohen, 1988).
References
Cohen J. (1988). Statistical Power Analysis for the Behavioral Sciences. New York, NY: Routledge Academic.