An open API service indexing awesome lists of open source software.

https://github.com/tylerjpike/sovereign

State-Dependent Empirical Analysis: tools for state-dependent forecasts, impulse response functions, historical decomposition, and forecast error variance decomposition.
https://github.com/tylerjpike/sovereign

econometrics forecasting impulse-response local-projection macroeconomics r state-dependent time-series vector-autoregression

Last synced: 8 months ago
JSON representation

State-Dependent Empirical Analysis: tools for state-dependent forecasts, impulse response functions, historical decomposition, and forecast error variance decomposition.

Awesome Lists containing this project

README

          

# sovereign: State-Dependent Empirical Analysis

[![License: GPL v3](https://img.shields.io/badge/License-GPL%20v3-blue.svg)](http://www.gnu.org/licenses/gpl-3.0)
[![CRAN status](https://www.r-pkg.org/badges/version/sovereign)](https://CRAN.R-project.org/package=sovereign)
[![Lifecycle: stable](https://img.shields.io/badge/lifecycle-stable-brightgreen.svg)](https://lifecycle.r-lib.org/articles/stages.html)
[![codecov](https://codecov.io/gh/tylerJPike/sovereign/branch/main/graph/badge.svg?token=WXLWR6H93B)](https://app.codecov.io/gh/tylerJPike/sovereign)
[![R-CMD-check](https://github.com/tylerJPike/sovereign/workflows/R-CMD-check/badge.svg)](https://github.com/tylerJPike/sovereign/actions)

The sovereign package introduces a set of tools for state-dependent empirical analysis through both VAR- and local projection-based state-dependent forecasts, impulse response functions, forecast error variance decompositions, and historical decompositions.

See the sovereign package [website](https://tylerjpike.github.io/sovereign/) for examples and documentation.

----

## Installation

One may install `sovereign` through the package’s [GitHub](https://github.com/tylerJPike/sovereign) page with

devtools::install_github('tylerJPike/sovereign')

or through [CRAN](https://CRAN.R-project.org/package=sovereign) via

install.packages("sovereign")

----

## Available Tools

Unsupervised Regime Assignment
1. random forest
2. k-means clustering
3. EM
4. Bai & Perron (2003)

Local Projections (LP)
1. direct projection forecasting
1. impulse responses

Vector Auto-Regression (VAR), Structural VAR (SVAR),
and external instrument SVAR (Proxy-SVAR)
1. recursive forecasting
2. impulse responses
3. forecast error variance decomposition
4. historical error decomposition

----

## Basic Workflow
# load packages
library(sovereign) # analysis
library(dplyr) # general cleaning
library(lubridate) # date functions

#-------------------------------------------
# create data
#-------------------------------------------
# pull and prepare data from FRED
quantmod::getSymbols.FRED(
c('UNRATE','INDPRO','GS10'),
env = globalenv())

Data = cbind(UNRATE, INDPRO, GS10)

Data = data.frame(Data, date = zoo::index(Data)) %>%
filter(lubridate::year(date) >= 1990,
lubridate::year(date) < 2020) %>%
na.omit()

# create a regime explicitly
Data.threshold = Data %>%
mutate(mp = if_else(GS10 > median(GS10), 1, 0))

#------------------------------------------
# learn regimes
#------------------------------------------
# assign regimes based on unsurpervised kmeans
# (will not be used further)
regimes =
regimes(
data = Data,
regime.n = 3,
method = 'kmeans')

#------------------------------------------
# single-regime var
#------------------------------------------
# estimate VAR
# (using IC lag selection and short-term
# impact restrictions, i.e. calculate structurals
errors via Cholesky decomposition)
var =
VAR(
data = Data,
horizon = 10,
freq = 'month',
lag.ic = 'BIC',
lag.max = 4)

# if impacts of the COVID shock are present,
# in the user's data, then the user can
# implement the Lenza-Primiceri COVID volatility
# correction for VARs:
# var = covid_volatility_correction(var)

# plot forecasts
plot_forecast(var$forecasts[[1]])

# plot residuals
plot_error(var$residuals[[1]])

# estimate IRF
irf =
IRF(
var,
CI = c(0.05,0.95))

# plot IRF
plot_irf(irf)

# estimate forecast error variance decomposition
fevd =
FEVD(
var,
horizon = 10)

# plot FEVD
plot_fevd(fevd)

# estimate historical decomposition
hd = HD(var)

# plot HD
plot_hd(hd)

#-------------------------------------------
# multi-regime var
#-------------------------------------------
# estimate multi-regime VAR
var =
RVAR(
data = Data.threshold,
regime = 'mp',
p = 1,
horizon = 1,
freq = 'month')

# estimate IRF
rvar.irf =
IRF(
rvar,
horizon = 10,
CI = c(0.05,0.95))

# plot IRF
# regime 1: low interest rates
plot_irf(rvar.irf[[1]])
# regime 2: high interest rates
plot_irf(rvar.irf[[2]])

# estimate forecast error variance decomposition
rvar.fevd =
FEVD(
rvar,
horizon = 10)

# plot FEVD
# regime 1: low interest rates
plot_fevd(rvar.fevd[[1]])
# regime 2: high interest rates
plot_fevd(rvar.fevd[[2]])

# estimate HD
rvar.hd = HD(rvar)

#-------------------------------------------
# single-regime local projections
#-------------------------------------------
# estimate single-regime forecasts
# (one or multiple horizons may be estimated)
lp =
LP(
data = Data,
p = 1,
horizon = 1,
freq = 'month')

# estimate single-regime IRF
lp.irf = IRF(lp)

# plot IRF
plot_irf(lp.irf)

#-------------------------------------------
# multi-regime local projections
#-------------------------------------------
# estimate multi-regime IRF
rlp =
RLP(
data = Data,
regime = 'mp',
p = 1,
horizon = 1,
freq = 'month')

# estimate multi-regime IRF
rlp.irf = IRF(rlp)

# plot IRF
# regime 1: low interest rates
plot_irf(rlp.irf[[1]])
# regime 2: high interest rates
plot_irf(rlp.irf[[2]])

---
## Contact
If you should have questions, concerns, or wish to collaborate, please contact [Tyler J. Pike](https://tylerjpike.github.io/)