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_data_finalclean.csv", header=T)

3 State Your Hypothesis

We predict that sleep hours and mental health issues are related to mental problems that people may face during the pandemic.

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':    1182 obs. of  6 variables:
##  $ sleep_hours: chr  "3 7-8 hours" "2 5-6 hours" "3 7-8 hours" "2 5-6 hours" ...
##  $ mhealth    : chr  "none or NA" "anxiety disorder" "none or NA" "none or NA" ...
##  $ iou        : num  3.19 4 1.59 3.37 1.7 ...
##  $ mfq_26     : num  4.2 3.35 4.65 4.65 4.5 4.3 5.25 4.45 4.7 4.05 ...
##  $ pas_covid  : num  3.22 4.56 3.33 4.22 3.22 ...
##  $ pss        : num  3.25 3.75 1 3.25 2 2 4 1.25 1.25 2.5 ...
d$sleep_hours <- as.factor(d$sleep_hours)
d$mhealth <- as.factor(d$mhealth)

table(d$sleep_hours, useNA = "always")
## 
##  1 < 5 hours  2 5-6 hours  3 7-8 hours 4 8-10 hours 5 > 10 hours         <NA> 
##           83          320          455          276           48            0
table(d$mhealth, useNA = "always")
## 
##              anxiety disorder                       bipolar 
##                           123                             5 
##                    depression              eating disorders 
##                            28                            28 
##                    none or NA obsessive compulsive disorder 
##                           916                            27 
##                         other                          ptsd 
##                            35                            20 
##                          <NA> 
##                             0
# you can use the describe() command on an entire datafrom (d) or just on a single variable (d$pss)
describe(d$iou)
##    vars    n mean  sd median trimmed  mad min max range skew kurtosis   se
## X1    1 1182 2.56 0.9   2.41     2.5 0.99   1   5     4  0.5    -0.59 0.03
describe(d$mfq_26)
##    vars    n mean   sd median trimmed  mad min max range  skew kurtosis   se
## X1    1 1182 4.31 0.67   4.35    4.33 0.67 1.8   6   4.2 -0.32     0.14 0.02
describe(d$pas_covid)
##    vars    n mean   sd median trimmed  mad min max range  skew kurtosis   se
## X1    1 1182 3.23 0.68   3.22    3.24 0.66   1   5     4 -0.19    -0.04 0.02
describe(d$pss)
##    vars    n mean   sd median trimmed  mad min max range skew kurtosis   se
## X1    1 1182 2.94 0.95      3    2.93 1.11   1   5     4 0.06    -0.76 0.03
# also use a histogram to examine your continuous variable
hist(d$iou)

hist(d$mfq_26)

hist(d$pas_covid)

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$iou, group=d$sleep_hours)
## 
##  Descriptive statistics by group 
## group: 1 < 5 hours
##    vars  n mean   sd median trimmed  mad  min max range  skew kurtosis  se
## X1    1 83 3.22 0.94   3.26    3.23 0.88 1.56   5  3.44 -0.04    -0.91 0.1
## ------------------------------------------------------------ 
## group: 2 5-6 hours
##    vars   n mean   sd median trimmed  mad  min  max range skew kurtosis   se
## X1    1 320 2.77 0.92   2.63    2.74 1.04 1.07 4.93  3.85 0.27    -0.85 0.05
## ------------------------------------------------------------ 
## group: 3 7-8 hours
##    vars   n mean   sd median trimmed  mad  min max range skew kurtosis   se
## X1    1 455 2.43 0.84    2.3    2.38 0.88 1.04 4.7  3.67 0.54    -0.45 0.04
## ------------------------------------------------------------ 
## group: 4 8-10 hours
##    vars   n mean  sd median trimmed  mad min  max range skew kurtosis   se
## X1    1 276 2.27 0.8   2.11    2.19 0.71   1 4.78  3.78 0.88     0.31 0.05
## ------------------------------------------------------------ 
## group: 5 > 10 hours
##    vars  n mean  sd median trimmed  mad  min  max range skew kurtosis   se
## X1    1 48 2.85 0.9   3.02    2.83 1.15 1.44 4.78  3.33 0.02    -1.17 0.13
describeBy(d$pss, group=d$mhealth)
## 
##  Descriptive statistics by group 
## group: anxiety disorder
##    vars   n mean   sd median trimmed  mad  min max range  skew kurtosis   se
## X1    1 123 3.49 0.82    3.5    3.52 0.74 1.25   5  3.75 -0.35    -0.41 0.07
## ------------------------------------------------------------ 
## group: bipolar
##    vars n mean   sd median trimmed  mad  min max range  skew kurtosis   se
## X1    1 5  3.9 1.26   4.25     3.9 0.37 1.75   5  3.25 -0.84    -1.16 0.56
## ------------------------------------------------------------ 
## group: depression
##    vars  n mean   sd median trimmed  mad min  max range skew kurtosis   se
## X1    1 28 2.96 0.82   2.75    2.96 0.93 1.5 4.25  2.75 0.17    -1.23 0.15
## ------------------------------------------------------------ 
## group: eating disorders
##    vars  n mean   sd median trimmed  mad min  max range  skew kurtosis   se
## X1    1 28 3.87 0.68      4    3.92 0.74   2 4.75  2.75 -0.76      0.1 0.13
## ------------------------------------------------------------ 
## group: none or NA
##    vars   n mean   sd median trimmed  mad min max range skew kurtosis   se
## X1    1 916 2.78 0.92   2.75    2.76 1.11   1   5     4 0.22    -0.59 0.03
## ------------------------------------------------------------ 
## group: obsessive compulsive disorder
##    vars  n mean   sd median trimmed  mad  min  max range  skew kurtosis   se
## X1    1 27 3.32 0.84   3.25    3.37 0.74 1.25 4.75   3.5 -0.52    -0.25 0.16
## ------------------------------------------------------------ 
## group: other
##    vars  n mean   sd median trimmed  mad min  max range  skew kurtosis   se
## X1    1 35 3.53 0.91   3.75     3.6 1.11   1 4.75  3.75 -0.67    -0.28 0.15
## ------------------------------------------------------------ 
## group: ptsd
##    vars  n mean   sd median trimmed  mad min max range  skew kurtosis   se
## X1    1 20 3.45 0.94   3.62    3.56 0.93   1 4.5   3.5 -0.88     0.07 0.21
# last, use a boxplot to examine your continuous and categorical variables together
boxplot(d$iou~d$sleep_hours)

