Guided project
Module 2 · Statistics · Self-checked
Analyse the birth weight data from start to finish in an RStudio Project of your own. Work through the six tasks, check each against its checkpoint, and tick it off. Nothing is submitted.
Research question
Is a history of hypertension in the mother (ht) associated with lower birth weight, and does the association hold after adjusting for smoking and the mother's weight?
Hypertension is persistently high blood pressure. In birthwt, the column ht records whether the mother had a history of it: 0 for no, 1 for yes. An association means that babies in one group tend to differ from babies in the other; it does not by itself say why. Adjusting means comparing mothers with and without hypertension who are alike in the other factors, here smoking and weight, using a regression model as in Counts and proportions and Correlation and linear regression.
How to approach this
This project repeats the whole of Module 2 on a new question, so every method you need has already appeared. The tasks build on each other: clean data feed the descriptions, the descriptions tell you which tests suit the data, and the tests and models feed the report. Work through them in order.
- Write everything in scripts. Use the numbered-script layout from A reproducible workflow, so that at the end you can delete results/ and figures/ and rebuild them.
- Look before you test. One group has only 12 mothers. Small groups make estimates unstable, so the plot in task 2 matters as much as any p-value.
- Report effects with intervals. For every comparison, write down the size of the difference, its 95% confidence interval and then the p-value, in that order, as in Comparing two groups.
- Use the checkpoints as a check, not a target. They give rounded values. If yours differ a little, rounding may explain it; if they differ a lot, the usual causes are a factor coded the wrong way round or the wrong group used as the reference.
Keep a short notes file as you go, recording each decision and why you made it, for example "used Fisher's test because one group has 12 mothers". Those notes become the methods and limitations sections of your report in task 6.
Before you run anything, write down which analysis answers the research question most directly. Here that is the adjusted linear model in task 5, because the question asks whether the association holds after adjusting for smoking and weight. The t-test, the Wilcoxon test and the two-by-two table are supporting analyses: they describe the same association from other angles. Deciding this in advance matters because, as Comparing two groups and Multiple testing and power showed, running several tests and reporting whichever looks best turns noise into findings. Report every analysis you ran, and say which one was the main one.
Be ready for an answer that is not clear-cut. With 12 mothers in one group, some intervals will be wide and some p-values will fall either side of 0.05. That is not a failed project. A careful report that says "the data are compatible with no difference and with a large one, and here is why" is a correct result, and it is more useful than a confident claim the data cannot support.
A note on the variable itself. ht records a history of hypertension, not blood pressure measured during the pregnancy, and the dataset gives no detail on how it was established. Keep that in mind when you write about what the variable means. birthwt has other columns too, such as previous premature labours (ptl) and uterine irritability (ui). Resist adding them all to the models: with 189 births, and only 12 mothers with hypertension, each extra term makes the hypertension estimate less stable.
Each task below has a short hint that points to the right lesson and method without giving the answer.
Set up and clean
Data frames and files and Cleaning data with dplyr and tidyr; A reproducible workflow
- Create an RStudio Project with the folder structure from A reproducible workflow.
- Turn
smoke,htandlowinto labelled factors.
Hint. Start from the mutate() and factor(…, levels = …, labels = …) pattern in Describing data, and make "no hypertension" the first level so it becomes the reference group. Then check your work with nrow() and a table() of the new ht column, as Data frames and files recommends after every load.
Why this step. Every later number depends on the coding. If "yes" became the first level by mistake, every difference would change sign and the reference group in the models would be the 12 mothers with hypertension, not the 177 without. With a group this small, one mother coded wrongly would also move the results noticeably, so the count of 12 is worth checking before anything else.
Checkpoint: 189 births; 12 mothers with a history of hypertension.
Describe
Figures with ggplot2; Describing data
- Summarise birth weight by hypertension: n, mean, SD, median.
- Plot the two groups with every baby shown as a point.
Hint. The group_by() and summarise() pipeline from Describing data gives all four numbers at once if you group by ht instead of smoke. For the plot, use geom_point() from Figures with ggplot2 with the hypertension groups on the x-axis; when many points sit on top of each other, a small sideways scatter (jitter) makes each baby visible. Keep the colours consistent with your other figures.
Why this step. A mean of 12 values can be moved a long way by one or two babies, and the SD of a group of 12 is itself uncertain. Showing every point lets you, and your readers, see whether the gap comes from the whole group or from a few very light babies. Compare the SDs of the two groups as well: if they differ clearly, that is one more reason to use Welch's version of the t-test. A bar chart of the two means would hide all of this, which is why the task asks for points.
Checkpoint: babies of mothers with hypertension weigh about 2,537 g on average, against about 2,972 g; with only 12 in one group, the points matter more than the means.
Compare
Comparing two groups
- Run Welch's t-test and the Wilcoxon test.
- Report the difference with its 95% confidence interval.
Hint. Use the formula form from Comparing two groups, birth weight explained by ht, for both t.test() and wilcox.test(). Check which group R subtracts from which by reading the "sample estimates" lines. For the width of the interval, recall from Describing data that a standard error is an SD divided by the square root of a sample size, and ask which of the two groups contributes most to the uncertainty.
Why this step. The two tests make different assumptions. Welch's t-test allows the spreads to differ but relies on the group means being roughly normal, which is less safe with 12 values than with 177, because the central limit theorem needs reasonably large groups. The Wilcoxon test uses only ranks, so it is a check that a few extreme babies are not driving the answer. If both give similar p-values, say so; if they disagree, look back at the plot from task 2. Either way, a p-value above 0.05 here means the comparison was inconclusive, not that hypertension makes no difference.
Checkpoint: a difference of about 435 g, with a 95% interval from about −154 to 1,024 g and p of about 0.13, so the data cannot rule out no difference. Explain why a group of 12 makes the interval so wide.
Low birth weight
Counts and proportions
- Make the two-by-two table of
htagainstlow, with row percentages. - Use Fisher's exact test, and say why it suits these counts.
Hint. Follow Counts and proportions: table() of the two factors, then prop.table(…, margin = 1) for percentages within each hypertension group. To justify Fisher's test, work out the expected count for the smallest cell with row total × column total ÷ grand total, and compare it with the rule of thumb of about 5 given in that lesson.
Why this step. Low birth weight is the clinically used category, so it is worth reporting even though, as Counts and proportions showed, cutting birth weight at 2,500 g throws information away and usually gives weaker evidence than the measurement in grams. Give the risks in both groups, not only the p-value, and remember that with 7 and 5 babies in the hypertension row, a single birth moving across the 2,500 g line would change the percentages by more than 8 points. Expect the odds ratio's interval from Fisher's test to be wide for the same reason.
Checkpoint: 7 of 12 babies (58%) of mothers with hypertension had low birth weight, against 52 of 177 (29%); Fisher's p is about 0.05.
Adjust
Counts and proportions and Correlation and linear regression
- Fit
lm(bwt ~ ht + smoke + lwt)and a logistic regression forlowwith the same factors. - Report each effect with its confidence interval, and check the diagnostic plots.
Hint. The linear model follows Correlation and linear regression and the logistic model Counts and proportions, with family = binomial and exp() to turn coefficients into odds ratios. Use confint() for the intervals and plot() on the linear model for the four diagnostic plots. If the adjusted effect differs from task 3, summarise the mother's weight (lwt) by hypertension group and think about which direction that difference would push the unadjusted comparison.
Why this step. The research question asks about mothers who are alike in smoking and weight, and only a model compares them that way. Read each coefficient as the effect of that factor with the others held fixed, and say so when you report it. Check the diagnostic plots before you trust the intervals, and look in the leverage plot for any of the 12 mothers with a large pull on the fit. For the logistic model, note how little information it has about hypertension: its estimate rests on just 12 mothers, 7 with a low-weight baby and 5 without, so its odds ratio will come with a very wide interval.
Checkpoint: after adjustment, hypertension is linked to a drop of about 586 g (95% CI about 165 to 1,008 g; p ≈ 0.007), larger and clearer than in task 3. Mothers with hypertension were heavier on average (about 158 against 128 lb), and heavier mothers tend to have heavier babies, so the unadjusted comparison hid part of the effect.
Report
Multiple testing and power and A reproducible workflow
- Write a short Quarto report with inline numbers: methods, results, one figure, limitations.
- State how many babies per group a study would need to detect a 300 g difference with 80% power.
Hint. A reproducible workflow shows the report structure and inline code, so the numbers in your text come straight from the models. For the sample size, use power.t.test() as in Multiple testing and power, with the SD of birth weight from your own description; compare the answer with the 12 mothers you actually had. In the limitations, ask what the data cannot tell you: who was studied, when and where, how many, and whether the design can show cause.
Why this step. The power calculation turns "not significant" in task 3 into something more useful: a statement of what this study could and could not have detected. Set your required sample size against the 12 mothers you had, and say plainly that the unadjusted comparison was underpowered. In the methods, list every test you ran and which one was the main analysis. In the results, put each effect and its interval before its p-value, and generate every number with inline code so that the report can be rebuilt from the scripts alone.
Checkpoint: your limitations mention the small hypertension group, one centre in 1986, and that this is an association, not proof of cause.
Go deeper: how much does one mother matter?
With only 12 mothers in the hypertension group, a single unusual birth can move the estimate. A sensitivity analysis checks this by refitting the model 12 times, leaving out one of those mothers each time. Using the raw coded data, where ht is 0 or 1: ids <- which(birthwt$ht == 1); loo <- sapply(ids, function(i) coef(lm(bwt ~ ht + smoke + lwt, data = birthwt[-i, ]))["ht"]); range(loo).
Each of the 12 values is the adjusted hypertension effect without one mother. If the range is narrow, no single birth drives the result. If dropping one mother halves the effect, say so in your limitations. Run it after task 5 and compare the range with your full-model estimate. This is a check on stability, not a new test, so it does not replace the confidence interval or p-value you report.
This project used every part of Module 2: describing groups, choosing a test from the design, counts and odds ratios, regression with adjustment, multiple testing and power, and a report that rebuilds itself. One part of it, the figure in task 6, has been treated briefly so far. Module 3, Figures that tell the truth, builds on exactly that step. It starts from the kind of comparison you just made, a group of 12 points beside a group of 177, and asks how to choose a chart that shows the distribution rather than hiding it, how to show the confidence intervals you calculated here, and how colour, axes and layout can make a figure honest or misleading.
Take it further
Put the project on GitHub with a short README. Then use R on sequencing data in Module 4: RNA-seq analysis, or make your figures publication-ready in Module 3: figures that tell the truth.
A README is a short text file at the top of the project that tells a newcomer what the project is, which data it uses and how to run it, for example "open the .Rproj file, run the scripts in order, then render report.qmd". GitHub shows it on the project's front page. Before you publish, check that nothing in the repository identifies a person; birthwt is a public teaching dataset, but real clinical data usually cannot be shared.
Further reading · Module 2Greenland et al. 2016: 25 common misreadings of p-values and confidence intervals. · All reading for this course
Up next
Lesson 1
Choosing the right chart