---
title: "Problem Set X: Gapminder"
author: "Jane Student"
date: today
format:
  html:
    embed-resources: true
---

```{r}
#| label: setup
#| message: false
library(tidyverse)
library(gapminder)
```

This report uses the `gapminder` dataset, which contains panel data on life expectancy, population, and GDP per capita for 142 countries between 1952 and 2007.


# Q1: Which countries experienced the largest improvements in life expectancy over time?

To answer this I calculate the change in life expectancy between the first (1952) and last (2007) observation for each country, then look at the top performers.

```{r}
life_exp_change = gapminder |>
  filter(year %in% c(1952, 2007)) |>
  select(country, continent, year, lifeExp) |>
  pivot_wider(names_from = year, values_from = lifeExp,
              names_prefix = "yr_") |>
  mutate(change = yr_2007 - yr_1952) |>
  arrange(desc(change))

life_exp_change |>
  slice_head(n = 10) |>
  knitr::kable(digits = 1,
               col.names = c("Country", "Continent", "1952", "2007", "Change"),
               caption = "Top 10 countries by life expectancy gains, 1952–2007")
```

The largest gains are concentrated in Asia and the Middle East. Oman leads with a roughly 40-year increase, reflecting rapid development driven by oil wealth and public health investment. Several East Asian countries (Vietnam, Indonesia, South Korea) also appear, consistent with the region's dramatic postwar development.

```{r}
#| label: fig-top-trajectories
#| fig-cap: "Life expectancy over time for the 5 countries with the largest gains"

top5 = life_exp_change |> slice_head(n = 5) |> pull(country)

gapminder |>
  filter(country %in% top5) |>
  ggplot(aes(x = year, y = lifeExp, color = country)) +
  geom_line(linewidth = 1) +
  geom_point(size = 1.5) +
  labs(x = NULL, y = "Life expectancy (years)", color = NULL) +
  theme_minimal()
```

@fig-top-trajectories shows that the gains were not uniform over time---most of the improvement happened between the 1960s and 1990s, with growth leveling off as countries approached the global frontier.


# Q2: Is there a relationship between GDP per capita and life expectancy?

The classic Preston curve suggests a positive but concave relationship between income and health. I examine this using the 2007 cross-section.

```{r}
#| label: fig-preston
#| fig-cap: "GDP per capita and life expectancy in 2007 (log scale on x-axis)"

gapminder |>
  filter(year == 2007) |>
  ggplot(aes(x = gdpPercap, y = lifeExp)) +
  geom_point(aes(size = pop, color = continent), alpha = 0.7) +
  geom_smooth(method = "loess", se = TRUE, color = "black",
              linewidth = 0.8) +
  scale_x_log10(labels = scales::label_dollar()) +
  scale_size_continuous(guide = "none") +
  labs(x = "GDP per capita (log scale)",
       y = "Life expectancy (years)",
       color = NULL) +
  theme_minimal()
```

@fig-preston confirms the expected pattern: life expectancy rises steeply with income at low levels of GDP per capita but flattens out among wealthier countries. A few African countries fall well below the trend, likely reflecting the impact of the HIV/AIDS epidemic. The log transformation of GDP straightens the relationship considerably, which makes sense---the difference between \$1,000 and \$5,000 matters much more for health than the difference between \$30,000 and \$34,000.

A simple regression of life expectancy on log GDP per capita quantifies this:

```{r}
fit = lm(lifeExp ~ log(gdpPercap), data = gapminder |> filter(year == 2007))
summary(fit)
```

A one-unit increase in log GDP per capita (roughly a doubling of income) is associated with about a `r round(coef(fit)[2], 1)`-year increase in life expectancy. The $R^2$ of `r round(summary(fit)$r.squared, 2)` indicates that income alone explains a substantial share of cross-national variation in health, though clearly other factors matter.


# Q3: How has the distribution of life expectancy changed across continents over time?

Finally, I examine how the *distribution* of life expectancy has evolved within each continent.

```{r}
#| label: fig-ridges
#| fig-cap: "Distribution of life expectancy by continent, 1952 vs. 2007"

gapminder |>
  filter(year %in% c(1952, 2007)) |>
  mutate(year = factor(year)) |>
  ggplot(aes(x = lifeExp, y = continent, fill = year)) +
  geom_boxplot(alpha = 0.7, position = position_dodge(width = 0.8)) +
  labs(x = "Life expectancy (years)", y = NULL, fill = NULL) +
  theme_minimal()
```

Two things stand out in @fig-ridges. First, every continent shifted rightward between 1952 and 2007---life expectancy improved nearly everywhere. Second, the distributions *compressed*: there is less within-continent variation in 2007 than in 1952, especially in Asia, where the 1952 distribution was extremely spread out. Africa is the exception---while the median improved, the 2007 distribution remains wide, reflecting divergent trajectories across the continent.


## AI disclosure

I used Claude to help debug a `pivot_wider` call in Q1 and to refine the axis labels in Q2. All interpretive text is my own.
