Note: This document was converted to R-Markdown from this page by M. Drew LaMar. You can download the R-Markdown here.

Download the R code on this page as a single file here

New methods

Hover over a function argument for a short description of its meaning. The variable names are plucked from the examples further below.

One-sample \(t\)-test:

t.test(heat$temperature, mu = 98.6)

Other new methods:

Confidence intervals for the variance and the standard deviation.

Example 11.2. Stalk-eyed flies

Confidence intervals for the population mean, variance, and standard deviation using eye span measurements from a sample of stalk-eyed flies.

Read and inspect the data.

stalkie <- read.csv(url("http://whitlockschluter.zoology.ubc.ca/wp-content/data/chapter11/chap11e2Stalkies.csv"))
stalkie
##   individual eyespan
## 1          1    8.69
## 2          2    8.15
## 3          3    9.25
## 4          4    9.45
## 5          5    8.96
## 6          6    8.65
## 7          7    8.43
## 8          8    8.79
## 9          9    8.63

Histogram with options

hist(stalkie$eyespan, right = FALSE, col = "firebrick", las = 1,xlab = "Eye span (mm)", ylab = "Frequency", main = "")

95% confidence interval for the mean. Adding $conf.int after the function t.test causes R to give the 95% confidence interval for the mean.

t.test(stalkie$eyespan)$conf.int
## [1] 8.471616 9.083940
## attr(,"conf.level")
## [1] 0.95

99% confidence interval for the mean. Adding the argument conf.level=0.99 changes the confidence level of the confidence interval.

t.test(stalkie$eyespan, conf.level = 0.99)$conf.int
## [1] 8.332292 9.223264
## attr(,"conf.level")
## [1] 0.99

95% confidence interval for variance. R has no built-in function for the confidence interval of a variance, so must we compute it using the formula in the book:

df <- length(stalkie$eyespan) - 1
varStalkie <- var(stalkie$eyespan)
lower = varStalkie * df / qchisq(0.05/2, df, lower.tail = FALSE)
upper = varStalkie * df / qchisq(1 - 0.05/2, df, lower.tail = FALSE)
c(lower = lower, variance = varStalkie, upper = upper)
##      lower   variance      upper 
## 0.07238029 0.15864444 0.58225336

95% confidence interval for standard deviation. Calculated from the confidence interval of the variance, which we just calculated above.

c(lower = sqrt(lower), std.dev = sqrt(varStalkie), upper = sqrt(upper))
##     lower   std.dev     upper 
## 0.2690359 0.3983020 0.7630553

Example 11.3. Human body temperature

Uses a one-sample \(t\)-test to compare body temperature in a random sample of people with the “expected” temperature 98.6\(^\circ\)F.

Read and inspect the data.

heat <- read.csv(url("http://whitlockschluter.zoology.ubc.ca/wp-content/data/chapter11/chap11e3Temperature.csv"))
head(heat)
##   individual temperature
## 1          1        98.4
## 2          2        98.6
## 3          3        97.8
## 4          4        98.8
## 5          5        97.9
## 6          6        99.0

Histogram with options.

hist(heat$temperature, right = FALSE, breaks = seq(97, 100.5, by = 0.5), 
     col = "firebrick", las = 1, xlab = "Body temperature (degrees F)", 
     ylab = "Frequency", main = "")

One-sample \(t\)-test can be calculate using t.test. The mu argument gives the value stated in the null hypothesis.

t.test(heat$temperature, mu = 98.6)
## 
##  One Sample t-test
## 
## data:  heat$temperature
## t = -0.56065, df = 24, p-value = 0.5802
## alternative hypothesis: true mean is not equal to 98.6
## 95 percent confidence interval:
##  98.24422 98.80378
## sample estimates:
## mean of x 
##    98.524