t-Test Lab

Author

Dori Clousing

Loading Libraries

library(psych) # for the describe() command
library(car) # for the leveneTest() command
library(effsize) # for the cohen.d() command

Importing Data

d <- read.csv(file="Data/mydata.csv", header=T)

State Your Hypothesis

Democrats and Republicans will report similar levels of stress.

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

table(d$party_rc, useNA = "always")

 apolitical    democrat independent  republican        <NA> 
        325        1089         218         504           0 
table(d$stress)

1.3 1.4 1.5 1.6 1.7 1.8 1.9   2 2.1 2.2 2.3 2.4 2.5 2.6 2.7 2.8 2.9   3 3.1 3.2 
  2   3   5   7  14  22  21  27  40  53  52  77  93  98 115 130 130 154 151 138 
3.3 3.4 3.5 3.6 3.7 3.8 3.9   4 4.1 4.2 4.3 4.4 4.5 4.6 
140 125 101  92  78  53  49  53  39  19  19  17   6  13 
d <- subset(d, party_rc != "apolitical")
d <- subset(d, party_rc != "independent")
d <- subset(d, party_rc != "NA")

table(d$party_rc, useNA = "always")

  democrat republican       <NA> 
      1089        504          0 
# # check your variable types
str(d)
'data.frame':   1593 obs. of  6 variables:
 $ age     : chr  "1 between 18 and 25" "1 between 18 and 25" "1 between 18 and 25" "1 between 18 and 25" ...
 $ party_rc: chr  "democrat" "democrat" "democrat" "democrat" ...
 $ mindful : num  2.4 3 3.4 3.87 3.6 ...
 $ efficacy: num  3.4 3 3 3 2.9 2.5 3.4 2.4 3.1 2.8 ...
 $ npi     : num  0.6923 0.0769 0.0769 0.9231 0.6923 ...
 $ stress  : num  3.3 2.4 2.9 2.5 2.8 3.3 2.5 1.8 2.4 3.6 ...
# # make sure that your IV is recognized as a factor by R
d$party_rc <- as.factor(d$party_rc)

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!

leveneTest(stress~party_rc, data = d)
Levene's Test for Homogeneity of Variance (center = median)
        Df F value Pr(>F)
group    1  2.2865 0.1307
      1591               

Check Normality

# use it to check the skew and kurtosis of your DV
describe(d$stress)
   vars    n mean  sd median trimmed  mad min max range  skew kurtosis   se
X1    1 1593 3.07 0.6    3.1    3.07 0.59 1.3 4.6   3.3 -0.01    -0.21 0.02
describeBy(d$stress, 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 1089  3.1 0.59    3.1    3.09 0.59 1.5 4.6   3.1 0.01    -0.26 0.02
------------------------------------------------------------ 
group: republican
   vars   n mean   sd median trimmed  mad min max range skew kurtosis   se
X1    1 504    3 0.63      3    3.01 0.59 1.3 4.6   3.3    0    -0.18 0.03
# also use a histogram to examine your continuous variable
hist(d$stress)

# 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$stress~d$party_rc)

Issues with My Data

We dropped participants who had a political party affiliation other than democrat or republican (apolitical, independent, NA). We also confirmed homogeneity of variance using Levene’s test (p=.13) and that our dependent variable is normally distributed (skew and curtosis between -+2)

Run a T-test

t_output <- t.test(d$stress~d$party_rc)

View Test Output

t_output

    Welch Two Sample t-test

data:  d$stress by d$party_rc
t = 2.7304, df = 921.36, p-value = 0.006447
alternative hypothesis: true difference in means between group democrat and group republican is not equal to 0
95 percent confidence interval:
 0.02548739 0.15577654
sample estimates:
  mean in group democrat mean in group republican 
                3.095592                 3.004960 

Calculate Cohen’s d

d_output <- cohen.d(d$stress~d$party_rc)

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.1508511 (negligible)
95 percent confidence interval:
     lower      upper 
0.04504979 0.25665239 

Write Up Results

We tested our hypothesis that democrats and republicans would report similar levels of stress using an independent samples t-test. Our data met all of the assumptions of a t-test, and we found a statistically significant difference, t(2.73) = 921.36, p = .006, d = .20, 95% [0.025, .156]. (refer to Fig. 1)

We dropped participants who had a political party affiliation other than democrat or republican (apolitical, independent, NA). We also confirmed homogeneity of variance using Levene’s test (p=.13) and that our dependent variable is normally distributed (skew and curtosis between -+2).

Our effect size was negligible according to Cohen (1988).

References

Cohen J. (1988). Statistical Power Analysis for the Behavioral Sciences. New York, NY: Routledge Academic.