1 Loading Libraries

# install any packages you have not previously used, then comment them back out.

# install.packages("car")
# install.packages("effsize")

library(psych) # for the describe() command
library(car) # for the leveneTest() command
## Warning: package 'car' was built under R version 4.5.3
## Loading required package: carData
## Warning: package 'carData' was built under R version 4.5.3
## 
## Attaching package: 'car'
## The following object is masked from 'package:psych':
## 
##     logit
library(effsize) # for the cohen.d() command
## Warning: package 'effsize' was built under R version 4.5.3
## 
## Attaching package: 'effsize'
## The following object is masked from 'package:psych':
## 
##     cohen.d

2 Importing Data

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

# For the HW, you will import the project dataset you cleaned previously
# This will be the dataset you'll use for HWs throughout the rest of the semester

3 State Your Hypothesis

We predict that individuals with depression will report significantly different levels of resilience than individuals with no clinically diagnosed mental health disorder.

4 Check Your Variables

# you **only** need to check the variables you're using in the current analysis

## Checking the Categorical variable (IV)

str(d)
## 'data.frame':    263 obs. of  7 variables:
##  $ X        : int  7888 7365 8747 7357 8760 8654 8272 8738 7911 8463 ...
##  $ exercise : chr  "2 1-2 hours" "2 1-2 hours" "2 1-2 hours" "2 1-2 hours" ...
##  $ mhealth  : chr  "none or NA" "none or NA" "none or NA" "none or NA" ...
##  $ covid_pos: int  12 1 7 5 5 9 8 3 4 9 ...
##  $ covid_neg: int  2 4 3 0 2 0 5 2 1 3 ...
##  $ big5_neu : num  6.67 4.33 2 4 3.33 ...
##  $ brs      : num  2 3.83 3.83 4 4.67 ...
# if the categorical variable you're using is showing as a "chr" (character), you must change it to be a ** factor ** -- using the next line of code (as.factor)

d$mhealth <- as.factor(d$mhealth)

str(d)
## 'data.frame':    263 obs. of  7 variables:
##  $ X        : int  7888 7365 8747 7357 8760 8654 8272 8738 7911 8463 ...
##  $ exercise : chr  "2 1-2 hours" "2 1-2 hours" "2 1-2 hours" "2 1-2 hours" ...
##  $ mhealth  : Factor w/ 8 levels "anxiety disorder",..: 5 5 5 5 5 5 5 5 5 5 ...
##  $ covid_pos: int  12 1 7 5 5 9 8 3 4 9 ...
##  $ covid_neg: int  2 4 3 0 2 0 5 2 1 3 ...
##  $ big5_neu : num  6.67 4.33 2 4 3.33 ...
##  $ brs      : num  2 3.83 3.83 4 4.67 ...
table(d$mhealth, useNA = "always")
## 
##              anxiety disorder                       bipolar 
##                            35                             3 
##                    depression              eating disorders 
##                             6                            14 
##                    none or NA obsessive compulsive disorder 
##                           177                            10 
##                         other                          ptsd 
##                            11                             7 
##                          <NA> 
##                             0
## Checking the Continuous variable (DV)

# you can use the describe() command on an entire dataframe (d) or just on a single variable within your dataframe -- which we will do here

describe(d$brs)
##    vars   n mean   sd median trimmed  mad min max range skew kurtosis   se
## X1    1 263 2.68 0.85   2.67    2.68 0.99   1   5     4 0.13    -0.56 0.05
# also use a histogram to visualize your continuous variable

hist(d$brs)

# 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$brs, 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 35 2.53 0.93   2.33    2.45 0.74   1   5     4 0.83     0.36 0.16
## ------------------------------------------------------------ 
## group: bipolar
##    vars n mean  sd median trimmed  mad  min  max range skew kurtosis   se
## X1    1 3 1.83 0.5   1.83    1.83 0.74 1.33 2.33     1    0    -2.33 0.29
## ------------------------------------------------------------ 
## group: depression
##    vars n mean   sd median trimmed  mad  min  max range skew kurtosis   se
## X1    1 6 2.75 0.66   2.67    2.75 0.37 1.83 3.83     2 0.29    -1.12 0.27
## ------------------------------------------------------------ 
## group: eating disorders
##    vars  n mean   sd median trimmed  mad min  max range skew kurtosis  se
## X1    1 14 2.56 0.74   2.33    2.51 0.62 1.5 4.17  2.67 0.69    -0.67 0.2
## ------------------------------------------------------------ 
## group: none or NA
##    vars   n mean   sd median trimmed  mad min  max range  skew kurtosis   se
## X1    1 177 2.81 0.82   2.83    2.83 0.99   1 4.67  3.67 -0.12    -0.59 0.06
## ------------------------------------------------------------ 
## group: obsessive compulsive disorder
##    vars  n mean   sd median trimmed  mad min  max range  skew kurtosis   se
## X1    1 10 2.27 0.79   2.17    2.29 0.99   1 3.33  2.33 -0.09    -1.59 0.25
## ------------------------------------------------------------ 
## group: other
##    vars  n mean   sd median trimmed  mad min  max range skew kurtosis   se
## X1    1 11 2.24 1.04      2     2.2 0.99   1 3.83  2.83 0.44    -1.49 0.31
## ------------------------------------------------------------ 
## group: ptsd
##    vars n mean   sd median trimmed  mad min  max range skew kurtosis   se
## X1    1 7  2.1 0.54      2     2.1 0.49 1.5 3.17  1.67 0.85    -0.57 0.21
# lastly, use a boxplot to examine your chosen continuous and categorical variables together

