Visualising Macroeconomic Sectoral Balances Using R

When the European Central Bank hosted its annual Forum on Central Banking in Sintra, Portugal earlier this year, one presentation at the Young Economists' Session stood out to me: Cían Allen's research on current accounts and sectoral balances. His paper prompted me to dig up some code I wrote during my 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 since being introduced to the approach by Michael Pettis and Martin Wolf. In essence, sectoral balances capture net saving in each sector of the economy: the government, the private sector (households and corporations), and the rest of the world. If a sector saves more than it spends, it's a net saver and its balance is positive; if it spends more than it saves, it's a net borrower and its balance is negative. By definition, all sectors must sum to zero.

UK sectoral balances

While the framework is straightforward, getting actual data on sectoral balances can be tricky. Statistical authorities have different methods for capturing sectoral data and finding cross-country statistics can be tricky. The OECD's National Accounts at a Glance is a good starting point, but their data often lags official sources. Eurostat usually offers more up-to-date information for European countries, but here too, different methodologies complicate matters. National sources typically provide the longest coverage and highest frequency.

US sectoral balances

I wrote an R script to streamline compilation of the data. The script fetches data from the OECD and Eurostat, and adds data for the US from the St. Louis Fed, and data for Japan from the Bank of Japan and the Cabinet Office. For Eurostat, I used non-financial transaction data; for the US, I used capital account data; and for 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 using an R script is that the charts can be easily refreshed.

View sectoral balances charts · Download sectoral balances data Download R script · R markdown document

Update (14 February 2020): Replaced deprecated indicators, streamlined code.

Instructions for running the R script

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

  2. For Japanese data, visit the BoJ's Statistics Download page, scroll to Search by exact series code and search for the below codes. Set 1945 as the start year and download the data in CSV format. Place the file (named 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, register on e-Stat's Japanese-language website to obtain an appId, which you then set up in the R script using appId <- "XXXXX". To avoid this, set use.estat <- FALSE in the header to simply grab the latest GDP data from the Japanese Cabinet Office. You you will need to manually change the CSV file path to update the data in this case.

  4. The R script will create a /charts subfolder in the working directory to store all sectoral balances charts as PNG and PDF files. The script also creates interactive plotly charts stored in a list. Download and run this R markdown code to generate an HTML page for browser viewing.

Further Reading