Skip to content

Instantly share code, notes, and snippets.

@timriffe
Last active December 6, 2023 21:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save timriffe/e5a3bdd9cc59de1d61c80d9e254d1008 to your computer and use it in GitHub Desktop.
Save timriffe/e5a3bdd9cc59de1d61c80d9e254d1008 to your computer and use it in GitHub Desktop.
extinct cohort method in tidy-speak
library(HMDHFDplus)
library(tidyverse)
library(janitor)
d <- readHMDweb("ESP","Deaths_lexis",
username = Sys.getenv("us"),
password = Sys.getenv("pw"))
p <- readHMDweb("ESP","Population",
username = Sys.getenv("us"),
password = Sys.getenv("pw"))
ecm <-
d |>
clean_names() |>
select(-open_interval) |>
filter(age < 110) |>
pivot_longer(c(female, male, total),
names_to = "sex",
values_to = "deaths") |>
summarize(deaths = sum(deaths), .by = c(sex, year, cohort)) |>
arrange(sex, cohort, desc(year)) |>
mutate(pop_ecm = cumsum(deaths),
age = year - cohort - 1,
.by = c(sex, cohort)) |>
filter(age >= 80,
cohort <= 1920)
p |>
clean_names() |>
select(year, age, female = female1, male = male1, total =total1) |>
pivot_longer(c(male, female, total),
names_to = "sex",
values_to = "pop") |>
right_join(ecm, by = join_by(sex, year, age)) |>
filter(year == 1980) |>
ggplot(aes(x=age,y=pop_ecm, color = sex)) +
geom_line() +
geom_line(mapping = aes(y = pop), linewidth=2, linetype=2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment