# 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
## 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
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
There will be a significant difference in score on the Intolerance of Uncertainty scale by people’s level of an individual’s Mental Health between anxiety disorder and depression.
# you **only** need to check the variables you're using in the current analysis
## Checking the Categorical variable (IV)
str(d)
## 'data.frame': 666 obs. of 7 variables:
## $ X : int 520 2814 3146 3295 717 6056 4753 2044 1965 1246 ...
## $ age : chr "1 under 18" "1 under 18" "1 under 18" "1 under 18" ...
## $ mhealth : chr "none or NA" "none or NA" "none or NA" "none or NA" ...
## $ iou : num 2.22 1.52 1.78 1.85 2.22 ...
## $ mfq_26 : num 2.7 4.55 4.8 3.8 4.5 4 5.8 4.5 5.25 5.15 ...
## $ mfq_state: num 3 4.38 4.88 4.88 4.88 ...
## $ pss : num 2.75 2.25 3 2 1.75 2 1 3 1.25 1.25 ...
# 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': 666 obs. of 7 variables:
## $ X : int 520 2814 3146 3295 717 6056 4753 2044 1965 1246 ...
## $ age : chr "1 under 18" "1 under 18" "1 under 18" "1 under 18" ...
## $ mhealth : Factor w/ 8 levels "anxiety disorder",..: 5 5 5 5 5 5 5 5 5 5 ...
## $ iou : num 2.22 1.52 1.78 1.85 2.22 ...
## $ mfq_26 : num 2.7 4.55 4.8 3.8 4.5 4 5.8 4.5 5.25 5.15 ...
## $ mfq_state: num 3 4.38 4.88 4.88 4.88 ...
## $ pss : num 2.75 2.25 3 2 1.75 2 1 3 1.25 1.25 ...
table(d$mhealth, useNA = "always")
##
## anxiety disorder bipolar
## 76 3
## depression eating disorders
## 11 19
## none or NA obsessive compulsive disorder
## 515 15
## other ptsd
## 18 9
## <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$iou)
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 666 2.68 0.92 2.56 2.65 1.04 1 4.78 3.78 0.29 -0.85 0.04
# also use a histogram to visualize your continuous variable
hist(d$iou)
# 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$mhealth)
##
## Descriptive statistics by group
## group: anxiety disorder
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 76 3.17 0.85 3.19 3.17 0.88 1.37 4.78 3.41 -0.08 -0.81 0.1
## ------------------------------------------------------------
## group: bipolar
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 3 3.07 1.31 2.85 3.07 1.43 1.89 4.48 2.59 0.16 -2.33 0.76
## ------------------------------------------------------------
## group: depression
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 11 2.7 1.07 2.63 2.68 1.26 1.26 4.33 3.07 0.18 -1.5 0.32
## ------------------------------------------------------------
## group: eating disorders
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 19 3.35 0.75 3.52 3.37 0.82 2 4.22 2.22 -0.55 -1.16 0.17
## ------------------------------------------------------------
## group: none or NA
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 515 2.51 0.85 2.41 2.46 0.99 1 4.78 3.78 0.44 -0.61 0.04
## ------------------------------------------------------------
## group: obsessive compulsive disorder
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 15 3.41 0.9 3.78 3.44 0.99 1.7 4.78 3.07 -0.38 -1.08 0.23
## ------------------------------------------------------------
## group: other
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 18 3.9 0.68 4.09 3.94 0.82 2.33 4.7 2.37 -0.55 -0.73 0.16
## ------------------------------------------------------------
## group: ptsd
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 9 3.49 0.89 3.48 3.49 0.71 1.56 4.52 2.96 -0.83 -0.23 0.3
# lastly, use a boxplot to examine your chosen continuous and categorical variables together
boxplot(d$iou~d$mhealth)
# 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
## 76 0
## depression eating disorders
## 11 19
## none or NA obsessive compulsive disorder
## 515 15
## other ptsd
## 18 9
## <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
## 76 11
## eating disorders none or NA
## 19 515
## obsessive compulsive disorder other
## 15 18
## ptsd <NA>
## 9 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 != "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 depression
## 76 11
## eating disorders none or NA
## 0 515
## obsessive compulsive disorder other
## 15 18
## ptsd <NA>
## 9 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
## 76 11
## none or NA obsessive compulsive disorder
## 515 15
## other ptsd
## 18 9
## <NA>
## 0
d <- subset(d, mhealth != "none or NA") # 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
## 76 11
## none or NA obsessive compulsive disorder
## 0 15
## other ptsd
## 18 9
## <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
## 76 11
## obsessive compulsive disorder other
## 15 18
## ptsd <NA>
## 9 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 depression
## 76 11
## obsessive compulsive disorder other
## 0 18
## ptsd <NA>
## 9 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 other ptsd
## 76 11 18 9
## <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 depression other ptsd
## 76 11 0 9
## <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 ptsd <NA>
## 76 11 9 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 depression ptsd <NA>
## 76 11 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 depression <NA>
## 76 11 0
d <- subset(d, mhealth != "NA") # 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 <NA>
## 76 11 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 <NA>
## 76 11 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.
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(iou~mhealth, data = d)
## Levene's Test for Homogeneity of Variance (center = median)
## Df F value Pr(>F)
## group 1 1.0681 0.3043
## 85
Levene’s test revealed that our data has is not significantly different variances between the two comparison groups, Mental Health and Intolerance of uncertainty, between anxiety disorder and depression.
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.
My independent variable, Mental Health has more than two levels. To proceed with this analysis, I dropped participants with Bipolar Disorder, eating disorders,Obsessive Compulsive Disorder, PTSD, “other” reported disorders, and anyone who qualified as NA and did not have any mental health disorders 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.
Since Levene’s test showed that there was no statistically significant variance between the two groups, my data appears to have no issues regarding homogeneity of variance. Regardless, I will use Welch’s t-test instead of Student’s t-test in my analysis.
# 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$iou~d$mhealth) # t_output will now show in your Global Environment
t_output
##
## Welch Two Sample t-test
##
## data: d$iou by d$mhealth
## t = 1.3839, df = 11.897, p-value = 0.1918
## alternative hypothesis: true difference in means between group anxiety disorder and group depression is not equal to 0
## 95 percent confidence interval:
## -0.2680265 1.1987372
## sample estimates:
## mean in group anxiety disorder mean in group depression
## 3.165692 2.700337
# 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$iou~d$mhealth) # d_output will now show in your Global Environment
d_output
##
## Cohen's d
##
## d estimate: 0.5307288 (medium)
## 95 percent confidence interval:
## lower upper
## -0.1156442 1.1771018
## Remember to always take the ABSOLAUTE VALUE of the effect size value (i.e., it will never be negative)
To test my hypothesis that there will be a significant difference in score on the Intolerance of Uncertainty scale by people’s level of an individual’s Mental Health between anxiety disorder and depression, we used an independent samples t-test. This required me to drop numerous participants from our sample, as we are limited to a two group comparison when using this test. The participants who were dropped and are not included in the data analyzed are individualds with: Bipolar Disorder, Obsessive Compulsive Disorder, PTSD, any “other” disorders uncategorized, and any participants were “NA” and did not report any mental health idsorders. I tested only individuals with Anxiety Disorder and Depression to satisfy the two group comparison limitation of the t-test. I tested the homogeneity of variance with Levene’s test and did not find signs of heterogeneity (p > .05). This suggests that there is not an increased chance of Type 1 error. Regardless, I used Welch’s t-test, which does not assume homogeneity of variance to ensure accuracy. My data met all the assumptions of an independent samples t-test.
The results indicate that we are unable to reject the null hypothesis, and that there is not a statistically significant difference in mean Intolerance of Uncertainty scores between people with Anxiety Disorder and Depression regarding Mental Health. I found that individuals with Anxiety Disorder (M = 3.17, SD = 0.85) did not have significantly different levels of Intolerance of Uncertainty than individuals with Depression (M = 2.7, SD = 1.07); t(11.897) = 1.38, p <.001 (see Figure 1). The effect size was calculated using Cohen’s d, with a value of 0.53 (medium effect; Cohen, 1988).
References
Cohen J. (1988). Statistical Power Analysis for the Behavioral Sciences. New York, NY: Routledge Academic.