Practice 11 Conducting Two-sample t-test in R
11.1 Directions
In this practice exercise, you will conduct a two-sample t-test in R.
11.2 A closer look at the code
Will be using the mtcars
data set to test the hypothesis the average miles per gallon for cars with automatic transmistions is different from cars with manual transmissions. Thus, the null hypothesis is that \(\bar{mpg}_{auto} = \bar{mpg}_{manual}\) the alternative hypothsis is that \(\bar{mpg}_{auto} \ne \bar{mpg}_{manual}\). Note that in are, !
is the not apoerator, so !=
means not equal.
11.2.1 Conduct the t-test
To conduct a t-test, we need the following steps
- Plot the data
- Use
t.test()
to conduct a two-sample t-test
11.2.1.1 Step 1: Plot the Data
Use the boxplot()
command to plot mpg by am
. See Practice 06: Box plots in R. Note that am
is equal to 0 for automatic transmission and 1 for manual transmission.
# Load the data
data("mtcars")
attach(mtcars)
## The following objects are masked from mtcars (pos = 3):
##
## am, carb, cyl, disp, drat, gear, hp, mpg, qsec, vs, wt
## The following object is masked from package:ggplot2:
##
## mpg
# Plot the data
boxplot(mpg~am)
Note that the inter-quartile range for mpg
of cars is larger for manual transmissions am=1
than for automatic transmission am=0
.
11.2.1.2 Step 2: Use t.test()
to conduct the test.
# conduct t-test
t.test(mpg~am, mu=0, conf.level = 0.95, alternative = "two.sided")
##
## Welch Two Sample t-test
##
## data: mpg by am
## t = -3.7671, df = 18.332, p-value = 0.001374
## alternative hypothesis: true difference in means is not equal to 0
## 95 percent confidence interval:
## -11.280194 -3.209684
## sample estimates:
## mean in group 0 mean in group 1
## 17.14737 24.39231
11.3 R code used in the VoiceThread
# Load the data
data("mtcars")
attach(mtcars)
# Plot the data
boxplot(mpg~am)
# conduct t-test
t.test(mpg~am, mu=0, conf.level = 0.95, alternative = "two.sided", var.equal=F)
11.4 Now you try
Use R to complete the following activities (this is just for practice you do not need to turn anything in).
Using the mtcars
data set, test the is average miles per gallon is the same for transmission type.