Comparing Means

t-Tests

The t-test compares the means of two samples, or the means of two variables measured in the same sample. It can also be used to test whether the mean of a variable differs from a given value.

t-Tests and data

There are two varieties of t-tests in R: One that uses the formula method, i.e. a structure of the type depvar ~ othervar(s), and one that does not.

In the formula variety, you may, in addition to what is shown in the following examples, refer to a data.frame or a matrix object with a data = mydatastatement instead via the extract operator $

Varieties of t-Test

The typical t-test (comparing means of two groups) is requested as follows:

t.test(mydata$income ~ mydata$gender)

By default, unequal variances are assumed. To find out whether this is indeed the case use var.test(income ~ gender). In the case of equal variances, use

t.test(mydata$income ~ mydata$gender, var.equal=T)

If the variable under investigation is stored in two different columns (one for each group), the command is as follows:

t.test(mydata$m.income, mydata$f.income)

Finally, a paired t-test is requested by

t.test(mydata$income.1, mydata$income.2, paired=T)

By default, the p-value for a two-tailed is displayed. In the case of one-tailed tests, the alternative hypothesis has to be specified as follows:

t.test(mydata$income ~ mydata$gender, alternative = "greater")

The keyword "greater" (or, alternatively, "less") refers to the first group.

To test whether a mean differs from a specified value (so called "one sample t-test"), write:

t.test(mydata$income, mu=4000)

(Simple, or one factor) Analysis of variance

Analysis of variance compares the means of several groups. The grouping variable has to be defined as a factor.

Assuming the "occupation" is a factor, an analysis of variance can be requested as follows:

aov.1 <- aov(income ~ occupation, data = my.data)

The results are summarized by, well, summary(aov.1). To obtain the mean of each group, write:

print(model.tables(aov.1,"means"))

The number of digits displayed can be influence by adding , digits=#, with # replaced by the number desired. Instead of the means, you may be interested in the "effects", i.e. the differences of the group means from the grand mean. Just type "effects" instead of "mean".

© W. Ludwig-Mayerhofer, R Guide | Last update: 23 Sep 2016