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 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.
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.
But 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 by several quarters or years. Eurostat is usually more up to date and covers most European countries, but even here different series capturing similar concepts often complicate matters. National sources are typically best, providing the longest possible coverage and highest possible frequency, but are not practical if one wants to compare a larger number of countries.
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.
Instructions for running R script
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.
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
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 <- FALSEin 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.
Once all of this is done, let the R script run through. It will create a subfolder
/chartswithin 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.
- Poster accompanying Cían Allen's ECB paper
- Bank of England paper by Barwell and Burrows showing how financial balances can help understand the Global Financial Crisis: Growing Fragilities? Balance Sheets in the Great Moderation (Bank of England Financial Stability Paper No. 10 – April 2011)
- Bank of England paper by Burgess, Burrows, Godin, Kinsella, and Millard which uses financial balances in an empirical model of the UK economy: A dynamic model of financial balances for the United Kingdom (Bank of England Staff Working Paper No. 614 – September 2016)
- ECB paper by Bê Duc and Le Breton demonstrating some of the useful features of Flow of Funds data more broadly: Flow-of-funds analysis at the ECB (European Central Bank Occasional Paper No. 105 – August 2009)