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
d2 <- read.csv(file="data/mydata.csv", header=T)

3 State Your Hypothesis

I predict that there will be a significant difference in Intolerance of uncertainty scale by Mental health disorders with two levels. I predict that there will be a significant difference in Intolerance of Uncertainty scale in participants with Obsessive compulsive disorder and none or NA.

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(d2)
## 'data.frame':    1030 obs. of  6 variables:
##  $ 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 ...
##  $ mfq_state: num  3.62 3 5.88 4 4.62 ...
##  $ edeq12   : num  1.58 1.83 1 1.67 1.42 ...
##  $ mhealth  : chr  "none or NA" "anxiety disorder" "none or NA" "none or NA" ...
##  $ treatment: chr  "no psychological disorders" "in treatment" "not in treatment" "no psychological disorders" ...
d2$mhealth <- as.factor(d2$mhealth)

table(d2$mhealth, useNA = "always")
## 
##              anxiety disorder                       bipolar 
##                           121                             5 
##                    depression              eating disorders 
##                            29                            29 
##                    none or NA obsessive compulsive disorder 
##                           768                            25 
##                         other                          ptsd 
##                            33                            20 
##                          <NA> 
##                             0
# you can use the describe() command on an entire data frame (d) or just on a single variable (d$pss)
describe(d2$iou)
##    vars    n mean   sd median trimmed  mad min max range skew kurtosis   se
## X1    1 1030  2.6 0.91   2.44    2.54 0.99   1   5     4 0.45    -0.66 0.03
# also use a histogram to examine your continuous variable
hist(d2$iou)

# 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(d2$iou, group=d2$mhealth)
## 
##  Descriptive statistics by group 
## group: anxiety disorder
##    vars   n mean   sd median trimmed  mad  min  max range skew kurtosis   se
## X1    1 121 3.16 0.86   3.15    3.15 0.93 1.11 4.93  3.81    0    -0.79 0.08
## ------------------------------------------------------------ 
## group: bipolar
##    vars n mean   sd median trimmed  mad  min  max range skew kurtosis   se
## X1    1 5 2.59 1.19   2.37    2.59 0.71 1.37 4.48  3.11 0.54    -1.49 0.53
## ------------------------------------------------------------ 
## group: depression
##    vars  n mean   sd median trimmed  mad  min  max range skew kurtosis   se
## X1    1 29  2.3 0.87   2.07    2.24 0.77 1.04 4.33   3.3 0.76    -0.25 0.16
## ------------------------------------------------------------ 
## group: eating disorders
##    vars  n mean   sd median trimmed  mad min  max range  skew kurtosis   se
## X1    1 29 3.41 0.67   3.52    3.45 0.77   2 4.22  2.22 -0.65    -0.76 0.12
## ------------------------------------------------------------ 
## group: none or NA
##    vars   n mean   sd median trimmed  mad min max range skew kurtosis   se
## X1    1 768 2.42 0.83    2.3    2.35 0.88   1   5     4 0.61     -0.3 0.03
## ------------------------------------------------------------ 
## group: obsessive compulsive disorder
##    vars  n mean   sd median trimmed  mad  min  max range skew kurtosis   se
## X1    1 25  3.2 0.91   3.07     3.2 1.04 1.59 4.78  3.19 0.06    -1.08 0.18
## ------------------------------------------------------------ 
## group: other
##    vars  n mean   sd median trimmed  mad min max range skew kurtosis   se
## X1    1 33  3.4 1.02   3.41    3.48 1.04 1.3 4.7  3.41 -0.5    -0.76 0.18
## ------------------------------------------------------------ 
## group: ptsd
##    vars  n mean   sd median trimmed mad  min max range  skew kurtosis   se
## X1    1 20 3.27 1.07   3.46    3.32 0.8 1.41   5  3.59 -0.51    -0.92 0.24
# last, use a boxplot to examine your continuous and categorical variables together
boxplot(d2$iou~d2$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!

d2 <- subset(d2, mhealth != "other")
table(d2$mhealth, useNA = "always")
## 
##              anxiety disorder                       bipolar 
##                           121                             5 
##                    depression              eating disorders 
##                            29                            29 
##                    none or NA obsessive compulsive disorder 
##                           768                            25 
##                         other                          ptsd 
##                             0                            20 
##                          <NA> 
##                             0
d2$mhealth <- droplevels(d2$mhealth) # using droplevels() to drop the empty factor


#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~mhealth, data = d2)
## Levene's Test for Homogeneity of Variance (center = median)
##        Df F value Pr(>F)
## group   6  0.9366 0.4677
##       990

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 depression, other, bipolar, anxiety disorder, eating disorders, ptsd 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 mhealth other groups
d2 <- subset(d2, mhealth == "obsessive compulsive disorder" | mhealth == "none or NA")
d2$mhealth <- droplevels(d2$mhealth)

table(d2$mhealth)
## 
##                    none or NA obsessive compulsive disorder 
##                           768                            25

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(d2$iou~d2$mhealth)

7 View Test Output

t_output
## 
##  Welch Two Sample t-test
## 
## data:  d2$iou by d2$mhealth
## t = -4.2454, df = 25.304, p-value = 0.0002578
## alternative hypothesis: true difference in means between group none or NA and group obsessive compulsive disorder is not equal to 0
## 95 percent confidence interval:
##  -1.1646186 -0.4040736
## sample estimates:
##                    mean in group none or NA 
##                                    2.415654 
## mean in group obsessive compulsive disorder 
##                                    3.200000

8 Calculate Cohen’s d

# once again, we use our formula to calculate cohen's d
d2_output <- effsize::cohen.d(d2$iou~d2$mhealth)

9 View Effect Size

d2_output
## 
## Cohen's d
## 
## d estimate: -0.9448712 (large)
## 95 percent confidence interval:
##      lower      upper 
## -1.3465128 -0.5432296

10 Write Up Results

To test our hypothesis that participants with obsessive compulsive disorder in our sample would report significantly higher on the intolerance of uncertainty scale than people reporting none or NA for mental health disorders, we used an two-sample or independent t-test. This required us to drop our participants with depression, anxiety disorders, bipolar, ptsd, eating disorders and other mental disorders 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 = .0002578). 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 obsessive compulsive disorder (M = 3.20, SD = 0.83) reported significantly higher intolerance of uncertainty than participants who reported none or NA for mental health disorders (M = 2.416, SD = .89); t(25.304) = -4.25, p < .001 (see Figure 1). The effect size was calculated using Cohen’s d, with a value of -0.945 (small effect; Cohen, 1988).

References

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