Reproduce economic indicators from ‘The Economist’

Economic data (% change on year ago)
Gross domestic product
Industrial production
Consumer prices
Unemployment rate, %
latest quarter* 2021 2022 latest latest 2021 latest
United States +0.4 Q1 +1.6 +6.4 +3.5 +16.9 Apr +5.0 May +2.3 +5.8 May
China +21.2 Q1 -15.9 +8.4 +5.6 +5.4 Nov +1.3 May +1.2 +3.9 Q1
Japan -1.5 Q1 -1.0 +3.3 +2.5 +14.1 Apr -0.1 May +0.1 +3.0 May
Britain -6.1 Q1 -1.6 +5.3 +5.1 +27.5 Apr +2.1 May +1.5 +4.7 Mar
Canada +0.3 Q1 +1.4 +5.0 +4.7 +1.7 Mar +3.6 May +1.7 +8.2 May
Euro area -1.3 Q1 -0.3 +4.4 +3.8 +38.9 Apr +2.0 May +1.4 +8.0 Apr
Austria -4.5 Q1 -1.1 +3.5 +4.0 +12.6 Mar +2.8 May +1.6 +5.6 Apr
Belgium -0.6 Q1 +1.0 +4.0 +3.1 +28.3 Apr +1.5 May +1.7 +5.3 Apr
France +1.2 Q1 -0.1 +5.8 +4.2 +43.9 Apr +1.4 May +1.1 +7.3 Apr
Germany -3.1 Q1 -1.8 +3.6 +3.4 +34.4 Apr +2.5 May +2.2 +4.4 Apr
Greece -2.3 Q1 +4.4 +3.8 +5.0 +21.7 Apr +0.1 May +0.2 +15.8 Dec
Italy -0.8 Q1 +0.1 +4.2 +3.6 +78.6 Apr +1.3 May +0.8 +10.7 Apr
Netherlands -2.3 Q1 -0.8 +3.5 +3.0 +8.4 Apr +2.1 May +1.4 +3.4 Apr
Spain -4.2 Q1 -0.4 +6.4 +4.7 +48.2 Apr +2.7 May +1.0 +15.4 Apr
Czech Republic -2.1 Q1 -0.3 +4.2 +4.3 +53.3 Apr +2.9 May +2.3 +3.4 Apr
Denmark -1.3 Q1 -1.3 +2.8 +2.9 +6.6 Apr +1.7 May +1.1 +5.7 Apr
Norway -0.2 Q1 -0.6 +3.9 +4.0 +0.7 Mar +2.7 May +2.2 +5.0 Nov
Poland -1.4 Q1 +1.1 +3.5 +4.5 +29.7 May +4.7 May +3.2 +3.1 Apr
Russia -2.0 Q1 -0.0 +3.8 +3.8 +0.5 Mar +6.0 May +4.5 +5.5 Q1
Sweden -0.1 Q1 +0.8 +3.1 +3.0 +22.7 Apr +1.8 May +1.5 +9.1 Apr
Switzerland -0.5 Q1 -0.5 +3.5 +2.8 +5.4 Q4 +0.6 May +0.1 +2.4 May§
Turkey +6.7 Q1 +1.7 +6.0 +3.5 +65.4 Apr +16.6 May +13.6 +12.2 Jan
Australia +1.1 Q1 +1.8 +4.5 +2.8 -1.6 Q1 +1.1 Q1 +1.7 +5.1 May
Hong Kong +8.0 Q1 +5.4 +4.3 +3.8 -6.0 Q4 +1.1 May +1.4 +6.5 Q3
India +1.8 Q1 +2.1 +12.5 +6.9 +2.6 Dec +5.3 May +4.9 +7.1 Year
Indonesia -0.5 Q1 +1.6 +4.3 +5.8 -3.7 Apr§ +1.7 May +2.0 +5.4 Q3
Malaysia +4.4 Q4§ NA +6.5 +6.0 +3.1 Mar§ +1.7 Mar +2.0 +4.7 Q3
Pakistan -0.4 Year NA +1.5 +4.0 +23.0 Mar +8.0 Dec +8.7 +4.4 Q2
Philippines -0.1 Q1 -5.1 +6.9 +6.5 -73.4 Mar +4.5 Mar +3.4 +1.4 Q2
Singapore +1.1 Q4§ +0.2 +5.2 +3.2 +8.6 Jan +2.4 May +0.2 +3.1 Q4
South Korea +1.9 Q1 +1.7 +3.6 +2.8 +8.9 Apr +2.6 May +1.4 +3.8 May
Taiwan +3.0 Q4§ NA +4.7 +3.0 NA +0.7 Q4§ +0.9 +3.7 Q1
Thailand -2.6 Q1 +0.2 +2.6 +5.6 -6.6 Q1 +1.2 Jun +1.3 +1.4 Q1
Argentina +2.2 Q1 +2.6 +5.8 +2.5 +4.4 Q3 +48.8 May NA +12.1 Q4
Brazil +2.3 Q1 +1.2 +3.7 +2.6 +32.2 Apr +8.1 May +4.6 +8.0 Nov
Chile +1.2 Q1 +3.2 +6.2 +3.8 +5.2 Apr +3.6 May +3.1 +10.4 Mar
Colombia +2.0 Q1 +2.9 +5.2 +3.6 -1.1 Dec +3.3 May +2.1 +15.0 Apr
Mexico -2.8 Q1 +0.8 +5.0 +3.0 -29.3 Apr +5.9 May +3.5 +4.2 May
Peru -11.1 Year NA +8.5 +5.2 +20.3 Apr +2.6 Mar +2.0 +7.0 Q4
Egypt +3.6 Year NA +2.5 +5.7 +6.2 Mar +4.5 Mar +4.8 +7.9 Q4§
Israel -0.4 Q1 -1.6 +5.0 +4.3 +11.6 Mar +1.5 May +0.3 +5.5 May
Saudi Arabia -2.3 Q1 -0.5 +2.9 +4.0 +1.6 Q3 +5.7 May +2.7 +7.4 Year
South Africa -2.7 Q1 +1.1 +3.1 +2.0 +1.3 Aug +5.2 May +4.3 +32.3 Q1
Source: DBnomics (Eurostat, ILO, IMF, OECD and national sources). Click on the figures in the `latest` columns to see the full time series.
* % change on previous quarter, annual rate IMF estimation/forecast 2020 § 2019



