library(psych) # for the describe() command
library(car) # for the leveneTest() command
## Warning: package 'car' was built under R version 4.3.3
## Loading required package: carData
## Warning: package 'carData' was built under R version 4.3.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.3.3
##
## Attaching package: 'effsize'
## The following object is masked from 'package:psych':
##
## cohen.d
# for the homework: import the dataset you cleaned previously
# this will be the dataset you'll use throughout the rest of the semester
d <- read.csv(file="Data/mydata.csv", header=T)
We predict that women will report to have higher levels of anxiety than men, as measured by the general anxiety disorder (gad-7).
(Note: The above hypothesis is for the lab. You will need to come up with your own hypothesis, specific to your chosen variables, for the homework.)
# you only need to check the variables you're using in the current analysis
# although you checked them previously, it's always a good idea to look them over again and be sure that everything is correct
str(d)
## 'data.frame': 147 obs. of 7 variables:
## $ X : int 7851 7856 7861 7862 7874 7875 7876 7880 7881 7884 ...
## $ gender : chr "male" "male" "female" "female" ...
## $ ethnicity : chr "White - British, Irish, other" "White - British, Irish, other" "White - British, Irish, other" "White - British, Irish, other" ...
## $ gad : num 3.86 3.43 4 1.86 3.86 ...
## $ isolation : num 3.25 3.25 2.25 2 2.75 3.5 3.5 3 3.25 3 ...
## $ pss : num 3.75 3.25 3.75 3.5 3.75 5 4 4.25 2.75 4.5 ...
## $ school_covid_support: num 1.4 1.2 1 1.6 1.4 ...
d$gender <- as.factor(d$gender)
table(d$gender, useNA = "always")
##
## female I use another term male Prefer not to say
## 116 4 25 2
## <NA>
## 0
# you can use the describe() command on an entire datafrom (d) or just on a single variable (d$gad)
describe(d$gad)
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 147 2.41 0.94 2.29 2.39 1.27 1 4 3 0.19 -1.29 0.08
# also use a histogram to examine your continuous variable
hist(d$gad)
# can 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 116 2.42 0.92 2.36 2.39 1.16 1 4 3 0.21 -1.28 0.09
## ------------------------------------------------------------
## group: I use another term
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 4 3.25 0.47 3.29 3.25 0.53 2.71 3.71 1 -0.09 -2.28 0.24
## ------------------------------------------------------------
## group: male
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 25 2.17 1.06 2 2.1 1.27 1 4 3 0.57 -1.13 0.21
## ------------------------------------------------------------
## group: Prefer not to say
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 2 3.29 0.2 3.29 3.29 0.21 3.14 3.43 0.29 0 -2.75 0.14
# last, use a boxplot to examine your continuous and categorical variables together
boxplot(d$gad~d$gender)
Some of these we can check in R, while others are down to our research design. These assumptions are confirmed by our research design, so we don’t have to do anything now:
This assumption is not met:
This assumption was confirmed in the section above:
So we only have one assumption to test:
# subetting to drop the nb group so that our IV only has two levels
#if you need to drop multiple level
d<- subset(d, gender == c("female","male"))
## Warning in `==.default`(gender, c("female", "male")): longer object length is
## not a multiple of shorter object length
## Warning in is.na(e1) | is.na(e2): longer object length is not a multiple of
## shorter object length
d$gender <- droplevels(d$gender) # using droplevels() to drop the empty factor
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
# 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 1.3727 0.2455
## 67
As you can see, our data is not very close to being significant. 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 assumptions as Student’s t-test (the default type of t-test) about variance. 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 a solution for any potential problems.
My independent variable has more than two levels. To proceed with this analysis, I will drop the non-binary participants 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 potential issues regarding homogeneity of variance. Although Levene’s test was not significant, it was close to the significance threshold. To accommodate any potential heterogeneity of variance, I will use Welch’s t-test instead of Student’s t-test.
# very simple! we specify the dataframe alongside the variables instead of having a separate argument for the dataframe like we did for leveneTest()
t_output <- t.test(d$gad~d$gender)
t_output
##
## Welch Two Sample t-test
##
## data: d$gad by d$gender
## t = 0.4686, df = 16.884, p-value = 0.6454
## alternative hypothesis: true difference in means between group female and group male is not equal to 0
## 95 percent confidence interval:
## -0.4721340 0.7415575
## sample estimates:
## mean in group female mean in group male
## 2.503759 2.369048
# once again, we use our formula to calculate cohen's d
d_output <- cohen.d(d$gad~d$gender)
d_output
##
## Cohen's d
##
## d estimate: 0.1413205 (negligible)
## 95 percent confidence interval:
## lower upper
## -0.4930897 0.7757307
To test our hypothesis that women in our sample would report to experience having more anxiety than men, we used an two-sample or independent t-test. This required us to drop our non-binary and other 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 some signs of heterogeneity (p = .24). To correct for this possible issue, we use 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 women (M = 2.50, SD = .96) reported having higher anxiety than men (M = 2.37, SD = .89); t(16.884) = 0.4686, p > .001 (see Figure 1). The effect size was calculated using Cohen’s d, with a value of .14 (negligible effect; Cohen, 1988).
References
Cohen J. (1988). Statistical Power Analysis for the Behavioral Sciences. New York, NY: Routledge Academic.