library(psych) # for the describe() command
library(car) # for the leveneTest() command
library(effsize) # for the cohen.d() command
t-Test HW
Loading Libraries
Importing Data
# UPDATE THIS FOR HOMEWORK!!!!
<- read.csv(file="Data/mydata.csv", header=T) d
State Your Hypothesis - PART OF YOUR WRITEUP
People who identify as white will have significantly more social support than those people who identify as any other race other than white.
Check Your Assumptions
T-test Assumptions
- Data values must be independent (independent t-test only) (confirmed by data report)
- Data obtained via a random sample (confirmed by data report)
- IV must have two levels (will check below)
- Dependent variable must be normally distributed (will check below. if issues, note and proceed)
- Variances of the two groups must be approximately equal, aka ‘homogeneity of variance’. Lacking this makes our results inaccurate (will check below - this really only applies to Student’s t-test, but we’ll check it anyway)
Checking IV levels
# preview the levels and counts for your IV
table(d$race_rc, useNA = "always")
asian black hispanic multiracial nativeamer other
140 185 226 204 8 80
white <NA>
1278 0
# # note that the table() output shows you exactly how the levels of your variable are rewritten. when recoding, make sure you are spelling them exactly as they appear
#
# # to drop levels from your variable
# # this subsets the data and says that any participant who is coded as 'LEVEL BAD' should be removed
# # if you don't need this for the homework, comment it out (add a # at the beginning of the line)
# d <- subset(d, pet != "bird")
# d <- subset(d, pet != "cat and dog")
# d <- subset(d, pet != "fish")
# d <- subset(d, pet != "multiple types of pet")
# d <- subset(d, pet != "no pets")
# d <- subset(d, pet != "other")
table(d$race_rc, useNA = "always")
asian black hispanic multiracial nativeamer other
140 185 226 204 8 80
white <NA>
1278 0
# # to combine levels
# # this says that where any participant is coded as 'LEVEL BAD' it should be replaced by 'LEVEL GOOD'
# # you can repeat this as needed, changing 'LEVEL BAD' if you have multiple levels that you want to combine into a single level
# # if you don't need this for the homework, comment it out (add a # at the beginning of the line)
$race_rc2[d$race_rc == "white"] <- "white"
d$race_rc2[d$race_rc == "black"] <- "race other than white"
d$race_rc2[d$race_rc == "asian"] <- "race other than white"
d$race_rc2[d$race_rc == "hispanic"] <- "race other than white"
d$race_rc2[d$race_rc == "multiracial"] <- "race other than white"
d$race_rc2[d$race_rc == "nativeamer"] <- "race other than white"
d$race_rc2[d$race_rc == "other"] <- "race other than white"
d
table(d$race_rc2, useNA = "always")
race other than white white <NA>
843 1278 0
table(d$race_rc, d$race_rc2, useNA = "always")
race other than white white <NA>
asian 140 0 0
black 185 0 0
hispanic 226 0 0
multiracial 204 0 0
nativeamer 8 0 0
other 80 0 0
white 0 1278 0
<NA> 0 0 0
# # preview your changes and make sure everything is correct
# table(d$pet, useNA = "always")
table(d$race_rc2, useNA = "always")
race other than white white <NA>
843 1278 0
#
# # check your variable types
str(d)
'data.frame': 2121 obs. of 7 variables:
$ race_rc : chr "white" "white" "white" "other" ...
$ age : chr "1 between 18 and 25" "1 between 18 and 25" "1 between 18 and 25" "1 between 18 and 25" ...
$ moa_safety: num 2.75 3.25 3 1.25 2.25 2.5 4 3.25 2.75 3.5 ...
$ 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 ...
$ race_rc2 : chr "white" "white" "white" "race other than white" ...
# # make sure that your IV is recognized as a factor by R
# d$pet <- as.factor(d$pet)
$race_rc2 <- as.factor(d$race_rc2) d
Testing Homogeneity of Variance with Levene’s Test
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(support~race_rc2, data = d)
Levene's Test for Homogeneity of Variance (center = median)
Df F value Pr(>F)
group 1 23.724 1.194e-06 ***
2119
---
Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
This is more of a formality in our case, because we are 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 extra effort on our part!
Check Normality
# 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
# you can use the describe() command on an entire datafrom (d) or just on a single variable (d$pss)
# use it to check the skew and kurtosis of your DV
describe(d$support)
vars n mean sd median trimmed mad min max range skew kurtosis se
X1 1 2121 5.53 1.14 5.75 5.66 0.99 0 7 7 -1.1 1.36 0.02
# 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$race_rc2)
Descriptive statistics by group
group: race other than white
vars n mean sd median trimmed mad min max range skew kurtosis se
X1 1 843 5.33 1.22 5.5 5.45 1.24 0 7 7 -0.91 0.78 0.04
------------------------------------------------------------
group: white
vars n mean sd median trimmed mad min max range skew kurtosis se
X1 1 1278 5.66 1.06 5.92 5.79 0.99 1 7 6 -1.2 1.84 0.03
# also use a histogram to examine your continuous variable
hist(d$support)
# last, use a boxplot to examine your continuous and categorical variables together
# categorical/IV goes on the right, continuous/DV goes on the left
boxplot(d$support~d$race_rc2)
Issues with My Data - PART OF YOUR WRITEUP
We combined participants who were any race other than white into a category named ‘race other than white’ and had participants who were white in their own category. We also confirmed homogeneity of varience using Levene’s test (p= 1.194e-06) and that our dependent variable is normally distributed (skew and kurtosis between -2 and +2).
Run a 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.test(d$support~d$race_rc2) t_output
View Test Output
t_output
Welch Two Sample t-test
data: d$support by d$race_rc2
t = -6.4299, df = 1615.1, p-value = 1.677e-10
alternative hypothesis: true difference in means between group race other than white and group white is not equal to 0
95 percent confidence interval:
-0.4319467 -0.2300168
sample estimates:
mean in group race other than white mean in group white
5.333729 5.664710
Calculate Cohen’s d
# # once again, we use our formula to calculate cohen's d
<- cohen.d(d$support~d$race_rc2) d_output
View Effect Size
- Trivial: < .2
- Small: between .2 and .5
- Medium: between .5 and .8
- Large: > .8
d_output
Cohen's d
d estimate: -0.2940113 (small)
95 percent confidence interval:
lower upper
-0.3814742 -0.2065485
Write Up Results
We tested our hypothesis that people who identify as white will have significantly more social support than those people who identify as any other race other than white using an indpendent samples t-test. Our data met all of the assumptions of a t-test, and we did find a significant difference, t(1615.1) = -6.43, p = 1.677e-10, d = -0.29, 95% [-0.38, -0.21] (refer to Figure 1).
Our effect size was small according to Cohen (1988).
References
Cohen J. (1988). Statistical Power Analysis for the Behavioral Sciences. New York, NY: Routledge Academic.