t-Test Homework

Author

Kristen Occorso

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

State Your Hypothesis - PART OF YOUR WRITEUP

females worry more than males

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

            female I use another term               male  Prefer not to say 
              1020                 28                195                 16 
              <NA> 
                 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, gender != "I use another term")
d <- subset(d, gender != "Prefer not to say")

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

         1 under 18 2 between 18 and 25 3 between 26 and 35 4 between 36 and 45 
                791                  72                  12                 116 
          5 over 45                <NA> 
                224                   0 
 table(d$age_rc, useNA = "always")

<NA> 
   0 
# table(d$mhealth, d$mhealth_rc, useNA = "always")
# # 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$age_rc[d$age == "1 under 18"] <- "younger"
d$age_rc[d$age == "2 between 18 and 25"] <- "younger"
d$age_rc[d$age == "3 between 26 and 35"] <- "older"
d$age_rc[d$age == "5 over 45"] <- "older"
d$age_rc[d$age == "4 between 36 and 45"] <- "older"

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

  older younger    <NA> 
    352     863       0 
table(d$age, d$age_rc, useNA = "always")
                     
                      older younger <NA>
  1 under 18              0     791    0
  2 between 18 and 25     0      72    0
  3 between 26 and 35    12       0    0
  4 between 36 and 45   116       0    0
  5 over 45             224       0    0
  <NA>                    0       0    0
# 
# 
# # preview your changes and make sure everything is correct
table(d$gender, useNA = "always")

female   male   <NA> 
  1020    195      0 
table(d$age_rc, useNA = "always")

  older younger    <NA> 
    352     863       0 
# 
# # check your variable types
str(d)
'data.frame':   1215 obs. of  7 variables:
 $ gender   : chr  "male" "female" "female" "female" ...
 $ ethnicity: chr  "White - British, Irish, other" "White - British, Irish, other" "White - British, Irish, other" "White - British, Irish, other" ...
 $ age      : chr  "1 under 18" "1 under 18" "4 between 36 and 45" "4 between 36 and 45" ...
 $ big5_open: num  5.33 5 6 4.33 6.67 ...
 $ pswq     : num  0.851 -1.1235 1.1627 1.8077 0.0159 ...
 $ big5_ext : num  1.67 6 5 4.33 5.67 ...
 $ age_rc   : chr  "younger" "younger" "older" "older" ...
# 
# # make sure that your IV is recognized as a factor by R
d$gender <- as.factor(d$gender)
d$age_rc <- as.factor(d$age_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(pswq~gender, data = d)
Levene's Test for Homogeneity of Variance (center = median)
        Df F value Pr(>F)
group    1  0.1661 0.6837
      1213               

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$pswq)
   vars    n  mean sd median trimmed  mad   min  max range  skew kurtosis   se
X1    1 1215 -0.04  1      0   -0.03 1.15 -2.25 2.38  4.63 -0.06    -0.92 0.03
# 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$pswq, group=d$gender)

 Descriptive statistics by group 
group: female
   vars    n mean   sd median trimmed  mad   min  max range  skew kurtosis   se
X1    1 1020 0.07 0.97    0.1    0.08 1.12 -2.16 2.38  4.54 -0.14    -0.85 0.03
------------------------------------------------------------ 
group: male
   vars   n mean   sd median trimmed  mad   min max range skew kurtosis   se
X1    1 195 -0.6 0.97  -0.77   -0.65 1.08 -2.25 1.7  3.95 0.48    -0.67 0.07
# also use a histogram to examine your continuous variable
hist(d$pswq)

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

Issues with My Data - PART OF YOUR WRITEUP

I dropped participants who didn’t identify as female or male (e.g., I use another term and prefer not to say). Also confirmed homogeneity of variance using Levene’s test (p= 0.683) and the 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$pswq~d$gender)

View Test Output

t_output

    Welch Two Sample t-test

data:  d$pswq by d$gender
t = 8.7327, df = 273.79, p-value = 2.511e-16
alternative hypothesis: true difference in means between group female and group male is not equal to 0
95 percent confidence interval:
 0.5127948 0.8112919
sample estimates:
mean in group female   mean in group male 
          0.06644685          -0.59559651 

Calculate Cohen’s d

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

Write Up Results

I tested the hypothesis that females report significanlty more worrying than males using independent samples t-test. The data met all of the assumes of a t-test and did find a significant difference with effect size of medium, t(273.79) = 8.73, p= . 2.5lle-16 or 0.0000000000000002511, d= .68, 95% [0.512, 0.811]. (refer to figure 1).

References

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