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 dataframe (d) or just on a single variable (d$pss)
describe(d$pss)
##    vars    n mean   sd median trimmed  mad min max range skew kurtosis   se
## X1    1 1250 2.93 0.95      3    2.93 1.11   1   5     4 0.09    -0.74 0.03
# also use a histogram to examine your continuous variable
hist(d$pss)

# 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$pss, 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.97 0.95      3    2.97 1.11   1   5     4 0.04    -0.73 0.03
## ------------------------------------------------------------ 
## group: m
##    vars   n mean   sd median trimmed  mad min max range skew kurtosis   se
## X1    1 197  2.6 0.89    2.5    2.55 0.74   1   5     4  0.5    -0.24 0.06
## ------------------------------------------------------------ 
## group: nb
##    vars  n mean   sd median trimmed  mad min max range  skew kurtosis   se
## X1    1 33 3.73 0.66   3.75    3.77 0.74   2   5     3 -0.48    -0.18 0.11
# last, use a boxplot to examine your continuous and categorical variables together
boxplot(d$pss~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(pss~gender_rc, data = d)
## Levene's Test for Homogeneity of Variance (center = median)
##         Df F value  Pr(>F)  
## group    1  3.3618 0.06697 .
##       1215                  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

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.

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$pss~d$gender_rc)

7 View Test Output

t_output
## 
##  Welch Two Sample t-test
## 
## data:  d$pss by d$gender_rc
## t = 5.4247, df = 288.7, p-value = 1.23e-07
## alternative hypothesis: true difference in means between group f and group m is not equal to 0
## 95 percent confidence interval:
##  0.2415443 0.5166297
## sample estimates:
## mean in group f mean in group m 
##        2.974265        2.595178

8 Calculate Cohen’s d

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

9 View Effect Size

d_output
## 
## Cohen's d
## 
## d estimate: 0.4044459 (small)
## 95 percent confidence interval:
##     lower     upper 
## 0.2509173 0.5579745

10 Write Up Results

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.