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

d <- read.csv(file="Data/mydata.csv", header=T)

3 State Your Hypothesis

We predict that low income individuals will report significantly more stress than high income individuals, as measured by the perceived stress questionnaire.

4 Check Your Variables

str(d)
## 'data.frame':    3084 obs. of  6 variables:
##  $ gender  : chr  "f" "m" "m" "f" ...
##  $ income  : chr  "1 low" "1 low" "rather not say" "rather not say" ...
##  $ moa_role: num  3 2.67 2.5 2 2.67 ...
##  $ idea    : num  3.75 3.88 3.75 3.75 3.5 ...
##  $ efficacy: num  3.4 3.4 2.2 2.8 3 2.4 2.3 3 3 3.7 ...
##  $ stress  : num  3.3 3.3 4 3.2 3.1 3.5 3.3 2.4 2.9 2.7 ...
d$income <- as.factor(d$income)


table(d$income, useNA = "always")
## 
##          1 low       2 middle         3 high rather not say           <NA> 
##            864            861            523            836              0
describe(d$stress)
##    vars    n mean  sd median trimmed  mad min max range skew kurtosis   se
## X1    1 3084 3.05 0.6      3    3.05 0.59 1.3 4.7   3.4 0.04    -0.17 0.01
hist(d$stress)

describeBy(d$stress, group=d$income)
## 
##  Descriptive statistics by group 
## group: 1 low
##    vars   n mean  sd median trimmed  mad min max range skew kurtosis   se
## X1    1 864 3.05 0.6      3    3.04 0.59 1.4 4.6   3.2 0.04    -0.23 0.02
## ------------------------------------------------------------ 
## group: 2 middle
##    vars   n mean  sd median trimmed  mad min max range skew kurtosis   se
## X1    1 861    3 0.6      3       3 0.59 1.3 4.6   3.3 0.02    -0.19 0.02
## ------------------------------------------------------------ 
## group: 3 high
##    vars   n mean   sd median trimmed  mad min max range skew kurtosis   se
## X1    1 523 3.02 0.63      3    3.01 0.59 1.3 4.7   3.4 0.09    -0.27 0.03
## ------------------------------------------------------------ 
## group: rather not say
##    vars   n mean   sd median trimmed  mad min max range skew kurtosis   se
## X1    1 836 3.13 0.57    3.1    3.12 0.59 1.4 4.7   3.3 0.06    -0.08 0.02
boxplot(d$stress~d$income)

4.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

4.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!

# 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(stress~income, data = d)
## Levene's Test for Homogeneity of Variance (center = median)
##         Df F value  Pr(>F)  
## group    3  2.3415 0.07134 .
##       3080                  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

As you can see, our data is insignificant. 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.

4.3 Issues with My Data

My independent variable has more than two levels. To proceed with this analysis, I will drop the middle income and “rather not say” 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 middle income group
d <- subset(d, income != "2 middle")
d <- subset(d, income != "rather not say")
table(d$income, useNA = "always")
## 
##          1 low       2 middle         3 high rather not say           <NA> 
##            864              0            523              0              0
d$income <- droplevels(d$income) # using droplevels() to drop the empty factor

5 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$stress~d$income)

6 View Test Output

t_output
## 
##  Welch Two Sample t-test
## 
## data:  d$stress by d$income
## t = 0.71659, df = 1057.3, p-value = 0.4738
## alternative hypothesis: true difference in means between group 1 low and group 3 high is not equal to 0
## 95 percent confidence interval:
##  -0.04285692  0.09216690
## sample estimates:
##  mean in group 1 low mean in group 3 high 
##             3.046644             3.021989

7 Calculate Cohen’s d

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

8 View Effect Size

d_output
## 
## Cohen's d
## 
## d estimate: 0.04020448 (negligible)
## 95 percent confidence interval:
##     lower     upper 
## -0.068488  0.148897

9 Write Up Results

To test our hypothesis that low income individuals in our sample would report significantly more stress than high income individuals, we used a two-sample or independent t-test. This required us to drop our middle income and other income 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 signs of heterogeneity (p = .07134). 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.

Contrary to our prediction, we found that low income individuals (M = 3.05, SD = .6) reported similar stress levels to high income individuals (M = 3.02, SD = .63); t(1057.3) = 0.71659, p > .05 (see Figure 1). The effect size was calculated using Cohen’s d, with a value of .04 (negligible effect; Cohen, 1988). In conclusion, we failed to reject the null hypothesis.

References

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