library(dplyr)
library(ggplot2)
load(url(
"https://raw.githubusercontent.com/GarciaRios/govt_3990/gh-pages/Labs/lab5/nc.RData"
))Lab 7: Inference for Numerical Data
Hypothesis testing and confidence intervals
Download the student lab template
North Carolina births
In 2004, the state of North Carolina released a large data set containing information on births recorded in the state. These data are useful for researchers and policymakers interested in maternal and infant health.
We will work with a random sample of observations from this data set.
The data include the following variables:
| Variable | Description |
|---|---|
fage |
Father’s age in years |
mage |
Mother’s age in years |
mature |
Maternal age category |
weeks |
Length of pregnancy in weeks |
premie |
Premature or full-term birth |
visits |
Number of prenatal visits |
marital |
Mother’s marital status at birth |
gained |
Weight gained during pregnancy |
weight |
Birth weight in pounds |
lowbirthweight |
Low birth weight classification |
gender |
Baby’s gender |
habit |
Mother’s smoking status |
whitemom |
Whether mother was coded as White or not White |
Take a quick look at the data:
glimpse(nc)Exploratory analysis
Consider the possible relationship between a mother’s smoking status and the birth weight of her baby.
A graph is a useful first step.
ggplot(nc, aes(x = habit, y = weight)) +
geom_boxplot() +
labs(
x = "Mother's smoking status",
y = "Birth weight (pounds)",
title = "Birth weight by maternal smoking status"
) +
theme_minimal()We can also compare the group means:
nc |>
group_by(habit) |>
summarise(
mean_weight = mean(weight, na.rm = TRUE),
sd_weight = sd(weight, na.rm = TRUE),
n = sum(!is.na(weight))
)What are the cases in this data set? Approximately how many cases are in the sample?
What does the boxplot suggest about the relationship between smoking status and birth weight?
How do the average birth weights of the two groups compare?
There is an observed difference. The next question is whether that difference is large enough to be difficult to explain by sampling variability alone.
Inference
Before conducting a hypothesis test, consider whether the conditions for inference are reasonably satisfied.
nc |>
count(habit)- Are the conditions for comparing the two population means reasonably satisfied? Comment briefly on independence, sample size, and the shape of the distributions.
Now write the hypotheses for testing whether average birth weight differs between babies born to smoking and nonsmoking mothers.
Let
[ _N = ]
and
[ _S = . ]
- Write the null and alternative hypotheses.
The null hypothesis should represent no difference between the two population means.
Conduct the hypothesis test
We can conduct the test directly with t.test():
birthweight_test <- t.test(
weight ~ habit,
data = nc,
alternative = "two.sided",
conf.level = 0.95
)
birthweight_testRecord the two sample means, the test statistic, and the p-value.
Using a significance level of (= 0.05), do you reject or fail to reject the null hypothesis?
Interpret the conclusion in the context of birth weight and smoking status.
A small p-value provides evidence against the null hypothesis. It does not tell us the probability that the null hypothesis is true.
Confidence interval
The output above also reports a 95% confidence interval for the difference between the two population means.
Record the confidence interval.
Does the interval contain 0? Does this agree with your conclusion from the hypothesis test?
Interpret the interval in context.
A two-sided test at (= 0.05) and a 95% confidence interval should lead to the same inferential conclusion.
From statistical evidence to policy interpretation
Suppose a public-health agency is deciding whether smoking during pregnancy deserves attention in maternal-health programming.
The analysis above can tell us whether birth weight differs across the two groups in this sample and whether the difference is statistically distinguishable from zero.
It cannot, by itself, prove that smoking caused the difference. These are observational data, and the two groups may differ in other ways that are related to birth weight.
Name one or two possible confounding variables that could matter here.
In 2–3 sentences, summarize what this analysis contributes to a policy discussion and what it does not establish.
Group analysis
Now apply the same workflow to a new question.
Each group will analyze one relationship and prepare a two-minute report for the class.
Group A: Smoking and pregnancy length
Do mothers who report smoking and nonsmoking mothers have different average pregnancy lengths?
- response:
weeks - group:
habit
Group B: Maternal age and prenatal care
Does the average number of prenatal visits differ between younger and mature mothers?
- response:
visits - group:
mature
Group C: Racial disparities in prenatal care
Does the average number of prenatal visits differ between mothers coded as White and mothers coded as not White?
- response:
visits - group:
whitemom
This analysis identifies a disparity. It does not establish that race itself causes the difference.
Group D: Marital status and birth weight
Does average birth weight differ according to the mother’s marital status at birth?
- response:
weight - group:
marital
Group E: Maternal age and pregnancy weight gain
Does average pregnancy weight gain differ between younger and mature mothers?
- response:
gained - group:
mature
What each group should do
- Create one useful visualization.
- Calculate the mean, standard deviation, and sample size for each group.
- State (H_0) and (H_A).
- Conduct the two-sample t-test.
- Report the p-value and 95% confidence interval.
- Write a brief conclusion in context.
- Identify one limitation or caution.
Useful code:
nc |>
group_by(GROUP) |>
summarise(
mean = mean(OUTCOME, na.rm = TRUE),
sd = sd(OUTCOME, na.rm = TRUE),
n = sum(!is.na(OUTCOME))
)ggplot(nc, aes(x = GROUP, y = OUTCOME)) +
geom_boxplot() +
theme_minimal()t.test(
OUTCOME ~ GROUP,
data = nc,
alternative = "two.sided",
conf.level = 0.95
)Two-minute report
Be ready to tell the class:
We examined…
We found…
The hypothesis test indicates…
For policy, this may matter because…
One important limitation is…
On Your Own
Choose one numerical response variable and one two-category explanatory variable from the nc data that your group did not analyze.
Possible numerical variables include:
weightweeksvisitsgained
Possible grouping variables include:
habitmaturemaritalwhitemomgender
Write a research question that can be answered by comparing two population means.
Create an appropriate visualization and calculate the relevant descriptive statistics.
State (H_0) and (H_A).
Conduct the appropriate hypothesis test.
Report the p-value and 95% confidence interval.
Write a short conclusion in plain language. Include:
- the direction of the observed difference;
- whether the evidence is statistically significant at ();
- what the confidence interval adds;
- one limitation of the analysis.
Do not stop at “significant” or “not significant.” Explain what the estimated difference means in the context of the question.