Suggested Citation:

Mendez C. (2020). Long Run vs Short Run Decompositions in R: The HP filter vs the Hamilton filter. R Studio/RPubs. Available at https://rpubs.com/quarcs-lab/long-run-filters

This work is licensed under the Creative Commons Attribution-Share Alike 4.0 International License.

1 Set parameters of the program

  • Name of the series
seriesName <- "RGDPNAIDA666NRUG"

Code examples for other series

  • Total GDP of Japan: “JPNRGDPEXP”
  • GDP per capita of Japan: “RGDPCHJPA625NUPN”
  • GPD per capita of Bolivia: “NYGDPPCAPKDBOL”
  • Total GDP of Bolivia: “RGDPNABOA666NRUG”
  • Total GDP of Indonesia: “RGDPNAIDA666NRUG”

2 Load libraries

library(mFilter)
library(quantmod)
library(dplyr)
library(ggplot2)
library(dygraphs)
library(xts)
library(neverhpfilter)

# Change the presentation of decimal numbers to 4 and avoid scientific notation
options(prompt="R> ", digits=3, scipen=999)

3 Import data

seriesName <- getSymbols(seriesName, src="FRED", auto.assign = FALSE)
## 'getSymbols' currently uses auto.assign=TRUE by default, but will
## use auto.assign=FALSE in 0.5-0. You will still be able to use
## 'loadSymbols' to automatically load data. getOption("getSymbols.env")
## and getOption("getSymbols.auto.assign") will still be checked for
## alternate defaults.
## 
## This message is shown once per session and may be disabled by setting 
## options("getSymbols.warning4.0"=FALSE). See ?getSymbols for details.
periodicity(seriesName)
## Yearly periodicity from 1960-01-01 to 2019-01-01

4 Transform the data

  • Take the log of the series
seriesName <- log(seriesName)

5 Plot evolution of the variable

dygraph(seriesName) %>% 
  dyRangeSelector()

6 Apply the HP filter

seriesName_filtered_HP <- hpfilter(seriesName, 
                                  freq = 6.25      
                                     )

6.1 Plot the HP filter

6.1.1 Long-run trend

Create matrix of actual, trend , and cycle values

actual  <- seriesName_filtered_HP[["x"]]
trendHP <- seriesName_filtered_HP[["trend"]]
cycleHP <- actual - trendHP

colnames(actual)   <- c("actual")
colnames(trendHP)  <- c("trendHP")
colnames(cycleHP)  <- c("cycleHP")

actual_and_trend <- cbind(actual, trendHP)
dygraph(actual_and_trend[,1:2]) %>%
  dyRangeSelector()

6.1.2 Short-run fluctuations

dygraph(cycleHP)  %>% 
  dyRangeSelector()

7 Apply the Hamilton filter

seriesName_filtered_Hamilton <- yth_filter(seriesName, 
                                           h = 2, 
                                           p = 4, 
                                           output = c("x", "trend", "cycle"))

7.1 Plot the Hamiltion filter

7.1.1 Long-run trend

Rename columns

colnames(seriesName_filtered_Hamilton)  <- c("actual",
                                             "trendHamilton",
                                             "cycleHamilton")
dygraph(seriesName_filtered_Hamilton[,1:2])  %>% 
  dyRangeSelector()

7.1.2 Short-run fluctuation

dygraph(seriesName_filtered_Hamilton[,3])  %>% 
  dyRangeSelector()

8 Run it in the cloud

Tip: Copy and paste this link another tab of your browser.

https://rstudio.cloud/project/25043

Or simply ClickHERE