The aim of this blog post is to reproduce part of the economic indicators table from ‘The Economist’ using only free tools. We take data directly from DBnomics. The DBnomics API can be accessed through R with the rdbnomics package. All the following code is written in R, thanks to the RCoreTeam (2016) and the RStudioTeam (2016). To update the table, just download the code here and re-run it.

if (!"pacman" %in% installed.packages()[,"Package"]) install.packages("pacman", repos='http://cran.r-project.org')
pacman::p_load(tidyverse,rdbnomics,magrittr,zoo,lubridate,knitr,kableExtra,formattable)

opts_chunk$set(fig.align="center", message=FALSE, warning=FALSE)

currentyear <- year(Sys.Date())
lastyear <- currentyear-1
beforelastyear <- currentyear-2
CountryList <- c("United States","China","Japan","Britain","Canada",
                 "Euro area","Austria","Belgium","France","Germany","Greece","Italy","Netherlands","Spain",
                 "Czech Republic","Denmark","Norway","Poland","Russia","Sweden","Switzerland","Turkey",
                 "Australia","Hong Kong","India","Indonesia","Malaysia","Pakistan","Philippines","Singapore","South Korea","Taiwan","Thailand",
                 "Argentina","Brazil","Chile","Colombia","Mexico","Peru",
                 "Egypt","Israel","Saudi Arabia","South Africa")

Download

gdp <- rdb("OECD","MEI",ids=".NAEXKP01.GPSA+GYSA.Q")
hongkong_philippines_thailand_saudi_singapore_gdp_level <- 
  rdb("IMF","IFS",mask="Q.HK+PH+TH+SA+SG.NGDP_R_SA_XDC") %>%
  rename(Country=`Reference Area`) %>%
  mutate(Country=case_when(Country=="Hong Kong, China" ~ "Hong Kong",
                           TRUE ~ Country))
gdp_qoq_hongkong_philippines_thailand_saudi_singapore <-
  hongkong_philippines_thailand_saudi_singapore_gdp_level %>% 
  arrange(Country,period) %>% 
  group_by(Country) %>% 
  mutate(value=(value/lag(value)-1)*100,
         MEASURE="GPSA")
gdp_yoy_hongkong_philippines_thailand_saudi_singapore <-
  hongkong_philippines_thailand_saudi_singapore_gdp_level %>% 
  arrange(Country,period) %>% 
  mutate(quarter=quarter(period)) %>% 
  group_by(Country,quarter) %>% 
  mutate(value=(value/lag(value)-1)*100,
         MEASURE="GYSA")
malaysia_gdp <- 
  rdb("BI/TABEL9_1/21.Q") %>% 
  mutate(Country="Malaysia",
         MEASURE="GYSA")
taiwan_gdp <- 
  rdb("BI/TABEL9_1/17.Q") %>% 
  mutate(Country="Taiwan",
         MEASURE="GYSA")
