## Copy and paste the code chunk below into the top of your R Markdown document
library(ggplot2)
library(dplyr)
theme_set(theme_bw())
## Penguin data
penguins <- read.csv("https://collinn.github.io/data/penguins.csv")
This lab is intended to serve as an opportunity to practice some old skills and learn some new ones. In particular, our goals are to practice using:
dplyr to create variables, subset data, and find
summariesggplot to visualize the observations we are working
withR as a calculatort.test() to construct confidence intervals and test
hypothesesThe statistics here are the same ones we have been working with in class. The new part is asking R to do some of the calculations for us, then making sense of what it gives back.
Create your own R Markdown document with your code, output, plots, and brief answers. You do not need to copy the questions. Include units when reporting an estimate or interval, and explain your conclusions in the context of the problem. Bullet points are fine.
Plan to work through Problems 1–4 first. Problem 5 is an extension; we can return to unfinished work on Monday. Keep your critical value sheet nearby. You will use it before asking R for an exact p-value.
There are many ways to go about testing hypotheses and constructing
confidence intervals in R. Here we will explore two. The first involves
a manual construction: we determine the standard error, find the
critical value on our sheet, and compute the associated values. While
this is a bit more laborious, it lets us save or validate our pieces as
we go. The second method introduces t.test(), a function
built into R that performs the calculations for us.
For this lab, all hypothesis tests ask whether a population mean differs from a hypothesized value. These are two-sided tests: observations unusually far above or below the hypothesized mean count as evidence against the null.
This may be obvious, but it is worthwhile understanding convenient ways to use R as a calculator. Rather than putting every calculation into one long expression, we can assign names to the pieces.
For example, the built-in mtcars dataset includes fuel
economy in miles per gallon. Suppose we want to test \(H_0:\mu=18\) for mean fuel economy.
## Save the pieces
xbar <- mean(mtcars$mpg)
s <- sd(mtcars$mpg)
n <- length(mtcars$mpg)
mu0 <- 18
## Estimated standard error
se <- s / sqrt(n)
## Test statistic
t_stat <- (xbar - mu0) / se
t_stat
## [1] 1.9622
The parentheses matter: we want the entire difference \(\overline{x}-\mu_0\) divided by the standard error.
We can also use our critical value sheet to find the positive 90% critical value for \(df=31\), then construct an interval:
C <- 1.70
xbar + c(-1, 1) * C * se
## [1] 18.279 21.902
Here, c(-1, 1) tells R to subtract the margin of error
once and add it once, giving us the lower and upper endpoints.
In R, we can perform hypothesis tests and create standard confidence
intervals based on the t-distribution using t.test(). While
the function can take a number of useful arguments, for now we only
concern ourselves with three:
x: a numeric vector containing the individual
observationsmu: the hypothesized population mean, \(\mu_0\)conf.level: the confidence level, expressed as a
decimalThe x argument takes the observations themselves,
not their sample mean and not a table
of group means.
t.test(x = mtcars$mpg,
mu = 18,
conf.level = 0.90)
##
## One Sample t-test
##
## data: mtcars$mpg
## t = 1.96, df = 31, p-value = 0.059
## alternative hypothesis: true mean is not equal to 18
## 90 percent confidence interval:
## 18.284 21.897
## sample estimates:
## mean of x
## 20.091
From the output, note a few things:
mu.The confidence level and error rate are related by \(\alpha=1-\text{confidence level}\). For example, a 90% confidence interval corresponds to a test at \(\alpha=0.10\).
If you leave out conf.level, R uses 0.95. If you leave
out mu, R tests against a population mean of
zero. That is not usually the question we intend to
ask, so specify both arguments in this lab. The default alternative is
two-sided, which is what we want here.
See ?t.test() for more information.
The reference labs are dplyr and ggplot. Some reminders:
| Function | What it does |
|---|---|
filter() |
Keeps observations that meet a condition |
mutate() |
Creates or transforms variables |
group_by() |
Establishes groups for the following operations |
summarize() |
Calculates summaries, usually one row per group |
arrange() |
Orders the rows |
Use %>% to connect operations. Within
summarize(), n() counts rows. The functions
mean() and sd() calculate a mean and standard
deviation. For plots, start with ggplot(data, aes(...)) and
add the appropriate geometry with +.
We have used these penguins before. Each row describes one penguin, and this curated dataset has no missing values. Some variables we will use are:
| Variable | Meaning |
|---|---|
species |
Adelie, Chinstrap, or Gentoo |
sex |
female or male |
bill_length_mm |
Bill length in millimeters |
flipper_length_mm |
Flipper length in millimeters |
body_mass_g |
Body mass in grams |
For this lab, we will treat the penguin dataset as a sample from a larger population rather than as the population itself.
Part A Use mutate() to add a
variable called body_mass_kg to penguins. Then
create a summary with one row per species giving the number of penguins,
mean bill length, SD of bill length, and mean body mass in kilograms.
Save this summary under a new name so that penguins still
contains the individual observations. Arrange the table from largest to
smallest mean bill length. Which species has the greatest mean bill
length? Does that tell you which species has the greatest mean body
mass?
Part B Create a boxplot comparing bill
lengths across species. Use the individual observations in
penguins, rather than the summary table. Include axis
labels with units. Briefly describe what you see: how do the groups
differ in center and spread?
Part C Use filter() to create a
dataset called gentoo containing only Gentoo penguins.
Create a histogram of their bill lengths. What does one observation in
this histogram represent? Is this a sampling distribution of sample
means? Explain briefly.
The following code takes 20 observations from your
gentoo dataset that was created in Problem 1.
set.seed() makes the random selection repeatable: if we
start with the same rows in the same order, we will select the same
penguins. Copy and paste this code into your own Rmd file.
## Controls the randomness in the sampling step
set.seed(123)
## Sample 20 row numbers
idx <- sample(seq_len(nrow(gentoo)), size = 20)
## Keep the selected observations
gentoo_sample <- gentoo[idx, ]
For this problem, test whether the population mean bill length differs from 45.7 mm:
\[ H_0:\mu=45.7,\qquad H_A:\mu\ne45.7. \]
Part A Using gentoo_sample,
calculate and save \(n\), \(\overline{x}\), \(s\), \(SE=s/\sqrt{n}\), and \(t=(\overline{x}-45.7)/SE\). You may use
summarize() or assign the quantities separately as we did
in the calculator section above. Explain the difference between what
\(s\) describes and what \(SE\) describes.
Part B Before using t.test(), find
the 90% and 95% critical values on your sheet. Between which critical
values does \(|t|\) fall? Give bounds
on its p-value. What decision would you make at \(\alpha=0.10\)? What about \(\alpha=0.05\)?
Part C Use t.test() with the
20 individual bill lengths, mu = 45.7, and
conf.level = 0.90. Report the sample mean, t-statistic, df,
exact p-value reported by R, and 90% confidence interval. Does the
p-value fall within your bounds? Does the interval give the same
decision as your test at \(\alpha=0.10\)?
Part D Interpret the p-value in the context of penguin bill lengths. Does it tell us the probability that \(\mu=45.7\) is true?
Keep the same 20 observations and the same null hypothesis, \(H_0:\mu=45.7\). Do not take another sample.
Part A Before running anything else, predict
what will happen when you change conf.level from 0.90 to
0.95. Which of these should change: the sample mean, estimated SE,
t-statistic, df, p-value, and confidence interval?
Part B Run the test again with
conf.level = 0.95. Make a small comparison table with the
confidence level, corresponding alpha, p-value, CI endpoints, and
decision. Why can the decision change even though the data and p-value
do not? Does failing to reject at \(\alpha=0.05\) establish that the null is
true?
Keep the same 20 observations, but now consider \(H_0:\mu=47.5\) against \(H_A:\mu\ne47.5\).
Part A Use t.test() to test this
hypothesis with conf.level = 0.90. Compare the sample mean,
t-statistic, p-value, and CI with the original test of \(\mu=45.7\) at 90% confidence. Which
quantities changed? Why did the CI stay the same?
Part B Against which hypothesized mean does the sample provide stronger evidence? Explain using the p-values and the distances from \(\overline{x}\) in standard errors. Can you compare the magnitudes of these t-statistics directly here? Why?
Part C Write a conclusion for the test of \(\mu=47.5\) at \(\alpha=0.10\). Identify the population quantity and give the evidence supporting your decision. Do not describe the conclusion as proving the null true or false.
Return to penguins, including the
body_mass_kg variable you created. Use
filter() to select female Adelie penguins
and give the resulting dataset a useful name. Consider a claim that the
population mean body mass for female Adelie penguins is 3.4
kg.
Part A Use summarize() to report
the sample size, mean body mass, SD, and estimated SE. Make a histogram
or boxplot of the individual body masses in kilograms
and describe it briefly.
Part B State the null and two-sided alternative,
then use t.test() at 95% confidence to assess the claim.
Give the interval, p-value, decision, and a conclusion in
context.
Part C Someone accidentally tests against
mu = 3400 after converting the measurements to kilograms.
What went wrong? If both the data and hypothesized mean were instead
expressed correctly in grams, would t, p, or the decision change? What
would happen to the numerical CI endpoints?
This is the same workflow you will use when exploring a question of your own: identify the observations and variables, organize and summarize the data, make a useful plot, and explain what the analysis says in context.