Skip to content

quarcs-lab/tutorial-long-run-filters

Repository files navigation

RStudio: Binder DOI

Long Run vs Short Run Decompositions in R: The HP filter vs the Hamilton filter

Online Environments

  • R Studio cloud project: https://rstudio.cloud/project/25043

  • This notebook can also be executed online at GESIS Notebooks. Just copy the URL of this repository and paste it on the BINDER form To open a virtual R Studio session, make sure you change you click on File and change it to URL. Then, write rstudio in the field URL to open (optional). Finally, click on launch.

Creative Commons Lizenzvertrag


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 . DOI: https://zenodo.org/badge/latestdoi/239955444


Tutorial

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”

Load libraries

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

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

Import data

seriesName <- getSymbols(seriesName, src="FRED", auto.assign = FALSE)
periodicity(seriesName)
## Yearly periodicity from 1960-01-01 to 2019-01-01

Transform the data

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

Plot evolution of the variable

dygraph(seriesName) %>%
  dyRangeSelector()

Apply the HP filter

seriesName_filtered_HP <- hpfilter(seriesName,
                                  freq = 6.25      
                                     )

Plot the HP filter

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()

Short-run fluctuations

dygraph(cycleHP)  %>%
  dyRangeSelector()

Apply the Hamilton filter

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

Plot the Hamiltion filter

Long-run trend

Rename columns

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

Short-run fluctuation

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

Run it in the cloud

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

https://rstudio.cloud/project/25043

Or simply ClickHERE

About

Long Run vs Short Run Decompositions in R

Topics

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages