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 people with an anxiety diagnosis will report significantly higher levels of overall worry than people without a mental health diagnosis.

There will be a significant difference in overall worry by people’s type of mental health diagnosis, between anxiety and none/NA

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':    290 obs. of  7 variables:
##  $ X        : int  7888 7365 8747 7357 8760 8654 8272 8738 7911 8463 ...
##  $ education: chr  "2 equivalent to high school completion" "1 equivalent to not completing high school" "2 equivalent to high school completion" "2 equivalent to high school completion" ...
##  $ mhealth  : chr  "none or NA" "none or NA" "none or NA" "none or NA" ...
##  $ pswq     : num  3.43 2.14 1.57 1.43 1.5 ...
##  $ pas_covid: num  4.44 3.11 3.22 1.78 2.78 ...
##  $ brs      : num  2 3.83 3.83 4 4.67 ...
##  $ support  : num  4.83 5 4.83 3.33 4.83 ...
# 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':    290 obs. of  7 variables:
##  $ X        : int  7888 7365 8747 7357 8760 8654 8272 8738 7911 8463 ...
##  $ education: chr  "2 equivalent to high school completion" "1 equivalent to not completing high school" "2 equivalent to high school completion" "2 equivalent to high school completion" ...
##  $ mhealth  : Factor w/ 8 levels "anxiety disorder",..: 5 5 5 5 5 5 5 5 5 5 ...
##  $ pswq     : num  3.43 2.14 1.57 1.43 1.5 ...
##  $ pas_covid: num  4.44 3.11 3.22 1.78 2.78 ...
##  $ brs      : num  2 3.83 3.83 4 4.67 ...
##  $ support  : num  4.83 5 4.83 3.33 4.83 ...
table(d$gender, useNA = "always")
## 
## <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$pswq)
##    vars   n mean   sd median trimmed  mad  min max range  skew kurtosis   se
## X1    1 290 2.91 0.68      3    2.95 0.74 1.14   4  2.86 -0.43    -0.62 0.04
# also use a histogram to visualize your continuous variable

hist(d$pswq)

# 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$pswq, 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 39  3.1 0.61   3.14    3.15 0.53 1.71 3.93  2.21 -0.79    -0.07 0.1
## ------------------------------------------------------------ 
## group: bipolar
##    vars n mean   sd median trimmed  mad min  max range skew kurtosis   se
## X1    1 3 2.62 0.15   2.57    2.62 0.11 2.5 2.79  0.29 0.29    -2.33 0.09
## ------------------------------------------------------------ 
## group: depression
##    vars n mean   sd median trimmed  mad  min  max range skew kurtosis   se
## X1    1 5 2.29 0.54   2.14    2.29 0.64 1.71 3.07  1.36 0.34    -1.81 0.24
## ------------------------------------------------------------ 
## group: eating disorders
##    vars  n mean   sd median trimmed  mad min  max range  skew kurtosis   se
## X1    1 14 3.03 0.68   3.21    3.08 0.69 1.5 3.93  2.43 -0.65    -0.49 0.18
## ------------------------------------------------------------ 
## group: none or NA
##    vars   n mean   sd median trimmed  mad  min max range skew kurtosis   se
## X1    1 198 2.81 0.68   2.89    2.84 0.69 1.14   4  2.86 -0.3    -0.68 0.05
## ------------------------------------------------------------ 
## group: obsessive compulsive disorder
##    vars  n mean   sd median trimmed  mad  min max range skew kurtosis   se
## X1    1 11 3.38 0.46   3.36    3.39 0.64 2.71   4  1.29    0    -1.65 0.14
## ------------------------------------------------------------ 
## group: other
##    vars  n mean   sd median trimmed  mad  min max range  skew kurtosis   se
## X1    1 13 3.48 0.48    3.5    3.55 0.42 2.14   4  1.86 -1.42     1.74 0.13
## ------------------------------------------------------------ 
## group: ptsd
##    vars n mean   sd median trimmed  mad  min  max range  skew kurtosis  se
## X1    1 7 3.28 0.79    3.5    3.28 0.53 1.64 3.93  2.29 -1.12    -0.21 0.3
# lastly, use a boxplot to examine your chosen continuous and categorical variables together

boxplot(d$pswq~d$mhealth)

5 Check Your Assumptions

5.1 T-test Assumptions

  • IV must have only 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.
