# 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
I predict that there will be a significant difference in pandemic anxiety by people’s level of mental health disorder, between those who have anxiety disorder and those who have no mental health disorder.
# you **only** need to check the variables you're using in the current analysis
## Checking the Categorical variable (IV)
str(d)
## 'data.frame': 284 obs. of 7 variables:
## $ X : int 7888 7365 8747 7357 8760 8654 8738 7911 8463 8834 ...
## $ 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 3.48 1.59 1.59 1.26 1.93 ...
## $ mfq_state: num 3.75 4 5.88 4.75 5.5 ...
## $ pas_covid: num 4.44 3.11 3.22 1.78 2.78 ...
## $ 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': 284 obs. of 7 variables:
## $ X : int 7888 7365 8747 7357 8760 8654 8738 7911 8463 8834 ...
## $ 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 3.48 1.59 1.59 1.26 1.93 ...
## $ mfq_state: num 3.75 4 5.88 4.75 5.5 ...
## $ pas_covid: num 4.44 3.11 3.22 1.78 2.78 ...
## $ brs : num 2 3.83 3.83 4 4.67 ...
table(d$mhealth, useNA = "always")
##
## anxiety disorder bipolar
## 40 3
## depression eating disorders
## 5 14
## none or NA obsessive compulsive disorder
## 191 11
## other ptsd
## 13 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$pas_covid)
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 284 3.34 0.64 3.44 3.37 0.66 1 5 4 -0.4 0.65 0.04
# also use a histogram to visualize your continuous variable
hist(d$pas_covid)
# 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$pas_covid, 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 40 3.5 0.68 3.56 3.52 0.66 1.89 4.78 2.89 -0.39 -0.3 0.11
## ------------------------------------------------------------
## group: bipolar
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 3 2.96 0.23 2.89 2.96 0.16 2.78 3.22 0.44 0.29 -2.33 0.13
## ------------------------------------------------------------
## group: depression
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 5 2.84 0.68 3 2.84 0.16 1.67 3.44 1.78 -0.87 -1.11 0.31
## ------------------------------------------------------------
## group: eating disorders
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 14 3.54 0.58 3.67 3.57 0.58 2.33 4.33 2 -0.38 -0.74 0.15
## ------------------------------------------------------------
## group: none or NA
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 191 3.28 0.61 3.33 3.3 0.49 1 4.78 3.78 -0.41 0.66 0.04
## ------------------------------------------------------------
## group: obsessive compulsive disorder
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 11 3.68 0.43 3.56 3.67 0.49 3 4.44 1.44 0.21 -1.21 0.13
## ------------------------------------------------------------
## group: other
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 13 3.42 0.91 3.22 3.52 0.49 1.22 4.56 3.33 -0.73 0.13 0.25
## ------------------------------------------------------------
## group: ptsd
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 7 3.75 0.66 3.56 3.75 0.49 3 5 2 0.72 -0.83 0.25
# lastly, use a boxplot to examine your chosen continuous and categorical variables together
boxplot(d$pas_covid~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 != "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 bipolar
## 40 3
## depression eating disorders
## 5 0
## none or NA obsessive compulsive disorder
## 191 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 bipolar
## 40 3
## depression none or NA
## 5 191
## obsessive compulsive disorder other
## 11 13
## ptsd <NA>
## 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 bipolar
## 40 3
## depression none or NA
## 5 191
## obsessive compulsive disorder other
## 11 13
## ptsd <NA>
## 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 bipolar
## 40 3
## depression none or NA
## 5 191
## obsessive compulsive disorder other
## 11 13
## <NA>
## 0
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
## 40 0
## depression none or NA
## 5 191
## obsessive compulsive disorder other
## 11 13
## <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
## 40 5
## none or NA obsessive compulsive disorder
## 191 11
## other <NA>
## 13 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
## 40 5
## none or NA obsessive compulsive disorder
## 191 0
## other <NA>
## 13 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 none or NA other
## 40 5 191 13
## <NA>
## 0
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 none or NA other
## 40 0 191 13
## <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 other <NA>
## 40 191 13 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 <NA>
## 40 191 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>
## 40 191 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(pas_covid~mhealth, data =d)
## Levene's Test for Homogeneity of Variance (center = median)
## Df F value Pr(>F)
## group 1 0.238 0.6261
## 229
Levene’s test revealed that my data does not have significantly different variances between the two comparison groups, anxiety disorder and none or NA, on their levels of pandemic anxiety.
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 has more than two levels. To proceed with this analysis, I will drop the participants who indicate eating disorders, bipolar, depression, obsessive compulsive disorder, 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 does not have an issue regarding homogeneity of variance, as Levene’s test was non-significant. Despite meeting the assumption for homogeneity of variance, 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$pas_covid~d$mhealth) # t_output will now show in your Global Environment
t_output
##
## Welch Two Sample t-test
##
## data: d$pas_covid by d$mhealth
## t = 1.8983, df = 52.943, p-value = 0.06311
## 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.0125646 0.4564273
## sample estimates:
## mean in group anxiety disorder mean in group none or NA
## 3.500000 3.278069
# 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_output will now show in your Global Environment
#d_output
## Remember to always take the ABSOLAUTE VALUE of the effect size value (i.e., it will never be negative)
To test my hypothesis that those with anxiety disorder in my sample would report significantly different levels of pandemic anxiety than those with no mental health disorder, I used an independent samples t-test. This required me to drop the eating disorders, bipolar, depression, obsessive compulsive disorder, ptsd, and other participants from my sample, as I am limited to a two-group comparison when using this test. I tested the homogeneity of variance with Levene’s test and found signs of homogeneity (p = 0.626). This suggests that there is not an increased chance of Type I error. Despite meeting the assumption of homogeneity of variance, I used Welch’s t-test, which does not assume homogeneity of variance. My data met all other assumptions of an independent samples t-test.
Contrary to my prediction, I found that those with anxiety disorder (M = 3.50, SD = 0.68) did not report significantly different levels of pandemic anxiety than those with no mental health disorder (M = 3.28, SD = 0.61); t(52.94) = 1.90, p = 0.063 (see Figure 1).
References
Cohen J. (1988). Statistical Power Analysis for the Behavioral Sciences. New York, NY: Routledge Academic.