# 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.4.3
## Loading required package: carData
##
## 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.4.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 people who are not in treatment will report significantly lower levels of self-esteem than people who are 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:256] "seeking treatment" "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$treatment)
## Factor w/ 6 levels "in treatment",..: 5 2 2 3 3 2 2 2 2 3 ...
table(d$treatment, useNA = "always")
##
## in treatment no psychological disorders
## 31 83
## not in treatment other
## 105 5
## seeking treatment treatment disrupted by COVID-19
## 11 21
## <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$rse)
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 256 2.15 0.65 2 2.11 0.59 1 4 3 0.51 -0.27 0.04
# also use a histogram to visualize your continuous variable
hist(d$rse)
# 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$rse, 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 31 1.79 0.53 1.7 1.76 0.59 1 3.1 2.1 0.53 -0.35 0.09
## ------------------------------------------------------------
## group: no psychological disorders
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 83 2.41 0.66 2.3 2.39 0.74 1.1 4 2.9 0.31 -0.62 0.07
## ------------------------------------------------------------
## group: not in treatment
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 105 2.14 0.64 2 2.1 0.59 1 3.8 2.8 0.51 -0.37 0.06
## ------------------------------------------------------------
## group: other
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 5 2 0.19 2.1 2 0.15 1.8 2.2 0.4 -0.18 -2.18 0.08
## ------------------------------------------------------------
## group: seeking treatment
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 11 1.85 0.47 1.9 1.86 0.3 1 2.7 1.7 0.02 -0.84 0.14
## ------------------------------------------------------------
## group: treatment disrupted by COVID-19
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 21 1.9 0.54 1.9 1.87 0.59 1.1 2.9 1.8 0.39 -1.04 0.12
# lastly, use a boxplot to examine your chosen continuous and categorical variables together
boxplot(d$rse~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") # remove "no psychological disorders"
table(d$treatment, useNA = "always") # verify zero participants in that level
##
## in treatment no psychological disorders
## 31 0
## not in treatment other
## 105 5
## seeking treatment treatment disrupted by COVID-19
## 11 21
## <NA>
## 0
d$treatment <- droplevels(d$treatment) # drop the empty factor level
table(d$treatment, useNA = "always") # verify the level is fully removed
##
## in treatment not in treatment
## 31 105
## other seeking treatment
## 5 11
## treatment disrupted by COVID-19 <NA>
## 21 0
## Repeat for remaining levels we are not comparing
d <- subset(d, treatment != "other") # remove "other"
table(d$treatment, useNA = "always")
##
## in treatment not in treatment
## 31 105
## other seeking treatment
## 0 11
## treatment disrupted by COVID-19 <NA>
## 21 0
d$treatment <- droplevels(d$treatment)
table(d$treatment, useNA = "always")
##
## in treatment not in treatment
## 31 105
## seeking treatment treatment disrupted by COVID-19
## 11 21
## <NA>
## 0
d <- subset(d, treatment != "seeking treatment") # remove "seeking treatment"
table(d$treatment, useNA = "always")
##
## in treatment not in treatment
## 31 105
## seeking treatment treatment disrupted by COVID-19
## 0 21
## <NA>
## 0
d$treatment <- droplevels(d$treatment)
table(d$treatment, useNA = "always")
##
## in treatment not in treatment
## 31 105
## treatment disrupted by COVID-19 <NA>
## 21 0
d <- subset(d, treatment != "treatment disrupted by COVID-19") # remove "treatment disrupted by COVID-19"
table(d$treatment, useNA = "always")
##
## in treatment not in treatment
## 31 105
## treatment disrupted by COVID-19 <NA>
## 0 0
d$treatment <- droplevels(d$treatment)
table(d$treatment, useNA = "always") # should now only show "in treatment" and "not in treatment"
##
## in treatment not in treatment <NA>
## 31 105 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(rse~treatment, data=d)
## Levene's Test for Homogeneity of Variance (center = median)
## Df F value Pr(>F)
## group 1 1.1718 0.281
## 134
Levene’s test revealed that our data has [equal/significantly different] variances between the two comparison groups, those in treatment and those not in treatment, on their levels of self-esteem.
My independent variable has more than two levels. To proceed with this analysis, I will drop the participants in the “no psychological disorders,” “other,” “seeking treatment,” and “treatment disrupted by COVID-19” groups from my sample, as we are limited to a two-group comparison when using this test. 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 [does/does not] have an issue regarding homogeneity of variance, as Levene’s test was [significant/non-significant]. [If significant: To accommodate for this heterogeneity of variance, I will use Welch’s t-test instead of Student’s t-test in my analysis. / If non-significant: As R defaults to Welch’s t-test regardless, no additional corrections are needed.]
# 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$rse~d$treatment) # t_output will now show in your Global Environment
t_output
##
## Welch Two Sample t-test
##
## data: d$rse by d$treatment
## t = -3.0484, df = 58.833, p-value = 0.003444
## 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.5744925 -0.1191480
## sample estimates:
## mean in group in treatment mean in group not in treatment
## 1.790323 2.137143
# 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$rse~d$treatment) # d_output will now show in your Global Environment
d_output
##
## Cohen's d
##
## d estimate: -0.5595905 (medium)
## 95 percent confidence interval:
## lower upper
## -0.9694022 -0.1497789
## 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 not in treatment would report significantly lower levels of self-esteem than people in treatment, we used an independent samples t-test. This required us to drop participants in the “no psychological disorders,” “other,” “seeking treatment,” and “treatment disrupted by COVID-19” groups 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 no evidence of heterogeneity (p = .281). Our data met all other assumptions of an independent samples t-test. Contrary to our hypothesis, we found that people in treatment (M = 1.79, SD = 0.53) reported significantly lower levels of self-esteem than people not in treatment (M = 2.14, SD = 0.64) t(58.83) = -3.05, p = .003 (see Figure 1). The effect size was calculated using Cohen’s d, with a value of 0.56 (medium effect; Cohen, 1988).
References
Cohen J. (1988). Statistical Power Analysis for the Behavioral Sciences. New York, NY: Routledge Academic.