# 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 != "bipolar") # 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 
##                            39                             0 
##                    depression              eating disorders 
##                             5                            14 
##                    none or NA obsessive compulsive disorder 
##                           198                            11 
##                         other                          ptsd 
##                            13                             7 
##                          <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 
## 
##              anxiety disorder                    depression 
##                            39                             5 
##              eating disorders                    none or NA 
##                            14                           198 
## obsessive compulsive disorder                         other 
##                            11                            13 
##                          ptsd                          <NA> 
##                             7                             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.
d <- subset(d, 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                    depression 
##                            39                             0 
##              eating disorders                    none or NA 
##                            14                           198 
## obsessive compulsive disorder                         other 
##                            11                            13 
##                          ptsd                          <NA> 
##                             7                             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
## 
##              anxiety disorder              eating disorders 
##                            39                            14 
##                    none or NA obsessive compulsive disorder 
##                           198                            11 
##                         other                          ptsd 
##                            13                             7 
##                          <NA> 
##                             0
d <- subset(d, mhealth != "eating disorders") # 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              eating disorders 
##                            39                             0 
##                    none or NA obsessive compulsive disorder 
##                           198                            11 
##                         other                          ptsd 
##                            13                             7 
##                          <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
## 
##              anxiety disorder                    none or NA 
##                            39                           198 
## obsessive compulsive disorder                         other 
##                            11                            13 
##                          ptsd                          <NA> 
##                             7                             0
d <- subset(d, mhealth != "obsessive compulsive disorder") # 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                    none or NA 
##                            39                           198 
## obsessive compulsive disorder                         other 
##                             0                            13 
##                          ptsd                          <NA> 
##                             7                             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
## 
## anxiety disorder       none or NA            other             ptsd 
##               39              198               13                7 
##             <NA> 
##                0
d <- subset(d, mhealth != "other") # 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       none or NA            other             ptsd 
##               39              198                0                7 
##             <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
## 
## anxiety disorder       none or NA             ptsd             <NA> 
##               39              198                7                0
d <- subset(d, mhealth != "ptsd") # 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       none or NA             ptsd             <NA> 
##               39              198                0                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
## 
## anxiety disorder       none or NA             <NA> 
##               39              198                0

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(pswq~mhealth, data = d)
## Levene's Test for Homogeneity of Variance (center = median)
##        Df F value Pr(>F)
## group   1  2.2209 0.1375
##       235

Levene’s test revealed that our data does not have significantly different variances between the two comparison groups, anxiety diagnosis and no mental health diagnosis, on their levels of overall worry.

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 has more than two levels. To proceed with this analysis, I will drop the participants who have mental health diagnoses of depression, bipolar disorder, eating disorders, obsessive compulsive dosorder, PTSD, and other from my sample. I will make a note to discuss this issue in my Methods section write-up and in my Discussion section as a limitation of my study.

My data has no issues regarding homogeneity of variance, as Levene’s test was not significant. Regardless, I will use Welch’s t-test instead of Student’s t-test in my analysis.

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(d$pswq~d$mhealth)  # t_output will now show in your Global Environment

7 View Test Output

t_output
## 
##  Welch Two Sample t-test
## 
## data:  d$pswq by d$mhealth
## t = 2.6112, df = 58.451, p-value = 0.01144
## alternative hypothesis: true difference in means between group anxiety disorder and group none or NA is not equal to 0
## 95 percent confidence interval:
##  0.06631316 0.50156341
## sample estimates:
## mean in group anxiety disorder       mean in group none or NA 
##                       3.097070                       2.813131

8 Calculate Cohen’s d - Effect Size

# once again, we use the same formula, y~x, to calculate cohen's d

# We **only** calculate effect size if the test is SIG!

d_output <- cohen.d(d$pswq~d$mhealth)  # d_output will now show in your Global Environment

9 View Effect Size

d_output
## 
## Cohen's d
## 
## d estimate: 0.4236505 (small)
## 95 percent confidence interval:
##      lower      upper 
## 0.07638443 0.77091661
## Remember to always take the ABSOLAUTE VALUE of the effect size value (i.e., it will never be negative)

10 Write Up Results

To test our hypothesis that people with anxiety disorders in our sample would report significantly higher levels of overall worry than people without mental health disorders, we used an independent samples t-test. This required us to drop our participants who had any other type of mental health disorder (depression, bipolar disorder, eating disorders, obsessive compulsive disorder, PTSD, other) 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 no signs of heterogeneity (p = 0.14). Although this does not increase chances of a Type I error, we used Welch’s t-test, which does not assume homogeneity of variance. Our data met all other assumptions of an independent samples t-test.

As predicted, we found that people with anxiety disorders (M = 3.10, SD = 0.61) reported significantly higher levels of overall worry than people without mental health disorders (M = 2.81, SD = 0.68); t(58.451) = 2.6112, p = .01 (see Figure 1). The effect size was calculated using Cohen’s d, with a value of 0.42 (small effect; Cohen, 1988).

References

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