-
Notifications
You must be signed in to change notification settings - Fork 0
/
be23-AUdata.qmd
125 lines (98 loc) · 2.71 KB
/
be23-AUdata.qmd
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
---
title: "Australian Data Forecasting with Package BVAR"
author:
- name: "Type your name"
format:
html:
theme: cosmo
toc: true
---
> **Task:** Adjust this **Quarto** template to perform your Bayesian VAR model estimation and forecasting. Search for the variables of interest using function `readrba::browse_rba_series()`, download the chosen 4 variables using function `readrba::read_rba()`, and create a data matrix. Subsequently employ the model estimation and forecasting workflow using package **BVAR** and report the basic outputs of your analysis.
# Download the data
```{r interest data}
#| echo: true
#| message: false
#| warning: false
# download daily interest rates
icr_dwnld = readrba::read_rba(series_id = "FIRMMCRTD") # Cash Rate Target
icr_tmp = xts::xts(icr_dwnld$value, icr_dwnld$date)
by1m_dwnld = readrba::read_rba(series_id = "FIRMMBAB30D")
by1m_tmp = xts::xts(by1m_dwnld$value, by1m_dwnld$date)
short = na.omit(merge(icr_tmp, by1m_tmp))
forecast_day = "/2023-09-06"
# daily data
variables = short
colnames(variables) = c("cash rate", "1m")
variables = variables[forecast_day]
# monthly data
monthly = xts::to.monthly(variables, OHLC = FALSE)
plot(monthly)
```
# Prior Setup
```{r priors}
set.seed(42)
library(BVAR)
# priors
mn = bv_minnesota(
lambda = bv_lambda(mode = 0.2, sd = 0.4, min = 0.0001, max = 5),
alpha = bv_alpha(mode = 2),
psi <- bv_psi(scale = 0.004, shape = 0.004, mode = "auto", min = "auto", max = "auto"),
var = 1e07
)
soc = bv_soc(mode = 1, sd = 1, min = 1e-04, max = 50)
sur = bv_sur(mode = 1, sd = 1, min = 1e-04, max = 50)
priors = bv_priors(hyper = "auto", mn = mn, soc = soc, sur = sur)
# MH setup
mh = bv_metropolis(
scale_hess = c(0.05, 0.0001, 0.0001),
adjust_acc = TRUE,
acc_lower = 0.25,
acc_upper = 0.45
)
```
# Estimation
## Estimate the model
```{r estimation}
#| echo: true
#| message: false
#| warning: false
#| error: false
# estimation
p = 1
run = bvar(
monthly,
lags = p,
n_draw = 3e4,
n_burn = 1e4,
n_thin = 1,
priors = priors,
mh = mh,
verbose = FALSE # without progress bar
)
```
## Report MCMC convergence
```{r mcmc}
plot.ts(run$hyper, main = "", col = "#FF00FF", xlab = "s")
```
## Report the Posterior Moments
```{r means}
mean_A = t(apply(run$beta, 2:3, mean))
rownames(mean_A) = colnames(monthly)
knitr::kable(mean_A, caption = "Posterior estimates for autoregressive parameters", digits = 3)
```
# Forecasting
```{r forecasting}
# forecasting
predict(run) <- predict(
run,
horizon = 20,
conf_bands = seq(from = 0.05, to = 0.4, by = 0.01)
)
plot(
predict(run),
area = TRUE,
t_back = 32
)
```
# Conclusion!
Thank you!