Use R for calculations and plots. Show the code and output needed to support your answers, and use complete sentences for interpretations. Give confidence intervals in context and identify the order of subtraction whenever you compare two groups. Use two-sided tests unless otherwise stated. Keep intermediate calculations unrounded; round reported answers sensibly.
library(dplyr)
library(ggplot2)
knitr::opts_chunk$set(message = FALSE, warning = FALSE)
theme_set(theme_bw())
For this constructed example, battery life for a model of wireless headphones has a normal distribution with true mean 10 hours. We specify the true mean so that we can identify correct decisions and errors. The researchers estimate the population SD using their sample SDs.
Two researchers collect samples with the following summaries:
| Researcher | Sample size | Sample mean (hours) | Sample SD (hours) |
|---|---|---|---|
| Vixie | 20 | 9.90 | 2.10 |
| Tayo | 80 | 9.90 | 1.90 |
Part A For each researcher, calculate SE and the
t-statistic for each of the claims \(H_0:\mu=10\) and \(H_0:\mu=9.2\). Use qt() to
find the appropriate 95% critical values and decide whether to reject
each claim at \(\alpha=.05\). Report
the SE, t-statistic, and decision for each researcher and each
claim.
Part B Because the true population mean is known here, identify each decision in Part A as correct, a Type I error, or a Type II error. Can both types of error occur when testing the same fixed claim about this population? Explain.
Part C We now repeat the sampling rather than
keeping the observed sample mean and SD fixed. The code below generates
1,000 samples at each sample size. Each row of trials
records one sample and the p-values for testing the claims 10, 9.2, and
8 hours using that same sample. The third claim is for Part F. Each test
uses the sample SD to estimate SE. Copy and paste the code below into
your document and run it to create the simulations.
set.seed(20905)
B <- 1000
simulate_samples <- function(N) {
results <- replicate(B, {
x <- rnorm(N, mean = 10, sd = 2)
xbar <- mean(x)
SE <- sd(x) / sqrt(N)
c(xbar = xbar,
p_10 = 2 * pt(-abs((xbar - 10) / SE), df = N - 1),
p_92 = 2 * pt(-abs((xbar - 9.2) / SE), df = N - 1),
p_8 = 2 * pt(-abs((xbar - 8) / SE), df = N - 1))
})
data.frame(n = N, t(results))
}
trials <- bind_rows(simulate_samples(20), simulate_samples(80))
Create histograms of xbar, using
facet_wrap(~ n) to separate the sample sizes. What does one
observation represent? Compare the centers and spreads. Which quantities
stayed fixed as sample size increased, and which varied from sample to
sample?
Part D For each sample size, find the fraction of
p-values below .05 for the claims \(H_0: \mu =
10\) and \(H_0: \mu = 9.2\)
hours. You might use ifelse() to label each result as
“reject” or “fail to reject”, then table() to count the
decisions.
Rejections of the claim \(H_0: \mu =
10\) (p_10) estimate the Type I error rate;
rejections of the claim \(H_0: \mu
= 9.2\) (P_92) estimate power. Explain why. Use
your results to explain what increasing sample size does to each.
Part E For the claim 9.2, calculate the estimated Type II error rate at each sample size. Then repeat the rejection-rate summaries using a threshold of .01 for the claims 10 and 9.2 hours. Explain the effect of lowering alpha on false positives, Type II errors, and power, holding the population and sample size fixed. Does the larger sample change the size of the true difference from 9.2 hours?
Part F Keep \(n=20\) and compare the claims \(H_0:\mu=9.2\) and \(H_0:\mu=8\). What is the true effect size,
\(\mu-\mu_0\), for each claim? For the
claim 8, use p_8 to estimate power at \(\alpha=.05\) and \(\alpha=.01\). Compare these with your
results for the claim 9.2. For which effect does lowering alpha cause
the larger loss of power? Explain in terms of the sampling distribution
and rejection boundaries. Does the larger effect make the Type I error
rate at a fixed alpha smaller?
A food-safety inspector tests the claim that a restaurant meets sanitation standards. Rejecting that claim leads to its license being revoked.
Part A Describe a Type I and a Type II error in this context. Explain why the owner of a compliant restaurant and a diner at a noncompliant restaurant might prefer different evidence thresholds.
Across 200 inspections, suppose exactly 100 restaurants meet the standards and 100 do not. The procedure has a Type I error rate of 5% and power of 80% for the noncompliant restaurants in this example.
Part B Give the expected number of inspections in each of the following categories: a compliant restaurant loses its license; a compliant restaurant keeps its license; a noncompliant restaurant loses its license; and a noncompliant restaurant keeps its license. These are expected counts, not guaranteed outcomes. Also give the Type II error rate.
Part C Using the expected counts, what fraction of revoked licenses belong to restaurants that actually meet standards? Why is this fraction different from 5%? Identify the denominator used in each calculation.
In a separate study, an investigator randomly selects 100 restaurants
in Des Moines. Eighteen fail a sanitation inspection. Treat the outcomes
as independent observations and assume the conditions for
prop.test() are satisfied. Let \(p_{\mathrm{DM}}\) denote the population
proportion of restaurants in Des Moines that would fail an
inspection.
Part A Report the number inspected, the number that failed, and the observed proportion. Describe what the proportion represents in this setting.
Part B State the null and alternative hypotheses for
the claim that 10% of restaurants in Des Moines would fail inspection.
Use prop.test() to test this claim at \(\alpha=.05\). Report the p-value and 95%
confidence interval, and explain whether they give the same
decision.
Part C State the conclusion in context. If this decision is an error, which type would it be? Do the sample results establish whether an error actually occurred? Explain what additional information would be needed.
An independent random sample of 100 restaurants in Iowa City finds that 11 fail inspection. Use the Des Moines sample from Problem 3 and this new sample to investigate whether the cities differ. Define the difference as Des Moines minus Iowa City.
Part A Report the proportion of Iowa City restaurants that failed inspection. Calculate the observed difference between the cities in percentage points.
Part B State the null and alternative hypotheses.
Use prop.test() to compare the cities at \(\alpha=.05\), entering Des Moines first and
Iowa City second. Report the p-value and 95% confidence interval for the
difference. Give your conclusion and explain what the interval says
about the direction and plausible size of the population difference.
Part C Use prop.test() to test the
claim that 10% of Iowa City restaurants would fail inspection. A
classmate argues, “Des Moines differs significantly from 10%, and Iowa
City does not, so the two cities must differ significantly.” Does your
direct comparison support this reasoning? Explain why the two individual
tests do not answer the same question as Part B.
The cow-growth dataset records pre-treatment and post-treatment weights for 72 cows in Control, Testosterone, and bST groups. For this problem, compare only bST and Control. Define each animal’s weight change as post minus pre, and define the group comparison as mean bST change minus mean Control change. Treat cows as independent observations within and between groups. Report weights in the dataset’s recorded weight units.
cow <- read.csv("https://collinn.github.io/data/cowgrowth.csv")
Part A Create a variable called Change
containing each cow’s weight change and retain only the bST and Control
groups. Save this dataset as cow_compare. Report each
group’s sample size, mean change, and SD. Make a boxplot comparing the
individual weight changes. Is the boxplot alone enough to conclude that
the population mean weight changes differ? Explain.
Part B State the null and two-sided alternative in
terms of the two population mean changes. Use cow_compare
and t.test() to obtain a test and 95% interval for their
difference. Ensure that the reported difference is bST minus Control;
identify which mean is first in R’s output. Report the estimated
difference, interval, t-statistic, degrees of freedom, and p-value.
Part C At \(\alpha=.05\), what do you conclude? Interpret the interval as a difference in average changes, not simply a difference in final weights. Does statistical significance alone establish that the difference is large enough to be useful?
Part D The pre and post measurements belong to the same cow, but the bST and Control groups contain different cows. Explain why calculating an individual change is appropriate and why the comparison of those changes across groups is an independent two-sample comparison rather than a paired comparison between groups.
Suppose the investigator planned all three pairwise comparisons among the cow-treatment groups. The unadjusted two-sided p-values are shown below. You do not need to rerun the three tests.
| Comparison | Unadjusted p-value |
|---|---|
| bST - Control | 0.0022 |
| Testosterone - Control | 0.1280 |
| bST - Testosterone | 0.0485 |
Part A Which comparisons reject at an unadjusted .05 threshold? Apply the Bonferroni adjustment to control the family-wise error rate at .05. Give the adjusted threshold and state which conclusions change.
Part B Why does the adjustment use three tests even though Problem 5 asked you to compute only one? Would it be appropriate to choose the family after inspecting which p-values were small?
Part C Relate the Bonferroni adjustment to the power tradeoff in Problem 1. Why does lowering the p-value cutoff for rejection require stronger evidence? Holding sample size, population variability, and the true effect fixed, explain what happens to power and the Type II error rate for each comparison. Does a comparison becoming nonsignificant after adjustment establish that the two population means are equal?
Suggested functions: mutate(), filter(),
group_by(), summarize(), n(),
ifelse(), table(),
proportions().