Practice 10 Conducting One-sample t-test in R
10.1 Directions
In this practice exercise, you will conduct a one-sample t-test in R.
10.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 in this data set is 10 mpg. Thus, the null hypothesis is that \(mu = 10\) the alternative hypothsis is that \(\mu \ne 10\). Note that in are, !
is the not apoerator, so !=
means not equal.
10.2.1 Conduct the t-test
To conduct a t-test, we need the following steps
- Calculate the mean
- Calculate the standard error of the mean
- Calculate a t-score for the sample mean, i.e. the distance from the hypothesized mean
- Calculate the p-value for the t-score
10.2.1.1 Step 1: Calculate the mean
Use the mean()
command to calculate the average mpg.
# Load the data
data("mtcars")
attach(mtcars)
## The following object is masked from package:ggplot2:
##
## mpg
# Calculate the mean
mean(mpg)
sample.mean <-print(c("sample.mean",sample.mean))
## [1] "sample.mean" "20.090625"
10.2.1.2 Step 2: Calculate the standard error of the mean
The formula for the standard error of the mean is \({S_{\bar X}} = \frac{\sigma}{{\sqrt n }}\), and if we do not know the population standard deviation \({S_{\bar X}} = \frac{s}{{\sqrt n }}\).
The sd()
command can be used to find the standard deviation. The length()
command can be use to determine the sample size.
# Calulate the standard error of the mean
length(mpg)
sample.n <- sd(mpg)
sample.sd <- sample.sd/sqrt(sample.n)
sample.se <-print(c("sample.se =",sample.se))
## [1] "sample.se =" "1.06542395937281"
10.2.1.3 Step 3: Calculate a t-score for the sample mean, i.e. the distance from the hypothesized mean
The formula for the t-score is \[t - score = \frac{{\bar x - \mu }}{{{\sigma _{\bar x}}}},\] \(\bar{x}\) is the sample mean, and \(\sigma _{\bar x}\) is the standard error of the mean.
# Calculate the t-statistic for the test
(sample.mean - 10)/sample.se
t.stat <-print(c("t.stat",t.stat))
## [1] "t.stat" "9.47099500741477"
10.2.1.4 Step 4. Calculate the p-value for the t-score
Here we use th pt()
command to find the probability of a t-score as far or father away from zero. To make things a little simpler, we use the abs()
command to ensure the t-score is positive. Finally, this is a two sided test so we need to multiply the value returned by pt()
by 2.
# Calculate the p-value
sample.n - 1
degrees.freedom = pt(q=abs(t.stat), df=degrees.freedom,lower.tail=F) * 2
p.value =print(c("Two-sided p.value",p.value))
## [1] "Two-sided p.value" "1.15459766493821e-10"
10.2.1.5 Calculate the p-value for a one sided test
For this, do not use the abs()
function. For a greater than test, lower.tail=F
, and for less than test, lower.tail=T
.
# Conduct a t-test to determine if mpg greater than 10
#H0: mu = 10
#Ha: mu > 10
pt(q=t.stat,df=degrees.freedom,lower.tail=F)
p.value <-print(c("One-sided p.value (greater than test)",p.value))
## [1] "One-sided p.value (greater than test)"
## [2] "5.77298832469105e-11"
# Conduct a t-test to determine if mpg is less than 10
#H0: mu = 10
#Ha: mu < 10
pt(q=t.stat,df=degrees.freedom,lower.tail=T)
p.value <-print(c("One-sided p.value (less than test)",p.value))
## [1] "One-sided p.value (less than test)" "0.99999999994227"
10.2.1.6 Finally, use t.test()
# For Ha: mu !=10
t.test(mpg, mu=10, level=0.95, alternative = "two.sided")
##
## One Sample t-test
##
## data: mpg
## t = 9.471, df = 31, p-value = 1.155e-10
## alternative hypothesis: true mean is not equal to 10
## 95 percent confidence interval:
## 17.91768 22.26357
## sample estimates:
## mean of x
## 20.09062
# For Ha: mu > 10
t.test(mpg, mu=10, level=0.95, alternative = "greater")
##
## One Sample t-test
##
## data: mpg
## t = 9.471, df = 31, p-value = 5.773e-11
## alternative hypothesis: true mean is greater than 10
## 95 percent confidence interval:
## 18.28418 Inf
## sample estimates:
## mean of x
## 20.09062
# For Ha: mu < 10
t.test(mpg, mu=10, level=0.95, alternative = "less")
##
## One Sample t-test
##
## data: mpg
## t = 9.471, df = 31, p-value = 1
## alternative hypothesis: true mean is less than 10
## 95 percent confidence interval:
## -Inf 21.89707
## sample estimates:
## mean of x
## 20.09062
10.3 R code used in the VoiceThread
# Load the data
data("mtcars")
attach(mtcars)
# Load the data
data("mtcars")
attach(mtcars)
# Conduct a t-test to determine if mpg is equal to 10
#H0: mu = 10
#Ha: mu != 10
# Step 1. Calculate the mean of miles per gallon
mean(mpg)
sample.mean <-print(c("sample.mean =",sample.mean))
# Step 2: Calculate the standard error of the mean
length(mpg)
sample.n <- sd(mpg)
sample.sd <- sample.sd/sqrt(sample.n)
sample.se <-print(c("sample.se =",sample.se))
# Step 3: Calculate the t-statistic for the test
(sample.mean-10)/sample.se
t.stat <-print(c("t.stat",t.stat))
# Step 4: Calculate the p-value
sample.n-1
degrees.freedom <- pt(q=abs(t.stat),df=degrees.freedom,lower.tail=F)*2
p.value <-print(c("Two-sided p.value",p.value))
# Conduct a t-test to determine if mpg greater than 10
#H0: mu = 10
#Ha: mu > 10
pt(q=t.stat,df=degrees.freedom,lower.tail=F)
p.value <-print(c("One-sided p.value (greater than test)",p.value))
# Conduct a t-test to determine if mpg is less than 10
#H0: mu = 10
#Ha: mu < 10
pt(q=t.stat,df=degrees.freedom,lower.tail=T)
p.value <-print(c("One-sided p.value (less than test)",p.value))
# t.test
t.test(mpg, mu=10, level=0.95, alternative="two.sided")
t.test(mpg, mu=10, level=0.95, alternative="greater")
t.test(mpg, mu=10, level=0.95, alternative="less")
10.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 hypothesis the average miles per gallon for care in this data set is 20 mpg.