Skip to content

Instantly share code, notes, and snippets.

@USMortality
Last active July 22, 2024 08:49
Show Gist options
  • Save USMortality/3ec2e77d8f5abc8584df68e889002559 to your computer and use it in GitHub Desktop.
Save USMortality/3ec2e77d8f5abc8584df68e889002559 to your computer and use it in GitHub Desktop.
Live Births Germany
library(readr)
library(dplyr)
library(tsibble)
library(TTR)
library(ggplot2)
# https://www-genesis.destatis.de/genesis/online?operation=table&code=12612-0002
df <- read_delim("https://apify.mortality.watch/genesis-12612-0002.csv",
delim = ";",
col_names = c("year", "month", "m", "f", "total"), skip = 6
)
df$month_int <- match(df$month, c(
"Januar", "Februar", "M\xe4rz", "April", "Mai", "Juni",
"Juli", "August", "September", "Oktober", "November", "Dezember"
))
ts <- df |>
filter(!is.na(month_int) & total != "...") |>
mutate(
date = make_yearmonth(year, month_int),
total = as.numeric(total),
moving_avg = SMA(total, n = 12)
) |>
select(date, total, moving_avg) |>
as_tsibble(index = date)
sf <- 2
options(vsc.dev.args = list(width = 600 * sf, height = 335 * sf, res = 72 * sf))
ggplot(ts, aes(x = date)) +
geom_line(aes(y = total), color = "black") +
geom_line(aes(y = moving_avg), color = "blue") +
labs(
title = "Total and 12-month Moving Average of Live Births in Germany",
subtitle = "Source: Destatis/Genesis: 12612-0002",
y = "Live Births", x = "Month"
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment