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/Data Cleaning & Basic Statistics HW/clean recoded.csv", header=T)
There will be a significant difference between the gender in all 4 continuous variables (in other words, men, women, and non-binary participants will be unevenly distributed across the gender 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': 3182 obs. of 7 variables:
## $ ResponseId: chr "R_BJN3bQqi1zUMid3" "R_2TGbiBXmAtxywsD" "R_12G7bIqN2wB2N65" "R_39pldNoon8CePfP" ...
## $ swb : num 4.33 4.17 1.83 5.17 3.67 ...
## $ belong : num 3.4 3.4 3.6 3.6 3.2 3.4 3.5 3.2 3.5 2.7 ...
## $ support : num 6 6.75 5.17 5.58 6 ...
## $ SocMedia : num 4.27 2.09 3.09 3.18 3.36 ...
## $ gender : chr "f" "m" "m" "f" ...
## $ income : chr "20,000-39,999" "20,000-39,999" "rather not say" "rather not say" ...
# 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 <- as.factor(d$income)
table(d$gender, useNA = "always")
##
## f m nb <NA>
## 2332 792 54 4
table(d$income, useNA = "always")
##
## 100,000-199,999 20,000-39,999 200,000-1 million
## 389 361 140
## 40,000-59,999 60,000-79,999 80,000-99,999
## 344 302 236
## over 1 million rather not say under twenty thousand
## 7 860 518
## <NA>
## 25
cross_cases(d, gender, income)
| income | |||||||||
|---|---|---|---|---|---|---|---|---|---|
| 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 twenty thousand | |
| gender | |||||||||
| f | 271 | 257 | 100 | 260 | 234 | 170 | 5 | 630 | 387 |
| m | 117 | 100 | 37 | 79 | 65 | 61 | 2 | 210 | 118 |
| nb | 1 | 4 | 3 | 5 | 3 | 5 | 20 | 13 | |
| #Total cases | 389 | 361 | 140 | 344 | 302 | 236 | 7 | 860 | 518 |
# Recoding Variables
My data consists of frequencies, have independent vairables and levels and there are two variables. however, I don’t have at least 5 participants in all cells. The number of non-binary participants is less than 5 in five of cells, and there are only 2 participants in the female cell(1).
To proceed with this analysis, I will drop the non-binary participants from my sample and add group 9 (over 1 million) participants to the 8 bracket. 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
# WHAT I USED
table(d$gender, useNA = "always")
##
## f m nb <NA>
## 2332 792 0 0
d <- subset(d, gender != "nb")
d$gender <- droplevels(d$gender)
# 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
# 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 (income2 identical to current variable (income)
d$income2 <- d$income
# we will use some of our previous code to recode over 1 million
d$income2[d$income == "over 1 million"] <- "200,000-1 million"
table(d$income2, useNA = "always")
##
## 100,000-199,999 20,000-39,999 200,000-1 million
## 388 357 144
## 40,000-59,999 60,000-79,999 80,000-99,999
## 339 299 231
## over 1 million rather not say under twenty thousand
## 0 840 505
## <NA>
## 21
# once again, we need to use the droplevels() command
d$income2 <- droplevels(d$income2)
table(d$income2, useNA = "always")
##
## 100,000-199,999 20,000-39,999 200,000-1 million
## 388 357 144
## 40,000-59,999 60,000-79,999 80,000-99,999
## 339 299 231
## rather not say under twenty thousand <NA>
## 840 505 21
# since I made changes to my variables, I am going to re-run the cross_cases() command
cross_cases(d, gender, income2)
| income2 | ||||||||
|---|---|---|---|---|---|---|---|---|
| 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 twenty thousand | |
| gender | ||||||||
| f | 271 | 257 | 105 | 260 | 234 | 170 | 630 | 387 |
| m | 117 | 100 | 39 | 79 | 65 | 61 | 210 | 118 |
| #Total cases | 388 | 357 | 144 | 339 | 299 | 231 | 840 | 505 |
# 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$income2)
# 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$income2
## X-squared = 10.318, df = 7, p-value = 0.1712
# 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$income2
## d$gender 100,000-199,999 20,000-39,999 200,000-1 million 40,000-59,999
## f -2.2862683 -1.1919613 -0.4674201 0.9511924
## m 2.2862683 1.1919613 0.4674201 -0.9511924
## d$income2
## d$gender 60,000-79,999 80,000-99,999 rather not say under twenty thousand
## f 1.5405484 -0.3555142 0.3328005 1.1622162
## m -1.5405484 0.3555142 -0.3328005 -1.1622162
To test our hypothesis that there would be gender differences in all four continuous variables, we conducted a chi-square test of independence. Our analysis included participants’ gender (male, female) and four continuous variables: subjective well-being, need to belong, perceived social support, and social media use. The variables met the criteria for running a chi-square test (categorical variables, independence assumption). However, we had a small number of non-binary participants, which did not meet the criteria for at least five participants per cell. Therefore, we excluded non-binary participants from the analysis. Additionally, to ensure sufficient cell frequencies, we combined participants with an income over 1 million with the existing category for participants from other small income brackets. The final sample for analysis consisted of 3124 participants.
| income2 | ||||||||
|---|---|---|---|---|---|---|---|---|
| 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 twenty thousand | |
| gender | ||||||||
| f | 271 | 257 | 105 | 260 | 234 | 170 | 630 | 387 |
| m | 117 | 100 | 39 | 79 | 65 | 61 | 210 | 118 |
The results of the chi-square test indicated no significant gender differences across the four continuous variables, χ^2(3, N = 3124) = 10.318, p = 0.1712. Although our hypothesis was not supported, it is worth noting that the p-value did not reach conventional levels of statistical significance.
I predict that women will report significantly less subjective well-being than men, as measured by the subjective well-being scale (swb).
# 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': 3124 obs. of 8 variables:
## $ ResponseId: chr "R_BJN3bQqi1zUMid3" "R_2TGbiBXmAtxywsD" "R_12G7bIqN2wB2N65" "R_39pldNoon8CePfP" ...
## $ swb : num 4.33 4.17 1.83 5.17 3.67 ...
## $ belong : num 3.4 3.4 3.6 3.6 3.2 3.4 3.5 3.2 3.5 2.7 ...
## $ support : num 6 6.75 5.17 5.58 6 ...
## $ SocMedia : num 4.27 2.09 3.09 3.18 3.36 ...
## $ gender : Factor w/ 2 levels "f","m": 1 2 2 1 2 1 1 1 1 1 ...
## $ income : Factor w/ 9 levels "100,000-199,999",..: 2 2 8 8 6 8 9 2 1 8 ...
## $ income2 : Factor w/ 8 levels "100,000-199,999",..: 2 2 7 7 6 7 8 2 1 7 ...
d$gender <- as.factor(d$gender)
table(d$gender, useNA = "always")
##
## f m <NA>
## 2332 792 0
# you can use the describe() command on an entire datafrom (d) or just on a single variable (d$pss)
describe(d$swb)
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 3120 4.49 1.32 4.67 4.54 1.48 1 7 6 -0.37 -0.44 0.02
# also use a histogram to examine your continuous variable
hist(d$swb)
# 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$swb, group = d$gender)
##
## Descriptive statistics by group
## group: f
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 2329 4.47 1.3 4.5 4.54 1.48 1 7 6 -0.38 -0.45 0.03
## ------------------------------------------------------------
## group: m
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 791 4.52 1.36 4.67 4.57 1.48 1 7 6 -0.34 -0.46 0.05
# last, use a boxplot to examine your continuous and categorical variables together
boxplot(d$swb~d$gender)
# 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
d$swb <- as.numeric(d$swb)
leveneTest(swb~gender, data = d)
## Levene's Test for Homogeneity of Variance (center = median)
## Df F value Pr(>F)
## group 1 2.0455 0.1528
## 3118
str(d)
## 'data.frame': 3124 obs. of 8 variables:
## $ ResponseId: chr "R_BJN3bQqi1zUMid3" "R_2TGbiBXmAtxywsD" "R_12G7bIqN2wB2N65" "R_39pldNoon8CePfP" ...
## $ swb : num 4.33 4.17 1.83 5.17 3.67 ...
## $ belong : num 3.4 3.4 3.6 3.6 3.2 3.4 3.5 3.2 3.5 2.7 ...
## $ support : num 6 6.75 5.17 5.58 6 ...
## $ SocMedia : num 4.27 2.09 3.09 3.18 3.36 ...
## $ gender : Factor w/ 2 levels "f","m": 1 2 2 1 2 1 1 1 1 1 ...
## $ income : Factor w/ 9 levels "100,000-199,999",..: 2 2 8 8 6 8 9 2 1 8 ...
## $ income2 : Factor w/ 8 levels "100,000-199,999",..: 2 2 7 7 6 7 8 2 1 7 ...
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 != "nb")
d$gender <- droplevels(d$gender) # using droplevels() to drop the empty factor
table(d$gender, useNA = "always")
##
## f m <NA>
## 2332 792 0
table(d$gender, useNA = "always")
##
## f m <NA>
## 2332 792 0
d <- subset(d, gender != "nb")
d$gender <- droplevels(d$gender)
# 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$swb~d$gender)
t_output
##
## Welch Two Sample t-test
##
## data: d$swb by d$gender
## t = -0.86381, df = 1311.5, p-value = 0.3879
## alternative hypothesis: true difference in means between group f and group m is not equal to 0
## 95 percent confidence interval:
## -0.15688631 0.06096318
## sample estimates:
## mean in group f mean in group m
## 4.473952 4.521913
# once again, we use our formula to calculate cohen's d
d_output <- cohen.d(d$swb~d$gender)
d_output
##
## Cohen's d
##
## d estimate: -0.03638119 (negligible)
## 95 percent confidence interval:
## lower upper
## -0.1170765 0.0443141
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 (p = .067). 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.
As predicted, we found that women (M = 2.97, SD = .95) reported significantly higher stress than men (M = 2.60, SD = .89); t(288.7) = 5.43, p < .001 (see Figure 1). The effect size was calculated using Cohen’s d, with a value of .40 (small effect; Cohen, 1988).
References
Cohen J. (1988). Statistical Power Analysis for the Behavioral Sciences. New York, NY: Routledge Academic.