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 social media use than men, as measured by the Social Media Use Scale (EAMMI Data, page 2).
# 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': 3133 obs. of 6 variables:
## $ gender : chr "f" "m" "m" "f" ...
## $ sibling : chr "at least one sibling" "at least one sibling" "at least one sibling" "at least one sibling" ...
## $ moa_maturity: num 3.67 3.33 3.67 3 3.67 ...
## $ support : num 6 6.75 5.17 5.58 6 ...
## $ socmeduse : int 47 23 34 35 37 13 37 43 37 29 ...
## $ 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>
## 2298 781 54 0
# you can use the describe() command on an entire dataframe (d) or just on a single variable (d$pss)
describe(d$socmeduse)
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 3133 34.48 8.56 35 34.75 7.41 11 55 44 -0.31 0.26 0.15
# also use a histogram to examine your continuous variable
hist(d$socmeduse)
# 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$socmeduse, group=d$gender)
##
## Descriptive statistics by group
## group: f
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 2298 35.15 8.25 35 35.35 7.41 11 55 44 -0.28 0.33 0.17
## ------------------------------------------------------------
## group: m
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 781 32.48 9.14 33 32.77 8.9 11 55 44 -0.24 -0.05 0.33
## ------------------------------------------------------------
## group: nb
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 54 34.57 8.69 35 35.2 7.41 11 54 43 -0.64 0.82 1.18
# last, use a boxplot to examine your continuous and categorical variables together
boxplot(d$socmeduse~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>
## 2298 781 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(socmeduse~gender, data = d)
## Levene's Test for Homogeneity of Variance (center = median)
## Df F value Pr(>F)
## group 1 11.536 0.0006911 ***
## 3077
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
As you can see, our data is very close to 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 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.
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.
# once again, subetting to drop the nb group
d <- subset(d, gender != "nb")
d$gender <- droplevels(d$gender) # using droplevels() to drop the empty factor
# 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$socmeduse~d$gender)
t_output
##
## Welch Two Sample t-test
##
## data: d$socmeduse by d$gender
## t = 7.2362, df = 1238.9, p-value = 8.066e-13
## alternative hypothesis: true difference in means between group f and group m is not equal to 0
## 95 percent confidence interval:
## 1.949864 3.400433
## sample estimates:
## mean in group f mean in group m
## 35.15274 32.47759
# once again, we use our formula to calculate cohen's d
d_output <- cohen.d(d$socmeduse~d$gender)
d_output <- effsize::cohen.d(d$socmeduse~d$gender)
d_output
##
## Cohen's d
##
## d estimate: 0.3152919 (small)
## 95 percent confidence interval:
## lower upper
## 0.2336981 0.3968857
To test our hypothesis that women in our sample would report significantly more social media use 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 little signs of heterogeneity (p < .001). Still, 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 = 35.15, SD = .95) reported significantly higher social media use than men (M = 32.48, SD = .89); t(1238.9) = 7.24, p < .001 (see Figure 1). The effect size was calculated using Cohen’s d, with a value of .32 (small effect; Cohen, 1988).
References
Cohen J. (1988). Statistical Power Analysis for the Behavioral Sciences. New York, NY: Routledge Academic.