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 for homework
<- read.csv(file="Data/mydata.csv", header=T) d
State Your Hypothesis - PART OF YOUR WRITEUP
Democrats will report a higher satisfaction with life than republicans.
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$party_rc, useNA = "always")
apolitical democrat independent republican <NA>
437 1596 326 782 0
# # note that the table() output shows you exactly how the levels of your variable are written. 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)
<- subset(d, party_rc != "apolitical")
d <- subset(d, party_rc != "independent")
d <- subset(d, party_rc != "NA")
d
table(d$edu, useNA = "always")
1 High school diploma or less, and NO COLLEGE
40
2 Currently in college
1941
3 Completed some college, but no longer in college
23
4 Complete 2 year College degree
124
5 Completed Bachelors Degree
107
6 Currently in graduate education
96
7 Completed some graduate degree
47
<NA>
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)
# d$mhealth_rc[d$mhealth == "anxiety disorder"] <- "mental health diagnosis"
# d$mhealth_rc[d$mhealth == "bipolar"] <- "mental health diagnosis"
# d$mhealth_rc[d$mhealth == "depression"] <- "mental health diagnosis"
# d$mhealth_rc[d$mhealth == "eating disorders"] <- "mental health diagnosis"
# d$mhealth_rc[d$mhealth == "obsessive compulsive disorder"] <- "mental health diagnosis"
# d$mhealth_rc[d$mhealth == "other"] <- "mental health diagnosis"
# d$mhealth_rc[d$mhealth == "ptsd"] <- "mental health diagnosis"
# d$mhealth_rc[d$mhealth == "none or NA"] <- "none or NA"
# table(d$mhealth_rc, useNA = "always")
# table(d$mhealth, d$mhealth_rc, useNA = "always")
# # preview your changes and make sure everything is correct
table(d$party_rc, useNA = "always")
democrat republican <NA>
1596 782 0
table(d$edu, useNA = "always")
1 High school diploma or less, and NO COLLEGE
40
2 Currently in college
1941
3 Completed some college, but no longer in college
23
4 Complete 2 year College degree
124
5 Completed Bachelors Degree
107
6 Currently in graduate education
96
7 Completed some graduate degree
47
<NA>
0
#
# # check your variable types
str(d)
'data.frame': 2378 obs. of 6 variables:
$ edu : chr "2 Currently in college" "2 Currently in college" "2 Currently in college" "2 Currently in college" ...
$ party_rc: chr "democrat" "democrat" "democrat" "democrat" ...
$ npi : num 0.6923 0.0769 0.0769 0.9231 0.6923 ...
$ mindful : num 2.4 3 3.4 3.87 3.6 ...
$ swb : num 4.33 5.5 5 3 5.17 ...
$ support : num 6 6.08 6 6 6.08 ...
#
# # make sure that your IV is recognized as a factor by R
$party_rc <- as.factor(d$party_rc)
d$edu <- as.factor(d$edu) 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(swb~party_rc, data = d)
Levene's Test for Homogeneity of Variance (center = median)
Df F value Pr(>F)
group 1 9.7925 0.001773 **
2376
---
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$swb)
vars n mean sd median trimmed mad min max range skew kurtosis se
X1 1 2378 4.52 1.32 4.67 4.58 1.48 1 7 6 -0.4 -0.43 0.03
#
# # 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$swb, group=d$party_rc)
Descriptive statistics by group
group: democrat
vars n mean sd median trimmed mad min max range skew kurtosis se
X1 1 1596 4.37 1.33 4.5 4.42 1.48 1 7 6 -0.32 -0.52 0.03
------------------------------------------------------------
group: republican
vars n mean sd median trimmed mad min max range skew kurtosis se
X1 1 782 4.84 1.23 5 4.91 1.24 1 7 6 -0.54 -0.15 0.04
#
# # also use a histogram to examine your continuous variable
hist(d$swb)
#
# # last, use a boxplot to examine your continuous and categorical variables together
#categorical/IV on right and continuous/DV on left
boxplot(d$swb~d$party_rc)
Issues with My Data - PART OF YOUR WRITEUP
We dropped participants who were apart of political parties other than Democrat or Republican (e.g., apoliticals and independents). Before proceeding with analysis, we confirmed that all t-test assumptions were met. Levene’s test found significant heterogeneity of variance (p=.001). As a result, Welch’s t-test will be used despite the fact 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$swb~d$party_rc) t_output
View Test Output
t_output
Welch Two Sample t-test
data: d$swb by d$party_rc
t = -8.5107, df = 1662.7, p-value < 2.2e-16
alternative hypothesis: true difference in means between group democrat and group republican is not equal to 0
95 percent confidence interval:
-0.5781963 -0.3616069
sample estimates:
mean in group democrat mean in group republican
4.367481 4.837383
Calculate Cohen’s d
# # once again, we use our formula to calculate cohen's d
<- cohen.d(d$swb~d$party_rc) 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.3618601 (small)
95 percent confidence interval:
lower upper
-0.4480727 -0.2756475
Write Up Results
We tested our hypothesis that Democrats will report a higher satisfaction with life than republicans using an independent samples t-test. We dropped participants who were apart of political parties other than Democrat or Republican (e.g., apoliticals and independents). Before proceeding with analysis, we confirmed that all t-test assumptions were met. Levene’s test found significant heterogeneity of variance (p=.001). As a result, Welch’s t-test will be used despite the fact that our dependent variable is normally distributed (skew and kurtosis between -2 and +2). Our data met all of the assumptions of a t-test, however, we found a small significant difference, t(1662.7)=-8.51, p < .001, d = -0.36, 95% CI [-0.45, -0.28], (refer to Figure 1).
References
Cohen J. (1988). Statistical Power Analysis for the Behavioral Sciences. New York, NY: Routledge Academic.