Business Cycle Dating using R

What is Business Cycle Dating?

The chronology that identifies the dates of peaks and troughs that frame economic recessions and expansions. A recession is the period between a peak of economic activity and its subsequent trough, or lowest point.

Steps in Business Cycle Dating

  • Step-1: Time series is adjusted for the seasonality.

  • Step -2: Cycle component of the seasonal-adjusted data is extracted using any filtering technique. In this case HP filter is used.

  • Step-3: Dating algorithm is employed to the cyclical component.

Installing Packages

In the first step the packages are installed using the following codes that are used in the Dating coding.

#### INSTALLING PACKAGES ######
if (!require(pacman)) install.packages("pacman")
## Loading required package: pacman
pacman::p_load(openxlsx,readxl,seasonal,dplR,BCDating,mFilter, ggplot2, utils,astsa,RColorBrewer)
###############################

Dating Code

Here I write the Dating code for quarterly and monthly data. The inputs of the code are
  • x: the time series vector in numeric class that will be seasonally adjusted. For example, GDP at constant price, price index, index of industrial production, etc.
  • year: starting year of the data, i.e., x.
  • number: if it is quarterly, the quarter number or if monthly, it is month number. For example, For 2007 third quarter year=2007, number=3 and for 2010 October month year=2010, number=10.
  • freq : it is a character quarterly or monthly.
  • filename : name of the file.csv in which the dated data needs to be stored.
# x=variable to be dated
# year = starting year of data
# number = quarter or month number 
# freq= "quarterly" or "monthly"

DATING=function(x,year,number,freq, filename="Dating Data.csv"){
  if(freq=="quarterly"){  
    xts=ts(x,start=c(year,number),frequency = 4)
    xseas= seas(xts)
    xsa=log(xseas$data[,1])
    xcyc=hpfilter(xsa, type="lambda",freq=1600, drift=TRUE)$cycle
    Dating=BBQ(xcyc, name="Dating", mincycle =5, minphase=2)
    plot(Dating,xcyc)
    return(summary(Dating))
    BCData=data.frame(list(as.numeric(Dating@y),as.numeric(Dating@states)))
colnames(BCData)=c("Variable","Phase")
write.table(BCData, filename,na = "",row.names =FALSE,col.names = TRUE,append = FALSE,sep = ",")
  }
  else{
    xts=ts(x,start=c(year,number),frequency = 12)
    xseas= seas(xts)
    xsa=log(xseas$data[,1])
    xcyc=hpfilter(xsa, type="lambda",freq=1600*(3)^4, drift=TRUE)$cycle
    Dating=BBQ(xcyc, name="Dating", mincycle =15, minphase = 6)
    plot(Dating,cbind(xcyc)) 
    return(summary(Dating))
      BCData=data.frame(list(as.numeric(Dating@y),as.numeric(Dating@states)))
colnames(BCData)=c("Variable","Phase")
write.table(BCData, filename,na = "",row.names =FALSE,col.names = TRUE,append = FALSE,sep = ",")
  }
}

Example

We have taken quarterly GDP data of India for dating the business cycle and monthly inflation data to date the downward fall in inflation.

# setting the directory where downloaded file to be stored
setwd("C:/Users/RBI/Desktop")

#Quarterly Data
data_q=read.xlsx("https://mudulisilu.files.wordpress.com/2021/11/quarterly_data.xlsx")
DATING(data_q$GDP,2006,3,"quarterly", "DatingData.csv")

##       Phase ]Start  ;End] Duration LevStart LevEnd Amplitude
## 1 Expansion   <NA> 2007Q4       NA       NA      0        NA
## 2 Recession 2007Q4 2009Q1        5        0      0       0.0
## 3 Expansion 2009Q1 2011Q1        8        0      0       0.0
## 4 Recession 2011Q1 2014Q1       12        0      0       0.0
## 5 Expansion 2014Q1 2016Q3       10        0      0       0.0
## 6 Recession 2016Q3 2017Q1        2        0      0       0.0
## 7 Expansion 2017Q1 2019Q3       10        0      0       0.0
## 8 Recession 2019Q3 2020Q2        3        0      0       0.3
## 9 Expansion 2020Q2   <NA>       NA        0     NA        NA
## 
##           Amplitude Duration
## Exp=]T;P]       0.0      9.3
## Rec=]P;T]       0.1      5.5
#Monthly Data
data_m=read.xlsx("https://mudulisilu.files.wordpress.com/2021/11/monthly_data.xlsx")
DATING(data_m$CPIInflation,2009,7,"monthly")

##       Phase  ]Start   ;End] Duration LevStart LevEnd Amplitude
## 1 Recession    <NA> 2011M11       NA       NA      0        NA
## 2 Expansion 2011M11  2013M9       22        0      0       0.9
## 3 Recession  2013M9  2014M9       12        0     -1       1.1
## 4 Expansion  2014M9  2016M5       20       -1      0       0.9
## 5 Recession  2016M5  2017M4       11        0     -1       1.3
## 6 Expansion  2017M4 2017M10        6       -1      0       1.3
## 7 Recession 2017M10 2018M11       13        0     -1       0.9
## 8 Expansion 2018M11    <NA>       NA       -1     NA        NA
## 
##           Amplitude Duration
## Exp=]T;P]       1.0       16
## Rec=]P;T]       1.1       12