t-Test Lab

Author

Wyatt Sluder

Loading Libraries

library(psych) # for the describe() command
library(car) # for the leveneTest() command
library(effsize) # for the cohen.d() command

Importing Data

# update for home work 
 d <- read.csv(file="Data/mydata.csv", header=T)

State Your Hypothesis - PART OF YOUR WRITEUP

Extroversion will be higher during the covid pandemic for those who are employed than those who are unemployed

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

1 high school equivalent     2 college/university               3 employed 
                    1147                       33                      435 
            4 unemployed                5 retired        prefer not to say 
                      79                        4                       24 
                    <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, employment != "prefer not to say")
d <- subset(d, employment != "1 high school equivalent")
d <- subset(d, employment != "2 college/university")
d <- subset(d, employment != "5 retired")


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

  3 employed 4 unemployed         <NA> 
         435           79            0 
# 
# # check your variable types
 str(d)
'data.frame':   514 obs. of  6 variables:
 $ employment: chr  "3 employed" "3 employed" "3 employed" "3 employed" ...
 $ gender    : chr  "female" "female" "female" "female" ...
 $ big5_neu  : num  6 4 4.33 5 5.33 ...
 $ big5_agr  : num  4.33 4.67 7 6.67 5.33 ...
 $ big5_open : num  5.33 6 4.33 6.67 2 ...
 $ big5_ext  : num  2 5 4.33 5.67 4 ...
# 
# # make sure that your IV is recognized as a factor by R
 d$employment <- as.factor(d$employment)

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(big5_ext~employment, data = d)
Levene's Test for Homogeneity of Variance (center = median)
       Df F value Pr(>F)
group   1  0.1593 0.6899
      512               

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$big5_ext)
   vars   n mean   sd median trimmed  mad  min max range  skew kurtosis   se
X1    1 514 4.59 1.34   4.67    4.65 1.48 1.33   7  5.67 -0.34    -0.68 0.06
# 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_ext, group=d$employment)

 Descriptive statistics by group 
group: 3 employed
   vars   n mean   sd median trimmed  mad  min max range  skew kurtosis   se
X1    1 435 4.62 1.34   4.67    4.68 1.48 1.33   7  5.67 -0.39    -0.68 0.06
------------------------------------------------------------ 
group: 4 unemployed
   vars  n mean   sd median trimmed  mad  min max range  skew kurtosis   se
X1    1 79 4.47 1.34   4.33    4.48 1.48 1.67   7  5.33 -0.07    -0.68 0.15
# also use a histogram to examine your continuous variable
hist(d$big5_ext)

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

Issues with My Data - PART OF YOUR WRITEUP

Lab data- dropped participants who had employment status other than employed or not (college, retired, prefer not to say, high school). I also confirmed homogeneity using levene’s test and I checked that the dependent variable had a reasonable skew and kurtosis.

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$big5_ext~d$employment)

View Test Output

 t_output

    Welch Two Sample t-test

data:  d$big5_ext by d$employment
t = 0.87776, df = 108.51, p-value = 0.382
alternative hypothesis: true difference in means between group 3 employed and group 4 unemployed is not equal to 0
95 percent confidence interval:
 -0.1805594  0.4675957
sample estimates:
  mean in group 3 employed mean in group 4 unemployed 
                  4.616092                   4.472574 

Calculate Cohen’s d

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

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.1070573 (negligible)
95 percent confidence interval:
     lower      upper 
-0.1333021  0.3474167 

Write Up Results

My hypothesis that employed people would have higher extroversion was tested using a t-test, and the data met all the assumptions for the test. There was no statistical difference T(108)=0.88, p = .382, d = 0.11 95% {-0.13, 0.35} .

References

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