egypt_pakistan_peru_gdp <-
  rdb("IMF","WEO:latest",mask="EGY+PAK+PER.NGDP_RPCH") %>% 
  rename(Country=`WEO Country`) %>% 
  mutate(MEASURE="GYSA") %>% 
  filter(year(period)<currentyear)
china_gdp_level <- 
  rdb(ids="OECD/MEI/CHN.NAEXCP01.STSA.Q")
gdp_qoq_china <-
  china_gdp_level %>% 
  arrange(period) %>% 
  mutate(value=(value/lag(value)-1)*100,
         MEASURE="GPSA")
gdp_yoy_china <-
  china_gdp_level %>% 
  arrange(period) %>% 
  mutate(quarter=quarter(period)) %>% 
  group_by(quarter) %>% 
  mutate(value=(value/lag(value)-1)*100,
         MEASURE="GYSA")
argentina_gdp_level <-
  rdb(ids="Eurostat/naidq_10_gdp/Q.SCA.KP_I10.B1GQ.AR") %>% 
  rename(Country=`Geopolitical entity (reporting)`)
gdp_qoq_argentina <-
  argentina_gdp_level %>% 
  arrange(period) %>% 
  mutate(value=(value/lag(value)-1)*100,
         MEASURE="GPSA")
gdp_yoy_argentina <-
  argentina_gdp_level %>% 
  arrange(period) %>% 
  mutate(quarter=quarter(period)) %>% 
  group_by(quarter) %>% 
  mutate(value=(value/lag(value)-1)*100,
         MEASURE="GYSA")
gdp <- bind_rows(gdp,
                 gdp_qoq_hongkong_philippines_thailand_saudi_singapore,
                 gdp_yoy_hongkong_philippines_thailand_saudi_singapore,
                 malaysia_gdp,
                 taiwan_gdp,
                 egypt_pakistan_peru_gdp,
                 gdp_yoy_china,
                 gdp_qoq_china,
                 gdp_yoy_argentina,
                 gdp_qoq_argentina)

indprod <- rdb("OECD","MEI",ids=".PRINTO01.GYSA.M")
australia_swiss_indprod <- rdb("OECD","MEI","AUS+CHE.PRINTO01.GYSA.Q")
china_egypt_mexico_malaysia_indprod <-
  rdb("IMF","IFS",mask="M.CN+EG+MX+MY.AIP_PC_CP_A_PT") %>% 
  rename(Country=`Reference Area`)
indonesia_pakistan_peru_philippines_singapore_southafrica_indprod <-
  rdb("IMF","IFS",mask="M.ID+PK+PE+PH+SG+ZA.AIPMA_PC_CP_A_PT") %>% 
  rename(Country=`Reference Area`)
argentina_hongkong_saudiarabia_thailand_indprod <- 
  rdb("IMF","IFS",mask="Q.AR+HK+SA+TH.AIPMA_PC_CP_A_PT") %>% 
  rename(Country=`Reference Area`) %>% 
  mutate(Country=case_when(Country=="Hong Kong, China" ~ "Hong Kong",
                           TRUE ~ Country))
indprod <- bind_rows(indprod,australia_swiss_indprod,china_egypt_mexico_malaysia_indprod,indonesia_pakistan_peru_philippines_singapore_southafrica_indprod,argentina_hongkong_saudiarabia_thailand_indprod)

cpi <- rdb("OECD","MEI",ids=".CPALTT01.GY.M")
australia_cpi <- rdb("OECD","MEI",ids="AUS.CPALTT01.GY.Q")
taiwan_cpi <- 
  rdb("BI/TABEL9_2/17.Q") %>% 
  mutate(Country="Taiwan")
other_cpi <- 
  rdb("IMF","IFS",mask="M.EG+HK+MY+PE+PH+PK+SG+TH.PCPI_PC_CP_A_PT") %>% 
  rename(Country=`Reference Area`) %>% 
  mutate(Country=case_when(Country=="Hong Kong, China" ~ "Hong Kong",
                           TRUE ~ Country))
cpi <- bind_rows(cpi,australia_cpi,taiwan_cpi,other_cpi)

unemp <- rdb("OECD","MEI",ids=".LRHUTTTT.STSA.M")
swiss_unemp <- rdb("OECD","MEI",mask="CHE.LMUNRRTT.STSA.M")
brazil_unemp <- rdb("OECD","MEI",mask="BRA.LRUNTTTT.STSA.M")
southafrica_russia_unemp <- rdb("OECD","MEI",mask="ZAF+RUS.LRUNTTTT.STSA.Q")
china_unemp <- 
  rdb(ids="BUBA/BBXL3/Q.CN.N.UNEH.TOTAL0.NAT.URAR.RAT.I00") %>% 
  mutate(Country="China")
