# 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
We predict that individuals with a mental health disorder will report significantly higher levels of worry than individuals without a 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': 697 obs. of 7 variables:
## $ X : int 520 2814 3146 3295 717 6056 4753 5365 2044 1965 ...
## $ mhealth : chr "none or NA" "none or NA" "none or NA" "none or NA" ...
## $ sleep_hours: chr "2 5-6 hours" "3 7-8 hours" "2 5-6 hours" "4 8-10 hours" ...
## $ big5_neu : num 5.33 2.67 1 3.67 4.33 ...
## $ big5_con : num 3 4 6 4 3.33 ...
## $ pswq : num 2.71 1.43 1.86 1.79 2.36 ...
## $ covid_pos : int 0 0 0 0 0 0 0 0 0 0 ...
# 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': 697 obs. of 7 variables:
## $ X : int 520 2814 3146 3295 717 6056 4753 5365 2044 1965 ...
## $ mhealth : Factor w/ 8 levels "anxiety disorder",..: 5 5 5 5 5 5 5 5 5 5 ...
## $ sleep_hours: chr "2 5-6 hours" "3 7-8 hours" "2 5-6 hours" "4 8-10 hours" ...
## $ big5_neu : num 5.33 2.67 1 3.67 4.33 ...
## $ big5_con : num 3 4 6 4 3.33 ...
## $ pswq : num 2.71 1.43 1.86 1.79 2.36 ...
## $ covid_pos : int 0 0 0 0 0 0 0 0 0 0 ...
table(d$mhealth, useNA = "always")
##
## anxiety disorder bipolar
## 78 3
## depression eating disorders
## 12 20
## none or NA obsessive compulsive disorder
## 539 15
## other ptsd
## 17 13
## <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 697 2.66 0.76 2.71 2.67 0.95 1 4 3 -0.15 -0.98 0.03
# 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 78 3.09 0.59 3.14 3.15 0.53 1.64 3.93 2.29 -0.68 -0.15 0.07
## ------------------------------------------------------------
## 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 12 2.63 0.58 2.79 2.69 0.48 1.36 3.29 1.93 -0.78 -0.56 0.17
## ------------------------------------------------------------
## group: eating disorders
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 20 3.15 0.7 3.21 3.21 0.85 1.5 4 2.5 -0.56 -0.73 0.16
## ------------------------------------------------------------
## group: none or NA
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 539 2.52 0.74 2.5 2.52 0.95 1 4 3 0.01 -1 0.03
## ------------------------------------------------------------
## group: obsessive compulsive disorder
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 15 3.35 0.45 3.36 3.35 0.64 2.71 4 1.29 0.1 -1.58 0.12
## ------------------------------------------------------------
## group: other
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 17 3.34 0.55 3.5 3.37 0.53 2.14 4 1.86 -0.73 -0.69 0.13
## ------------------------------------------------------------
## group: ptsd
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 13 3.27 0.67 3.36 3.36 0.64 1.64 4 2.36 -1 0.18 0.18
# lastly, use a boxplot to examine your chosen continuous and categorical variables together
boxplot(d$pswq~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 %in% c("anxiety disorder", "depression")) # use subset() to remove all participants from the additional level
# Check the table to make sure only the two levels remain
table(d$mental_health_disorders, useNA = "always")
##
## <NA>
## 0
# Drop the unused factor levels
d$mhealth <- droplevels(d$mhealth)
# Verify again
table(d$mhealth, useNA = "always")
##
## anxiety disorder depression <NA>
## 78 12 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(pswq~mhealth, data =d)
## Levene's Test for Homogeneity of Variance (center = median)
## Df F value Pr(>F)
## group 1 0.0098 0.9213
## 88
Levene’s test revealed that variances of PSWQ scores were equal between participants with anxiety disorder and participants with 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 had more than two levels initially, so I dropped all levels except “anxiety disorder” and “depression” to meet the assumptions of a t-test. I will note this in my methods and discussion sections as a limitation of the study.
Levene’s test indicated that the variances of PSWQ scores were equal between the two groups. Although there is no concern regarding heterogeneity of variance, we will use Welch’s t-test to compare the groups, as this is the recommended approach for the class.
# 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
t_output
##
## Welch Two Sample t-test
##
## data: d$pswq by d$mhealth
## t = 2.6063, df = 14.753, p-value = 0.02005
## alternative hypothesis: true difference in means between group anxiety disorder and group depression is not equal to 0
## 95 percent confidence interval:
## 0.08478309 0.85203009
## sample estimates:
## mean in group anxiety disorder mean in group depression
## 3.093407 2.625000
# 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(pswq~mhealth, data=d) # d_output will now show in your Global Environment
d_output
##
## Cohen's d
##
## d estimate: 0.7961112 (medium)
## 95 percent confidence interval:
## lower upper
## 0.1686973 1.4235251
## Remember to always take the ABSOLAUTE VALUE of the effect size value (i.e., it will never be negative)
To test our hypothesis that participants with anxiety disorders would report significantly higher levels of worry than participants with depression, we used an independent samples t-test. This required us to limit our comparison to these two groups, as the t-test is restricted to two levels of the independent variable. We tested the homogeneity of variance with Levene’s test and found that the variances were approximately equal (F(1, 88) = 0.0098, p = 0.921). Our data met all other assumptions of an independent samples t-test.
As predicted, participants with anxiety disorders (M = 3.09, SD = 0.98) reported significantly higher levels of worry than participants with depression (M = 2.63, SD = 0.95); t(14.75) = 2.61, p = 0.020. The effect size was calculated using Cohen’s d, with a value of 0.81 (large effect), indicating a substantial difference between the two groups.
References
Cohen J. (1988). Statistical Power Analysis for the Behavioral Sciences. New York, NY: Routledge Academic.