T-Tests

KEY:

ds = dataset you are currently using.

DV = dependent variable of interest

IV = independent variable of interest

XYXY = dummy name for a variable, matrix, or data frame into which you are moving information.

Performing a T-Test with between-subjects data

Reminder: Always begin by performing a test of homogeneity of variance. The car package includes Lavene's test.

_____________________________

library(car)
leveneTest(DV ~ IV, center = mean, data=ds)

_____________________________

 

If p<.05 you have a situation where group variances are different (var.equal=FALSE). If p>.05, there is insufficient evidence to draw such a conclusion (var.equal=TRUE).

"paired" is set to FALSE to indicate that the data are between-subjects, and thus an independent-samples test should be run.

If your IV has only two levels

_____________________________

XYXY <- t.test(DV ~ IV, var.equal=??????, paired = FALSE, data=ds)

_____________________________

If your IV has more than two levels

r has a command (pairwise.t.test) that can perform multiple tests concurrently. However, this does not provide t values or df information. Adding a subset command to t.test will allow you to test specified levels, while producing critical output. Just remember to adjust for multiple tests.

For subset:

  • %in% will pair your IV name with each of the components in the c() list. Make sure to put the names in "".

  • L1, L2, L3 are the names you may have given to the different levels of the IV

_____________________________

XYXY1 <- t.test(DV ~ IV, var.equal=??????, paired = FALSE, data=ds, subset = IV %in% c("L1", "L2"))

XYXY2 <- t.test(DV ~ IV, var.equal=??????, paired = FALSE, data=ds, subset = IV %in% c("L1", "L3"))

XYXY3 <- t.test(DV ~ IV, var.equal=??????, paired = FALSE, data=ds, subset = IV %in% c("L2", "L3"))

_____________________________

Performing a T-Test with within-subjects data

"paired" is set to TRUE to indicate  that the data are within-subjects, and thus an paired-samples test should be run.

_____________________________

XYXY <- t.test(DV ~ IV, paired = TRUE, data=ds)

_____________________________

Post-Hoc T-Tests using the results of a one-way ANOVA *not complete*

The lsr package can use the results of an aov file to perform multiple t-tests and make correct for multiple comparisons.

At present, this function is a demo, but it will provide fast-access to p-values.

_____________________________

library(lsr)

XYXY <-posthocPairwiseT(x=aovFile, p.adjust.method="bonferroni")

_____________________________