t-Test Lab

Author

Heather Perkins

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/labdata.csv", header=T)

State Your Hypothesis - PART OF YOUR WRITEUP

Cat owners will report higher levels of stress than dog owners.

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

                 bird                   cat           cat and dog 
                    5                   211                   136 
                  dog                  fish multiple types of pet 
                  246                    35                   104 
              no pets                 other                  <NA> 
                  396                    68                     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, pet != "bird")
d <- subset(d, pet != "cat and dog")
d <- subset(d, pet != "fish")
d <- subset(d, pet != "multiple types of pet")
d <- subset(d, pet != "no pets")
d <- subset(d, pet != "other")

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

             anxiety disorder                       bipolar 
                           61                             4 
                   depression              eating disorders 
                           16                             9 
                   none or NA obsessive compulsive disorder 
                          340                             8 
                        other                          ptsd 
                            8                            11 
                         <NA> 
                            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)
d$mhealth_rc[d$mhealth == "anxiety disorder"] <- "mental health diagnosis"
d$mhealth_rc[d$mhealth == "bipolar"] <- "mental health diagnosis"
d$mhealth_rc[d$mhealth == "eating disorders"] <- "mental health diagnosis"
d$mhealth_rc[d$mhealth == "depression"] <- "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 == "none or NA"] <- "none or NA"

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

mental health diagnosis              none or NA                    <NA> 
                    106                     340                      11 
# # preview your changes and make sure everything is correct
table(d$pet, useNA = "always")

 cat  dog <NA> 
 211  246    0 
table(d$mhealth_rc, useNA = "always")

mental health diagnosis              none or NA                    <NA> 
                    106                     340                      11 
# 
# check your variable types
str(d)
'data.frame':   457 obs. of  7 variables:
 $ pet       : chr  "cat" "cat" "dog" "cat" ...
 $ mhealth   : chr  "none or NA" "anxiety disorder" "none or NA" "none or NA" ...
 $ iou       : num  3.19 4 1.59 2.48 2.52 ...
 $ rse       : num  2.3 1.6 3.9 3 2.9 2.8 2.4 1.6 3.2 3.3 ...
 $ phq       : num  1.33 3.33 1 1.56 2.22 ...
 $ pss       : num  3.25 3.75 1 2.5 2.25 3 2.75 3 2.5 2.25 ...
 $ mhealth_rc: chr  "none or NA" "mental health diagnosis" "none or NA" "none or NA" ...
# 
# # make sure that your IV is recognized as a factor by R
d$pet <- as.factor(d$pet)

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(pss~pet, data = d)
Levene's Test for Homogeneity of Variance (center = median)
       Df F value Pr(>F)
group   1  0.5715 0.4501
      455               

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$pss)
   vars   n mean   sd median trimmed  mad min max range skew kurtosis   se
X1    1 457    3 0.96      3    2.99 1.11   1   5     4 0.02    -0.78 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$pss, group=d$pet)

 Descriptive statistics by group 
group: cat
   vars   n mean   sd median trimmed  mad min max range  skew kurtosis   se
X1    1 211 3.08 0.93      3    3.08 1.11   1   5     4 -0.03    -0.81 0.06
------------------------------------------------------------ 
group: dog
   vars   n mean   sd median trimmed  mad min max range skew kurtosis   se
X1    1 246 2.93 0.98      3    2.93 1.11   1   5     4 0.07    -0.78 0.06
# 
# # also use a histogram to examine your continuous variable
hist(d$pss)

# 
# # last, use a boxplot to examine your continuous and categorical variables together
boxplot(d$pss~d$pet)

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.

A note that might be helpful: the opposite of ‘homogeneity of variance’ (the thing we want) is ‘heterogeneity of variance’ (the thing we don’t want). So, you could say something like this, if needed:

“Before proceeding with analysis, we confirmed that all t-test assumptions were met. Levene’s test found significant heterogeneity of variance (p = .###). 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_output <- t.test(d$pss~d$pet)

View Test Output

t_output

    Welch Two Sample t-test

data:  d$pss by d$pet
t = 1.6401, df = 450.53, p-value = 0.1017
alternative hypothesis: true difference in means between group cat and group dog is not equal to 0
95 percent confidence interval:
 -0.02917209  0.32344442
sample estimates:
mean in group cat mean in group dog 
         3.077014          2.929878 

Calculate Cohen’s d

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

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.1532511 (negligible)
95 percent confidence interval:
      lower       upper 
-0.03141488  0.33791717 

Write Up Results

Write up your results. Again, make sure to maintain an appropriate tone, and follow APA guidelines for reporting statistical results. I recommend following the below outline:

  • Briefly restate your hypothesis
  • Describe any issues with your data (you can copy/paste from above, just make sure everything flows).
  • Report your results. Remember to include means of your groups, your t-value, your degrees of freedom, your p-value, your d-value, and your confidence interval.
  • If your test is significant, interpret your effect size (trivial, small, medium, or large) and include the citation.
  • Make sure to include a reference to Figure 1 (created using the code below)

References

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