saudiarabia_unemp <-
  rdb(ids="ILO/UNE_DEAP_SEX_AGE_RT/SAU.BA_627.AGE_AGGREGATE_TOTAL.SEX_T.A") %>%
  rename(Country=`Reference area`) %>%
  filter(year(period)<currentyear)
india_unemp <-
  rdb(ids="ILO/UNE_2EAP_SEX_AGE_RT/IND.XA_1976.AGE_YTHADULT_YGE15.SEX_T.A") %>%
  rename(Country=`Reference area`) %>%
  filter(year(period)<currentyear)
indonesia_pakistan_unemp <-
  rdb("ILO","UNE_DEAP_SEX_AGE_EDU_RT",mask="IDN+PAK..AGE_AGGREGATE_TOTAL.EDU_AGGREGATE_TOTAL.SEX_T.Q") %>% 
  rename(Country=`Reference area`)
other_unemp <-
  rdb("ILO","UNE_DEA1_SEX_AGE_RT",mask="ARG+EGY+HKG+MYS+PER+PHL+SGP+THA+TWN..AGE_YTHADULT_YGE15.SEX_T.Q") %>%
  rename(Country=`Reference area`) %>%
  mutate(Country=case_when(Country=="Hong Kong, China" ~ "Hong Kong",
                           Country=="Taiwan, China" ~ "Taiwan",
                           TRUE ~ Country))
unemp <- bind_rows(unemp,brazil_unemp,southafrica_russia_unemp,swiss_unemp,china_unemp,saudiarabia_unemp,india_unemp,indonesia_pakistan_unemp,other_unemp)

forecast_gdp_cpi_ea <- 
  rdb("IMF","WEOAGG:latest",mask="163.NGDP_RPCH+PCPIPCH") %>% 
  rename(`WEO Country`=`WEO Countries group`)
forecast_gdp_cpi <- 
  rdb("IMF","WEO:latest",mask=".NGDP_RPCH+PCPIPCH") %>% 
  bind_rows(forecast_gdp_cpi_ea) %>% 
  transmute(Country=`WEO Country`,
            var=`WEO Subject`,
            value,
            period) %>% 
  mutate(Country=str_trim(Country),
         var=str_trim(var)) %>% 
  mutate(Country=case_when(Country=="United Kingdom" ~ "Britain",
                           Country=="Hong Kong SAR" ~ "Hong Kong",
                           Country=="Korea" ~ "South Korea",
                           Country=="Taiwan Province of China" ~ "Taiwan",
                           TRUE ~ Country),
         var=case_when(var=="Gross domestic product, constant prices" ~ "GDP",
                       var=="Inflation, average consumer prices" ~ "CPI",
                       TRUE ~ var))
forecast_gdp_cpi <- left_join(data.frame(Country=CountryList),forecast_gdp_cpi,by="Country")

Transform

gdp_yoy_latest_period <-
  gdp %>% 
  filter(MEASURE=="GYSA") %>% 
  filter(!is.na(value)) %>% 
  group_by(Country) %>% 
  summarise(period=max(period))
gdp_yoy_latest <-
  gdp %>% 
  filter(MEASURE=="GYSA") %>% 
  inner_join(gdp_yoy_latest_period) %>% 
  mutate(var="GDP",measure="latest")

gdp_qoq_latest_period <-
  gdp %>% 
  filter(MEASURE=="GPSA") %>% 
  filter(!is.na(value)) %>% 
  group_by(Country) %>% 
  summarise(period=max(period))
gdp_qoq_latest <-
  gdp %>% 
  filter(MEASURE=="GPSA") %>% 
  inner_join(gdp_qoq_latest_period) %>% 
  mutate(var="GDP",
         measure="quarter")

gdp_2021_2022 <-
  forecast_gdp_cpi %>% 
  filter(var=="GDP" & (period=="2021-01-01" | period=="2022-01-01")) %>% 
  mutate(measure=as.character(year(period)))

indprod_latest_period <-
  indprod %>% 
  filter(!is.na(value)) %>% 
  group_by(Country) %>% 
  summarise(period=max(period))
indprod_latest <-
  indprod %>% 
  inner_join(indprod_latest_period) %>% 
  mutate(var="indprod",measure="latest")

cpi_latest_period <-
  cpi %>% 
  filter(!is.na(value)) %>% 
  group_by(Country) %>% 
  summarise(period=max(period))
cpi_latest <-
  cpi %>% 
  inner_join(cpi_latest_period) %>% 
  mutate(var="CPI",measure="latest")

