Lab 1: Introduction to Data Analysis in R
Historical birth records, data transformation, and visualization
Download the student lab template
The question
Do birth records show a stable difference between the numbers of boys and girls born each year? We will use a historical dataset compiled by John Arbuthnot and then examine more recent U.S. records.
The purpose is not merely to reproduce calculations. It is to practice a complete analysis cycle:
- ask a clear question;
- inspect the data;
- transform variables when necessary;
- visualize the relevant pattern;
- calculate a useful summary; and
- explain what the evidence does—and does not—show.
Setup
library(dplyr)
library(ggplot2)
load(url("https://raw.githubusercontent.com/GarciaRios/lbj_ug_quant/main/data/arbuthnot.RData"))The load() command creates a data frame named arbuthnot.
1. Inspect the data
arbuthnot
dim(arbuthnot)
names(arbuthnot)
glimpse(arbuthnot)Each row is a year. The columns contain the year and the recorded numbers of boys and girls baptized in London.
Answer before moving on:
- What is the unit of observation?
- How many observations and variables are present?
- What years are covered?
- What limitations might baptism records have as a measure of births?
Individual variables can be accessed with $:
arbuthnot$boys
arbuthnot$girls2. Visualize change over time
A line graph is appropriate because year has a meaningful order.
ggplot(arbuthnot, aes(x = year, y = girls)) +
geom_line(color = "#BF5700", linewidth = 0.8) +
labs(
x = NULL,
y = "Recorded baptisms",
title = "Girls recorded in London baptism data"
) +
theme_minimal()Describe the overall pattern. Look for direction, unusual years, and changes in variability. Avoid explaining causes that the data cannot establish.
Now construct the corresponding graph for boys.
3. Calculate totals and create variables
The total number of girls in the records is:
sum(arbuthnot$girls)To compare births within each year, create total births and the proportion recorded as boys:
arbuthnot <- arbuthnot %>%
mutate(
total = boys + girls,
boy_prop = boys / total
)Inspect the new variables:
select(arbuthnot, year, total, boy_prop)Why is a proportion often easier to compare across years than a raw difference?
4. Evaluate a claim
Arbuthnot argued that more boys than girls were born in every year of his records. We can translate that statement into a logical comparison.
arbuthnot <- arbuthnot %>%
mutate(more_boys = boys > girls)
count(arbuthnot, more_boys)A logical variable is either TRUE or FALSE. The code checks the claim year by year; it does not establish why the pattern occurs.
Visualize the proportion of boys:
ggplot(arbuthnot, aes(x = year, y = boy_prop)) +
geom_hline(yintercept = 0.5, color = "gray60", linetype = "dashed") +
geom_line(color = "#172A46", linewidth = 0.8) +
labs(
x = NULL,
y = "Proportion recorded as boys",
title = "Share of recorded births classified as boys"
) +
theme_minimal()Explain the dashed line and summarize the substantive pattern in two or three sentences.
On Your Own: More recent U.S. births
Load the second dataset:
load(url("https://raw.githubusercontent.com/GarciaRios/lbj_ug_quant/main/data/present.RData"))
glimpse(present)Complete the following in the downloadable report:
- Identify the unit of observation, years covered, and variables.
- Create
totalandboy_prop. - Make a clear line graph of total births over time.
- Make a line graph of
boy_propwith a reference line at 0.5. - Determine whether boys outnumber girls in every observed year.
- Write a short interpretation comparing the recent pattern with the Arbuthnot records.
A strong interpretation states the pattern, uses relevant quantities, and acknowledges what these descriptive data cannot explain.