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)
There will be a significant difference in social media use by race/ethnicity.
# 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': 3082 obs. of 6 variables:
## $ race_rc : chr "white" "white" "white" "other" ...
## $ usdream : chr "american dream is important and achievable for me" "american dream is important and achievable for me" "american dream is not important and maybe not achievable for me" "american dream is not important and maybe not achievable for me" ...
## $ moa_independence: num 3.67 3.67 3.5 3 3.83 ...
## $ idea : num 3.75 3.88 3.75 3.75 3.5 ...
## $ support : num 6 6.75 5.17 5.58 6 ...
## $ socmeduse : int 47 23 34 35 37 13 37 43 37 29 ...
d$race_rc <- as.factor(d$race_rc)
table(d$race_rc, useNA = "always")
##
## asian black hispanic multiracial nativeamer other
## 201 234 277 285 10 91
## white <NA>
## 1984 0
# you can use the describe() command on an entire dataframe (d) or just on a single variable (d$socmeduse)
describe(d$socmeduse)
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 3082 34.45 8.6 35 34.72 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$race_rc)
##
## Descriptive statistics by group
## group: asian
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 201 34.64 7.99 36 35.19 7.41 11 52 41 -0.68 0.45 0.56
## ------------------------------------------------------------
## group: black
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 234 33.2 9.56 34 33.28 8.9 11 55 44 -0.07 -0.08 0.63
## ------------------------------------------------------------
## group: hispanic
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 277 33.2 9.02 33 33.4 8.9 11 55 44 -0.19 0.05 0.54
## ------------------------------------------------------------
## group: multiracial
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 285 34.01 8.91 34 34 7.41 11 55 44 -0.02 0.22 0.53
## ------------------------------------------------------------
## group: nativeamer
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 10 33.1 6.74 32.5 32.5 5.19 23 48 25 0.69 -0.04 2.13
## ------------------------------------------------------------
## group: other
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 91 33.46 8.43 34 33.3 7.41 15 55 40 0.2 -0.01 0.88
## ------------------------------------------------------------
## group: white
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 1984 34.86 8.42 35 35.2 7.41 11 55 44 -0.39 0.37 0.19
# last, use a boxplot to examine your continuous and categorical variables together
boxplot(d$socmeduse~d$race_rc)
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, race_rc != "other")
d <- subset(d, race_rc != "hispanic")
d <- subset(d, race_rc != "asian")
d <- subset(d, race_rc != "nativeamer")
d <- subset(d, race_rc != "multiracial")
table(d$race_rc, useNA= "always")
##
## asian black hispanic multiracial nativeamer other
## 0 234 0 0 0 0
## white <NA>
## 1984 0
d$race_rc <- droplevels(d$race_rc) # 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~race_rc, data = d)
## Levene's Test for Homogeneity of Variance (center = median)
## Df F value Pr(>F)
## group 1 5.4972 0.01913 *
## 2216
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
As you can see, our data is 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 all race/ethnicity that is not “black” or “white”. 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, subsetting to drop multiple race/ethnicity group
d <- subset(d, race_rc != "other")
d <- subset(d, race_rc != "hispanic")
d <- subset(d, race_rc != "asian")
d <- subset(d, race_rc != "nativeamer")
d <- subset(d, race_rc != "multiracial")
d$race_rc <- droplevels(d$race_rc) # 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$race_rc)
t_output
##
## Welch Two Sample t-test
##
## data: d$socmeduse by d$race_rc
## t = -2.552, df = 277.27, p-value = 0.01125
## alternative hypothesis: true difference in means between group black and group white is not equal to 0
## 95 percent confidence interval:
## -2.9525739 -0.3810782
## sample estimates:
## mean in group black mean in group white
## 33.19658 34.86341
# once again, we use our formula to calculate cohen's d
d_output <- effsize::cohen.d(d$socmeduse~d$race_rc)
d_output
##
## Cohen's d
##
## d estimate: -0.1950276 (negligible)
## 95 percent confidence interval:
## lower upper
## -0.33069539 -0.05935982
To test our hypothesis that white participants would report significantly more social media use than black participants, we used a two-sample or independent t-test. This required us to drop all other race/ethnicity 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 = .019). This suggests that there is an increased chance of Type I error. 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 white participants (M = 34.86, SD = 8.42) reported significantly higher social media use than black participants (M = 33.20, SD = 9.56); t(277.27) = -2.55, p = 0.011 (see Figure 1). The effect size was calculated using Cohen’s d, with a value of -0.20 (negligible effect; Cohen, 1988).
References
Cohen J. (1988). Statistical Power Analysis for the Behavioral Sciences. New York, NY: Routledge Academic.