rm(list = ls())
set.seed(123); options(scipen=999); options(warn=-1)
list.of.packages <- c("pacman")
new.packages <- list.of.packages[!(list.of.packages %in% installed.packages()[,"Package"])]
if(length(new.packages)) install.packages(new.packages)
pacman::p_load(aTSA,dplyr,tidyr,gamm4,ggplot2,lubridate,plotly,reshape2,mgcv,aws.s3,stringr,tseries,data.table,tibbletime,tsbox)
pacman::p_load(dynlm,dplyr,ISOweek,readxl,ggplot2,reshape2,sandwich,tidyquant,
tidyverse,tseries,rsdmx, pco, lmtest, smooth,
zoo,lubridate,x12,tidyr,lpirfs)
### Full replication: data, analysis, results, plotting via Jupyter Notebook (Markdown)
url <- "https://stats.oecd.org/restsdmx/sdmx.ashx/GetData/PRICES_CPI/OECD.CPALTT01.IXOB.M/all?startTime=1988-01&endTime=2023-03"
xml_data <- readSDMX(url)
dat1 <- as.data.frame(xml_data)
dat1 = dat1 %>%
dplyr::select(LOCATION, SUBJECT, obsTime, obsValue) %>%
dcast(obsTime +LOCATION ~ SUBJECT , value.var = "obsValue") %>%
mutate(Y = noquote( substr(obsTime, 1, 4) ) %>% as.numeric()) %>%
dplyr::rename(p_c = CPALTT01) %>%
dplyr::filter(LOCATION %in% 'OECD') %>%
mutate(date = paste0(obsTime,'-01') %>% as.Date('%Y-%m-%d')) %>%
dplyr::select(date, p_c)
# https://www.eia.gov/international/data/world/petroleum-and-other-liquids/quarterly-petroleum-and-other-liquids-production?pd=5&p=0000000000000000000000000000000000vg&u=0&f=Q&v=mapbubble&a=-&i=none&vo=value&&t=C&g=00000000000000000000000000000000000000000000000001&l=249-ruvvvvvfvtvnvv1vrvvvvfvvvvvvfvvvou20evvvvvvvvvvnvvvs0008&s=94694400000&e=1656633600000
obj =
c("q y oil_y
Q1 1973 54766.04444
Q2 1973 55701.24176
Q3 1973 57299.8913
Q4 1973 54930.44565
Q1 1974 55930.25556
Q2 1974 57084.49451
Q3 1974 54970.72826
Q4 1974 54896.94565
Q1 1975 51728.33333
Q2 1975 51530.12088
Q3 1975 55187.96739
Q4 1975 52828.42391
Q1 1976 54541.15385
Q2 1976 55659.23077
Q3 1976 57854.69565
Q4 1976 61272.75
Q1 1977 59505.1
Q2 1977 59191.67033
Q3 1977 58978.3913
Q4 1977 61141.6413
Q1 1978 57865.62222
Q2 1978 59386.87912
Q3 1978 60778.06522
Q4 1978 62544.8587
Q1 1979 60760.61111
Q2 1979 62817
Q3 1979 63388.88043
Q4 1979 63688.1413
Q1 1980 62226.56712
Q2 1980 60056.50288
Q3 1980 59126.48186
Q4 1980 56855.13458
Q1 1981 58290.23041
Q2 1981 57035.71434
Q3 1981 54431.3247
Q4 1981 54501.61771
Q1 1982 53650.0401
Q2 1982 52143.94271
Q3 1982 53205.08733
Q4 1982 54805.36478
Q1 1983 51146.63961
Q2 1983 52167.607
Q3 1983 54763.37364
Q4 1983 54864.29809
Q1 1984 54713.38903
Q2 1984 55014.31589
Q3 1984 54061.62383
Q4 1984 54214.36011
Q1 1985 53995.75293
Q2 1985 52775.74756
Q3 1985 52875.17825
Q4 1985 56202.10259
Q1 1986 55485.43249
Q2 1986 56313.00746
Q3 1986 57477.0388
Q4 1986 56008.56748
Q1 1987 54922.81557
Q2 1987 55311.02344
Q3 1987 58270.71084
Q4 1987 58043.61303
Q1 1988 57313.48682
Q2 1988 57589.78125
Q3 1988 58578.4194
Q4 1988 61278.66243
Q1 1989 58531.1817
Q2 1989 59019.20755
Q3 1989 60155.02311
Q4 1989 61465.78591
Q1 1990 61401.40899
Q2 1990 61152.1956
Q3 1990 58992.19276
Q4 1990 60467.99387
Q1 1991 60521.45736
Q2 1991 59128.17713
Q3 1991 60068.66548
Q4 1991 60789.90975
Q1 1992 60486.6509
Q2 1992 59457.08011
Q3 1992 59789.1281
Q4 1992 60675.12004
Q1 1993 60596.30368
Q2 1993 59589.62256
Q3 1993 59962.19434
Q4 1993 60557.60348
Q1 1994 60990.42222
Q2 1994 60808.16593
Q3 1994 60896.28261
Q4 1994 61993.54348
Q1 1995 61916.3
Q2 1995 62126.21978
Q3 1995 62724.37826
Q4 1995 62951.0413
Q1 1996 63447.53187
Q2 1996 63444.47802
Q3 1996 63658.18478
Q4 1996 64712.77174
Q1 1997 65411.69111
Q2 1997 65355.29451
Q3 1997 65770.81522
Q4 1997 66667.825
Q1 1998 67914.23333
Q2 1998 67380.23407
Q3 1998 66260.175
Q4 1998 66602.93804
Q1 1999 67088.78
Q2 1999 65071.88681
Q3 1999 65757.77174
Q4 1999 65969.87174
Q1 2000 66867.24182
Q2 2000 68038.77535
Q3 2000 69251.55892
Q4 2000 69925.70126
Q1 2001 69111.68411
Q2 2001 67476.15101
Q3 2001 68109.00412
Q4 2001 67844.97655
Q1 2002 66933.77761
Q2 2002 66620.62097
Q3 2002 67185.90715
Q4 2002 68405.36115
Q1 2003 68988.40387
Q2 2003 68522.75773
Q3 2003 69101.80584
Q4 2003 71207.56301
Q1 2004 71872.00545
Q2 2004 72048.28886
Q3 2004 73036.51587
Q4 2004 73417.83776
Q1 2005 73661.28499
Q2 2005 74206.87844
Q3 2005 73696.41432
Q4 2005 73918.00328
Q1 2006 73665.56804
Q2 2006 73335.09049
Q3 2006 73913.75482
Q4 2006 73592.49375
Q1 2007 73235.0726
Q2 2007 73053.56667
Q3 2007 73006.61495
Q4 2007 73994.61788
Q1 2008 74799.36854
Q2 2008 74623.65997
Q3 2008 74436.40858
Q4 2008 74024.42987
Q1 2009 72585.14956
Q2 2009 72681.95543
Q3 2009 73381.37023
Q4 2009 73917.18733
Q1 2010 74248.13548
Q2 2010 74178.43862
Q3 2010 74222.45989
Q4 2010 74976.14229
Q1 2011 75279.52155
Q2 2011 74135.37077
Q3 2011 74950.67687
Q4 2011 75965.4284
Q1 2012 76859.02493
Q2 2012 76673.87873
Q3 2012 76331.64311
Q4 2012 76871.14626
Q1 2013 76310.38839
Q2 2013 76692.09898
Q3 2013 76923.02369
Q4 2013 77134.92485
Q1 2014 77836.71658
Q2 2014 77625.61125
Q3 2014 78598.54348
Q4 2014 80359.1582
Q1 2015 80382.06985
Q2 2015 80667.77136
Q3 2015 81249.13661
Q4 2015 81546.80213
Q1 2016 81308.9794
Q2 2016 79909.16779
Q3 2016 80624.17246
Q4 2016 82308.673
Q1 2017 81027.45634
Q2 2017 80778.47934
Q3 2017 81435.77217
Q4 2017 81761.33372
Q1 2018 82308.01815
Q2 2018 82090.54566
Q3 2018 83159.52752
Q4 2018 84357.65868
Q1 2019 82390.70383
Q2 2019 81796.00347
Q3 2019 81414.49958
Q4 2019 83075.95641
Q1 2020 82498.38313
Q2 2020 74713.89115
Q3 2020 72327.8824
Q4 2020 74554.62876
Q1 2021 75444.61573
Q2 2021 76017.95273
Q3 2021 77611.53893
Q4 2021 79490.93369
Q1 2022 80263.51067
Q2 2022 79289.71383
Q3 2022 81095.96257")
# read ----
x <- read.table(textConnection(obj), header=TRUE)
x$date = seq(as.Date(paste0(min(x$y),"/03/1")),
length.out = length(x$y), by = 'quarter')
# growth variables define year-on-year
lag_diff = 4
x$doil_y = c(rep(NA, lag_diff), x$oil_y %>% log %>% diff(1, lag = lag_diff) * 100)
x = x %>%
dplyr::select(date, doil_y, oil_y) %>%
mutate(oil_y = log(oil_y))
oil = c('OECDPRINTO01IXOBSAM',
"USREC",
'OECDNMERECM',
'WUIGLOBALWEIGHTAVG',
'CPIAUCSL') %>%
tq_get(get = "economic.data",
from="1980-01-01",
to="2022-06-01") %>%
as.data.frame() %>%
reshape2::dcast(
formula = date~symbol,
value.var="price") %>%
mutate(dates = as.yearqtr(date) ) %>%
mutate(WUIGLOBALWEIGHTAVG = dplyr::lag(WUIGLOBALWEIGHTAVG, n=2))
oil = merge(oil, dat1, key = 'date', all = F)
oil = merge(oil, x, key = 'date', all = T)
oil = oil[complete.cases(oil$doil_y), ]
oil = oil %>%
dplyr::filter(date >= '1988-03-01',
date <= '2022-06-01')
oil$OECD_gap = mFilter::hpfilter(oil$OECDPRINTO01IXOBSAM %>% log,freq = 1600)$cycle
oil$dOECD = c(rep(NA, lag_diff), oil$OECDPRINTO01IXOBSAM %>% log %>% diff(1, lag = lag_diff) * 100)
oil$dwui = c(rep(NA, lag_diff), log(oil$WUIGLOBALWEIGHTAVG) %>% diff(1, lag = lag_diff) * 100)
obj =
c("date CRUDE_AVG CRUDE_BRENT CRUDE_DUBAI CRUDE_WTI
197901 17.45 18.95 15.95 NA
197902 20.75 22.00 19.50 NA
197903 22.02 23.25 20.80 NA
197904 22.43 23.65 21.20 NA
197905 33.50 32.75 34.25 NA
197906 34.67 36.50 32.85 NA
197907 33.50 35.00 32.00 NA
197908 34.00 35.75 32.25 NA
197909 35.50 36.50 34.50 NA
197910 37.25 38.50 36.00 NA
197911 40.75 42.00 39.50 NA
197912 39.75 40.50 39.00 NA
198001 39.00 40.00 38.00 NA
198002 37.25 38.50 36.00 NA
198003 37.00 38.25 35.75 NA
198004 36.58 38.15 35.00 NA
198005 37.05 38.50 35.60 NA
198006 37.00 38.00 36.00 NA
198007 35.38 37.40 33.35 NA
198008 32.95 33.60 32.30 NA
198009 32.83 33.40 32.25 NA
198010 37.35 37.90 36.80 NA
198011 40.30 40.85 39.75 NA
198012 39.75 40.15 39.35 NA
198101 39.77 40.30 39.25 NA
198102 37.90 38.70 37.10 NA
198103 37.60 38.35 36.85 NA
198104 36.37 37.19 35.55 NA
198105 34.28 35.19 33.38 NA
198106 32.71 33.25 32.17 NA
198107 33.51 35.06 31.96 NA
198108 33.98 35.80 32.17 NA
198109 33.99 35.91 32.06 NA
198110 34.61 36.54 32.68 NA
198111 35.68 37.12 34.24 NA
198112 35.41 36.70 34.12 NA
198201 35.05 35.85 34.00 35.30
198202 32.27 31.62 30.40 34.80
198203 30.17 29.30 28.60 32.60
198204 31.57 33.12 30.88 30.70
198205 33.04 35.18 33.35 30.60
198206 32.77 34.60 32.65 31.05
198207 32.92 33.70 31.70 33.35
198208 32.42 32.85 31.20 33.20
198209 33.47 34.25 33.05 33.10
198210 33.78 35.00 33.40 32.95
198211 32.85 33.80 31.75 33.00
198212 31.47 31.75 30.10 32.55
198301 30.65 30.85 30.05 31.06
198302 29.11 29.10 29.00 29.24
198303 28.32 28.20 28.10 28.66
198304 29.69 29.75 28.70 30.63
198305 29.36 29.60 28.50 29.98
198306 29.97 30.20 28.75 30.95
198307 30.47 30.80 29.00 31.60
198308 30.65 31.10 28.90 31.96
198309 30.07 30.35 28.60 31.26
198310 29.62 29.85 28.60 30.40
198311 29.17 29.20 28.30 30.00
198312 28.81 28.95 28.25 29.24
198401 29.04 29.56 27.73 29.84
198402 29.20 29.88 27.65 30.08
198403 29.53 30.08 27.79 30.73
198404 29.57 30.13 27.96 30.63
198405 29.46 29.90 28.03 30.45
198406 29.05 29.20 27.91 30.04
198407 27.99 27.91 27.26 28.79
198408 28.17 28.11 27.23 29.17
198409 28.35 28.36 27.40 29.30
198410 27.85 27.81 27.11 28.64
198411 27.65 27.70 27.09 28.16
198412 26.81 26.98 26.76 26.70
198501 26.63 26.95 27.30 25.65
198502 27.60 28.20 27.25 27.35
198503 27.85 28.10 27.15 28.30
198504 27.80 27.90 26.65 28.85
198505 26.65 26.65 25.65 27.65
198506 26.37 26.45 25.45 27.20
198507 26.38 26.60 25.70 26.85
198508 27.02 27.15 26.30 27.60
198509 26.98 27.15 26.15 27.65
198510 27.82 27.90 26.70 28.85
198511 28.60 29.05 26.90 29.85
198512 26.50 25.85 26.30 27.35
198601 24.68 25.75 25.40 22.90
198602 15.92 17.55 14.80 15.40
198603 12.65 13.85 11.45 12.65
198604 11.90 12.50 10.30 12.90
198605 13.72 14.20 11.50 15.45
198606 11.85 11.85 10.20 13.50
198607 9.62 9.45 7.85 11.55
198608 13.70 13.65 12.15 15.30
198609 14.08 14.20 13.10 14.95
198610 13.95 13.80 13.15 14.90
198611 14.45 14.55 13.55 15.25
198612 15.68 15.90 14.90 16.25
198701 18.07 18.40 17.20 18.60
198702 17.25 17.30 16.70 17.75
198703 17.75 17.90 16.90 18.45
198704 17.90 18.10 16.95 18.65
198705 18.40 18.75 17.05 19.40
198706 18.72 18.85 17.25 20.05
198707 19.62 19.80 17.75 21.30
198708 18.82 18.95 17.30 20.20
198709 18.28 18.35 17.00 19.50
198710 18.57 18.80 17.05 19.85
198711 17.75 17.80 16.60 18.85
198712 16.63 17.10 15.50 17.30
198801 16.47 16.85 15.40 17.15
198802 15.85 15.75 15.05 16.75
198803 14.78 14.75 13.40 16.20
198804 16.47 16.60 14.95 17.85
198805 16.22 16.40 14.85 17.40
198806 15.33 15.55 13.80 16.65
198807 14.48 14.90 13.05 15.50
198808 14.55 14.95 13.15 15.55
198809 13.10 13.30 11.55 14.45
198810 12.18 12.45 10.30 13.80
198811 12.53 13.00 10.60 14.00
198812 14.65 15.15 12.50 16.30
198901 16.48 17.00 14.45 18.00
198902 16.35 16.65 14.60 17.80
198903 18.03 18.70 15.95 19.45
198904 19.20 19.75 16.90 20.95
198905 18.02 18.35 15.65 20.05
198906 17.63 17.50 15.40 20.00
198907 17.67 17.75 15.50 19.75
198908 16.88 17.10 15.00 18.55
198909 17.67 17.80 15.60 19.60
198910 18.42 19.00 16.15 20.10
198911 18.37 19.15 16.15 19.80
198912 19.35 19.85 17.10 21.10
199001 20.35 20.95 17.45 22.65
199002 19.60 19.90 16.80 22.10
199003 18.22 18.45 15.80 20.40
199004 16.55 16.75 14.30 18.60
199005 16.58 16.70 14.60 18.45
199006 15.27 15.70 13.25 16.85
199007 17.17 17.55 15.30 18.65
199008 26.40 27.05 25.00 27.15
199009 32.70 34.10 30.30 33.70
199010 34.50 36.05 31.55 35.90
199011 31.08 33.00 27.95 32.30
199012 26.13 28.00 23.25 27.15
199101 22.58 23.65 19.40 24.70
199102 18.13 19.40 14.45 20.55
199103 18.07 19.45 14.85 19.90
199104 18.47 19.25 15.35 20.80
199105 18.82 19.30 15.90 21.25
199106 17.93 18.20 15.40 20.20
199107 19.05 19.45 16.25 21.45
199108 19.37 19.75 16.65 21.70
199109 20.05 20.50 17.80 21.85
199110 21.47 22.20 18.95 23.25
199111 20.77 21.25 18.45 22.60
199112 17.75 18.40 15.30 19.55
199201 17.38 18.15 15.20 18.80
199202 17.62 18.10 15.75 19.00
199203 17.45 17.60 15.80 18.95
199204 18.63 18.95 16.70 20.25
199205 19.50 19.90 17.60 21.00
199206 20.83 21.15 19.00 22.35
199207 20.17 20.25 18.50 21.75
199208 19.62 19.75 17.80 21.30
199209 20.15 20.25 18.30 21.90
199210 20.08 20.30 18.25 21.70
199211 18.88 19.20 17.10 20.35
199212 17.93 18.15 16.25 19.40
199301 17.22 17.40 15.20 19.05
199302 18.17 18.45 16.00 20.05
199303 18.47 18.75 16.30 20.35
199304 18.43 18.65 16.35 20.30
199305 18.17 18.50 16.00 20.00
199306 17.47 17.65 15.60 19.15
199307 16.32 16.80 14.25 17.90
199308 16.48 16.70 14.75 18.00
199309 15.90 16.00 14.20 17.50
199310 16.52 16.60 14.80 18.15
199311 15.20 15.15 13.70 16.75
199312 13.77 13.60 12.15 15.55
199401 14.13 14.25 13.15 15.00
199402 13.78 13.80 12.80 14.75
199403 13.62 13.95 12.25 14.65
199404 15.08 15.15 13.80 16.30
199405 16.28 16.20 14.80 17.85
199406 17.17 16.75 15.70 19.05
199407 17.88 17.60 16.40 19.65
199408 17.00 16.80 15.85 18.35
199409 16.20 15.90 15.25 17.45
199410 16.47 16.40 15.35 17.65
199411 17.08 17.20 15.95 18.10
199412 15.94 15.96 14.70 17.16
199501 16.90 16.63 16.08 17.99
199502 17.42 17.08 16.64 18.53
199503 17.35 17.01 16.49 18.54
199504 18.65 18.67 17.40 19.87
199505 18.42 18.38 17.25 19.64
199506 17.36 17.35 16.24 18.50
199507 16.08 15.86 14.97 17.42
199508 16.47 16.07 15.37 17.96
199509 16.82 16.78 15.65 18.03
199510 16.12 16.12 14.92 17.33
199511 16.74 16.88 15.56 17.79
199512 17.87 17.96 16.81 18.83
199601 17.80 17.94 16.57 18.89
199602 17.70 17.97 16.05 19.07
199603 19.40 19.99 17.05 21.16
199604 20.66 21.01 17.77 23.20
199605 19.06 19.12 16.99 21.08
199606 18.51 18.27 17.00 20.27
199607 19.59 19.61 17.79 21.36
199608 20.44 20.58 18.77 21.97
199609 22.26 22.59 20.26 23.92
199610 23.61 24.18 21.71 24.94
199611 22.39 22.64 20.87 23.66
199612 23.62 23.90 21.65 25.32
199701 23.23 23.47 21.28 24.93
199702 20.42 20.83 18.60 21.83
199703 19.33 19.21 18.09 20.69
199704 17.88 17.47 16.77 19.40
199705 19.37 19.14 18.46 20.50
199706 17.92 17.55 17.34 18.87
199707 18.33 18.40 17.29 19.30
199708 18.70 18.71 17.77 19.62
199709 18.66 18.45 17.95 19.59
199710 20.04 19.85 19.06 21.21
199711 19.09 19.00 18.38 19.88
199712 17.09 17.00 16.18 18.09
199801 15.00 15.09 13.40 16.51
199802 14.10 14.06 12.43 15.81
199803 13.12 13.08 11.53 14.76
199804 13.50 13.39 11.79 15.32
199805 14.03 14.39 12.79 14.90
199806 12.48 12.06 11.67 13.71
199807 12.70 12.01 11.98 14.11
199808 12.49 11.88 12.18 13.40
199809 13.80 13.36 13.07 14.98
199810 13.26 12.56 12.81 14.42
199811 11.88 10.92 11.76 12.96
199812 10.41 9.80 10.12 11.31
199901 11.44 11.06 10.78 12.49
199902 10.75 10.20 10.05 12.01
199903 13.17 12.47 12.37 14.66
199904 15.87 15.30 14.96 17.34
199905 16.06 15.14 15.30 17.75
199906 16.39 15.77 15.52 17.89
199907 18.99 19.01 17.88 20.07
199908 20.27 20.22 19.34 21.25
199909 22.70 22.40 21.84 23.86
199910 21.95 21.95 21.26 22.64
199911 24.16 24.59 23.05 24.85
199912 25.10 25.59 23.63 26.08
200001 25.31 25.38 23.28 27.27
200002 27.22 27.70 24.68 29.28
200003 27.49 27.47 25.08 29.92
200004 23.47 22.54 22.03 25.84
200005 27.19 27.34 25.39 28.83
200006 29.62 29.68 27.32 31.86
200007 28.18 28.53 26.03 29.97
200008 29.26 29.43 27.05 31.31
200009 32.08 32.62 29.74 33.89
200010 31.40 30.93 30.22 33.05
200011 32.33 32.52 30.10 34.37
200012 25.20 25.13 22.09 28.40
200101 25.96 25.64 22.69 29.55
200102 27.24 27.41 24.74 29.57
200103 25.02 24.40 23.43 27.24
200104 25.72 25.64 24.09 27.42
200105 27.55 28.45 25.58 28.61
200106 26.97 27.72 25.61 27.56
200107 24.80 24.54 23.43 26.44
200108 25.82 25.70 24.32 27.45
200109 25.21 25.54 23.96 26.12
200110 20.73 20.48 19.54 22.18
200111 18.69 18.94 17.53 19.59
200112 18.52 18.60 17.64 19.31
200201 19.15 19.49 18.29 19.69
200202 19.98 20.29 18.91 20.72
200203 23.64 23.69 22.85 24.38
200204 25.43 25.65 24.41 26.24
200205 25.67 25.39 24.59 27.04
200206 24.49 24.13 23.83 25.51
200207 25.75 25.77 24.57 26.92
200208 26.78 26.63 25.32 28.37
200209 28.28 28.34 26.83 29.67
200210 27.53 27.55 26.18 28.85
200211 24.54 24.18 23.16 26.28
200212 27.89 28.52 25.70 29.44
200301 30.75 31.25 28.05 32.95
200302 32.88 32.65 30.20 35.80
200303 30.36 30.34 27.42 33.32
200304 25.56 25.02 23.43 28.22
200305 26.06 25.81 24.25 28.13
200306 27.92 27.55 25.50 30.71
200307 28.59 28.40 26.63 30.75
200308 29.68 29.83 27.62 31.58
200309 26.88 27.10 25.27 28.28
200310 29.01 29.59 27.13 30.32
200311 29.12 28.77 27.50 31.09
200312 29.97 29.93 27.86 32.12
200401 31.37 31.18 28.68 34.24
200402 31.33 30.87 28.40 34.73
200403 33.67 33.80 30.46 36.73
200404 33.71 33.36 31.07 36.71
200405 37.56 37.92 34.47 40.29
200406 35.54 35.19 33.41 38.03
200407 37.89 38.37 34.48 40.82
200408 42.08 43.03 38.30 44.92
200409 41.60 43.38 35.48 45.93
200410 46.88 49.82 37.58 53.25
200411 42.13 43.05 34.88 48.45
200412 39.04 39.64 34.26 43.23
200501 42.97 44.28 37.81 46.82
200502 44.82 45.56 40.94 47.96
200503 50.94 53.08 45.58 54.17
200504 50.64 51.86 47.10 52.96
200505 47.83 48.67 45.00 49.81
200506 53.89 54.31 50.98 56.39
200507 56.37 57.58 52.85 58.67
200508 61.89 64.09 56.63 64.96
200509 61.69 62.98 56.54 65.54
200510 58.19 58.52 53.67 62.36
200511 55.04 55.54 51.31 58.28
200512 56.43 56.75 53.13 59.41
200601 62.46 63.57 58.31 65.48
200602 59.70 59.92 57.58 61.62
200603 60.93 62.25 57.65 62.89
200604 67.97 70.44 64.06 69.41
200605 68.68 70.19 64.91 70.93
200606 68.29 68.86 65.08 70.93
200607 72.45 73.90 69.05 74.40
200608 71.81 73.61 68.78 73.04
200609 62.12 62.77 59.77 63.82
200610 57.91 58.38 56.50 58.85
200611 58.14 58.48 56.82 59.13
200612 60.99 62.31 58.67 62.00
200701 53.52 54.30 52.01 54.24
200702 57.56 57.76 55.68 59.25
200703 60.60 62.14 59.05 60.60
200704 65.06 67.40 63.84 63.94
200705 65.16 67.48 64.54 63.45
200706 68.19 71.32 65.76 67.49
200707 73.60 77.20 69.46 74.14
200708 70.13 70.80 67.21 72.38
200709 76.76 77.13 73.25 79.91
200710 81.97 82.86 77.14 85.90
200711 91.34 92.53 86.73 94.76
200712 89.52 91.45 85.75 91.36
200801 90.69 91.92 87.17 92.98
200802 93.39 94.82 89.96 95.38
200803 101.84 103.28 96.78 105.47
200804 108.76 110.19 103.47 112.62
200805 122.63 123.94 118.95 125.01
200806 131.52 133.05 127.59 133.93
200807 132.83 133.87 131.22 133.38
200808 114.57 113.85 113.21 116.64
200809 99.66 99.06 95.97 103.94
200810 72.69 72.84 68.62 76.61
200811 53.97 53.24 51.38 57.29
200812 41.34 41.58 41.00 41.44
200901 43.86 44.86 44.97 41.74
200902 41.84 43.24 43.14 39.15
200903 46.65 46.84 45.58 47.52
200904 50.28 50.85 50.18 49.81
200905 58.15 57.94 57.40 59.13
200906 69.15 68.62 69.21 69.62
200907 64.67 64.91 64.97 64.12
200908 71.63 72.50 71.32 71.06
200909 68.35 67.69 67.91 69.44
200910 74.08 73.19 73.28 75.77
200911 77.55 77.04 77.63 78.00
200912 74.88 74.67 75.49 74.49
201001 77.12 76.37 76.64 78.36
201002 74.76 74.31 73.56 76.41
201003 79.30 79.27 77.37 81.25
201004 84.18 84.98 83.09 84.48
201005 75.62 76.25 76.87 73.73
201006 74.73 74.84 73.98 75.35
201007 74.58 74.74 72.65 76.35
201008 75.83 76.69 74.18 76.60
201009 76.12 77.79 75.27 75.29
201010 81.72 82.92 80.34 81.90
201011 84.53 85.67 83.70 84.24
201012 90.01 91.80 89.07 89.15
201101 92.69 96.29 92.37 89.41
201102 97.91 103.96 100.25 89.53
201103 108.65 114.44 108.58 102.92
201104 116.24 123.07 115.70 109.96
201105 108.07 114.46 108.46 101.28
201106 105.85 113.76 107.52 96.25
201107 107.92 116.46 109.98 97.31
201108 100.49 110.08 105.06 86.32
201109 100.82 110.88 106.00 85.58
201110 99.85 109.47 103.67 86.41
201111 105.41 110.50 108.59 97.12
201112 104.23 107.91 106.22 98.56
201201 107.07 111.16 109.78 100.29
201202 112.69 119.70 116.15 102.21
201203 117.79 124.93 122.28 106.15
201204 113.67 120.46 117.25 103.28
201205 104.09 110.52 107.05 94.68
201206 90.73 95.59 94.24 82.36
201207 96.75 103.14 99.22 87.90
201208 105.27 113.34 108.37 94.11
201209 106.28 113.38 110.96 94.51
201210 103.41 111.97 108.73 89.52
201211 101.17 109.71 107.13 86.68
201212 101.19 109.68 105.69 88.22
201301 105.10 112.97 107.58 94.74
201302 107.64 116.52 111.09 95.30
201303 102.52 109.24 105.42 92.91
201304 98.85 102.88 101.66 92.02
201305 99.37 103.03 100.31 94.76
201306 99.74 103.11 100.33 95.79
201307 105.26 107.72 103.36 104.70
201308 108.16 110.96 106.96 106.55
201309 108.76 111.62 108.40 106.25
201310 105.43 109.48 106.30 100.50
201311 102.63 108.08 105.85 93.95
201312 105.48 110.67 107.92 97.85
201401 102.10 107.42 104.01 94.86
201402 104.83 108.81 104.94 100.73
201403 104.04 107.40 104.15 100.57
201404 104.87 107.79 104.73 102.08
201405 105.71 109.68 105.60 101.86
201406 108.37 111.87 108.01 105.24
201407 105.23 106.98 105.76 102.94
201408 100.05 101.92 101.85 96.38
201409 95.85 97.34 96.99 93.22
201410 86.08 87.27 86.57 84.40
201411 76.99 78.44 76.73 75.81
201412 60.70 62.33 60.52 59.26
201501 47.11 48.07 45.98 47.27
201502 54.79 57.93 55.83 50.61
201503 52.83 55.79 54.91 47.78
201504 57.54 59.39 58.80 54.44
201505 62.51 64.56 63.69 59.27
201506 61.31 62.34 61.78 59.80
201507 54.34 55.87 56.25 50.90
201508 45.69 46.99 47.22 42.86
201509 46.28 47.24 46.15 45.45
201510 46.96 48.12 46.55 46.20
201511 43.11 44.42 42.22 42.70
201512 36.57 37.72 34.77 37.23
201601 29.78 30.80 27.00 31.54
201602 31.03 33.20 29.50 30.39
201603 37.34 39.07 35.18 37.77
201604 40.75 42.25 39.04 40.96
201605 45.94 47.13 43.95 46.73
201606 47.69 48.48 45.83 48.75
201607 44.13 45.07 42.62 44.69
201608 44.88 46.14 43.74 44.75
201609 45.04 46.19 43.74 45.20
201610 49.29 49.73 48.26 49.89
201611 45.26 46.44 43.77 45.57
201612 52.62 54.07 51.78 52.01
201701 53.59 54.89 53.37 52.51
201702 54.35 55.49 54.17 53.40
201703 50.90 51.97 51.16 49.58
201704 52.16 52.98 52.45 51.06
201705 49.89 50.87 50.31 48.50
201706 46.17 46.89 46.44 45.17
201707 47.66 48.69 47.63 46.65
201708 49.94 51.37 50.43 48.03
201709 52.95 55.16 53.86 49.83
201710 54.92 57.62 55.58 51.56
201711 59.93 62.57 60.58 56.65
201712 61.19 64.21 61.41 57.94
201801 66.23 68.99 66.02 63.67
201802 63.46 65.42 62.79 62.17
201803 64.17 66.45 63.29 62.76
201804 68.79 71.63 68.43 66.32
201805 73.43 76.65 73.66 69.98
201806 71.98 75.19 73.22 67.52
201807 72.67 74.44 72.72 70.84
201808 71.08 73.13 72.13 67.99
201809 75.36 78.86 77.02 70.21
201810 76.73 80.47 78.96 70.75
201811 62.32 65.17 65.11 56.67
201812 53.96 56.46 56.47 48.95
201901 56.58 59.27 58.96 51.52
201902 61.13 64.13 64.32 54.95
201903 63.79 66.41 66.80 58.15
201904 68.58 71.20 70.66 63.87
201905 66.83 70.53 69.13 60.84
201906 59.76 63.30 61.30 54.68
201907 61.48 64.00 62.91 57.52
201908 57.67 59.25 58.92 54.84
201909 60.04 62.33 60.84 56.95
201910 57.27 59.37 58.47 53.98
201911 60.40 62.74 61.41 57.06
201912 63.35 65.85 64.41 59.80
202001 61.63 63.60 63.76 57.52
202002 53.35 55.00 54.51 50.53
202003 32.20 32.98 33.75 29.88
202004 21.04 23.34 23.27 16.52
202005 30.38 31.02 31.56 28.56
202006 39.46 39.93 40.14 38.30
202007 42.07 42.81 42.64 40.75
202008 43.44 44.26 43.71 42.36
202009 40.60 41.09 41.10 39.60
202010 39.90 40.47 39.70 39.53
202011 42.30 43.23 42.58 41.10
202012 48.73 49.87 49.26 47.05
202101 53.60 54.55 54.16 52.10
202102 60.46 61.96 60.37 59.06
202103 63.83 65.19 63.95 62.35
202104 62.95 64.77 62.37 61.71
202105 66.40 68.04 65.98 65.18
202106 71.80 73.07 70.96 71.38
202107 73.28 74.39 73.00 72.46
202108 68.87 70.02 68.85 67.73
202109 72.80 74.60 72.24 71.56
202110 82.06 83.65 81.22 81.32
202111 79.92 80.77 79.80 79.18
202112 72.87 74.31 72.76 71.53
202201 83.92 85.53 83.11 83.12
202202 93.54 95.76 93.13 91.74
202203 112.40 115.59 113.11 108.49
202204 103.41 105.78 102.68 101.78
202205 110.10 112.37 108.32 109.60
202206 116.80 120.08 115.73 114.59
202207 105.08 108.92 106.48 99.85
202208 95.97 98.60 97.75 91.57
202209 88.22 90.16 90.63 83.87
202210 90.33 93.13 90.59 87.26
202211 87.38 91.07 86.28 84.78
202212 78.07 80.90 76.78 76.52")
# read ----
x <- read.table(textConnection(obj), header=TRUE)
x$date = seq(as.Date(paste0(1979,"/01/1")),
length.out = length(x$date), by = 'month')
head(x,1)
library(x12)
x$CRUDE_AVG = ts(x$CRUDE_AVG, start = c(1979,1) ,frequency = 12)
CRUDE_AVG <- x12::x12(x$CRUDE_AVG,list(transform="auto",automdl=T))
x$CRUDE_AVG <- CRUDE_AVG@d11
x = x %>%
dplyr::select(date, CRUDE_AVG)
oil = merge(oil, x, key = 'date', all = T )
oil = oil[complete.cases(oil$USREC), ]
oil = oil %>%
mutate(
impact_doecd = smooth::cma(dOECD, order = 7)$fitted,
impact_oecd_gap = smooth::cma(OECD_gap, order = 7)$fitted)
# convert nominal oil price to real oil price
oil = oil %>%
mutate(p_o_a = CRUDE_AVG / CPIAUCSL) %>%
dplyr::select(-CRUDE_AVG)
oil$dp_oecd = c(rep(NA, lag_diff), oil$p_c %>% log %>% diff(1, lag = lag_diff) * 100)
oil$doil = c(rep(NA, lag_diff), oil$p_o_a %>% log %>% diff(1, lag = lag_diff) * 100)
oil = oil[complete.cases(oil$doil), ]
oil = oil[complete.cases(oil$WUIGLOBALWEIGHTAVG), ]
# set starting point to 1
oil$p_o_a = oil$p_o_a/oil$p_o_a[1]
head(oil,2)
oil = oil %>%
mutate(OIL = doil) %>%
mutate(WUI = WUIGLOBALWEIGHTAVG %>% log) %>%
dplyr::filter(date >= '1988-03-01',
date <= '2022-06-01')
library(mgcv)
gam = mgcv::gam(WUI ~ t2(OECD_gap, doil, dp_oecd,
bs='ps' ) ,
data = oil %>%
dplyr::filter(date <= '2021-06-01'))
summary(gam)
mgcv::vis.gam(gam,
type='link',
view=c("OECD_gap", "doil"),
n.grid = 20,
theta = 33,
phi = 25,
# zlim=c(4,6),
xlab = "Output Gap",
ylab ="Oil Inflation",
zlab ="WUI",
ticktype = "detailed",
color = "heat")
mgcv::vis.gam(gam,
type='link',
view=c("OECD_gap", "dp_oecd"),
n.grid = 20,
theta = 33,
phi = 25,
# zlim=c(4,6),
xlab = "Output Gap",
ylab ="Inflation",
zlab ="WUI",
ticktype = "detailed",
color = "heat")
m1 = dynlm(WUI ~
doil_y +
OECD_gap+
OIL+
+ trend(WUI)
,
data = oil)
rob_1 <- sqrt(diag(vcovHC(m1, type = "HC1")))
m2 = dynlm(WUI ~
# doil_y +
OECD_gap+
OIL+
+ trend(WUI)
,
data = oil %>%
mutate(OIL_p = ifelse(OIL>=0, OIL, 0),
OIL_n = ifelse(OIL<0, OIL, 0)))
# Adjust standard errors
rob_2 <- sqrt(diag(vcovHC(m2, type = "HC1")))
m3 = dynlm(WUI ~
doil_y +
OECD_gap+
OECDNMERECM+
OIL+
I(OIL*OECDNMERECM)
+ trend(WUI)
,
data = oil)
rob_3 <- sqrt(diag(vcovHC(m3, type = "HC1")))
m4 = dynlm(WUI ~
OECD_gap+
OECDNMERECM+
OIL+
I(OIL*OECDNMERECM)
+ trend(WUI)
,
data = oil)
# Adjust standard errors
rob_4 <- sqrt(diag(vcovHC(m4, type = "HC1")))
BIC(m1)
BIC(m2)
BIC(m3)
BIC(m4)
stargazer::stargazer(m1, m2,m3, m4,
type = c("text", "latex")[1],
se = list(rob_1, rob_2,rob_3, rob_4),
no.space=T)
m3 = dynlm(OIL ~
dOECD,
data = oil %>%
dplyr::filter(date <= '2021-06-01'))
rob_3 <- sqrt(diag(vcovHC(m3, type = "HC1")))
m4 = dynlm(doil_y ~
dOECD,
data = oil %>%
dplyr::filter(date <= '2021-06-01'))
rob_4 <- sqrt(diag(vcovHC(m4, type = "HC1")))
stargazer::stargazer(m3,m4,
type = c("text", "latex")[1],
se = list(rob_3,rob_4),
no.space=T)
oil = oil %>%
mutate(oecd_log = OECDPRINTO01IXOBSAM %>% log,
p_c = p_c %>% log)
glimpse(oil)
oil %>%
dplyr::select( date,
#doil_y,
OECD_gap,
dp_oecd,
doil,
WUI) %>%
dplyr::rename(#'Oil Supply' = doil_y,
'Output Gap' = OECD_gap,
'Aggregate Inflation' = dp_oecd,
'Oil Inflation' = doil) %>%
melt(id.vars=c("date")) %>%
dplyr::rename(Legend = variable) %>%
ggplot(aes(x=date, y = value)) + #, linetype = Legend
geom_rect(aes(xmin = as.Date('1990-01-01'),
xmax = as.Date('1993-07-01'),
ymin= -Inf,
ymax = Inf), fill = 'grey90', alpha = .2015) +
geom_rect(aes(xmin = as.Date('1997-09-01'),
xmax = as.Date('1998-11-01'),
ymin= -Inf,
ymax = Inf), fill = 'grey90', alpha = .2015) +
geom_rect(aes(xmin = as.Date('2000-08-01'),
xmax = as.Date('2003-04-01'),
ymin= -Inf,
ymax = Inf), fill = 'grey90', alpha = .2015) +
geom_rect(aes(xmin = as.Date('2008-01-01'),
xmax = as.Date('2009-04-01'),
ymin= -Inf,
ymax = Inf), fill = 'grey90', alpha = .2015) +
geom_rect(aes(xmin = as.Date('2011-10-01'),
xmax = as.Date('2012-12-01'),
ymin= -Inf,
ymax = Inf), fill = 'grey90', alpha = .2015) +
geom_rect(aes(xmin = as.Date('2013-11-01'),
xmax = as.Date('2016-08-01'),
ymin= -Inf,
ymax = Inf), fill = 'grey90', alpha = .2015) +
geom_rect(aes(xmin = as.Date('2018-04-01'),
xmax = as.Date('2020-05-01'),
ymin= -Inf,
ymax = Inf), fill = 'grey90', alpha = .2015) +
geom_line(aes(y = value)) +
facet_wrap(~ Legend, scales = c('free',"fixed")[1], ncol = 2) +
theme_bw()
oil_ur = oil %>%
dplyr::select(oecd_log,dOECD, p_c,dp_oecd, p_o_a,doil, oil_y,doil_y, WUI,dwui) %>%
mutate(p_o_a = log(p_o_a*100) )
choose = 3
lg=3
crit=c("Fixed")[1]
# partial sample
o1 = urca::ur.df(oil_ur[-c(127:130),1],type = c("none", "drift", "trend")[choose],lags=lg,selectlags = crit)
o2 = urca::ur.df(oil_ur[-c(127:130),2],type = c("none", "drift", "trend")[choose],lags=lg,selectlags = crit)
o3 = urca::ur.df(oil_ur[-c(127:130),3],type = c("none", "drift", "trend")[choose],lags=lg,selectlags = crit)
o4 = urca::ur.df(oil_ur[-c(127:130),4],type = c("none", "drift", "trend")[choose],lags=lg,selectlags = crit)
o5 = urca::ur.df(oil_ur[-c(127:130),5],type = c("none", "drift", "trend")[choose],lags=lg,selectlags = crit)
o6 = urca::ur.df(oil_ur[-c(127:130),6],type = c("none", "drift", "trend")[choose],lags=lg,selectlags = crit)
o7 = urca::ur.df(oil_ur[-c(127:130),7],type = c("none", "drift", "trend")[choose],lags=lg,selectlags = crit)
o8 = urca::ur.df(oil_ur[-c(127:130),8],type = c("none", "drift", "trend")[choose],lags=lg,selectlags = crit)
o9 = urca::ur.df(oil_ur[-c(127:130),9],type = c("none", "drift", "trend")[choose],lags=lg,selectlags = crit)
o10 = urca::ur.df(oil_ur[-c(127:130),10] %>% na.omit,type = c("none", "drift", "trend")[choose],lags=lg,selectlags = crit)
df1 = data.frame(a = c(o1@teststat[1],o2@teststat[1]),
b = c(o3@teststat[1],o4@teststat[1]),
c = c(o5@teststat[1],o6@teststat[1]),
d = c(o7@teststat[1],o8@teststat[1]),
e = c(o9@teststat[1],o10@teststat[1])) %>% t %>% round(2)
# full sample
o1 = urca::ur.df(oil_ur[,1],type = c("none", "drift", "trend")[choose],lags=lg,selectlags = crit)
o2 = urca::ur.df(oil_ur[,2],type = c("none", "drift", "trend")[choose],lags=lg,selectlags = crit)
o3 = urca::ur.df(oil_ur[,3],type = c("none", "drift", "trend")[choose],lags=lg,selectlags = crit)
o4 = urca::ur.df(oil_ur[,4],type = c("none", "drift", "trend")[choose],lags=lg,selectlags = crit)
o5 = urca::ur.df(oil_ur[,5],type = c("none", "drift", "trend")[choose],lags=lg,selectlags = crit)
o6 = urca::ur.df(oil_ur[,6],type = c("none", "drift", "trend")[choose],lags=lg,selectlags = crit)
o7 = urca::ur.df(oil_ur[,7],type = c("none", "drift", "trend")[choose],lags=lg,selectlags = crit)
o8 = urca::ur.df(oil_ur[,8],type = c("none", "drift", "trend")[choose],lags=lg,selectlags = crit)
o9 = urca::ur.df(oil_ur[,9],type = c("none", "drift", "trend")[choose],lags=lg,selectlags = crit)
o10 = urca::ur.df(oil_ur[,10] %>% na.omit,type = c("none", "drift", "trend")[choose],lags=lg,selectlags = crit)
df2 = data.frame(a = c(o1@teststat[1],o2@teststat[1]),
b = c(o3@teststat[1],o4@teststat[1]),
c = c(o5@teststat[1],o6@teststat[1]),
d = c(o7@teststat[1],o8@teststat[1]),
e = c(o9@teststat[1],o10@teststat[1])) %>% t %>% round(2)
cbind(df1,df2)
oil$OECDNMERECM[is.na(oil$OECDNMERECM)] = 0
# adapted from https://github.com/AdaemmerP/lpirfs/blob/master/R/plot_nl.R
plot_nl1 = function (results_nl)
{
col_regime_1 <- "red"
col_regime_2 <- "red"
specs <- results_nl$specs
if (specs$model_type == 0) {
irf_s1_mean <- results_nl[[1]]
irf_s1_low <- results_nl[[2]]
irf_s1_up <- results_nl[[3]]
irf_s2_mean <- results_nl[[4]]
irf_s2_low <- results_nl[[5]]
irf_s2_up <- results_nl[[6]]
gg_s1 <- rep(list(NaN), specs$endog * specs$endog)
gg_s2 <- rep(list(NaN), specs$endog * specs$endog)
plot_num <- 1
for (rr in 1:(specs$endog)) {
for (ss in 1:(specs$endog)) {
tbl_s1_mean <- as.matrix(t(irf_s1_mean[, 1:specs$hor,
ss]))[, rr]
tbl_s1_low <- as.matrix(t(irf_s1_low[, 1:specs$hor,
ss]))[, rr]
tbl_s1_up <- as.matrix(t(irf_s1_up[, 1:specs$hor,
ss]))[, rr]
tbl_s1 <- data.frame(x = 1:specs$hor, mean = tbl_s1_mean,
low = tbl_s1_low, up = tbl_s1_up)
tbl_s1_mean <- as.matrix(t(irf_s2_mean[, 1:specs$hor,
ss]))[, rr]
tbl_s2_low <- as.matrix(t(irf_s2_low[, 1:specs$hor,
ss]))[, rr]
tbl_s2_up <- as.matrix(t(irf_s2_up[, 1:specs$hor,
ss]))[, rr]
tbl_s2 <- data.frame(x = 1:specs$hor, mean = tbl_s1_mean,
low = tbl_s2_low, up = tbl_s2_up)
gg_s1[[plot_num]] <- ggplot() + geom_line(data = tbl_s1,
aes(y = mean, x = x), col = col_regime_1) +
geom_ribbon(data = tbl_s1, aes(x = x, ymin = low,
ymax = up), col = "blue", fill = "blue",
alpha = 0.1) + theme_classic() + ggtitle(paste(specs$column_names[ss],
"on", specs$column_names[rr], sep = " ")) +
xlab("") + ylab("") + theme(title = element_text(size = 8),
plot.title = element_text(hjust = 0.5)) +
scale_y_continuous(expand = c(0, 0)) + scale_x_continuous(expand = c(0,
0), breaks = seq(0, specs$hor, 2)) + geom_hline(yintercept = 0,
col = "black", size = 0.25, linetype = "dashed")
gg_s2[[plot_num]] <- ggplot() + geom_line(data = tbl_s2,
aes(y = mean, x = x), col = col_regime_2) +
geom_ribbon(data = tbl_s2, aes(x = x, ymin = low,
ymax = up), col = "blue", fill = "blue",
alpha = 0.1) + theme_classic() + ggtitle(paste(specs$column_names[ss],
"on", specs$column_names[rr], sep = " ")) +
xlab("") + ylab("") + theme(title = element_text(size = 8),
plot.title = element_text(hjust = 0.5)) +
scale_y_continuous(expand = c(0, 0)) + scale_x_continuous(expand = c(0,
0), breaks = seq(0, specs$hor, 2)) + geom_hline(yintercept = 0,
col = "black", size = 0.25, linetype = "dashed")
plot_num <- plot_num + 1
}
}
}
else if (specs$model_type == 1 | specs$model_type == 2) {
gg_s1 <- rep(list(NaN), specs$endog)
gg_s2 <- rep(list(NaN), specs$endog)
plot_num <- 1
for (rr in 1:(specs$endog)) {
tbl_s1_mean <- results_nl$irf_s1_mean[rr, ]
tbl_s1_low <- results_nl$irf_s1_low[rr, ]
tbl_s1_up <- results_nl$irf_s1_up[rr, ]
tbl_s1 <- data.frame(x = 1:specs$hor, mean = tbl_s1_mean,
low = tbl_s1_low, up = tbl_s1_up)
tbl_s2_mean <- results_nl$irf_s2_mean[rr, ]
tbl_s2_low <- results_nl$irf_s2_low[rr, ]
tbl_s2_up <- results_nl$irf_s2_up[rr, ]
tbl_s2 <- data.frame(x = 1:specs$hor, mean = tbl_s2_mean,
low = tbl_s2_low, up = tbl_s2_up)
gg_s1[[rr]] <- ggplot() + geom_line(data = tbl_s1,
aes(y = mean, x = x), col = col_regime_1) +
geom_ribbon(data = tbl_s1, aes(x = x, ymin = low,
ymax = up), col = "grey", fill = "blue", alpha = 0.2) +
theme_classic() + ggtitle(paste("Shock", "on",
specs$column_names[rr], sep = " ")) + xlab("") +
ylab("") + theme(title = element_text(size = 8),
plot.title = element_text(hjust = 0.5)) + scale_y_continuous(expand = c(0,
0)) + scale_x_continuous(expand = c(0, 0), breaks = seq(0,
specs$hor, 2)) + geom_hline(yintercept = 0,
col = "black", size = 0.25, linetype = "dashed")
gg_s2[[rr]] <- ggplot() + geom_line(data = tbl_s2,
aes(y = mean, x = x), col = col_regime_2) +
geom_ribbon(data = tbl_s2, aes(x = x, ymin = low,
ymax = up), col = "grey", fill = "blue", alpha = 0.2) +
theme_classic() + ggtitle(paste("Shock", "on",
specs$column_names[rr], sep = " ")) + xlab("") +
ylab("") + theme(title = element_text(size = 8),
plot.title = element_text(hjust = 0.5)) + scale_y_continuous(expand = c(0,
0)) + scale_x_continuous(expand = c(0, 0), breaks = seq(0,
specs$hor, 2)) + geom_hline(yintercept = 0,
col = "black", size = 0.25, linetype = "dashed")
}
}
list(gg_s1 = gg_s1, gg_s2 = gg_s2)
}
lags_considered = 4
criteria = 'AIC'
options(repr.plot.width=10, repr.plot.height=7)
results_nl_iv <- lp_nl(oil %>%
dplyr::select(
OECD_gap,
dp_oecd,
doil,
WUI) %>%
as.data.frame() %>%
dplyr::rename(Output = OECD_gap,
Inflation = dp_oecd,
Oil = doil),
max_lags = lags_considered,
lags_criterion = criteria,
lags_endog_nl = NaN,
lags_endog_lin = NaN,
shock_type = 0,
lags_exog = 1,
trend = 1,
use_logistic = T,
lag_switching = T,
use_nw = T,
confint = 1.66,
hor = 8,
use_hp = T,
lambda = 1600,
switching = oil$impact_oecd_gap %>% as.data.frame(),
gamma = 1.5)
nl_plots <- plot(results_nl_iv)
nl_plots <- plot_nl1(results_nl_iv)
single_plots <- nl_plots$gg_s1[c(3,7,11,15)]
single_plots[5:8] <- nl_plots$gg_s2[c(3,7,11,15)]
all_plots <- sapply(single_plots, ggplotGrob)
gridExtra::marrangeGrob(all_plots, nrow = 4, ncol = 2, top = NULL)
# Transition function
fz <- results_nl_iv$fz
specs <- results_nl_iv$specs
# Make date sequence and store data in a data.frame for ggplot.
dates = seq(as.Date(paste0(min(year(oil$date)),"/03/1")),
length.out = length(fz[[1]]), by = 'quarter')
data_df <- data.frame(x = dates,
fz = fz[[1]],
switch_var = specs$switching$.[ (length(oil[,1]) - length(dates) + 1) :length(oil[,1])] )
### rescaled [0,1] followed http://rstudio-pubs-static.s3.amazonaws.com/329613_f53e84d1a18840d5a1df55efb90739d9.html
a <- range(data_df$fz)
b <- range(data_df$switch_var)
scale_factor <- diff(a)/diff(b)
data_df$switch_var <- ((data_df$switch_var - b[1]) * scale_factor) + a[1]
trans <- ~ ((. - a[1]) / scale_factor) + b[1]
ggplot(data_df, aes(x = x)) +
geom_rect(aes(xmin = as.Date('1990-01-01'),
xmax = as.Date('1993-07-01'),
ymin= -Inf,
ymax = Inf), fill = 'grey90', alpha = .2015) +
geom_rect(aes(xmin = as.Date('1997-09-01'),
xmax = as.Date('1998-11-01'),
ymin= -Inf,
ymax = Inf), fill = 'grey90', alpha = .2015) +
geom_rect(aes(xmin = as.Date('2000-08-01'),
xmax = as.Date('2003-04-01'),
ymin= -Inf,
ymax = Inf), fill = 'grey90', alpha = .2015) +
geom_rect(aes(xmin = as.Date('2008-01-01'),
xmax = as.Date('2009-04-01'),
ymin= -Inf,
ymax = Inf), fill = 'grey90', alpha = .2015) +
geom_rect(aes(xmin = as.Date('2011-10-01'),
xmax = as.Date('2012-12-01'),
ymin= -Inf,
ymax = Inf), fill = 'grey90', alpha = .2015) +
geom_rect(aes(xmin = as.Date('2013-11-01'),
xmax = as.Date('2016-08-01'),
ymin= -Inf,
ymax = Inf), fill = 'grey90', alpha = .2015) +
geom_rect(aes(xmin = as.Date('2018-04-01'),
xmax = as.Date('2020-05-01'),
ymin= -Inf,
ymax = Inf), fill = 'grey90', alpha = .2015) +
theme_bw() +
scale_y_continuous(sec.axis = sec_axis(trans=trans , name = "Output Gap")) +
geom_line(aes(y = fz, colour = "Regime")) +
geom_line(aes(y = switch_var, colour = "Output Growth") ) +
scale_colour_manual(values = c("blue", "red")) +
labs(y = 'Transition Function',
x = NULL,
colour = "") +
theme(legend.position = 'bottom',legend.margin=margin(t=-10)) +
# ggtitle("Model A") +
theme(plot.title = element_text(hjust = 0.5))
# partial sample
results_nl_iv <- lp_nl(oil %>%
dplyr::filter(date <= '2021-06-01') %>%
dplyr::select(
OECD_gap,
dp_oecd,
doil,
WUI) %>%
as.data.frame() %>%
dplyr::rename(Output = OECD_gap,
Inflation = dp_oecd,
Oil = doil),
max_lags = lags_considered,
lags_criterion = criteria,
lags_endog_nl = NaN,
lags_endog_lin = NaN,
shock_type = 0,
lags_exog = 1,
trend = 1,
use_logistic = T,
lag_switching = T,
use_nw = T,
confint = 1.66,
hor = 8,
use_hp = T,
lambda = 1600,
switching = oil %>%
dplyr::filter(date <= '2021-06-01') %>%
dplyr::select(impact_oecd_gap) %>% as.data.frame(),
gamma = 1.5)
nl_plots <- plot_nl1(results_nl_iv)
single_plots <- nl_plots$gg_s1[c(3,7,11,15)]
single_plots[5:8] <- nl_plots$gg_s2[c(3,7,11,15)]
all_plots <- sapply(single_plots, ggplotGrob)
gridExtra::marrangeGrob(all_plots, nrow = 4, ncol = 2, top = NULL)
results_nl_iv <- lp_nl(oil %>%
dplyr::select(
dOECD,
dp_oecd,
doil,
WUI) %>%
as.data.frame() %>%
dplyr::rename(
Output = dOECD,
Inflation = dp_oecd,
Oil = doil),
max_lags = lags_considered,
lags_criterion = criteria,
lags_endog_nl = NaN,
lags_endog_lin = NaN,
shock_type = 0,
lags_exog = 1,
trend = 1,
use_logistic = T,
lag_switching = T,
use_nw = T,
confint = 1.66,
hor = 8,
use_hp = T,
lambda = 1600,
switching = oil$impact_doecd %>% as.data.frame(),
gamma = 1.5)
nl_plots <- plot_nl1(results_nl_iv)
single_plots <- nl_plots$gg_s1[c(3,7,11,15)]
single_plots[5:8] <- nl_plots$gg_s2[c(3,7,11,15)]
all_plots <- sapply(single_plots, ggplotGrob)
gridExtra::marrangeGrob(all_plots, nrow = 4, ncol = 2, top = NULL)
#
fz <- results_nl_iv$fz
specs <- results_nl_iv$specs
# Make date sequence and store data in a data.frame for ggplot.
dates = seq(as.Date(paste0(min(year(oil$date)),"/03/1")),
length.out = length(fz[[1]]), by = 'quarter')
data_df <- data.frame(x = dates,
fz = fz[[1]],
switch_var = specs$switching$.[ (length(oil[,1]) - length(dates) + 1) :length(oil[,1])] )
### rescaled [0,1] followed http://rstudio-pubs-static.s3.amazonaws.com/329613_f53e84d1a18840d5a1df55efb90739d9.html
a <- range(data_df$fz)
b <- range(data_df$switch_var)
scale_factor <- diff(a)/diff(b)
data_df$switch_var <- ((data_df$switch_var - b[1]) * scale_factor) + a[1]
trans <- ~ ((. - a[1]) / scale_factor) + b[1]
ggplot(data_df, aes(x = x)) +
geom_rect(aes(xmin = as.Date('1990-01-01'),
xmax = as.Date('1993-07-01'),
ymin= -Inf,
ymax = Inf), fill = 'grey90', alpha = .2015) +
geom_rect(aes(xmin = as.Date('1997-09-01'),
xmax = as.Date('1998-11-01'),
ymin= -Inf,
ymax = Inf), fill = 'grey90', alpha = .2015) +
geom_rect(aes(xmin = as.Date('2000-08-01'),
xmax = as.Date('2003-04-01'),
ymin= -Inf,
ymax = Inf), fill = 'grey90', alpha = .2015) +
geom_rect(aes(xmin = as.Date('2008-01-01'),
xmax = as.Date('2009-04-01'),
ymin= -Inf,
ymax = Inf), fill = 'grey90', alpha = .2015) +
geom_rect(aes(xmin = as.Date('2011-10-01'),
xmax = as.Date('2012-12-01'),
ymin= -Inf,
ymax = Inf), fill = 'grey90', alpha = .2015) +
geom_rect(aes(xmin = as.Date('2013-11-01'),
xmax = as.Date('2016-08-01'),
ymin= -Inf,
ymax = Inf), fill = 'grey90', alpha = .2015) +
geom_rect(aes(xmin = as.Date('2018-04-01'),
xmax = as.Date('2020-05-01'),
ymin= -Inf,
ymax = Inf), fill = 'grey90', alpha = .2015) +
theme_bw() +
scale_y_continuous(sec.axis = sec_axis(trans=trans , name = "Output Growth")) +
geom_line(aes(y = fz, colour = "Regime")) +
geom_line(aes(y = switch_var, colour = "Output Growth") ) +
scale_colour_manual(values = c("blue", "red")) +
labs(y = 'Transition Function',
x = NULL,
colour = "") +
theme(legend.position = 'bottom',legend.margin=margin(t=-10)) +
# ggtitle("Model A") +
theme(plot.title = element_text(hjust = 0.5))
results_nl_iv <- lp_nl(oil %>%
# partial sample
dplyr::filter(date <= '2021-06-01') %>%
dplyr::select(
dOECD,
dp_oecd,
doil,
WUI) %>%
as.data.frame() %>%
dplyr::rename(
Output = dOECD,
Inflation = dp_oecd,
Oil = doil),
max_lags = lags_considered,
lags_criterion = criteria,
lags_endog_nl = NaN,
lags_endog_lin = NaN,
shock_type = 0,
lags_exog = 1,
trend = 1,
use_logistic = T,
lag_switching = T,
use_nw = T,
confint = 1.66,
hor = 8,
use_hp = T,
lambda = 1600,
switching = oil %>%
dplyr::filter(date <= '2021-06-01') %>%
dplyr::select(impact_doecd) %>% as.data.frame(),
gamma = 1.5)
nl_plots <- plot_nl1(results_nl_iv)
single_plots <- nl_plots$gg_s1[c(3,7,11,15)]
single_plots[5:8] <- nl_plots$gg_s2[c(3,7,11,15)]
all_plots <- sapply(single_plots, ggplotGrob)
gridExtra::marrangeGrob(all_plots, nrow = 4, ncol = 2, top = NULL)
pacman::p_load(bvartools)
model <- gen_var(ts(oil %>%
dplyr::select(
OECD_gap,
dp_oecd,
doil,
WUI) %>%
dplyr::rename(
Output = OECD_gap,
Inflation = dp_oecd,
Oil = doil),
frequency = 4,
start=c(1990,1)),
p = 1,
tvp = T,
deterministic = "const",
iterations = 1500, burnin = 100)
model <- add_priors(model)
object <- draw_posterior(model)
options(repr.plot.width=14, repr.plot.height=10)
plot(object,type='hist')