Visualise macroeconomic sectoral balances using R

When the European Central Bank held its annual Forum on Central Banking in Sintra, Portugal, earlier this year, one contribution to the Young Economists' Session caught my eye: Cían Allen's research on current accounts and sectoral balances. His paper prompted me to dig up some code I wrote during my own dissertation to compile sectoral balances data for a number of economies and update it with some additional data for the US and Japan.px.gif

I've always been a fan of sectoral balances after being introduced to the approach by Michael Pettis and Martin Wolf. In a nutshell, sectoral balances capture net saving in each sector of the economy, i.e. the government, the private sector (which itself is made up of households and corporations) and the rest of the world. If a sector saves more than it spends, its balance is positive (net saver); if it spends more than it saves, its balance is negative (net borrower). By definition all sectors must sum to zero.

UK sectoral balances

While the framework itself is straightforward, getting actual data on sectoral balances can be difficult. Statistical authorities have different ways of capturing sectoral data and finding cross-country statistics can be tricky. The OECD's National Accounts at a Glance are a good place to start, but their data often trail official sources. Eurostat is usually more up to date and covers most European countries, but here too different methodologies complicate matters. National sources are typically best, providing the longest possible coverage and highest possible frequency.

US sectoral balances

So I wrote an R script to follow this process: Get data from OECD and Eurostat, then add data for the US from the St. Louis Fed, and data for Japan from the Bank of Japan and the Cabinet Office. In the case of Eurostat, I opted for non-financial transaction data; in case of the US, I went with capital account data; and in case of Japan I used Flow of Funds data. I stick to the OECD's four sector aggregates (unlike Allen who separates financial and non-financial corporations), but users can easily modify the code to split out different sectors if the base data allows. The advantage of putting all of this in an R script (aside from being able to draw on R's amazing data handling features) is that the charts can be easily refreshed.

View sectoral balances charts · Download sectoral balances data Download R script · R markdown document (Updated 2020/02/14 to replace deprecated indicators, streamline code)

Instructions for running the R script

  1. To use the R script, first make sure you have all necessary libraries are installed, especially the OECD, eurostat, pdfetch, and estatapi packages, in addition to the usual plotting and helper libraries.

  2. Getting the data for Japan requires a bit more legwork. Head over to the BoJ's Statistics Download page, scroll down to Search by exact series code and search for the below codes. Set 1945 as the starting year and download the data in CSV format (simple headers are okay). Drop the file (the name will be of the format nme_*.csv) in the same folder as the R script.

    FF'FFFA260L390
    FF'FFFA220L390
    FF'FFFA230L390
    FF'FFFA270L390
    FF'FFFA300L390
    FF'FFFA100L390
        
    FF'FOF_93FFAF410L700
    FF'FOF_93FFAF420L700
    FF'FOF_93FFAF430L700
    FF'FOF_93FFAF440L700
    FF'FOF_93FFAF500L700
    FF'FOF_93FFAF100L700
        
    FF'FOF_FFAF410L700
    FF'FOF_FFAF420L700
    FF'FOF_FFAF430L700
    FF'FOF_FFAF440L700
    FF'FOF_FFAF500L700
    FF'FOF_FFAF100L700
    
  3. To use Hiroaki Yutani's excellent estatapi package to download data by API from the Japanese government's e-Stat portal, you first need to register on e-Stat's Japanese-language website to obtain an appId which you then set up in our R script using appId <- "XXXXX". To avoid this, set use.estat <- FALSE in the header section to simply grab the latest GDP data from the Japanese Cabinet Office. In this case, the path to the corresponding CSV file will need to be changed manually later on to update the data.

  4. Once all of this is complete, let the R script run through. It will create a subfolder /charts within the current working directory where it will store all sectoral balances charts as PNG and PDF files. The script will also create interactive plotly charts and store them in a list. Download and run this R markdown code to put the charts on a HTML page to view in your browser.

Further reading