cpi_2021 <-
  forecast_gdp_cpi %>% 
  filter(var=="CPI" & period=="2021-01-01") %>% 
  mutate(measure=as.character(year(period)))

unemp_latest_period <-
  unemp %>% 
  filter(!is.na(value)) %>% 
  group_by(Country) %>% 
  summarise(period=max(period))
unemp_latest <- 
  unemp %>% 
  inner_join(unemp_latest_period) %>% 
  mutate(var="unemp",measure="latest")

Merge

df_all <- 
  bind_rows(gdp_yoy_latest,gdp_qoq_latest,gdp_2021_2022,indprod_latest,cpi_latest,cpi_2021,unemp_latest) %>% 
  mutate(value=ifelse(value>=0,
                      paste0("+",sprintf("%.1f",round(value,1))),
                      sprintf("%.1f",round(value,1)))) %>% 
  unite(measure,c(var,measure))

df_latest <- 
  df_all %>% 
  filter(measure %in% c("GDP_latest","indprod_latest","CPI_latest","unemp_latest")) %>% 
  mutate(value=case_when(`@frequency`=="quarterly" ~ paste(value," Q",quarter(period),sep=""),
                         `@frequency`=="monthly" ~ paste(value," ",month(period,label = TRUE, abbr = TRUE, locale = "en_US.utf8"),sep=""),
                         `@frequency`=="annual" ~ paste(value," Year",sep=""),
                         TRUE ~ value)) %>% 
  mutate(value=text_spec(ifelse(year(period)==lastyear,paste0(value,footnote_marker_symbol(3)),
                                ifelse(year(period)==beforelastyear,paste0(value,footnote_marker_symbol(4)),value)),
                         link = paste("https://db.nomics.world",provider_code,dataset_code,series_code,sep = "/"), 
                         color = "#333333",escape = F,extra_css="text-decoration:none"))

df_final <- 
  df_all %>% 
  filter(measure %in% c("GDP_quarter","GDP_2021","GDP_2022","CPI_2021")) %>% 
  bind_rows(df_latest) %>% 
  mutate(Country=case_when(Country=="United Kingdom" ~ "Britain",
                           Country=="Euro area (19 countries)" ~ "Euro area",
                           Country=="China (People's Republic of)" ~ "China",
                           Country=="Korea" ~ "South Korea",
                           TRUE ~ Country)) %>% 
  select(Country,value,measure) %>% 
  spread(measure,value) %>% 
  select(Country,GDP_latest,GDP_quarter,GDP_2021,GDP_2022,indprod_latest,CPI_latest,CPI_2021,unemp_latest)

df_final <- left_join(data.frame(Country=CountryList),df_final,by="Country")

Display

names(df_final)[1] <- ""
names(df_final)[2] <- "latest"
names(df_final)[3] <- paste0("quarter",footnote_marker_symbol(1))
names(df_final)[4] <- paste0("2021",footnote_marker_symbol(2))
names(df_final)[5] <- paste0("2022",footnote_marker_symbol(2))
names(df_final)[6] <- "latest"
names(df_final)[7] <- "latest"
names(df_final)[8] <- paste0("2021",footnote_marker_symbol(2))
names(df_final)[9] <- "latest"

df_final %>% 
  kable(row.names = F,escape = F,align = c("l",rep("c",8)),caption = "Economic data (% change on year ago)") %>% 
  kable_styling(bootstrap_options = c("striped", "hover","responsive"), fixed_thead = T, font_size = 13) %>% 
  add_header_above(c(" " = 1, "Gross domestic product" = 4, "Industrial production  " = 1, "Consumer prices"= 2, "Unemployment rate, %"=1)) %>% 
  column_spec(1, bold = T) %>% 
  row_spec(seq(1,nrow(df_final),by=2), background = "#D5E4EB") %>% 
  row_spec(c(5,14,22,33,39),extra_css = "border-bottom: 1.2px solid") %>% 
  footnote(general = "DBnomics (Eurostat, ILO, IMF, OECD and national sources). Click on the figures in the `latest` columns to see the full time series.",
           general_title = "Source: ",
           footnote_as_chunk = T,
           symbol = c("% change on previous quarter, annual rate ", "IMF estimation/forecast", paste0(lastyear),paste0(beforelastyear)))

Bibliography

R Core Team. R: A Language and Environment for Statistical Computing. R Foundation for Statistical Computing, Vienna, Austria, 2016. URL: https://www.R-project.org.

RStudio Team. RStudio: Integrated Development Environment for R. RStudio, Inc., Boston, MA, 2016. URL: http://www.rstudio.com/.