1 Loading Libraries

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
library(effsize) # for the cohen.d() command
## 
## Attaching package: 'effsize'
## The following object is masked from 'package:psych':
## 
##     cohen.d

2 Importing Data

# 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)

3 State Your Hypothesis

We predict that women will report significantly more stress than men, as measured by the perceived stress scale (PSS-4).

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':    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$rse)
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$rse~d$gender_rc)

5 Check Your Assumptions

5.1 T-test Assumptions

  • IV must have two levels
  • Data values must be independent (independent t-test only)
  • Data obtained via a random sample
  • Dependent variable must be normally distributed
  • Variances of the two groups are approximately equal

5.2 Testing Homogeneity of Variance with Levene’s Test

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.

5.3 Issues with My Data

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")
VARIABLE <- droplevels(d$gender_rc) # using droplevels() to drop the empty factor

6 Run a T-test

# 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)

7 View Test Output

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

8 Calculate Cohen’s d

# once again, we use our formula to calculate cohen's d
d_output <- cohen.d(d$rse~d$gender_rc)

9 View Effect Size

d_output
## 
## Cohen's d
## 
## d estimate: -0.3538613 (small)
## 95 percent confidence interval:
##      lower      upper 
## -0.5071922 -0.2005304

10 Write Up Results

To test our hypothesis that women would report significantly lower self-esteem than men, as measured by the Rosenberg Self-Esteem Inventory (RSE), we conducted an independent samples t-test. This analysis required us to exclude non-binary participants from our dataset, as the t-test is limited to comparing two groups. We ensured that the assumptions for the t-test were met. Specifically, we tested the homogeneity of variance using Levene’s test, which indicated no significant violation of this assumption (p = .418). Despite the non-significant result from Levene’s test, indicating that the variances of the two groups were approximately equal, we opted to use Welch’s t-test for our primary analysis to account for any potential heterogeneity. Welch’s t-test is robust to violations of the equal variance assumption and provides more reliable results under these conditions. The analysis revealed that women reported significantly lower self-esteem (M = 2.61, SD = 0.70) compared to men (M = 2.86, SD = 0.70), t(277.36) = -4.55, p < .001. The 95% confidence interval for the difference in means ranged from -0.355 to -0.141. The effect size, calculated using Cohen’s d, was -0.354 (95% CI: -0.507 to -0.201), indicating a small effect size according to Cohen’s conventions. These results support our hypothesis that women report significantly lower self-esteem than men, with a small but statistically significant difference in RSE scores between the two groups.

References

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