---
title: "Bayesian VAR and VHAR Models"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{Bayesian VAR and VHAR Models}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

```{r rmdsetup, include = FALSE}
knitr::opts_chunk$set(
  comment = "#>",
  collapse = TRUE,
  out.width = "70%",
  fig.align = "center",
  fig.width = 6,
  fig.asp = .618,
  error = TRUE # temporarily until investigating the error: 'recursive' must be of length 1
)
orig_opts <- options("digits")
options(digits = 3)
set.seed(1)
```

```{r setup}
library(bvhar)
```

```{r etfdat}
etf <- etf_vix[1:55, 1:3]
# Split-------------------------------
h <- 5
etf_eval <- divide_ts(etf, h)
etf_train <- etf_eval$train
etf_test <- etf_eval$test
```

# Bayesian VAR and VHAR

`var_bayes()` and `vhar_bayes()` fit BVAR and BVHAR each with various priors.

- `y`: Multivariate time series data. It should be data frame or matrix, which means that every column is numeric. Each column indicates variable, i.e. it sould be wide format.
- `p` or `har`: VAR lag, or order of VHAR
- `num_chains`: Number of chains
    - If OpenMP is enabled, parallel loop will be run.
- `num_iter`: Total number of iterations
- `num_burn`: Number of burn-in
- `thinning`: Thinning
- `bayes_spec`: Output of `set_ssvs()`
    - Minneosta prior
        - BVAR: `set_bvar()`
        - BVHAR: `set_bvhar()` and `set_weight_bvhar()`
        - Can induce prior on $\lambda$ using `lambda = set_lambda()`
    - SSVS prior: `set_ssvs()`
    - Horseshoe prior: `set_horseshoe()`
    - NG prior: `set_ng()`
    - DL prior: `set_dl()`
- `cov_spec`: Covariance prior specification. Use `set_ldlt()` for homoskedastic model.
- `include_mean = TRUE`: By default, you include the constant term in the model.
- `minnesota = c("no", "short", "longrun")`: Minnesota-type shrinkage.
- `verbose = FALSE`: Progress bar
- `num_thread`: Number of thread for OpenMP
    - Used in parallel multi-chain loop
    - This option is valid only when OpenMP in user's machine.

## Stochastic Search Variable Selection (SSVS) Prior

```{r fitssvs}
(fit_ssvs <- vhar_bayes(etf_train, num_chains = 1, num_iter = 20, bayes_spec = set_ssvs(), cov_spec = set_ldlt(), include_mean = FALSE, minnesota = "longrun"))
```

`autoplot()` for the fit (`bvharsp` object) provides coefficients heatmap.
There is `type` argument, and the default `type = "coef"` draws the heatmap.

```{r heatssvs}
autoplot(fit_ssvs)
```


## Horseshoe Prior

`bayes_spec` is the initial specification by `set_horseshoe()`. Others are the same.

```{r fiths}
(fit_hs <- vhar_bayes(etf_train, num_chains = 2, num_iter = 20, bayes_spec = set_horseshoe(), cov_spec = set_ldlt(), include_mean = FALSE, minnesota = "longrun"))
```

```{r heaths}
autoplot(fit_hs)
```

## Minnesota Prior

```{r fitmn}
(fit_mn <- vhar_bayes(etf_train, num_chains = 2, num_iter = 20, bayes_spec = set_bvhar(lambda = set_lambda()), cov_spec = set_ldlt(), include_mean = FALSE, minnesota = "longrun"))
```

## Normal-Gamma prior

```{r fitng}
(fit_ng <- vhar_bayes(etf_train, num_chains = 2, num_iter = 20, bayes_spec = set_ng(), cov_spec = set_ldlt(), include_mean = FALSE, minnesota = "longrun"))
```

## Dirichlet-Laplace prior

```{r fitdl}
(fit_dl <- vhar_bayes(etf_train, num_chains = 2, num_iter = 20, bayes_spec = set_dl(), cov_spec = set_ldlt(), include_mean = FALSE, minnesota = "longrun"))
```

# Bayesian visualization

`autoplot()` also provides Bayesian visualization. `type = "trace"` gives MCMC trace plot.

```{r}
autoplot(fit_hs, type = "trace", regex_pars = "tau")
```

`type = "dens"` draws MCMC density plot. If specifying additional argument `facet_args = list(dir = "v")` of `bayesplot`, you can see plot as the same format with coefficient matrix.

```{r denshs}
autoplot(fit_hs, type = "dens", regex_pars = "kappa", facet_args = list(dir = "v", nrow = nrow(fit_hs$coefficients)))
```


```{r resetopts, include=FALSE}
options(orig_opts)
```
