t-Test HW

Author

Olivia Violette

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 - PART OF YOUR WRITEUP

Females will report higher levels of neuroticism than 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$gender, useNA = "always")

            female I use another term               male  Prefer not to say 
              1070                 36                223                 25 
              <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, gender != "I use another term")
d <- subset(d, gender != "Prefer not to say")

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

female   male   <NA> 
  1070    223      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 == "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"] <- "no 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':   1293 obs. of  6 variables:
 $ gender  : chr  "female" "male" "female" "female" ...
 $ mhealth : chr  "none or NA" "anxiety disorder" "none or NA" "none or NA" ...
 $ iou     : num  3.19 4 1.59 3.37 1.7 ...
 $ big5_neu: num  6 6.67 4 4 2.67 ...
 $ big5_con: num  6 3.33 5.33 5.67 6 ...
 $ mfq_26  : num  4.2 3.35 4.65 4.65 4.5 4.3 5.25 4.45 4.7 4.05 ...
# 
# # make sure that your IV is recognized as a factor by R
d$gender <- as.factor(d$gender)
# 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,homogeneity,no variance!

# # 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(big5_neu ~ gender, data = d)
Levene's Test for Homogeneity of Variance (center = median)
        Df F value  Pr(>F)  
group    1  2.8244 0.09308 .
      1291                  
---
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-- cutoff is -2 and 2
describe(d$big5_neu)
   vars    n mean   sd median trimmed  mad min max range  skew kurtosis   se
X1    1 1293 4.34 1.52   4.67    4.38 1.48   1   7     6 -0.26     -0.8 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$big5_neu, group=d$gender)

 Descriptive statistics by group 
group: female
   vars    n mean   sd median trimmed  mad min max range  skew kurtosis   se
X1    1 1070 4.48 1.48   4.67    4.54 1.48   1   7     6 -0.36    -0.67 0.05
------------------------------------------------------------ 
group: male
   vars   n mean   sd median trimmed  mad min max range skew kurtosis  se
X1    1 223  3.7 1.55   3.67    3.64 1.98   1   7     6 0.24    -0.83 0.1
# also use a histogram to examine your continuous variable
hist(d$big5_neu)

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

Issues with My Data - PART OF YOUR WRITEUP

We dropped participants who identified as other than female or male (e.g. ‘I use another term’ and ‘Prefer not to say’). We also confirmed homogeneity of variance using Levene’s Test (p = .093) 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$big5_neu~d$gender)

View Test Output

t_output

    Welch Two Sample t-test

data:  d$big5_neu by d$gender
t = 6.8531, df = 311.09, p-value = 3.884e-11
alternative hypothesis: true difference in means between group female and group male is not equal to 0
95 percent confidence interval:
 0.5537514 0.9997934
sample estimates:
mean in group female   mean in group male 
            4.476324             3.699552 

Calculate Cohen’s d

# # once again, we use our formula to calculate cohen's d
d_output <- cohen.d(d$big5_neu~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.5217022 (medium)
95 percent confidence interval:
    lower     upper 
0.3758921 0.6675123 

Write Up Results

We tested our hypothesis that females would report significantly higher levels of neuroticism than males using an independent samples t-test. Our data met all of the assumptions of a t-test and we did find a significant difference,t(311.09) = 6.85, p <.001, d = .52, 95% [0.55, 1.00] (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.