-
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 toURL
. Then, writerstudio
in the fieldURL to open (optional)
. Finally, click onlaunch
.
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
- 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”
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)
seriesName <- getSymbols(seriesName, src="FRED", auto.assign = FALSE)
periodicity(seriesName)
## Yearly periodicity from 1960-01-01 to 2019-01-01
- Take the log of the series
seriesName <- log(seriesName)
dygraph(seriesName) %>%
dyRangeSelector()
seriesName_filtered_HP <- hpfilter(seriesName,
freq = 6.25
)
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()
dygraph(cycleHP) %>%
dyRangeSelector()
seriesName_filtered_Hamilton <- yth_filter(seriesName,
h = 2,
p = 4,
output = c("x", "trend", "cycle"))
Rename columns
colnames(seriesName_filtered_Hamilton) <- c("actual",
"trendHamilton",
"cycleHamilton")
dygraph(seriesName_filtered_Hamilton[,1:2]) %>%
dyRangeSelector()
dygraph(seriesName_filtered_Hamilton[,3]) %>%
dyRangeSelector()
Tip: Copy and paste this link another tab of your browser.
https://rstudio.cloud/project/25043
Or simply ClickHERE