Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@ilarischeinin
Last active November 23, 2016 12:35
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 ilarischeinin/472fc67ce4e9a92668ee0f67bc009d08 to your computer and use it in GitHub Desktop.
Save ilarischeinin/472fc67ce4e9a92668ee0f67bc009d08 to your computer and use it in GitHub Desktop.
Finland’s dependency ratio and pension contributions - http://ilari.scheinin.fi/finlands-dependency-ratio-and-pension-contributions/
library(pxweb)
library(dplyr)
library(stringr)
library(tidyr)
library(ggplot2)
library(animation)
api <- "http://pxnet2.stat.fi/PXWeb/api/v1/"
past_original <- get_pxweb_data(
url=paste0(api, "en/StatFin/vrm/vaerak/125_vaerak_tau_106.px"),
dims=list(Vuosi="*", Sukupuoli="*", Ikä="*"),
clean=TRUE)
future_original <- get_pxweb_data(
url=paste0(api, "en/StatFin/vrm/vaenn/010_vaenn_tau_101.px"),
dims=list(Vuosi="*", Sukupuoli="*", Ikä="*"),
clean=TRUE)
past <- past_original %>%
filter(!str_detect(Age, "total"), Sex != "Both sexes") %>%
transmute(
year=as.integer(as.character(Year)),
age=as.integer(gsub("[^0-9]", "", Age)),
sex=tolower(as.character(Sex)),
population=values
) %>%
tbl_df()
future <- future_original %>%
filter(!Year %in% unique(past$year)) %>%
filter(!str_detect(Age, "total"), Sex != "Both sexes") %>%
transmute(
year=as.integer(as.character(Year)),
age=as.integer(gsub("[^0-9]", "", Age)),
sex=tolower(as.character(Sex)),
population=values
) %>%
tbl_df()
both <- rbind(past, future)
pension_fees <- get_pxweb_data(
url=paste0(api, "en/StatFin/jul/vermak/102_vermak_tau_120.px"),
dims=list(Verolaji = c("D61111_TYO", "D61121_TYO"), Sektori = c("S13141"),
Tieto = c("Suhde1"), Vuosi = c("*")),
clean=TRUE) %>%
tbl_df() %>%
select(-Sector, -Data) %>%
mutate(year=as.integer(as.character(Year))) %>%
group_by(year) %>%
summarise(contribution=sum(values))
dependency <- both %>%
group_by(year) %>%
summarise(
child=sum(population[age < 15]),
labor=sum(population[age >= 15 & age < 65]),
aged=sum(population[age >= 65]),
child_ratio=child / labor * 100,
aged_ratio=aged / labor * 100,
total_ratio=(child + aged) / labor * 100
) %>%
select(year, aged_ratio)
combined <- dependency %>%
left_join(pension_fees)
toplot <- combined %>%
gather(key, value, -year)
toplot$lty <- "actual"
toplot$lty[toplot$key == "aged_ratio" & toplot$year %in% future$year] <-
"forecast"
labs <- c(
expression("aged dependency ratio" ==
frac(
"number of people aged 65 and over",
"number of people aged between 15 and 64"
) %*% 100
),
"statutory pension contributions as percent of GDP")
pnga4("finland-dependency-ratio.png")
toplot %>%
ggplot(aes(year, value, color=key, linetype=lty)) +
geom_line(size=3) +
scale_color_manual(labels=labs, values=c("#ffffff", "#18447e")) +
scale_linetype(guide=FALSE) +
theme(text=element_text(size=16),
legend.background=element_rect(fill="#9ecae1"),
legend.key=element_rect(fill=NA, color=NA),
legend.key.width=unit(4, "cm"),
legend.text.align=0,
legend.title=element_blank(),
legend.position="bottom",
legend.direction="vertical",
legend.box="horizontal",
panel.background=element_rect(fill="#9ecae1"),
plot.background=element_rect(fill="#9ecae1")) +
labs(title="Finland's dependency ratio and pension contributions",
x="year", y="") +
annotate("text", x=1975.625, y=49.5, hjust=0, vjust=1,
label="By: Ilari Scheinin\nSource: Statistics Finland\nLicense: CC BY")
dev.off()
# 2016-10-20
# Ilari Scheinin
# CC BY
# EOF
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment