# 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 resilience by people’s level of mental health disorders, between none 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': 258 obs. of 7 variables:
## $ X : int 7888 7365 8760 8272 8738 7911 8834 6591 6638 8414 ...
## $ treatment: chr "seeking treatment" "no psychological disorders" "no psychological disorders" "not in treatment" ...
## $ mhealth : chr "none or NA" "none or NA" "none or NA" "none or NA" ...
## $ pas_covid: num 4.44 3.11 2.78 3.33 4.67 ...
## $ gad : num 4 1.43 1 1 3.43 ...
## $ brs : num 2 3.83 4.67 3.33 3 ...
## $ support : num 4.83 5 4.83 4.33 4.17 ...
# 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': 258 obs. of 7 variables:
## $ X : int 7888 7365 8760 8272 8738 7911 8834 6591 6638 8414 ...
## $ treatment: chr "seeking treatment" "no psychological disorders" "no psychological disorders" "not in treatment" ...
## $ mhealth : Factor w/ 8 levels "anxiety disorder",..: 5 5 5 5 5 5 5 5 5 5 ...
## $ pas_covid: num 4.44 3.11 2.78 3.33 4.67 ...
## $ gad : num 4 1.43 1 1 3.43 ...
## $ brs : num 2 3.83 4.67 3.33 3 ...
## $ support : num 4.83 5 4.83 4.33 4.17 ...
table(d$mhealth, useNA = "always")
##
## anxiety disorder bipolar
## 40 3
## depression eating disorders
## 5 14
## none or NA obsessive compulsive disorder
## 165 11
## other ptsd
## 12 8
## <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 258 2.56 0.86 2.5 2.54 0.74 1 5 4 0.26 -0.4 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 40 2.47 0.9 2.25 2.36 0.62 1 5 4 0.99 0.76 0.14
## ------------------------------------------------------------
## 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 5 2.77 0.73 2.67 2.77 0.49 1.83 3.83 2 0.2 -1.57 0.33
## ------------------------------------------------------------
## group: eating disorders
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 14 2.51 0.79 2.33 2.46 0.74 1.5 4.17 2.67 0.58 -0.83 0.21
## ------------------------------------------------------------
## group: none or NA
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 165 2.66 0.84 2.67 2.67 0.99 1 4.67 3.67 0.03 -0.47 0.07
## ------------------------------------------------------------
## group: obsessive compulsive disorder
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 11 2.23 0.76 2.17 2.24 0.99 1 3.33 2.33 0.04 -1.51 0.23
## ------------------------------------------------------------
## group: other
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 12 2.04 1.09 1.75 1.97 0.86 1 3.83 2.83 0.66 -1.32 0.31
## ------------------------------------------------------------
## group: ptsd
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 8 2.27 0.71 2 2.27 0.49 1.5 3.5 2 0.65 -1.28 0.25
# lastly, use a boxplot to examine your chosen continuous and categorical variables together
boxplot(d$brs~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 != "anxiety 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 bipolar
## 0 3
## depression eating disorders
## 5 14
## none or NA obsessive compulsive disorder
## 165 11
## other ptsd
## 12 8
## <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
##
## bipolar depression
## 3 5
## eating disorders none or NA
## 14 165
## obsessive compulsive disorder other
## 11 12
## ptsd <NA>
## 8 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
##
## bipolar depression
## 0 5
## eating disorders none or NA
## 14 165
## obsessive compulsive disorder other
## 11 12
## ptsd <NA>
## 8 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 eating disorders
## 5 14
## none or NA obsessive compulsive disorder
## 165 11
## other ptsd
## 12 8
## <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
##
## depression eating disorders
## 5 0
## none or NA obsessive compulsive disorder
## 165 11
## other ptsd
## 12 8
## <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
## 5 165
## obsessive compulsive disorder other
## 11 12
## ptsd <NA>
## 8 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
##
## depression none or NA
## 5 165
## obsessive compulsive disorder other
## 11 0
## ptsd <NA>
## 8 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
## 5 165
## obsessive compulsive disorder ptsd
## 11 8
## <NA>
## 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
##
## depression none or NA
## 5 165
## obsessive compulsive disorder ptsd
## 0 8
## <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 ptsd <NA>
## 5 165 8 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
##
## depression none or NA ptsd <NA>
## 5 165 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
##
## depression none or NA <NA>
## 5 165 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(brs~mhealth, data =d)
## Levene's Test for Homogeneity of Variance (center = median)
## Df F value Pr(>F)
## group 1 0.7056 0.4021
## 168
Levene’s test revealed that our data has no significantly different variances between the two comparison groups, depression and none or NA, on their levels of resilience.
[Revise the above statement for you HW assignment. Then delete this reminder.]
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 anxiety disorder, bipolar, ptsd, eating disorders, obsessive compulsive disorder, and other groups participants 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, regarding homogeneity of variance, as Levene’s test was not significant. I will still 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$brs~d$mhealth) # t_output will now show in your Global Environment
t_output
##
## Welch Two Sample t-test
##
## data: d$brs by d$mhealth
## t = 0.31164, df = 4.3221, p-value = 0.7698
## alternative hypothesis: true difference in means between group depression and group none or NA is not equal to 0
## 95 percent confidence interval:
## -0.7962668 1.0043476
## sample estimates:
## mean in group depression mean in group none or NA
## 2.766667 2.662626
# 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$brs~d$mhealth) # d_output will now show in your Global Environment
d_output
##
## Cohen's d
##
## d estimate: 0.1248024 (negligible)
## 95 percent confidence interval:
## lower upper
## -0.7714569 1.0210616
## Remember to always take the ABSOLAUTE VALUE of the effect size value (i.e., it will never be negative)
To test our hypothesis that people with no mental health disorder in our sample would report significantly higher levels of resilience than those with depression, we used an independent samples t-test. This required us to drop our anxiety disorder, bipolar, ptsd, eating disorders, obsessive compulsive disorder, and other groups participants 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 signs of homogeneity (p = 0.40). This suggests that there is an increased chance of type II error. We still 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 no mental health disorder (M = 2.66, SD = 0.84) reported no significantly higher levels of resilience than depression (M = 2.76, SD = 0.73); t(1.32) = 0.31, p< .76 (see Figure 1). The effect size was calculated using Cohen’s d, with a value of 0.12 (negligible effect; Cohen, 1988).
[Revise the above statements for you HW assignment.]
References
Cohen J. (1988). Statistical Power Analysis for the Behavioral Sciences. New York, NY: Routledge Academic.