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
# 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 significantly more support than men, as measured by the multidimensional scale of perceived social support.
# 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': 2107 obs. of 6 variables:
## $ gender : chr "f" "m" "m" "f" ...
## $ age : chr "1 between 18 and 25" "1 between 18 and 25" "1 between 18 and 25" "1 between 18 and 25" ...
## $ moa_independence: num 3.67 3.67 3.5 3 3.83 ...
## $ swb : num 4.33 4.17 1.83 5.17 3.67 ...
## $ support : num 6 6.75 5.17 5.58 6 ...
## $ stress : num 3.3 3.3 4 3.2 3.1 3.5 3.3 2.4 2.9 2.7 ...
d$gender <- as.factor(d$gender)
table(d$gender, useNA = "always")
##
## f m nb <NA>
## 1546 530 31 0
# you can use the describe() command on an entire data frame (d) or just on a single variable (d$swb)
describe(d$support)
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 2107 5.53 1.13 5.75 5.66 0.99 0 7 7 -1.1 1.35 0.02
# also use a histogram to examine your continuous variable
hist(d$support)
# 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$support, group=d$gender)
##
## Descriptive statistics by group
## group: f
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 1546 5.57 1.12 5.83 5.7 0.99 0 7 7 -1.15 1.56 0.03
## ------------------------------------------------------------
## group: m
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 530 5.44 1.16 5.67 5.56 1.11 1 7 6 -1.01 1.01 0.05
## ------------------------------------------------------------
## group: nb
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 31 5.15 1.16 5.42 5.19 1.36 2.5 7 4.5 -0.31 -0.94 0.21
# last, use a boxplot to examine your continuous and categorical variables together
boxplot(d$support~d$gender)
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!
d <- subset(d, gender != "nb")
table(d$gender, useNA = "always")
##
## f m nb <NA>
## 1546 530 0 0
d$gender <- droplevels(d$gender) # using droplevels() to drop the empty factor
# 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(support~gender, data = d)
## Levene's Test for Homogeneity of Variance (center = median)
## Df F value Pr(>F)
## group 1 1.2331 0.2669
## 2074
As you can see, our data is not significant. Although our data had no issues with homogeneity, we will still use the Welch’s t-test, as 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 changing 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.
# 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$support~d$gender)
t_output
##
## Welch Two Sample t-test
##
## data: d$support by d$gender
## t = 2.2066, df = 891.1, p-value = 0.0276
## alternative hypothesis: true difference in means between group f and group m is not equal to 0
## 95 percent confidence interval:
## 0.01410822 0.24110548
## sample estimates:
## mean in group f mean in group m
## 5.571475 5.443868
# once again, we use our formula to calculate cohen's d
d_output <- cohen.d(d$support~d$gender)
d_output
##
## Cohen's d
##
## d estimate: 0.1129135 (negligible)
## 95 percent confidence interval:
## lower upper
## 0.01414108 0.21168588
To test our hypothesis that women in our sample would report significantly more perceived support 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 = .267). To correct for this possible 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 women (M = 5.57, SD = 1.12) reported significantly more support than men (M = 5.44, SD = 1.16); t(891.1) = 2.21, “p” < .001 (see Figure 1). The effect size was calculated using Cohen’s “d”, with a value of .11 (negligible effect; Cohen, 1988).
References
Cohen J. (1988). Statistical Power Analysis for the Behavioral Sciences. New York, NY: Routledge Academic.