Skip to content

Commit 243f18f

Browse files
committed
first upload
1 parent 7cf4ead commit 243f18f

29 files changed

+32610
-2
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
.Rproj.user
2+
.Rhistory
3+
.RData
4+
.Ruserdata

License.png

7.09 KB
Loading

README.md

Lines changed: 179 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,179 @@
1-
# Tutorial-synthetic-control-methods-Felipe-Thesis-Chapter5
2-
This tutorial presents the analysis of the 5th chapter of my thesis
1+
RStudio: [![Binder](https://mybinder.org/badge_logo.svg)](https://mybinder.org/v2/gh/quarcs-lab/tutorial-long-run-filters/HEAD?urlpath=rstudio) [![DOI](https://zenodo.org/badge/239955444.svg)](https://zenodo.org/badge/latestdoi/239955444)
2+
3+
# Long Run vs Short Run Decompositions in R: The HP filter vs the Hamilton filter
4+
5+
- https://rpubs.com/quarcs-lab/long-run-filters
6+
7+
## Online Environments
8+
9+
- R Studio cloud project: https://rstudio.cloud/project/25043
10+
11+
- This notebook can also be executed online at [GESIS Notebooks](https://notebooks.gesis.org). Just copy the URL of this repository and paste it on the [BINDER form](https://notebooks.gesis.org/binder/) 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`.
12+
13+
14+
[![Creative Commons Lizenzvertrag](https://i.creativecommons.org/l/by-sa/4.0/88x31.png)](http://creativecommons.org/licenses/by-sa/4.0/)
15+
16+
---
17+
18+
Suggested Citation:
19+
20+
> Mendez C. (2020). Long Run vs Short Run Decompositions in R: The HP
21+
> filter vs the Hamilton filter. R Studio/RPubs. Available at
22+
> <https://rpubs.com/quarcs-lab/long-run-filters> . DOI: https://zenodo.org/badge/latestdoi/239955444
23+
24+
25+
---
26+
27+
# Tutorial
28+
29+
# Set parameters of the program
30+
31+
- Name of the series
32+
33+
<!-- end list -->
34+
35+
``` r
36+
seriesName <- "RGDPNAIDA666NRUG"
37+
```
38+
39+
Code examples for other series
40+
41+
- Total GDP of Japan: “JPNRGDPEXP”
42+
- GDP per capita of Japan: “RGDPCHJPA625NUPN”
43+
- GPD per capita of Bolivia: “NYGDPPCAPKDBOL”
44+
- Total GDP of Bolivia: “RGDPNABOA666NRUG”
45+
- Total GDP of Indonesia: “RGDPNAIDA666NRUG”
46+
47+
# Load libraries
48+
49+
``` r
50+
library(mFilter)
51+
library(quantmod)
52+
library(dplyr)
53+
library(ggplot2)
54+
library(dygraphs)
55+
library(xts)
56+
library(neverhpfilter)
57+
58+
# Change the presentation of decimal numbers to 3 and avoid scientific notation
59+
options(digits=3, scipen=999)
60+
```
61+
62+
# Import data
63+
64+
``` r
65+
seriesName <- getSymbols(seriesName, src="FRED", auto.assign = FALSE)
66+
```
67+
68+
``` r
69+
periodicity(seriesName)
70+
```
71+
72+
## Yearly periodicity from 1960-01-01 to 2019-01-01
73+
74+
# Transform the data
75+
76+
- Take the log of the series
77+
78+
<!-- end list -->
79+
80+
``` r
81+
seriesName <- log(seriesName)
82+
```
83+
84+
# Plot evolution of the variable
85+
86+
``` r
87+
dygraph(seriesName) %>%
88+
dyRangeSelector()
89+
```
90+
91+
![](tutorial-long-run-filters_files/figure-gfm/unnamed-chunk-6-1.png)<!-- -->
92+
93+
# Apply the HP filter
94+
95+
``` r
96+
seriesName_filtered_HP <- hpfilter(seriesName,
97+
freq = 6.25
98+
)
99+
```
100+
101+
## Plot the HP filter
102+
103+
### Long-run trend
104+
105+
Create matrix of actual, trend , and cycle values
106+
107+
``` r
108+
actual <- seriesName_filtered_HP[["x"]]
109+
trendHP <- seriesName_filtered_HP[["trend"]]
110+
cycleHP <- actual - trendHP
111+
112+
colnames(actual) <- c("actual")
113+
colnames(trendHP) <- c("trendHP")
114+
colnames(cycleHP) <- c("cycleHP")
115+
116+
actual_and_trend <- cbind(actual, trendHP)
117+
```
118+
119+
``` r
120+
dygraph(actual_and_trend[,1:2]) %>%
121+
dyRangeSelector()
122+
```
123+
124+
![](tutorial-long-run-filters_files/figure-gfm/unnamed-chunk-9-1.png)<!-- -->
125+
126+
### Short-run fluctuations
127+
128+
``` r
129+
dygraph(cycleHP) %>%
130+
dyRangeSelector()
131+
```
132+
133+
![](tutorial-long-run-filters_files/figure-gfm/unnamed-chunk-10-1.png)<!-- -->
134+
135+
# Apply the Hamilton filter
136+
137+
``` r
138+
seriesName_filtered_Hamilton <- yth_filter(seriesName,
139+
h = 2,
140+
p = 4,
141+
output = c("x", "trend", "cycle"))
142+
```
143+
144+
## Plot the Hamiltion filter
145+
146+
### Long-run trend
147+
148+
Rename columns
149+
150+
``` r
151+
colnames(seriesName_filtered_Hamilton) <- c("actual",
152+
"trendHamilton",
153+
"cycleHamilton")
154+
```
155+
156+
``` r
157+
dygraph(seriesName_filtered_Hamilton[,1:2]) %>%
158+
dyRangeSelector()
159+
```
160+
161+
![](tutorial-long-run-filters_files/figure-gfm/unnamed-chunk-13-1.png)<!-- -->
162+
163+
### Short-run fluctuation
164+
165+
``` r
166+
dygraph(seriesName_filtered_Hamilton[,3]) %>%
167+
dyRangeSelector()
168+
```
169+
170+
![](tutorial-long-run-filters_files/figure-gfm/unnamed-chunk-14-1.png)<!-- -->
171+
172+
# Run it in the cloud
173+
174+
Tip: Copy and paste this link another tab of your browser.
175+
176+
<https://rstudio.cloud/project/25043>
177+
178+
Or simply
179+
<a href="https://rstudio.cloud/project/25043" target="_blank">ClickHERE</a>

biblio.bib

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
@Article{angristEvans1998,
2+
author={Angrist, Joshua D and Evans, William N},
3+
title={Children and Their Parents' Labor Supply: Evidence from Exogenous Variation in Family Size},
4+
journal={American Economic Review},
5+
year={1998},
6+
volume={88},
7+
number={3},
8+
pages={450--477},
9+
url={http://www.jstor.org/stable/116844}
10+
},
11+
12+
@incollection{card1995,
13+
author = {Card, David},
14+
title = {Using Geographic Variation in College Proximity to Estimate the Return to Schooling},
15+
booktitle = {Aspects of Labour Market Behaviour: Essays in Honour of John Vanderkamp},
16+
publisher = {University of Toronto Press},
17+
pages = {201--222},
18+
address = {Toronto},
19+
year = {1995},
20+
editor = {Louis N. Christofides and E. Kenneth Grant and Robert Swidinsky}
21+
},
22+
23+
@article{meyer_al1995,
24+
author={Meyer, Bruce D. and Viscusi, W. Kip and Durbin, David L.},
25+
title={Workers' Compensation and Injury Duration: Evidence from a Natural Experiment},
26+
journal={American Economic Review},
27+
year=1995,
28+
volume={85},
29+
number={3},
30+
pages={322--340}
31+
},
32+
33+
@book{r4ds,
34+
author = {Hadley Wickham and Garrett Grolemund},
35+
title = {R for Data Science: Import, Tidy, Transform, Visualize, and Model Data},
36+
publisher = {O'Reilly Media},
37+
year = {2017},
38+
ISBN = {1491910399},
39+
URL = {http://r4ds.had.co.nz}
40+
},
41+
42+
@book{wooldridge,
43+
author = {Jeffrey M. Wooldridge},
44+
title = {Introductory Econometrics: A Modern Approach},
45+
publisher = {Cengage Learning},
46+
edition = {6},
47+
year = {2015},
48+
isbn = {130527010X}
49+
},
50+
51+
@article{solon_al2015,
52+
title={What are We Weighting For?},
53+
author={Solon, Gary and Haider, Steven J. and Wooldridge, Jeffrey M.},
54+
journal={Journal of Human Resources},
55+
volume={50},
56+
number={2},
57+
pages={301--316},
58+
year={2015},
59+
doi={10.3368/jhr.50.2.301}
60+
},
61+
62+
@incollection{stockYogo2005,
63+
author = {Stock, James H. and Motohiro Yogo},
64+
title = {Testing for Weak Instruments in Linear {IV} Regression},
65+
booktitle = {Identification and Inference for Econometric Models: Essays in Honor of Thomas Rothenberg},
66+
publisher = {Cambridge University Press},
67+
pages = {80--108},
68+
address = {Cambridge},
69+
year = {2005},
70+
editor = {Andrews, D.W.K. and Stock, J.H.}
71+
},
72+
73+
@article{winshipRadbill1994,
74+
author = {Christopher Winship and Larry Radbill},
75+
title ={Sampling Weights and Regression Analysis},
76+
journal = {Sociological Methods \& Research},
77+
volume = {23},
78+
number = {2},
79+
pages = {230-257},
80+
year = {1994},
81+
doi = {10.1177/0049124194023002004}
82+
}
83+
84+
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
channels:
2+
- conda-forge
3+
- defaults
4+
dependencies:
5+
- python =3.7
6+
- bokeh
7+
- pip
8+
- geopandas
9+
- seaborn
10+
- descartes
11+
- folium
12+
- ipython
13+
- ipywidgets
14+
- jupyterlab
15+
- pysal
16+
- splot
17+
- mapclassify
18+
- nbconvert
19+
- networkx
20+
- palettable
21+
- scikit-learn
22+
- seaborn
23+
- statsmodels
24+
- hvplot
25+
- holoviews
26+
- geoviews
27+
- watermark
28+
- rasterio
29+
- cenpy
30+
- contextily
31+
- vega_datasets
32+
- pip:
33+
- segregation==1.4

binder/apt.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#libgdal-dev
2+
#libproj-dev
3+
#libgeos-dev
4+
#libudunits2-dev

binder/install.R

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
#install.packages("AER", repos = "https://cloud.r-project.org/", dependencies=TRUE)
2+
#install.packages("basetheme", repos = "https://cloud.r-project.org/", dependencies=TRUE)
3+
#install.packages("barsurf", repos = "https://cloud.r-project.org/", dependencies=TRUE)
4+
#install.packages("bivariate", repos = "https://cloud.r-project.org/", dependencies=TRUE)
5+
#install.packages("BMS", repos = "https://cloud.r-project.org/", dependencies=TRUE)
6+
#install.packages("ConvergenceClubs", repos = "https://cloud.r-project.org/", dependencies=TRUE)
7+
#install.packages("dineq", repos = "https://cloud.r-project.org/", dependencies=TRUE)
8+
install.packages("dygraphs", repos = "https://cloud.r-project.org/", dependencies=TRUE)
9+
#install.packages("educineq", repos = "https://cloud.r-project.org/", dependencies=TRUE)
10+
#install.packages("ExPanDaR", repos = "https://cloud.r-project.org/", dependencies=TRUE)
11+
#install.packages("ggmap", repos = "https://cloud.r-project.org/", dependencies=TRUE)
12+
#install.packages("ggridges", repos = "https://cloud.r-project.org/", dependencies=TRUE)
13+
#install.packages("ggpointdensity", repos = "https://cloud.r-project.org/", dependencies=TRUE)
14+
#install.packages("ggspatial", repos = "https://cloud.r-project.org/", dependencies=TRUE)
15+
#install.packages("ggthemes", repos = "https://cloud.r-project.org/", dependencies=TRUE)
16+
#install.packages("GWmodel", repos = "https://cloud.r-project.org/", dependencies=TRUE)
17+
#install.packages("hdrcde", repos = "https://cloud.r-project.org/", dependencies=TRUE)
18+
#install.packages("ineq", repos = "https://cloud.r-project.org/", dependencies=TRUE)
19+
#install.packages("ineqJD", repos = "https://cloud.r-project.org/", dependencies=TRUE)
20+
#install.packages("intoo", repos = "https://cloud.r-project.org/", dependencies=TRUE)
21+
#install.packages("ivpack", repos = "https://cloud.r-project.org/", dependencies=TRUE)
22+
#install.packages("isoband", repos = "https://cloud.r-project.org/", dependencies=TRUE)
23+
#install.packages("KernSmooth", repos = "https://cloud.r-project.org/", dependencies=TRUE)
24+
#install.packages("lorenz", repos = "https://cloud.r-project.org/", dependencies=TRUE)
25+
#install.packages("margins", repos = "https://cloud.r-project.org/", dependencies=TRUE)
26+
#install.packages("McSpatial", repos = "https://cloud.r-project.org/", dependencies=TRUE)
27+
install.packages("mFilter", repos = "https://cloud.r-project.org/", dependencies=TRUE)
28+
install.packages("neverhpfilter", repos = "https://cloud.r-project.org/", dependencies=TRUE)
29+
#install.packages("np", repos = "https://cloud.r-project.org/", dependencies=TRUE)
30+
#install.packages("patchwork", repos = "https://cloud.r-project.org/", dependencies=TRUE)
31+
#install.packages("pder", repos = "https://cloud.r-project.org/", dependencies=TRUE)
32+
#install.packages("pdfCluster", repos = "https://cloud.r-project.org/", dependencies=TRUE)
33+
#install.packages("pglm", repos = "https://cloud.r-project.org/", dependencies=TRUE)
34+
#install.packages("plotly", repos = "https://cloud.r-project.org/", dependencies=TRUE)
35+
install.packages("quantmod", repos = "https://cloud.r-project.org/", dependencies=TRUE)
36+
#install.packages("RColorBrewer", repos = "https://cloud.r-project.org/", dependencies=TRUE)
37+
#install.packages("remotes", repos = "https://cloud.r-project.org/", dependencies=TRUE)
38+
#install.packages("rgeos", repos = "https://cloud.r-project.org/", dependencies=TRUE)
39+
#install.packages("sf", repos = "https://cloud.r-project.org/", dependencies=TRUE)
40+
#install.packages("skimr", repos = "https://cloud.r-project.org/", dependencies=TRUE)
41+
#install.packages("spanel", repos = "https://cloud.r-project.org/", dependencies=TRUE)
42+
#install.packages("spatialprobit", repos = "https://cloud.r-project.org/", dependencies=TRUE)
43+
#install.packages("spatialreg", repos = "https://cloud.r-project.org/", dependencies=TRUE)
44+
#install.packages("spdep", repos = "https://cloud.r-project.org/", dependencies=TRUE)
45+
#install.packages("spgwr", repos = "https://cloud.r-project.org/", dependencies=TRUE)
46+
#install.packages("sphet", repos = "https://cloud.r-project.org/", dependencies=TRUE)
47+
#install.packages("splm", repos = "https://cloud.r-project.org/", dependencies=TRUE)
48+
#install.packages("spsur", repos = "https://cloud.r-project.org/", dependencies=TRUE)
49+
#install.packages("strucchange", repos = "https://cloud.r-project.org/", dependencies=TRUE)
50+
#install.packages("tidyverse", repos = "https://cloud.r-project.org/", dependencies=TRUE)
51+
install.packages("ggplot2", repos = "https://cloud.r-project.org/", dependencies=TRUE)
52+
install.packages("dplyr", repos = "https://cloud.r-project.org/", dependencies=TRUE)
53+
#install.packages("tmap", repos = "https://cloud.r-project.org/", dependencies=TRUE)
54+
#install.packages("viridis", repos = "https://cloud.r-project.org/", dependencies=TRUE)
55+
#install.packages("wbstats", repos = "https://cloud.r-project.org/", dependencies=TRUE)
56+
install.packages("xts", repos = "https://cloud.r-project.org/", dependencies=TRUE)
57+
install.packages("tidyverse", repos = "https://cloud.r-project.org/", dependencies=TRUE)
58+
install.packages("readr", repos = "https://cloud.r-project.org/", dependencies=TRUE)
59+
install.packages("Synth", repos = "https://cloud.r-project.org/", dependencies=TRUE)
60+
install.packages("readxl", repos = "https://cloud.r-project.org/", dependencies=TRUE)
61+
62+
#install.packages("ggstatsplot", repos = "https://cloud.r-project.org/", dependencies=TRUE)

binder/runtime.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
r-2020-12-25
5.61 MB
Binary file not shown.

0 commit comments

Comments
 (0)