# 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 pandemic-specific anxiety by people’s level of mental health treatment during the COVID-19 lockdown, between receiving treatment and not receiving treatment.
# you **only** need to check the variables you're using in the current analysis
## Checking the Categorical variable (IV)
str(d)
## 'data.frame': 1166 obs. of 7 variables:
## $ X : int 1 321 401 1390 2183 2247 2526 2609 2689 2752 ...
## $ treatment: chr "no psychological disorders" "not in treatment" "not in treatment" "not in treatment" ...
## $ mhealth : chr "none or NA" "none or NA" "obsessive compulsive disorder" "none or NA" ...
## $ pswq : num 4.94 1.71 2.44 2.25 3.14 ...
## $ pas_covid: num 3.22 2.33 4 2.89 3.22 ...
## $ covid_neg: int 0 0 0 0 0 0 0 0 0 0 ...
## $ 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$treatment <- as.factor(d$treatment)
str(d)
## 'data.frame': 1166 obs. of 7 variables:
## $ X : int 1 321 401 1390 2183 2247 2526 2609 2689 2752 ...
## $ treatment: Factor w/ 6 levels "in treatment",..: 2 3 3 3 3 2 3 2 5 2 ...
## $ mhealth : chr "none or NA" "none or NA" "obsessive compulsive disorder" "none or NA" ...
## $ pswq : num 4.94 1.71 2.44 2.25 3.14 ...
## $ pas_covid: num 3.22 2.33 4 2.89 3.22 ...
## $ covid_neg: int 0 0 0 0 0 0 0 0 0 0 ...
## $ covid_pos: int 0 0 0 0 0 0 0 0 0 0 ...
table(d$treatment, useNA = "always")
##
## in treatment no psychological disorders
## 78 425
## not in treatment other
## 548 14
## seeking treatment treatment disrupted by COVID-19
## 46 55
## <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 1166 2.77 0.8 2.8 2.77 0.93 1 4.94 3.94 -0.03 -0.79 0.02
# 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$treatment)
##
## Descriptive statistics by group
## group: in treatment
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 78 3.27 0.63 3.36 3.32 0.64 1 4.75 3.75 -0.79 1 0.07
## ------------------------------------------------------------
## group: no psychological disorders
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 425 2.63 0.79 2.57 2.61 0.95 1.07 4.94 3.87 0.17 -0.71 0.04
## ------------------------------------------------------------
## group: not in treatment
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 548 2.71 0.8 2.75 2.7 1.01 1 4.94 3.94 0.09 -0.81 0.03
## ------------------------------------------------------------
## group: other
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 14 3.17 0.67 3.25 3.23 0.79 1.64 4 2.36 -0.73 -0.49 0.18
## ------------------------------------------------------------
## group: seeking treatment
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 46 3.26 0.62 3.18 3.26 0.69 1.57 4.69 3.12 -0.1 0.07 0.09
## ------------------------------------------------------------
## group: treatment disrupted by COVID-19
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 55 3.24 0.55 3.21 3.25 0.64 1.71 4.44 2.72 -0.33 -0.36 0.07
# lastly, use a boxplot to examine your chosen continuous and categorical variables together
boxplot(d$pswq~d$treatment)
# 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, treatment != "no psychological disorders") # use subset() to remove all participants from the additional level
table(d$treatment, useNA = "always") # verify that now there are ZERO participants in the additional level
##
## in treatment no psychological disorders
## 78 0
## not in treatment other
## 548 14
## seeking treatment treatment disrupted by COVID-19
## 46 55
## <NA>
## 0
d$treatment<- droplevels(d$treatment) # use droplevels() to drop the empty factor
table(d$treatment, useNA = "always") # verify that now the entire factor level is removed
##
## in treatment not in treatment
## 78 548
## other seeking treatment
## 14 46
## treatment disrupted by COVID-19 <NA>
## 55 0
d <- subset(d, treatment != "other") # use subset() to remove all participants from the additional level
table(d$treatment, useNA = "always") # verify that now there are ZERO participants in the additional level
##
## in treatment not in treatment
## 78 548
## other seeking treatment
## 0 46
## treatment disrupted by COVID-19 <NA>
## 55 0
d$treatment<- droplevels(d$treatment) # use droplevels() to drop the empty factor
table(d$treatment, useNA = "always") # verify that now the entire factor level is removed
##
## in treatment not in treatment
## 78 548
## seeking treatment treatment disrupted by COVID-19
## 46 55
## <NA>
## 0
d <- subset(d, treatment != "seeking treatment") # use subset() to remove all participants from the additional level
table(d$treatment, useNA = "always") # verify that now there are ZERO participants in the additional level
##
## in treatment not in treatment
## 78 548
## seeking treatment treatment disrupted by COVID-19
## 0 55
## <NA>
## 0
d$treatment<- droplevels(d$treatment) # use droplevels() to drop the empty factor
table(d$treatment, useNA = "always") # verify that now the entire factor level is removed
##
## in treatment not in treatment
## 78 548
## treatment disrupted by COVID-19 <NA>
## 55 0
d <- subset(d, treatment != "treatment disrupted by COVID-19") # use subset() to remove all participants from the additional level
table(d$treatment, useNA = "always") # verify that now there are ZERO participants in the additional level
##
## in treatment not in treatment
## 78 548
## treatment disrupted by COVID-19 <NA>
## 0 0
d$treatment<- droplevels(d$treatment) # use droplevels() to drop the empty factor
table(d$treatment, useNA = "always") # verify that now the entire factor level is removed
##
## in treatment not in treatment <NA>
## 78 548 0
d <- subset(d, treatment != "<NA>") # use subset() to remove all participants from the additional level
table(d$treatment, useNA = "always") # verify that now there are ZERO participants in the additional level
##
## in treatment not in treatment <NA>
## 78 548 0
d$treatment<- droplevels(d$treatment) # use droplevels() to drop the empty factor
table(d$treatment, useNA = "always") # verify that now the entire factor level is removed
##
## in treatment not in treatment <NA>
## 78 548 0
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~treatment, data =d)
## Levene's Test for Homogeneity of Variance (center = median)
## Df F value Pr(>F)
## group 1 13.467 0.0002636 ***
## 624
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
Levene’s test revealed that our data has significantly different variances between the two comparison groups, in treatment and not in treatment, on their levels of pandemic-specific 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 no psychological disorders, seeking treatment, treatment disrupted by COVID-19, and other 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 also has an issue regarding homogeneity of variance, as Levene’s test was significant. To accommodate for this heterogeneity of variance, I will use Welch’s t-test instead of student’s t-test in my analysis.
[Revise the above statements for you HW assignment. Then delete this reminder.]
# 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$treatment) # t_output will now show in your Global Environment
t_output
##
## Welch Two Sample t-test
##
## data: d$pswq by d$treatment
## t = 7.1783, df = 115.91, p-value = 7.238e-11
## alternative hypothesis: true difference in means between group in treatment and group not in treatment is not equal to 0
## 95 percent confidence interval:
## 0.4116781 0.7254297
## sample estimates:
## mean in group in treatment mean in group not in treatment
## 3.274840 2.706286
# 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$pswq~d$treatment) # d_output will now show in your Global Environment
d_output
##
## Cohen's d
##
## d estimate: 0.7246662 (medium)
## 95 percent confidence interval:
## lower upper
## 0.4836354 0.9656970
## 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 in treatment in our sample would report significantly different levels of pandemic-specific worry than people not in treatment, we used an independent samples t-test. This required us to drop our participants with no psychological disorders, seeking treatment, treatment disrupted by COVID-19, and other 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 heterogeneity (p < .001). This suggests that there is an increased chance of Type I error. To correct for this issue, we 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 people in treatment (M = 3.274 , SD = 0.63) reported significantly different levels of pandemic-specific worry than people not in treatment (M = 2.706, SD = 0.8); t(115.91) = 7.1783, p < 0.001 (see Figure 1). The effect size was calculated using Cohen’s d, with a value of 0.72 (medium 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.