t-Test Homework

Author

Alisha Patel

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

Participants who average 5-6 hours of sleep will report higher levels of intolerance of uncertainty than participants who average more than 8-10 hours of sleep.

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

 1 < 5 hours  2 5-6 hours  3 7-8 hours 4 8-10 hours 5 > 10 hours         <NA> 
          70          278          401          246           44            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)
d <- subset(d, sleep_hours != "1 < 5 hours")
d <- subset(d, sleep_hours != "3 7-8 hours")
d <- subset(d, sleep_hours != "5 > 10 hours")

# # 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)

# # preview your changes and make sure everything is correct
table(d$sleep_hours, useNA = "always")

 2 5-6 hours 4 8-10 hours         <NA> 
         278          246            0 
# 
# # check your variable types
str(d)
'data.frame':   524 obs. of  6 variables:
 $ exercise_cat: chr  "2 1-2 hours" "2 1-2 hours" "2 1-2 hours" "1 less than 1 hour" ...
 $ sleep_hours : chr  "2 5-6 hours" "2 5-6 hours" "4 8-10 hours" "4 8-10 hours" ...
 $ gad         : num  3.86 2 1 1.43 1.43 ...
 $ pas_covid   : num  4.56 4.22 2.33 3.67 2.67 ...
 $ iou         : num  4 3.37 1.56 2.48 3.22 ...
 $ rse         : num  1.6 1.7 3.5 3 2.5 3.4 2 2.9 3.2 3.6 ...
# # make sure that your IV is recognized as a factor by R
d$sleep_hours <- as.factor(d$sleep_hours)

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(iou~sleep_hours, data = d)
Levene's Test for Homogeneity of Variance (center = median)
       Df F value    Pr(>F)    
group   1  11.171 0.0008907 ***
      522                      
---
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$iou)
   vars   n mean   sd median trimmed  mad min  max range skew kurtosis   se
X1    1 524 2.52 0.89   2.37    2.45 0.93   1 4.78  3.78 0.58    -0.49 0.04
# 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$iou, group=d$sleep_hours)

 Descriptive statistics by group 
group: 2 5-6 hours
   vars   n mean   sd median trimmed  mad  min  max range skew kurtosis   se
X1    1 278 2.75 0.91   2.63    2.72 1.04 1.07 4.78   3.7 0.28    -0.89 0.05
------------------------------------------------------------ 
group: 4 8-10 hours
   vars   n mean   sd median trimmed  mad min  max range skew kurtosis   se
X1    1 246 2.26 0.78   2.11    2.18 0.71   1 4.78  3.78 0.94     0.61 0.05
# also use a histogram to examine your continuous variable
hist(d$iou)

# 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$iou~d$sleep_hours)

Issues with My Data - PART OF YOUR WRITEUP

“We dropped participants who averaged less than five hours of sleep, averaged 7-8 hours of sleep, and more than ten hours of sleep (e.g., < 5 hours, 7-8 hours, > 10 hours). We also confirmed homogeneity of variance using Levene’s test (p - .001) 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_output <- t.test(d$iou~d$sleep_hours)

View Test Output

t_output

    Welch Two Sample t-test

data:  d$iou by d$sleep_hours
t = 6.6376, df = 521.48, p-value = 8.012e-11
alternative hypothesis: true difference in means between group 2 5-6 hours and group 4 8-10 hours is not equal to 0
95 percent confidence interval:
 0.3466685 0.6381415
sample estimates:
 mean in group 2 5-6 hours mean in group 4 8-10 hours 
                  2.748201                   2.255796 

Calculate Cohen’s d

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

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.575597 (medium)
95 percent confidence interval:
    lower     upper 
0.4001236 0.7510705 

Write Up Results

“We tested our hypothesis that participants who average 5-6 hours of sleep will report higher levels of intolerance of uncertainty than participants who average more than 8-10 hours of sleep using an independent samples t-test.”We dropped participants who averaged less than five hours of sleep, averaged 7-8 hours of sleep, and more than ten hours of sleep (e.g., < 5 hours, 7-8 hours, > 10 hours). We also confirmed homogeneity of variance using Levene’s test (p - .001) and that our dependent variable is normally distributed (skew and kurtosis between -2 and +2).” Our data met all of the assumptions of the t-test. We did find a significant difference, t(521.48) = 6.64, p < .001, d = .58, 95% [2.75, 2.26].(refer to figure 1).”

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

References

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