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 for 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 had written during my own dissertation to quickly assemble 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 big fan of sectoral balances since coming across them in the work of Michael Pettis and Martin Wolf, who use the framework to illustrate where economies stand and what policy options they have available. In a nutshell, sectoral balances capture net saving by 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

But while the framework itself is straightforward enough, getting actual data on sectoral balances can be difficult. Statistical authorities have different ways of capturing data that correspond to sectoral balances and finding relevant statistics can be tricky. The OECD's National Accounts at a Glance are a good place to start, but their data typically trail data released by official sources by several quarters or years. Eurostat is usually more up to date and covers most European countries, but even here different series can give different impressions. National sources are best, providing the longest possible period of coverage and highest possible frequency, but are not practical if one wants to compare a larger number of countries.

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 updated with a single click.

View sectoral balances charts Download data

Download R script Download R markdown document

Instructions for running 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. Assembling the data for Japan more involved than just calling an API or a static URL. While Flow of Funds data and GDP data are available for download as flat files and by API, respectively, historical data is not, so some legwork is required. Head over to the BoJ's Statistics Download page, scroll down to Search by exact series code and download data for the below codes in CSV format. 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 would need to be changed manually later on to update the data.

  4. Once all of this is done, 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