Chapter 13
Testing Hypotheses about Means
Example 13.1: Normal human body temperature
A random sample of 16 normal body temperatures is provided in Example 13.1. The question is whether
the population mean normal body temperature is less than 98.6 degrees Fahrenheit. We will enter the
data, check if the data meet necessary conditions before applying a one-sample t-test, and then perform a
one-sided one-sample t-test. The t-test will be performed first using the t.test()function and a second
time simply using basic R commands.
Entering the data.
Although the data can be directly entered into R, you do risk making typos along the way. Therefore it is
suggested you either type the commands first into a text file using Notepad and then copy-and-paste into
R. (The use of writing code into Notepad or similar files is always good practice in case you make errors
and to save your work for future computing sessions.) If you wish, you can also get practice in saving the
data in a text file using Excel and then importing it into R using the read.table() function. After
getting the data into R, it is a good idea to calculate the mean and compare it to the text ( = 98.20) as a
secondary method of ensuring you entered the data correctly.
temperature <- c(98.4, 98.6, 98.8, 98.8, 98.0, 97.9, 98.5, 97.6,
98.4, 98.3, 98.9, 98.1, 97.3, 97.8, 98.4, 97.4)
mean( temperature )
Checking data for necessary conditions.
To use a one-sample t-test on a small sample, the population should be normally distributed. We will use
a boxplot to inspect for severe skewness or extreme outliers. Enter the following R boxplot command to
get the graph shown below: boxplot( temperature, main="body temperatures" ).
The boxplot is symmetric and shows no extreme outliers, thus suggesting a t-test is appropriate.
Performing a one-sided one-sample t-test.
The R function t.test() is straight forward. After reading help(t.test), we see that the
appropriate input for the alternative option is “less” since the alternative hypothesis is μ < 98.6 .
We also need to specify μ0 = 98.6 using mu=98.6 as an option in t.test() function. The results
show that, with a p-value of 0.0029, there is statistically significant evidence that the mean population
body temperature is less than 98.6 degrees.
> t.test( temperature, alternative="less", mu=98.6 )
One Sample t-test
data: temperature
t = -3.2215, df = 15, p-value = 0.002853
alternative hypothesis: true mean is less than 98.6
95 percent confidence interval:
-Inf 98.41767
sample estimates:
mean of x
98.2
We could have explicitly done the steps for calculating the t-statistic and then determining the area
beneath the t-distribution curve to the left of the test statistic. The results are the same, but the number of
R commands is more. The commands for you to type and the resulting t-statistic and p-value follow.
> ( mean(temperature) - 98.6 ) / ( sd(temperature)/sqrt(16) )
[1] -3.221549
> pt( -3.221549, df=15 )
[1] 0.002853201
Example 13.2: The effect of alcohol on useful consciousness
The data involves a paired t-test to investigate whether the number of seconds a pilot is able to perform a
task while deprived of adequate oxygen is less when under the influence of alcohol (alcohol) than
when alcohol free (sober). A paired t-test is essentially equivalent to a one-sample t-test, but is
performed on the paired differences. As with Example 13.1, you will have to enter the data yourself. You
will calculate the paired differences, check the data for the necessary conditions, and then perform a t-test.
Two ways of using t.test() will be demonstrated. The first method has t.test()calculate the
differences for you by your specifying the paired=T option. The second method simply has you input
the differences yourself and treat it as a one-sample t-test. The results are the same.
Entering the data.
You are advised to type the following commands first into Notepad or similar program and then copyand-paste them into R. This is so you can correct any mistakes. To check your accuracy, the mean of the
differences should
be 195.6.
sober <- c( 261, 565, 900, 630, 280, 365, 400, 735, 430, 900)
alcohol <- c( 185, 375, 310, 240, 215, 420, 405, 205, 255, 900)
difference <- sober - alcohol
mean( difference )
Checking the data for necessary conditions.
With a paired t-test, it is the population of differences that need to satisfy the normality conditions of a
one-sample t-test if the number of sampled pairs is small. A boxplot is an appropriate tool to check that
the data are not excessively skewed or has extreme outliers. The following R command produces the
boxplot displayed below showing that skewness is not severe nor are there extreme outliers.
boxplot( difference, main="sober-alcohol time" )
Performing the paired t-test
Now that we have calculated the paired differences and inspected them for the necessary conditions for a
t-test, we can perform a paired t-test. The first method has t.test()calculate the differences for you by
your specifying the paired=T option. The second method simply has you input the differences
yourself and treat it as a one-sample t-test. The results are the same with a t-statistic of 2.6832 and a pvalue of 0.013, suggesting there is statistically significant evidence that alcohol reduces pilots’ mean time
of functioning when deprived of oxygen. Both commands and their output follow.
> t.test( sober, alcohol, alternative="greater", paired=T, mu=0 )
Paired t-test
data: sober and alcohol
t = 2.6832, df = 9, p-value = 0.01254
alternative hypothesis:true difference in means is greater than 0
95 percent confidence interval:
61.96809 Inf
sample estimates:
mean of the differences
195.6
> t.test( difference, alternative="greater", mu=0 ) # equivalent
One Sample t-test
data: difference
t = 2.6832, df = 9, p-value = 0.01254
alternative hypothesis: true mean is greater than 0
95 percent confidence interval:
61.96809 Inf
sample estimates:
mean of x
195.6
Example 13.4: The effect of a stare on driving behavior.
Does a stare encourage a driver to cross though an intersection more quickly? The data are provided in
Example11.11. If necessary data conditions are satisfied, an unpooled two-sample t-test would be
appropriate. After you enter the data and use a boxplot to check the data to make sure there is not severe
skewness nor extreme outliers, the t.test() function will be put to use again.
Entering the data.
The data can be found in Example 11.11. To ensure you entered the data correctly, calculate the sample
means and standard deviations to see that for the No-stare data the mean and sd are 6.628571 and
1.362367 respectively. For the Stare data, they are 5.592308 and 0.8220518. The R commands are:
nostare<-c(8.3,5.5,6,8.1,8.8,7.5,7.8,7.1,5.7,6.5,4.7,6.9,5.2,4.7)
stare <- c(5.6,5,5.7,6.3,6.5,5.8,4.5,6.1,4.8,4.9,4.5,7.2,5.8 )
mean( nostare ); sd( nostare )
mean( stare ); sd( stare )
The semicolons in the last two lines of R code simply allow for more than one command to be typed on a
single line.
Checking for necessary data conditions.
Again, with small sample sizes, the t-test depends upon the populations being normally distributed. To
check for this, we will inspect a boxplot of the data for severe skewness or extreme. This time, however,
we add the option horizontal=T to have horizontal boxplots. Having the boxplots horizontal or
vertical is purely a personal choice and does not make a difference in interpretation of the boxplots. The R
command follows and the resulting boxplot graph. Lacking severe skewness or outliers suggests that a
two-sample t-test is valid.
boxplot( stare, nostare, names=c("Stare", "No stare"),
+
horizontal=T )
Performing the unpooled two-sample t-test.
The function t.test()can be used for two-sample as well as one-sample t-tests. For an unpooled twosample t-test, simply provide it the vector for each sample. (You could have specified var.equal=F;
however, that is the default.) Do not forget to specify that this is a one-sided t-test where we are testing
that the mean for the not-stared-at drivers will be greater than the stared-at driver. The R command
follows and the resulting output.
> t.test( nostare, stare, alternative="greater" )
Welch Two Sample t-test
data: nostare and stare
t = 2.4122, df = 21.596, p-value = 0.01241
alternative hypothesis:true difference in means is greater than 0
95 percent confidence interval:
0.297971 Inf
sample estimates:
mean of x mean of y
6.628571 5.592308
We see that the crossing time is statistically significantly quicker for the drivers who were stared at
(pvalue= 0.01241). The degrees-of-freedom are not an integer because we did not assume the two
populations to have equal variances.
Example 13.7: Legitimate pooled t-test for comparing male and female sleep time.
In Example 11.14, the mean sleep time was compared for male and female non-liberal art students at UC
Davis. Students from a Stat13 section were used. The data are found in UCDavis1.RData. After
importing the dataset and extracting the relevant data, we see that sample sizes are large ( 65 males, 83
females), thus we do not need to worry about normality. The variances are also similar (2.83, 3.08), thus
we can reasonably to a pooled two-sample t-test and assume equal variances. (See following R commands
and resulting output.)
> ucdavis1 <- load("C:/RData/UCDavis1.RData")
> ucdavis1 <- edit( ucdavis1 )
> names( ucdavis1 )
[1] "Sex"
"t"
"c"
"Sleep" "s"
"a"
"h"
[10] "e"
"g"
"Class"
> sleepmale <- Sleep[ Class=="N" & Sex=="M"]
> sleepfemale <- Sleep[ Class=="N" & Sex=="F"]
> length(sleepmale)
[1] 65
> length(sleepfemale)
[1] 83
> var(sleepmale)
[1] 2.829087
> var(sleepfemale)
[1] 3.079801
"m"
"d"
Performing a pooled t-test, we need to specify that the variances are equal using the var.equal=T
option. The default is False and performs an unpooled two-sample t-test. The output for the pooled
method follows:
> t.test( sleepmale, sleepfemale, alternative="two.sided",
+
var.equal=T)
Two Sample t-test
data: sleepmale and sleepfemale
t = -1.6158, df = 146, p-value = 0.1083
alternative hypothesis:true difference in means is not equal to 0
95 percent confidence interval:
-1.0253307 0.1029025
sample estimates:
mean of x mean of y
6.553846 7.015060
The result is a p-value of 0.1083 suggesting the difference between male and female sleep times is not
statistically significant. Out of curiosity, let us perform an unpooled two-sample t-test to see if the results
differ.
> t.test(sleepmale, sleepfemale, alternative="two.sided",
+
var.equal=F )
Welch Two Sample t-test
data: sleepmale and sleepfemale
t = -1.6243, df = 140.142, p-value = 0.1066
alternative hypothesis:true difference in means is not equal to 0
95 percent confidence interval:
-1.0226034 0.1001752
sample estimates:
mean of x mean of y
6.553846 7.015060
The p-value for the unpooled method is 0.1066 which is almost the same as the pooled method. This is a
result of the sample variances already almost being equal.
Table 13.4: Power based on true effect size values for one-sided, one-sample t-test.
The R function power.t.test() does power calculations for one and two-sample t tests. (Although
not discussed here, the function power.prop.test()does power calculations for two-sample tests of
proportions.) Exactly one of the parameters n (sample size), delta (effect size), power, sd
(standard deviation), and sig.level (significance level, α) needs to be NULL so that R will calculate
the NULL parameter. You also need to specify that it is a one-sample test and one-sided. For example,
the upper most right power value of Table 13.4 can be calculate with the following R command. The
output follows.
> power.t.test( n=20, delta=.1, sd=1, sig.level=.05, power=NULL,
+
type="one.sample", alternative="one.sided" )
One-sample t test power calculation
n
delta
sd
sig.level
power
alternative
=
=
=
=
=
=
20
0.1
1
0.05
0.1124921
one.sided
You can also provide one of the parameters as a vector to get more than one response per a command. By
putting the effect size into a vector, the following three power.t.test() functions reproduce Table
13.4. The commands and output follow.
> effectsize <- c(.1, .2, .5, .8)
> power.t.test( n=20, delta=effectsize, sd=1, power=NULL,
+
type="one.sample", alt="one.sided" )
One-sample t test power calculation
n
delta
sd
sig.level
power
alternative
=
=
=
=
=
=
20
0.1, 0.2, 0.5, 0.8
1
0.05
0.1124921, 0.2170752, 0.6951493, 0.9641728
one.sided
>
> power.t.test( n=50, delta=effectsize, sd=1, power=NULL,
+
type="one.sample", alt="one.sided" )
One-sample t test power calculation
n = 50
delta = 0.1, 0.2, 0.5, 0.8
sd = 1
sig.level = 0.05
power = 0.1716967, 0.4012215, 0.9672067, 0.9999579
alternative = one.sided
>
> power.t.test( n=100, delta=effectsize, sd=1, power=NULL,
+
type="one.sample", alt="one.sided" )
One-sample t test power calculation
n
delta
sd
sig.level
power
alternative
=
=
=
=
=
=
100
0.1, 0.2, 0.5, 0.8
1
0.05
0.2573029, 0.6336178, 0.9995509, 1.0000000
one.sided
© Copyright 2026 Paperzz