t-Test HW

Author

Nat Haseltine

Loading Libraries

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

Importing Data

# UPDATE THIS FOR HOMEWORK!!!!!!!!!!
d <- read.csv(file="Data/mydata.csv", header=T)

State Your Hypothesis - PART OF YOUR WRITEUP

Females experience more support compared to Males.

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$age, useNA = "always")

1 between 18 and 25 2 between 26 and 35 3 between 36 and 45           4 over 45 
               1989                 116                  38                  18 
               <NA> 
                  0 
table(d$gender, useNA = "always")

   f    m   nb <NA> 
1586  544   31    0 
# # note that the table() output shows you exactly how the levels of your variable are written. when recording, 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, age != "between 26 and 35")
d <- subset(d, age != "between 36 and 45")
d <- subset(d, age != "over 45")
d <- subset(d, gender != "nb")


# # 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"] <- "mental health diagnosis"
# 
# 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$pet, useNA = "always")
# table(d$mhealth_rc, useNA = "always")
# # 
# # # check your variable types
str(d)
'data.frame':   2130 obs. of  6 variables:
 $ gender   : chr  "f" "m" "m" "f" ...
 $ age      : chr  "1 between 18 and 25" "1 between 18 and 25" "1 between 18 and 25" "1 between 18 and 25" ...
 $ swb      : num  4.33 4.17 1.83 5.17 3.67 ...
 $ support  : num  6 6.75 5.17 5.58 6 ...
 $ socmeduse: int  47 23 34 35 37 13 37 43 37 29 ...
 $ stress   : num  3.3 3.3 4 3.2 3.1 3.5 3.3 2.4 2.9 2.7 ...
# # 
# # # make sure that your IV is recognized as a factor by R
d$gender <- as.factor(d$gender)
d$age <- as.factor(d$age)
# d$mhealth_rc <- as.factor(d$mhealth_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!

# # 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~gender, data = d)
Levene's Test for Homogeneity of Variance (center = median)
        Df F value Pr(>F)
group    1  0.7109 0.3993
      2128               

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 2130 5.54 1.13   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$gender)

 Descriptive statistics by group 
group: f
   vars    n mean   sd median trimmed  mad min max range  skew kurtosis   se
X1    1 1586 5.57 1.13   5.83     5.7 0.99   0   7     7 -1.15     1.57 0.03
------------------------------------------------------------ 
group: m
   vars   n mean   sd median trimmed  mad min max range  skew kurtosis   se
X1    1 544 5.45 1.14   5.67    5.56 1.11   1   7     6 -0.96     0.84 0.05
# 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$gender)

Issues with My Data - PART OF YOUR WRITEUP

An initial issue was that there were more than two variables being analyzed, which lead to certain groups to be dropped from the analysis. We dropped participants that were outside of male and female, which were non-binary. We dropped participants that were outside of the age range between 18 and 25 (e.g., between 26 and 35, between 36 and 45, and over 45).

Before proceeding with analysis, we confirmed that all t-test assumptions were met. Levene’s test found significant heterogeneity of variance (p = .031). As a result, Welch’s t-test will be used. Our dependent variable is normally distributed with a skew and kurtosis between -2 and +2 for both males and females.

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_output <- t.test(d$support~d$gender)

View Test Output

t_output

    Welch Two Sample t-test

data:  d$support by d$gender
t = 2.1529, df = 928.07, p-value = 0.03159
alternative hypothesis: true difference in means between group f and group m is not equal to 0
95 percent confidence interval:
 0.01077674 0.23301391
sample estimates:
mean in group f mean in group m 
       5.569199        5.447304 

Calculate Cohen’s d

# # once again, we use our formula to calculate cohen's d
d_output <- cohen.d(d$support~d$gender)

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.1078432 (negligible)
95 percent confidence interval:
     lower      upper 
0.01035008 0.20533631 

Write Up Results

We tested our hypothesis that females experience more support compared to males using an independent sample t-test. Our data met all of the assumptions of a t-test, however, we did not find a significant difference, t(928.07) = 2.15, p = 0.031, d = 0.108, 95% [0.01, .2].

Our effect size was small according to Coehn (1988).

References

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