# 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
## Warning: package 'car' was built under R version 4.5.3
## Loading required package: carData
## Warning: package 'carData' was built under R version 4.5.3
##
## Attaching package: 'car'
## The following object is masked from 'package:psych':
##
## logit
library(effsize) # for the cohen.d() command
## Warning: package 'effsize' was built under R version 4.5.3
##
## 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 there will be a significant difference in Mental Health Flexibility Questionnaire State scores by people’s level of Mental Health Treatment, between in treatment and not in treatment.
[Remember to revise the above hypothesis in you HW assignment. Then delete line 45 and this reminder.]
# you **only** need to check the variables you're using in the current analysis
## Checking the Categorical variable (IV)
str(d)
## 'data.frame': 599 obs. of 7 variables:
## $ X : int 2814 3295 717 6056 4753 5365 2044 1246 1250 1761 ...
## $ urban_rural: chr "town" "town" "village" "city" ...
## $ treatment : chr "not in treatment" "no psychological disorders" "not in treatment" "not in treatment" ...
## $ mfq_state : num 4.38 4.88 4.88 3.75 5.88 ...
## $ phq : num 1.44 1.33 1.44 1 1.33 ...
## $ support : num 3 4 3.67 3.67 5 ...
## $ rse : num 3.1 3 3 3 4 3.8 2.5 3.8 3.7 3.2 ...
# 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': 599 obs. of 7 variables:
## $ X : int 2814 3295 717 6056 4753 5365 2044 1246 1250 1761 ...
## $ urban_rural: chr "town" "town" "village" "city" ...
## $ treatment : Factor w/ 6 levels "in treatment",..: 3 2 3 3 3 3 3 3 2 3 ...
## $ mfq_state : num 4.38 4.88 4.88 3.75 5.88 ...
## $ phq : num 1.44 1.33 1.44 1 1.33 ...
## $ support : num 3 4 3.67 3.67 5 ...
## $ rse : num 3.1 3 3 3 4 3.8 2.5 3.8 3.7 3.2 ...
table(d$treatment, useNA = "always")
##
## in treatment no psychological disorders
## 45 230
## not in treatment other
## 264 11
## seeking treatment treatment disrupted by COVID-19
## 20 29
## <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$mfq_state)
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 599 3.95 0.98 4 3.98 0.93 1 6 5 -0.31 -0.1 0.04
# also use a histogram to visualize your continuous variable
hist(d$mfq_state)
# 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$mfq_state, 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 45 3.41 1.02 3.38 3.44 1.11 1.25 5.5 4.25 -0.19 -0.71 0.15
## ------------------------------------------------------------
## group: no psychological disorders
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 230 4.07 0.92 4 4.09 0.93 1 6 5 -0.23 0.3 0.06
## ------------------------------------------------------------
## group: not in treatment
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 264 4.06 0.98 4.12 4.1 0.93 1.12 6 4.88 -0.44 -0.19 0.06
## ------------------------------------------------------------
## group: other
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 11 3.35 0.64 3.62 3.39 0.56 2.25 4.12 1.88 -0.52 -1.3 0.19
## ------------------------------------------------------------
## group: seeking treatment
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 20 3.33 0.84 3.56 3.34 0.56 1.75 4.88 3.12 -0.22 -0.96 0.19
## ------------------------------------------------------------
## group: treatment disrupted by COVID-19
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 29 3.49 1.01 3.5 3.5 0.93 1.25 5.62 4.38 -0.1 -0.42 0.19
# lastly, use a boxplot to examine your chosen continuous and categorical variables together
boxplot(d$mfq_state~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 != "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 no psychological disorders
## 45 230
## not in treatment other
## 264 0
## seeking treatment treatment disrupted by COVID-19
## 20 29
## <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
## 45 230
## not in treatment seeking treatment
## 264 20
## treatment disrupted by COVID-19 <NA>
## 29 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.
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
## 45 0
## not in treatment seeking treatment
## 264 20
## treatment disrupted by COVID-19 <NA>
## 29 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
## 45 264
## seeking treatment treatment disrupted by COVID-19
## 20 29
## <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
## 45 264
## seeking treatment treatment disrupted by COVID-19
## 0 29
## <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
## 45 264
## treatment disrupted by COVID-19 <NA>
## 29 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
## 45 264
## 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>
## 45 264 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(mfq_state~treatment, data =d)
## Levene's Test for Homogeneity of Variance (center = median)
## Df F value Pr(>F)
## group 1 0.4681 0.4944
## 307
Levene’s test revealed that our data does not have significantly different variances between the two comparison groups, in treatment and not in treatment, on their levels of Mental Flexibility State.
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 in levels other, no psychological disorders, seeking treatment, and treatment disrupted by COVID-19 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 failed to reject the null hypothesis. To accommodate for no evidence of heterogeneity 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$mfq_state~d$treatment) # t_output will now show in your Global Environment
t_output
##
## Welch Two Sample t-test
##
## data: d$mfq_state by d$treatment
## t = -3.9578, df = 58.689, p-value = 0.0002068
## 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.9742490 -0.3198798
## sample estimates:
## mean in group in treatment mean in group not in treatment
## 3.408333 4.055398
# 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$mfq_state~d$treatment) # d_output will now show in your Global Environment
d_output
##
## Cohen's d
##
## d estimate: -0.6567637 (medium)
## 95 percent confidence interval:
## lower upper
## -0.9783406 -0.3351869
## 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 in different Mental Health treatment levels from our sample would report significant differences in Mental Flexibility State scores, we used an independent-samples t-test. This required us to drop 4 groups of 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 failed to reject the null hypothesis (p > .001). This suggests that a Type I error is not likely, but we went on to use Welch’s t-test, which does not assume homogeneity of variance. Our data met all other assumptions of an independent samples t-test.
We were not able to support our hypothesis, we found that the in treatment group (M = 3.41) reported insignificant differences in Mental Flexibility State scores compared to the not in treatment group (M = 4.06); t(58.689) = -3.958, p < .001 (see Figure 1). The effect size was calculated using Cohen’s d, with a value of -0.66 (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.