boxplot(d$pss~d$mhealth)

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!

# 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(iou~sleep_hours, data = d)
## Levene's Test for Homogeneity of Variance (center = median)
##         Df F value   Pr(>F)   
## group    4  3.8716 0.003942 **
##       1177                    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
leveneTest(pss~mhealth, data = d)
## Levene's Test for Homogeneity of Variance (center = median)
##         Df F value Pr(>F)
## group    7  1.4575 0.1786
##       1174

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 only use the data from participants with anxiety disorder and participants with depression. 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. The Levene’s test was not significant, and was far from 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, mhealth != "none or NA")
table(d$mhealth, useNA = "always")
## 
##              anxiety disorder                       bipolar 
##                           123                             5 
##                    depression              eating disorders 
##                            28                            28 
##                    none or NA obsessive compulsive disorder 
##                             0                            27 
##                         other                          ptsd 
##                            35                            20 
##                          <NA> 
##                             0
d$mhealth <- droplevels(d$mhealth) 

d <- subset(d, mhealth != "obsessive compulsive disorder")
table(d$mhealth, useNA = "always")
## 
##              anxiety disorder                       bipolar 
##                           123                             5 
##                    depression              eating disorders 
##                            28                            28 
## obsessive compulsive disorder                         other 
##                             0                            35 
##                          ptsd                          <NA> 
##                            20                             0
d$mhealth <- droplevels(d$mhealth) 

d <- subset(d, mhealth != "bipolar")
table(d$mhealth, useNA = "always")
## 
## anxiety disorder          bipolar       depression eating disorders 
##              123                0               28               28 
##            other             ptsd             <NA> 
##               35               20                0
d$mhealth <- droplevels(d$mhealth) 

d <- subset(d, mhealth != "eating disorders")
table(d$mhealth, useNA = "always")
## 
## anxiety disorder       depression eating disorders            other 
##              123               28                0               35 
##             ptsd             <NA> 
##               20                0
d$mhealth <- droplevels(d$mhealth) 

d <- subset(d, mhealth != "ptsd")
table(d$mhealth, useNA = "always")
## 
## anxiety disorder       depression            other             ptsd 
##              123               28               35                0 
##             <NA> 
##                0
d$mhealth <- droplevels(d$mhealth) 

d <- subset(d, mhealth != "other")
table(d$mhealth, useNA = "always")
## 
## anxiety disorder       depression            other             <NA> 
##              123               28                0                0
d$mhealth <- droplevels(d$mhealth) 
# 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$pss~d$mhealth)

7 View Test Output

t_output
## 
##  Welch Two Sample t-test
## 
## data:  d$pss by d$mhealth
## t = 3.1442, df = 40.479, p-value = 0.003115
## alternative hypothesis: true difference in means between group anxiety disorder and group depression is not equal to 0
## 95 percent confidence interval:
##  0.1925039 0.8845866
## sample estimates:
## mean in group anxiety disorder       mean in group depression 
##                       3.493902                       2.955357

8 Calculate Cohen’s d

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

9 View Effect Size

d_output
## 
## Cohen's d
## 
## d estimate: 0.654717 (medium)
## 95 percent confidence interval:
##     lower     upper 
## 0.2343147 1.0751193

10 Write Up Results

To test our hypothesis that sleep hours and mental health issues are related to mental problems that people may face during the pandemic. This required us to drop participants with mental health issues other than depression and anxiety disorder 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 = .0003115). 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 participants with anxiety disorder (M = 3.49) reported significantly higher stress than participants with depression (M = 2.96); t(40.479) = 3.1442, p > .001 (see Figure 1). The effect size was calculated using Cohen’s d, with a value of .65 (medium effect; Cohen, 1988).

References

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