Hypothesis Test

To perform a hypothesis test for two means you need to use the t.test() function. You need to supply four pieces of information, data, x, and y.

  • data is the dataset from which the x and y variables reside.
  • x is a variable.
  • y is a variable.
  • alternative By default the alternative hypothesis is "two.sided", but you can also use "less", or "greater".

There are two scenarios for which you may want to use the t.test() function.

  1. If the x and y variables in your dataset are listed in two columns you will want to use t.test() like this.
t.test(data = the.data, x = variable.1, y = variable.2, alternative="two.sided")
  1. If x is a categorical variable and y is a quantitative variable then you will want to use the t.test() function like this.
t.test(data = the.data, y ~ x, alternative="two.sided")

Confidence Interval

The steps for calculating a confidence interval are the same as those used for hypothesis testing. To calculate a confidence interval you need to specify conf.level and make sure that alternative=two.sided. By default, alternative = two.sided so it is not necessary for you to include this, but it may help to remind you. By adding $conf.int to the end of the function you will see only the confidence interval calculations.

t.test(data = the.data, x = variable.1, y = variable.2, alternative="two.sided")$conf.int

t.test(data = the.data, y ~ x, alternative="two.sided")$conf.int

Example

The data for this example comes from the sleep dataset. Suppose 20 individuals were randomized to one of two groups. Each group was given a set of tasks to perform. Participants recorded the amount of "extra" sleep they got after performing their tasks. Is the amount of extra sleep significantly different between the two groups?

# Hypothesis Test
t.test(data = sleep, extra ~ group, alternative = "two.sided" )
## 
##  Welch Two Sample t-test
## 
## data:  extra by group
## t = -1.8608, df = 17.776, p-value = 0.07939
## alternative hypothesis: true difference in means is not equal to 0
## 95 percent confidence interval:
##  -3.3654832  0.2054832
## sample estimates:
## mean in group 1 mean in group 2 
##            0.75            2.33
# Confidence Interval
t.test(data=sleep, extra~group, conf.level = .95 )$conf.int
## [1] -3.3654832  0.2054832
## attr(,"conf.level")
## [1] 0.95

Mathematicss, Computer Science, and Statistics Department Gustavus Adolphus College