# 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/arc_data_final_SP26.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 being in-treatment will report significantly lower levels of depression than not being in-treatment.
# you **only** need to check the variables you're using in the current analysis
## Checking the Categorical variable (IV)
str(d$treatment)
## chr [1:996] NA "not in treatment" NA "no psychological disorders" ...
# 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': 996 obs. of 40 variables:
## $ X : int 520 2814 3146 3295 717 6056 4753 5365 2044 1965 ...
## $ gender : chr "female" "male" "female" "male" ...
## $ trans : chr "no" "no" "no" "no" ...
## $ sexual_orientation : chr "Prefer not to say" "Heterosexual/Straight" "Heterosexual/Straight" "Heterosexual/Straight" ...
## $ ethnicity : chr "Prefer not to say" "White - British, Irish, other" "Asian/Asian British - Indian, Pakistani, Bangladeshi, other" "Asian/Asian British - Indian, Pakistani, Bangladeshi, other" ...
## $ relationship_status : chr "Single, never married" "Single, never married" "Prefer not to say" "Single, never married" ...
## $ age : chr "1 under 18" "1 under 18" "1 under 18" "1 under 18" ...
## $ urban_rural : chr "town" "town" "town" "town" ...
## $ income : chr NA NA NA NA ...
## $ education : chr "1 equivalent to not completing high school" "prefer not to say" "2 equivalent to high school completion" "prefer not to say" ...
## $ employment : chr "1 high school equivalent" "1 high school equivalent" "1 high school equivalent" "1 high school equivalent" ...
## $ treatment : Factor w/ 6 levels "in treatment",..: NA 3 NA 2 3 3 3 3 3 NA ...
## $ health : chr "something else or not applicable" "something else or not applicable" "prefer not to say" "lung disease" ...
## $ 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" ...
## $ exercise : chr "1 less than 1 hour" "1 less than 1 hour" "1 less than 1 hour" "1 less than 1 hour" ...
## $ pet : chr "cat" "other" "no pets" "no pets" ...
## $ covid_pos : int 0 0 0 0 0 0 0 0 0 0 ...
## $ covid_neg : int 0 0 0 0 0 0 0 0 0 0 ...
## $ big5_open : num 3.67 4.33 5.67 6 5.67 ...
## $ big5_con : num 3 4 6 4 3.33 ...
## $ big5_agr : num 4.33 2.67 5.67 5.67 5 ...
## $ big5_neu : num 5.33 2.67 1 3.67 4.33 ...
## $ big5_ext : num 2 2.67 4.67 4.33 1.67 ...
## $ pswq : num 2.71 1.43 1.86 1.79 2.36 ...
## $ 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.2 4.5 5.25 ...
## $ mfq_state : num 3 4.38 4.88 4.88 4.88 ...
## $ rse : num 2.6 3.1 3.7 3 3 3 4 3.8 2.5 4 ...
## $ school_covid_support: num NA NA NA NA NA NA NA NA NA NA ...
## $ school_att : num NA NA NA NA NA NA NA NA NA NA ...
## $ pas_covid : num 3 3.44 4.67 2.44 1.56 ...
## $ pss : num 2.75 2.25 3 2 1.75 2 1 1.25 3 1.25 ...
## $ phq : num 1.56 1.44 1.11 1.33 1.44 ...
## $ gad : num 1.14 1.29 1 1 1.14 ...
## $ edeq12 : num 1.33 1.08 1 1 1.17 ...
## $ brs : num NA NA NA NA NA NA NA NA NA NA ...
## $ swemws : num 3 2.86 4 3.57 3.86 ...
## $ isolation_c : num 1 1 1 1 1 1 1 1 1 1 ...
## $ support : num 2.83 3 4 4 3.67 ...
table(d$treatment, useNA = "always")
##
## in treatment no psychological disorders
## 65 302
## not in treatment other
## 388 19
## seeking treatment treatment disrupted by COVID-19
## 38 52
## <NA>
## 132
## 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$phq)
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 727 2.28 0.86 2.22 2.23 0.99 1 4 3 0.37 -0.9 0.03
# also use a histogram to visualize your continuous variable
hist(d$phq)
# 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$phq, 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 48 2.88 0.76 3 2.92 0.82 1 4 3 -0.49 -0.63 0.11
## ------------------------------------------------------------
## group: no psychological disorders
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 245 2.05 0.78 1.89 1.99 0.82 1 4 3 0.67 -0.36 0.05
## ------------------------------------------------------------
## group: not in treatment
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 282 2.25 0.87 2.22 2.2 0.99 1 4 3 0.38 -0.91 0.05
## ------------------------------------------------------------
## group: other
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 12 2.5 0.52 2.56 2.48 0.58 1.78 3.44 1.67 0.15 -1.25 0.15
## ------------------------------------------------------------
## group: seeking treatment
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 25 3 0.79 2.78 3.04 0.99 1.33 4 2.67 -0.2 -1.24 0.16
## ------------------------------------------------------------
## group: treatment disrupted by COVID-19
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 33 3 0.81 3.11 3.07 0.99 1.33 4 2.67 -0.53 -0.87 0.14
# lastly, use a boxplot to examine your chosen continuous and categorical variables together
boxplot(d$phq~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 != "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 no psychological disorders
## 65 302
## not in treatment other
## 388 19
## seeking treatment treatment disrupted by COVID-19
## 0 52
## <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 no psychological disorders
## 65 302
## not in treatment other
## 388 19
## treatment disrupted by COVID-19 <NA>
## 52 0
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
## 65 0
## not in treatment other
## 388 19
## treatment disrupted by COVID-19 <NA>
## 52 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
## 65 388
## other treatment disrupted by COVID-19
## 19 52
## <NA>
## 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
## 65 388
## other treatment disrupted by COVID-19
## 0 52
## <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
## 65 388
## treatment disrupted by COVID-19 <NA>
## 52 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
## 65 388
## 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>
## 65 388 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>
## 65 388 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>
## 65 388 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(phq~treatment, data = d)
## Levene's Test for Homogeneity of Variance (center = median)
## Df F value Pr(>F)
## group 1 2.6455 0.1048
## 328
Levene’s test revealed that our data has non-sig. variances between the two comparison groups, in treatment and not in treatment, on their levels of 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 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 heterogeneity 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$phq~d$treatment) # t_output will now show in your Global Environment
t_output
##
## Welch Two Sample t-test
##
## data: d$phq by d$treatment
## t = 5.1812, df = 70.052, p-value = 2.028e-06
## 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.3852052 0.8673559
## sample estimates:
## mean in group in treatment mean in group not in treatment
## 2.879630 2.253349
# 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$phq~d$treatment) # d_output will now show in your Global Environment
d_output
##
## Cohen's d
##
## d estimate: 0.7316135 (medium)
## 95 percent confidence interval:
## lower upper
## 0.4193859 1.0438411
## Remember to always take the ABSOLUTE VALUE of the effect size value (i.e., it will never be negative)
To test our hypothesis that people in treatment within our sample would report significantly higher levels of depression than people not in treatment, we used an independent-samples t-test. This required us to drop our no psychological disorders, seeking treatment, treatment disrupted by COVID-19, and other participants participants from our sample, as we are limited to a two-group comparison when using this test. We tested the homogeneity of variance with Levine’s test and found signs of heterogeneity (p < .001). This suggests that there is an increased chance of type 1 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 = 2.88, SD = 0.76) reported significantly … levels of conscientiousness than men (M = 2.25, SD = 0.87); t(70.052) = 5.1812, p < .001 (see Figure 1). The effect size was calculated using Cohen’s d, with a value of 0.7316135 (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.