t-Test Homework

Author

Emily Alden

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 HW
d <- read.csv(file="Data/mydata.csv", header=T)

State Your Hypothesis

People in college during the COVID-19 Pandemic experienced more eating disorder symptoms/diagnoses than minors.

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 under 18 2 between 18 and 25 4 between 36 and 45           5 over 45 
                273                  28                   9                  13 
               <NA> 
                  0 
# # note that the table() output shows you exactly how the levels of your variable are rewritten. 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, age != "4 between 36 and 45")
d <- subset(d, age != "5 over 45")

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

         1 under 18 2 between 18 and 25                <NA> 
                273                  28                   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)


# 
# # check your variable types
str(d)
'data.frame':   301 obs. of  6 variables:
 $ age      : chr  "1 under 18" "1 under 18" "1 under 18" "2 between 18 and 25" ...
 $ education: chr  "4 equivalent to AP/IB completion" "prefer not to say" "4 equivalent to AP/IB completion" "4 equivalent to AP/IB completion" ...
 $ edeq12   : num  2.5 2.17 1.17 2.17 2.08 ...
 $ brs      : num  4 2.5 2.33 2 2.83 ...
 $ pss      : num  2.75 2.75 4 4.25 2.75 3.5 4 4.5 3 4.25 ...
 $ rse      : num  3 2 2.1 2.1 2.2 2 2.1 1.9 3.2 1.8 ...
# 
# # make sure that your IV is recognized as a factor by R
d$age <- as.factor(d$age)

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(edeq12~age, data = d)
Levene's Test for Homogeneity of Variance (center = median)
       Df F value Pr(>F)
group   1  0.2189 0.6402
      299               

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$edeq12)
   vars   n mean   sd median trimmed  mad min max range skew kurtosis   se
X1    1 301 2.14 0.79   2.08    2.11 0.99   1   4     3 0.25    -1.02 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$edeq12, group=d$age)

 Descriptive statistics by group 
group: 1 under 18
   vars   n mean   sd median trimmed  mad min max range skew kurtosis   se
X1    1 273 2.12 0.79   2.08    2.09 0.99   1   4     3  0.3    -0.99 0.05
------------------------------------------------------------ 
group: 2 between 18 and 25
   vars  n mean   sd median trimmed  mad min max range  skew kurtosis   se
X1    1 28 2.32 0.75   2.38    2.33 0.74   1 3.5   2.5 -0.22    -1.16 0.14
#
 # also use a histogram to examine your continuous variable
 hist(d$edeq12)

 # last, use a boxplot to examine your continuous and categorical variables together
 #categorical/IV goes on left, continuous/DV goes on right 
 boxplot(d$edeq12~d$age)

Issues with My Data - PART OF YOUR WRITEUP

We dropped participants who had were not a minor or college-aged (i.e. people ages 26-35,36-45, over 45). We also confirmed homogeneity of variance using Levene’s test (p=.640) and that our dependent variable is normally distributed (skew and kurtosis are 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$edeq12~d$age)

View Test Output

t_output

    Welch Two Sample t-test

data:  d$edeq12 by d$age
t = -1.2817, df = 33.459, p-value = 0.2088
alternative hypothesis: true difference in means between group 1 under 18 and group 2 between 18 and 25 is not equal to 0
95 percent confidence interval:
 -0.4985993  0.1130681
sample estimates:
         mean in group 1 under 18 mean in group 2 between 18 and 25 
                         2.122711                          2.315476 

Calculate Cohen’s d

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

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.2436649 (small)
95 percent confidence interval:
     lower      upper 
-0.6346640  0.1473341 

Write Up Results

We tested our hypothesis that people who are college aged would report significantly more eating disorder symptoms than people who are minors using an independent samples t-test. Our data met all of the assumptions of a t-test, however, we did not find a significant difference, t(33.46) = -1.28, p = 0.21, d = -0.24, 95%[-0.50, 0.11] (refer to Figure 1).

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

References

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