Inflation

StatsSA's CPI inflation statistics

Whilst the Headline Consumer Price Index (CPI) is the price index that is most commonly referred to (it is the index for all urban areas, for all items), there are in fact many Consumer Price Indicies, across various different geographical regions, or types of goods.

Scrape and tidy the latest StatsSA inflation data

Do you need the latest inflation index, to show your analysis at the most recent level of prices? You can use this R code to scrape the latest price indicies from StatsSA's website, and tidy the dataset. If you cannot run R, you can use the tidy Excel file that I keep updated in my Dropbox storage, or you can pull the .csv file programmatically. (I keep it updated automatically, by getting my Windows Task Scheduler to run the R script in a .bat file once a day.) I try put the future CPI publication dates into the UCT Economics Society's online calendar.

For example, in Stata, type this at the beginning of your .do file:
import delimited using "https://dropbox.com/s/nuv21cnr5w4qaz8/CPI.csv?raw=1"
keep if region=="All urban areas"
scalar latest_CPI = allitems in -1
scalar latest_CPI_month= month in -1
scalar latest_CPI_year= year in -1

Then merge the data with your dataset. Or, in R, type:
CPI <- read_csv("https://www.dropbox.com/s/nuv21cnr5w4qaz8/CPI.csv?raw=1") %>%
select(1:5) %>%
filter(Region=="All urban areas")

# Headline CPI is the CPI for all urban areas, all items.
latest.CPI <- setNames(
CPI$`All Items`[nrow(CPI)],
paste(
month.name[month(CPI$date[nrow(CPI)])],
year(CPI$date[nrow(CPI)])
)
)

Graphs

If you would like to easily graph the various different types of inflation, have a look at this Excel file I created. The most recent version of Office 365 is probably required, since I used some new formulae. Comments? I don't keep this file up-to-date with the latest indicies, but I aim to use the concept to create a similar Shiny app in future, which I will put on this web page.

Whilst StatsSA typically reports year-on-year statistics, I also smooth the inflation rates over longer periods, with the moving trend average. That is, the OLS slope of log CPI, over rolling windows. So, each point on the smoothing function represents a slope average for the time window on either side of it. In Excel, the formula would be

=( exp( slope(<log CPI vector>, <time vector>) )^(time intervals per year) -1 ) *100%

For example, the following graph shows the trend of Headline CPI (the end of the period does not use future dates, so smaller windows are used there so that the trend is plotted up to the present time).