boxplot(d$brs ~ 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 approximately normally distributed
  • Variances of the two groups are approximately equal
# If the IV has more than 2 levels, you must DROP any additional levels in order to meet the first assumption of a t-test.

## NOTE: This is a FOUR STEP process!

d <- subset(d, mhealth == "none or NA" | mhealth == "depression") # use subset() to remove all participants from the additional level

table(d$mhealth, useNA = "always") # verify that now there are ZERO participants in the additional level
## 
##              anxiety disorder                       bipolar 
##                             0                             0 
##                    depression              eating disorders 
##                             6                             0 
##                    none or NA obsessive compulsive disorder 
##                           177                             0 
##                         other                          ptsd 
##                             0                             0 
##                          <NA> 
##                             0
 d$mhealth<- droplevels(d$mhealth) # use droplevels() to drop the empty factor

table(d$mhealth, useNA = "always") # verify that now the entire factor level is removed 
## 
## depression none or NA       <NA> 
##          6        177          0
## Repeat ALL THE STEPS ABOVE if your IV has more levels that need to be DROPPED. Copy the 4 lines of code, and replace the level name in the subset() command.

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
# it 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(brs ~ mhealth, data = d)
## Levene's Test for Homogeneity of Variance (center = median)
##        Df F value Pr(>F)
## group   1  1.8253 0.1784
##       181

Levene’s test revealed that there was no significant difference in variance between individuals with depression and individuals with no diagnosis, indicating that the assumption of homogeneity of variance was met (p = .178).

When running a t-test, we can account for heterogeneity in our variance by using the Welch’s t-test, which does not have the same assumption about variance as the Student’s t-test (the general default type of t-test in statistics). 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 using Levene’s test here to get into the habit of checking the homogeneity of our variance, even if we already have the solution for any potential problems.

5.3 Issues with My Data

My independent variable initially had more than two levels (multiple types of mental health diagnoses). To proceed with this analysis, I restricted the sample to participants with no diagnosis and those with depression, as required for a t-test. I will note this as a limitation in my methods and discussion sections.

There were no issues with homogeneity of variance, as Levene’s test was non-significant. Therefore, Welch’s t-test was used as the default, although the assumption of equal variances was met.

6 Run a T-test

# Very simple! we use the same formula of y~x, where y is our DV and x is our IV

t_output <- t.test(brs ~ mhealth, data =d)  # t_output will now show in your Global Environment

7 View Test Output

t_output
## 
##  Welch Two Sample t-test
## 
## data:  brs by mhealth
## t = -0.21755, df = 5.5408, p-value = 0.8356
## alternative hypothesis: true difference in means between group depression and group none or NA is not equal to 0
## 95 percent confidence interval:
##  -0.7460752  0.6264895
## sample estimates:
## mean in group depression mean in group none or NA 
##                 2.750000                 2.809793

8 Write Up Results

To test our hypothesis that individuals with depression would report significantly different levels of resilience than individuals with no clinically diagnosed mental health disorder, we conducted an independent samples t-test. Because a t-test requires a two-group comparison, we restricted the analysis to participants with depression and participants with no diagnosis. We tested the homogeneity of variance using Levene’s test and found that the result was non-significant (p = .178), indicating that the assumption of equal variances was met. Therefore, we proceeded with Welch’s t-test. Our data met all other assumptions of an independent samples t-test.

Results showed no significant difference in resilience between participants with depression (M = 2.75, SD = 0.66) and participants with no diagnosis (M = 2.81, SD = 0.82), t(5.54) = -0.22, p = .836 (see Figure 1).

One limitation of this analysis is that the depression group had a much smaller sample size than the no-diagnosis group (n = 6 vs. n = 177), which may limit the reliability and generalizability of the results.

References

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