-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtutorial-long-run-filters.Rmd
207 lines (133 loc) · 3.95 KB
/
tutorial-long-run-filters.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
---
title: "Long Run vs Short Run Decompositions in R"
author: "Carlos Mendez"
subtitle: The HP filter vs the Hamilton filter
output:
html_document:
code_folding: show
highlight: monochrome
number_sections: yes
theme: cosmo
toc: yes
toc_depth: 4
toc_float:
collapsed: no
smooth_scroll: no
github_document: default
always_allow_html: true
---
<style>
h1.title {font-size: 18pt; color: DarkBlue;}
body, h1, h2, h3, h4 {font-family: "Palatino", serif;}
body {font-size: 12pt;}
/* Headers */
h1,h2,h3,h4,h5,h6{font-size: 14pt; color: #00008B;}
body {color: #333333;}
a, a:hover {color: #8B3A62;}
pre {font-size: 12px;}
</style>
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.
![](License.png)
# Set parameters of the program
- Name of the series
```{r}
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
```{r message=FALSE, warning=FALSE}
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)
```
# Import data
```{r}
seriesName <- getSymbols(seriesName, src="FRED", auto.assign = FALSE)
```
```{r}
periodicity(seriesName)
```
# Transform the data
- Take the log of the series
```{r}
seriesName <- log(seriesName)
```
# Plot evolution of the variable
```{r}
dygraph(seriesName) %>%
dyRangeSelector()
```
# Apply the HP filter
```{r}
seriesName_filtered_HP <- hpfilter(seriesName,
freq = 6.25
)
```
## Plot the HP filter
### Long-run trend
Create matrix of actual, trend , and cycle values
```{r}
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)
```
```{r}
dygraph(actual_and_trend[,1:2]) %>%
dyRangeSelector()
```
### Short-run fluctuations
```{r}
dygraph(cycleHP) %>%
dyRangeSelector()
```
# Apply the Hamilton filter
```{r}
seriesName_filtered_Hamilton <- yth_filter(seriesName,
h = 2,
p = 4,
output = c("x", "trend", "cycle"))
```
## Plot the Hamiltion filter
### Long-run trend
Rename columns
```{r}
colnames(seriesName_filtered_Hamilton) <- c("actual",
"trendHamilton",
"cycleHamilton")
```
```{r}
dygraph(seriesName_filtered_Hamilton[,1:2]) %>%
dyRangeSelector()
```
### Short-run fluctuation
```{r}
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 <a href="https://rstudio.cloud/project/25043" target="_blank">ClickHERE</a>
# References
- [Schüler, Y., 2018. On the cyclical properties of Hamilton's regression filter (No. 03/2018). Deutsche Bundesbank.](https://www.econstor.eu/bitstream/10419/174891/1/1014338883.pdf)
- <http://past.rinfinance.com/agenda/2018/JustinShea.pdf>
- <https://www.r-econometrics.com/timeseries/economic-cycle-extraction/>
- <https://www.datacamp.com/community/blog/r-xts-cheat-sheet>
- <https://rstudio.github.io/dygraphs/>