# 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 GAD-7 scores, which measure rates of anxiety. This will be different by people’s type of gender identity, specifically between those who identify as male and female.
# you **only** need to check the variables you're using in the current analysis
## Checking the Categorical variable (IV)
str(d)
## 'data.frame': 1258 obs. of 7 variables:
## $ X : int 1 321 401 469 520 1390 1422 1849 2183 2247 ...
## $ gender : chr "female" "male" "female" "female" ...
## $ mhealth: chr "none or NA" "none or NA" "obsessive compulsive disorder" "depression" ...
## $ rse : num 2.3 3.8 3.1 3 2.6 3 1.3 2.1 3 3.2 ...
## $ pss : num 3.25 2.25 2.25 2.25 2.75 2.75 4.75 3.25 3.5 2.25 ...
## $ phq : num 1.33 1.89 2.44 1.22 1.56 ...
## $ gad : num 1.86 1 2.14 1.71 1.14 ...
# 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$gender <- as.factor(d$gender)
str(d)
## 'data.frame': 1258 obs. of 7 variables:
## $ X : int 1 321 401 469 520 1390 1422 1849 2183 2247 ...
## $ gender : Factor w/ 4 levels "female","I use another term",..: 1 3 1 1 1 3 1 1 1 1 ...
## $ mhealth: chr "none or NA" "none or NA" "obsessive compulsive disorder" "depression" ...
## $ rse : num 2.3 3.8 3.1 3 2.6 3 1.3 2.1 3 3.2 ...
## $ pss : num 3.25 2.25 2.25 2.25 2.75 2.75 4.75 3.25 3.5 2.25 ...
## $ phq : num 1.33 1.89 2.44 1.22 1.56 ...
## $ gad : num 1.86 1 2.14 1.71 1.14 ...
table(d$gender, useNA = "always")
##
## female I use another term male Prefer not to say
## 1010 33 196 19
## <NA>
## 0
d$mhealth <- as.factor(d$mhealth)
str(d)
## 'data.frame': 1258 obs. of 7 variables:
## $ X : int 1 321 401 469 520 1390 1422 1849 2183 2247 ...
## $ gender : Factor w/ 4 levels "female","I use another term",..: 1 3 1 1 1 3 1 1 1 1 ...
## $ mhealth: Factor w/ 8 levels "anxiety disorder",..: 5 5 6 3 5 5 5 1 5 5 ...
## $ rse : num 2.3 3.8 3.1 3 2.6 3 1.3 2.1 3 3.2 ...
## $ pss : num 3.25 2.25 2.25 2.25 2.75 2.75 4.75 3.25 3.5 2.25 ...
## $ phq : num 1.33 1.89 2.44 1.22 1.56 ...
## $ gad : num 1.86 1 2.14 1.71 1.14 ...
table(d$mhealth, useNA = "always")
##
## anxiety disorder bipolar
## 130 6
## depression eating disorders
## 32 30
## none or NA obsessive compulsive disorder
## 970 26
## other ptsd
## 37 27
## <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$gad)
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 1258 2.04 0.91 1.71 1.95 0.85 1 4 3 0.68 -0.72 0.03
# also use a histogram to visualize your continuous variable
hist(d$gad)
# 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$gad, group=d$gender)
##
## Descriptive statistics by group
## group: female
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 1010 2.05 0.89 1.71 1.97 0.85 1 4 3 0.67 -0.7 0.03
## ------------------------------------------------------------
## group: I use another term
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 33 3.15 0.82 3.43 3.22 0.85 1.29 4 2.71 -0.55 -1.06 0.14
## ------------------------------------------------------------
## group: male
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 196 1.77 0.87 1.43 1.63 0.64 1 4 3 1.09 0.06 0.06
## ------------------------------------------------------------
## group: Prefer not to say
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 19 2.3 0.92 2.29 2.28 1.27 1 4 3 0.13 -1.35 0.21
# lastly, use a boxplot to examine your chosen continuous and categorical variables together
boxplot(d$gad~d$gender)
# 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, gender != "Prefer not to say") # use subset() to remove all participants from the additional level
table(d$gender, useNA = "always") # verify that now there are ZERO participants in the additional level
##
## female I use another term male Prefer not to say
## 1010 33 196 0
## <NA>
## 0
d$gender <- droplevels(d$gender) # use droplevels() to drop the empty factor
table(d$gender, useNA = "always") # verify that now the entire factor level is removed
##
## female I use another term male <NA>
## 1010 33 196 0
d <- subset(d, gender != "I use another term") # use subset() to remove all participants from the additional level
table(d$gender, useNA = "always") # verify that now there are ZERO participants in the additional level
##
## female I use another term male <NA>
## 1010 0 196 0
d$gender <- droplevels(d$gender) # use droplevels() to drop the empty factor
table(d$gender, useNA = "always") # verify that now the entire factor level is removed
##
## female male <NA>
## 1010 196 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(gad~gender, data = d)
## Levene's Test for Homogeneity of Variance (center = median)
## Df F value Pr(>F)
## group 1 2.0068 0.1568
## 1204
Levene’s test revealed that our data has marginally different variances between the two comparison groups, males and females, on their levels of self-reported 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 “I use another term” participants as well as the “Prefer not to say” 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 does not have an issue regarding homogeneity of variance, as Levene’s test was marginally significant. 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$gad~d$gender) # t_output will now show in your Global Environment
t_output
##
## Welch Two Sample t-test
##
## data: d$gad by d$gender
## t = 4.1688, df = 279.56, p-value = 4.088e-05
## alternative hypothesis: true difference in means between group female and group male is not equal to 0
## 95 percent confidence interval:
## 0.1501172 0.4187163
## sample estimates:
## mean in group female mean in group male
## 2.051909 1.767493
# 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$gad~d$gender) # d_output will now show in your Global Environment
d_output
##
## Cohen's d
##
## d estimate: 0.3209781 (small)
## 95 percent confidence interval:
## lower upper
## 0.1673088 0.4746473
## Remember to always take the ABSOLAUTE VALUE of the effect size value (i.e., it will never be negative)
To test our hypothesis that females in our sample would report different levels of anxiety than males, we used an independent samples t-test. This required us to drop our non-binary/I use another term gender participants from our sample, as well as the Prefer Not to say participants, as we are limited to a two-group comparison when using this test. We tested the homogeneity of variance with Levene’s test and did not find signs of heterogeneity (p > .1, < 1). This does not suggest that there is an increased chance of Type I error. 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.
Differently than predicted, we found that females (M = 2.05, SD = 0.89) reported marginally different levels of anxiety than males (M = 1.77, SD = 0.87); t(279.56) = -4.17, p > 1 (see Figure 1). The effect size was calculated using Cohen’s d, with a value of 0.32 (medium effect; Cohen, 1988).
References
Cohen J. (1988). Statistical Power Analysis for the Behavioral Sciences. New York, NY: Routledge Academic.