# install any packages you have not previously used, then comment them back out.
#install.packages("car")
#install.packages("effsize")
library(psych)
library(car)
## Loading required package: carData
##
## Attaching package: 'car'
## The following object is masked from 'package:psych':
##
## logit
library(effsize)
##
## 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 with higher extroversion scores are more likely to report the negative effects of COVID-19, compared to those with lower extroversion scores.
# you only need to check the variables you're using in the current analysis
## Checking the Categorical variable (IV)
str(d)
## 'data.frame': 1337 obs. of 7 variables:
## $ X : int 20 30 31 48 49 57 58 69 79 81 ...
## $ age : chr "1 under 18" "1 under 18" "4 between 36 and 45" "4 between 36 and 45" ...
## $ mhealth : chr "anxiety disorder" "none or NA" "none or NA" "depression" ...
## $ 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 5.33 5 6 4.33 6.67 ...
## $ big5_ext : num 1.67 6 5 4.33 5.67 ...
# 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)
table(d$mhealth, useNA = "always")
##
## anxiety disorder bipolar
## 149 8
## depression eating disorders
## 38 31
## none or NA obsessive compulsive disorder
## 1028 27
## other ptsd
## 33 23
## <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$big5_ext)
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 1337 4.37 1.45 4.33 4.41 1.48 1 7 6 -0.24 -0.78 0.04
# also use a histogram to visualize your continuous variable
hist(d$big5_ext)
# 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$big5_ext, 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 149 3.84 1.45 3.67 3.83 1.98 1 7 6 0.11 -0.92 0.12
## ------------------------------------------------------------
## group: bipolar
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 8 4 1.7 4.17 4 2.22 2 6.67 4.67 0.14 -1.6 0.6
## ------------------------------------------------------------
## group: depression
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 38 4.25 1.32 4.67 4.27 1.48 1.67 6.33 4.67 -0.23 -1.22 0.21
## ------------------------------------------------------------
## group: eating disorders
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 31 3.51 1.48 3 3.37 1.48 1 7 6 0.57 -0.22 0.27
## ------------------------------------------------------------
## group: none or NA
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 1028 4.5 1.42 4.67 4.56 1.48 1 7 6 -0.32 -0.68 0.04
## ------------------------------------------------------------
## group: obsessive compulsive disorder
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 27 4.23 1.61 4.33 4.26 0.99 1 7 6 -0.27 -0.85 0.31
## ------------------------------------------------------------
## group: other
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 33 4.12 1.52 4 4.15 1.48 1 7 6 -0.09 -0.63 0.26
## ------------------------------------------------------------
## group: ptsd
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 23 4.07 1.47 4.33 4.07 1.98 1.67 6.67 5 -0.13 -1.28 0.31
# last, use a boxplot to examine your continuous and categorical variables together
boxplot(d$big5_ext~d$mhealth)
# If the IV has more than 2 levels, you must drop the additional levels so that you meet the first assumption of a t-test.
d <- subset(d, mhealth != "<NA>") #this variable would not go away, I am unsure why
table(d$mhealth, useNA = "always") #verify that now there are no participants in the removed level
##
## anxiety disorder bipolar
## 149 8
## depression eating disorders
## 38 31
## none or NA obsessive compulsive disorder
## 1028 27
## other ptsd
## 33 23
## <NA>
## 0
d$mhealth <- droplevels(d$mhealth) # use droplevels() to drop the empty factor
table(d$mhealth, useNA = "always") #verify that now the entire factor level is removed
##
## anxiety disorder bipolar
## 149 8
## depression eating disorders
## 38 31
## none or NA obsessive compulsive disorder
## 1028 27
## other ptsd
## 33 23
## <NA>
## 0
d <- subset(d, mhealth %in% c("anxiety disorder", "depression")) #I had to look up how to explicitly only use these two and find a code that would disregard <NA>?
levels(d$mhealth)
## [1] "anxiety disorder" "bipolar"
## [3] "depression" "eating disorders"
## [5] "none or NA" "obsessive compulsive disorder"
## [7] "other" "ptsd"
d$mhealth <- factor(d$mhealth, levels = c("anxiety disorder", "depression"))
table(d$mhealth, useNA = "always")
##
## anxiety disorder depression <NA>
## 149 38 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(d$big5_ext~d$mhealth, data = d)
## Levene's Test for Homogeneity of Variance (center = median)
## Df F value Pr(>F)
## group 1 0.5346 0.4656
## 185
As you can see, the data does not have significantly different variances between the two comparison groups.
When running a t-test, we can account for heterogeneity in our variance by using Welch’s t-test, which does not have the same assumption about variance as Student’s t-test (the general default type of t-test). 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 just 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 other types of mental health besides anxiety and depression disorders from my sample. I will make a note to discuss this issue in my Method write-up and in my Discussion as a limitation of my study.
My data also has some 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.
# 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$big5_ext~d$mhealth)
t_output
##
## Welch Two Sample t-test
##
## data: d$big5_ext by d$mhealth
## t = -1.6974, df = 61.975, p-value = 0.09464
## alternative hypothesis: true difference in means between group anxiety disorder and group depression is not equal to 0
## 95 percent confidence interval:
## -0.90474412 0.07382454
## sample estimates:
## mean in group anxiety disorder mean in group depression
## 3.838926 4.254386
# once again, we use the same formula, y~x, to calculate cohen's d
d_output <- cohen.d(d$big5_ext~d$mhealth)
d_output
##
## Cohen's d
##
## d estimate: -0.2911598 (small)
## 95 percent confidence interval:
## lower upper
## -0.65092529 0.06860575
To test our hypothesis that people with higher extraversion scores are more likely to report the negative effects of COVID-19, compared to those with lower extraversion scores, we used an independent samples t-test. This required us to drop our non-binary gender 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 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 a t-test.
As predicted, we found that individuals with depression (M = 4.25, SD=1.32) reported higher levels of extraversion than those with anxiety disorders (M = 3.84, SD=1.45); t(61.98)=−1.697t(61.98) = -1.697t(61.98)=−1.697, p=0.0946p = 0.0946p=0.0946. However, this difference was not statistically significant. The effect size, calculated using Cohen’s d, was -0.29, indicating a small effect (Cohen, 1988)
References
Cohen J. (1988). Statistical Power Analysis for the Behavioral Sciences. New York, NY: Routledge Academic.