library(psych) # for the describe() command
library(car) # for the leveneTest() command
library(effsize) # for the cohen.d() command
t-Test Homework
Loading Libraries
Importing Data
<- read.csv(file="Data/mydata.csv", header=T) d
State Your Hypothesis - PART OF YOUR WRITEUP
There is a significant difference in self-esteem between bisexual and gay/lesbian individuals.
State your t-test hypothesis. Remember, a t-test has one continuous variable as the dependent variable, and one categorical variable with two levels as the independent variable. If your IV of choice has more than two levels, you will need to pick two levels to compare and drop the rest, or combine levels until you only have two left.
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$sexual_orientation, useNA = "always")
Asexual Bi Gay/Lesbian
31 144 49
Heterosexual/Straight I use another term Prefer not to say
805 34 77
<NA>
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, sexual_orientation != "Heterosexual/Straight")
d <- subset(d, sexual_orientation != "Asexual")
d <- subset(d, sexual_orientation != "Prefer not to say")
d <- subset(d, sexual_orientation != "I use another term")
d
# # 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)
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!
The data needs to have h.v in order for a t-test to function as its intended, to confirm that we use Levene’s Test - We want the test to be non-significant becaue that indicates h.v
# # 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(rse~sexual_orientation, data = d)
Warning in leveneTest.default(y = y, group = group, ...): group coerced to
factor.
Levene's Test for Homogeneity of Variance (center = median)
Df F value Pr(>F)
group 1 1.1906 0.2766
191
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$rse)
vars n mean sd median trimmed mad min max range skew kurtosis se
X1 1 193 2.15 0.68 2 2.11 0.59 1 3.8 2.8 0.56 -0.42 0.05
#
# # 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$rse, group=d$sexual_orientation)
Descriptive statistics by group
group: Bi
vars n mean sd median trimmed mad min max range skew kurtosis se
X1 1 144 2.21 0.69 2.1 2.17 0.74 1 3.8 2.8 0.53 -0.56 0.06
------------------------------------------------------------
group: Gay/Lesbian
vars n mean sd median trimmed mad min max range skew kurtosis se
X1 1 49 1.97 0.61 1.9 1.94 0.59 1 3.7 2.7 0.57 -0.26 0.09
#
# # also use a histogram to examine your continuous variable
hist(d$rse)
#
# # last, use a boxplot to examine your continuous and categorical variables together
#Categorical/IV GOES ON RIGHT Continuous/DV GOES ON LEFT
boxplot(d$rse~d$sexual_orientation)
Issues with My Data - PART OF YOUR WRITEUP
Briefly describe any issues with your data and how you’ve resolved them. For instance, if you are using a gender variable that has three levels, you should say that you dropped or combined two of the levels for your analysis. This should be written in an appropriate scientific tone.
We dropped participants who identified with a sexual orientation other than bisexual and gay/lesbian (e.g., heterosexual/straight, asexual, “I use another term,” and “Prefer not to say”). We also confirmed homogeneity of variance using Levene’s test (p = 0.2766) and that our dependent variable is normally distributed (skew and kurtosis is between -2 and +2). As a result, Welch’s t-test will be used.
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$rse~d$sexual_orientation) t_output
View Test Output
t_output
Welch Two Sample t-test
data: d$rse by d$sexual_orientation
t = 2.2962, df = 92.221, p-value = 0.02393
alternative hypothesis: true difference in means between group Bi and group Gay/Lesbian is not equal to 0
95 percent confidence interval:
0.03247428 0.44836472
sample estimates:
mean in group Bi mean in group Gay/Lesbian
2.213889 1.973469
Calculate Cohen’s d
# # once again, we use our formula to calculate cohen's d
<- cohen.d(d$rse~d$sexual_orientation) 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.3588605 (small)
95 percent confidence interval:
lower upper
0.03065901 0.68706208
Write Up Results
We tested our hypothesis that bisexual and gay/lesbian individuals would differ significantly in self-esteem using an independent samples t-test. We dropped participants who identified with a sexual orientation other than bisexual and gay/lesbian (e.g., heterosexual/straight, asexual, “I use another term,” and “Prefer not to say”). Our data met all of the assumptions of a t-test, and we found a significant difference, t(92.221) = 2.2962, p = .023, d = .36, 95% [0.03, 0.45] (refer to Figure 1).
Our effect size was small according to Choen(1988).
References
Cohen J. (1988). Statistical Power Analysis for the Behavioral Sciences. New York, NY: Routledge Academic.