-
Notifications
You must be signed in to change notification settings - Fork 2
/
README.Rmd
205 lines (149 loc) · 5.32 KB
/
README.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
---
output: github_document
---
<!-- README.md is generated from README.Rmd. Please edit that file -->
```{r, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "man/figures/README-",
# custom setting-----------------
out.width = "70%",
fig.align = "center",
fig.width = 6,
fig.asp = .618
)
options(digits = 3)
```
# bvhar <img src='man/figures/logo.png' align="right" height="139" />
<!-- badges: start -->
[![R-CMD-check](https://github.com/ygeunkim/bvhar/actions/workflows/R-CMD-check.yaml/badge.svg?branch=master)](https://github.com/ygeunkim/bvhar/actions/workflows/R-CMD-check.yaml?query=branch%3Amaster)
[![Codecov test coverage](https://codecov.io/gh/ygeunkim/bvhar/graph/badge.svg?flag=r-package)](https://app.codecov.io/gh/ygeunkim/bvhar)
[![CRAN status](https://www.r-pkg.org/badges/version/bvhar)](https://CRAN.R-project.org/package=bvhar)
[![monthly downloads](https://cranlogs.r-pkg.org/badges/last-month/bvhar?color=blue)](https://cran.r-project.org/package=bvhar)
[![total downloads](https://cranlogs.r-pkg.org/badges/grand-total/bvhar?color=blue)](https://cran.r-project.org/package=bvhar)
<!-- badges: end -->
## Overview
`bvhar` provides functions to analyze and forecast multivariate time series using
- VAR
- VHAR (Vector HAR)
- BVAR (Bayesian VAR)
- **BVHAR (Bayesian VHAR)**
Basically, the package focuses on the research with forecasting.
## Installation
```{r craninst, eval=FALSE}
install.packages("bvhar")
```
### Development version
<!-- dev badges: start -->
[![dev-r-cmd-check](https://github.com/ygeunkim/bvhar/actions/workflows/R-CMD-check.yaml/badge.svg?branch=develop)](https://github.com/ygeunkim/bvhar/actions/workflows/R-CMD-check.yaml?query=branch%3Adevelop)
[![dev-codecov](https://codecov.io/github/ygeunkim/bvhar/branch/develop/graph/badge.svg?flag=r-package)](https://app.codecov.io/gh/ygeunkim/bvhar/tree/develop)
[![Development version updated](https://img.shields.io/github/last-commit/ygeunkim/bvhar/develop?label=dev%20updated)](https://github.com/ygeunkim/bvhar/tree/develop)
<!-- dev badges: end -->
You can install the development version from [develop branch](https://github.com/ygeunkim/bvhar/tree/develop).
```{r devinst, eval=FALSE}
# install.packages("remotes")
remotes::install_github("ygeunkim/bvhar@develop")
```
We started to develop a Python version in python directory.
- [bvhar for Python](https://ygeunkim.github.io/package/bvhar/python/)
- [Source code](https://github.com/ygeunkim/bvhar/tree/master/python)
## Models
```{r example, message=FALSE}
library(bvhar) # this package
library(dplyr)
```
Repeatedly, `bvhar` is a research tool to analyze multivariate time series model above
| Model | function | prior |
|:-----:|:--------:|:-----:|
| VAR | `var_lm()` | |
| VHAR | `vhar_lm()` | |
| BVAR | `bvar_minnesota()` | Minnesota (will move to `var_bayes()`) |
| BVHAR | `bvhar_minnesota()` | Minnesota (will move to `vhar_bayes()`) |
| BVAR | `var_bayes()` | SSVS, Horseshoe, Minnesota, NG, DL |
| BVHAR | `vhar_bayes()` | SSVS, Horseshoe, Minnesota, NG, DL |
This readme document shows forecasting procedure briefly.
Details about each function are in vignettes and help documents.
Note that each `bvar_minnesota()` and `bvhar_minnesota()` will be integrated into `var_bayes()` and `vhar_bayes()` and removed in the next version.
h-step ahead forecasting:
```{r datasplit}
h <- 19
etf_split <- divide_ts(etf_vix, h) # Try ?divide_ts
etf_tr <- etf_split$train
etf_te <- etf_split$test
```
### VAR
VAR(5):
```{r fitvar}
mod_var <- var_lm(y = etf_tr, p = 5)
```
Forecasting:
```{r predvar}
forecast_var <- predict(mod_var, h)
```
MSE:
```{r testvar}
(msevar <- mse(forecast_var, etf_te))
```
### VHAR
```{r fitvhar}
mod_vhar <- vhar_lm(y = etf_tr)
```
MSE:
```{r predvhar}
forecast_vhar <- predict(mod_vhar, h)
(msevhar <- mse(forecast_vhar, etf_te))
```
### BVAR
Minnesota prior:
```{r specbvar}
lam <- .3
delta <- rep(1, ncol(etf_vix)) # litterman
sig <- apply(etf_tr, 2, sd)
eps <- 1e-04
(bvar_spec <- set_bvar(sig, lam, delta, eps))
```
```{r fitbvar}
mod_bvar <- bvar_minnesota(y = etf_tr, p = 5, bayes_spec = bvar_spec)
```
MSE:
```{r }
forecast_bvar <- predict(mod_bvar, h)
(msebvar <- mse(forecast_bvar, etf_te))
```
### BVHAR
BVHAR-S:
```{r specbvhars}
(bvhar_spec_v1 <- set_bvhar(sig, lam, delta, eps))
```
```{r fitbvhars}
mod_bvhar_v1 <- bvhar_minnesota(y = etf_tr, bayes_spec = bvhar_spec_v1)
```
MSE:
```{r predbvhars}
forecast_bvhar_v1 <- predict(mod_bvhar_v1, h)
(msebvhar_v1 <- mse(forecast_bvhar_v1, etf_te))
```
BVHAR-L:
```{r specbvharl}
day <- rep(.1, ncol(etf_vix))
week <- rep(.1, ncol(etf_vix))
month <- rep(.1, ncol(etf_vix))
#----------------------------------
(bvhar_spec_v2 <- set_weight_bvhar(sig, lam, eps, day, week, month))
```
```{r fitbvharl}
mod_bvhar_v2 <- bvhar_minnesota(y = etf_tr, bayes_spec = bvhar_spec_v2)
```
MSE:
```{r predbvharl}
forecast_bvhar_v2 <- predict(mod_bvhar_v2, h)
(msebvhar_v2 <- mse(forecast_bvhar_v2, etf_te))
```
## Citation
Please cite this package with following BibTeX:
```{r cite-pkg, echo=FALSE, comment=NULL}
toBibtex(citation("bvhar", auto = FALSE))
```
## Code of Conduct
Please note that the bvhar project is released with a [Contributor Code of Conduct](https://contributor-covenant.org/version/2/0/CODE_OF_CONDUCT.html). By contributing to this project, you agree to abide by its terms.