---
title: "Inference for a normal population: R code for Chapter 11 examples"
author: "Michael Whitlock and Dolph Schluter"
output: 
  html_document:
    toc: yes
    toc_depth: 3
---

_Note: This document was converted to R-Markdown from [this page](http://whitlockschluter.zoology.ubc.ca/r-code/rcode11) by M. Drew LaMar.  You can download the R-Markdown [here](https://qubeshub.org/collections/post/1279/download/chap11.Rmd)._

Download the R code on this page as a single file [here](http://whitlockschluter.zoology.ubc.ca/wp-content/rcode/chap11.r)

## 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(<u><a title="The name of a vector of the numerical variable, measured on all individuals in the sample.">heat$temperature</a></u>, <u><a title="Specifies the mean stated by the null hypothesis.">mu = 98.6</a></u>)

**Other new methods:**

Confidence intervals for the variance and the standard deviation.

## Example 11.2. [Stalk-eyed flies](http://whitlockschluter.zoology.ubc.ca/wp-content/data/chapter11/chap11e2Stalkies.csv)

*__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.

```{r}
stalkie <- read.csv(url("http://whitlockschluter.zoology.ubc.ca/wp-content/data/chapter11/chap11e2Stalkies.csv"))
stalkie
```

**Histogram** with options

```{r, fig.width=4, fig.height=4}
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.

```{r}
t.test(stalkie$eyespan)$conf.int
```

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

```{r}
t.test(stalkie$eyespan, conf.level = 0.99)$conf.int
```

**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:

```{r}
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)
```

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

```{r}
c(lower = sqrt(lower), std.dev = sqrt(varStalkie), upper = sqrt(upper))
```

## Example 11.3. [Human body temperature](http://whitlockschluter.zoology.ubc.ca/wp-content/data/chapter11/chap11e3Temperature.csv)

*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.

```{r}
heat <- read.csv(url("http://whitlockschluter.zoology.ubc.ca/wp-content/data/chapter11/chap11e3Temperature.csv"))
head(heat)
```

**Histogram** with options.

```{r, fig.width=4, fig.height=4}
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.

```{r}
t.test(heat$temperature, mu = 98.6)
```