{"id":32203986,"url":"https://github.com/tylerjpike/sovereign","last_synced_at":"2025-10-22T04:49:38.448Z","repository":{"id":48492288,"uuid":"343567939","full_name":"tylerJPike/sovereign","owner":"tylerJPike","description":"State-Dependent Empirical Analysis: tools for state-dependent forecasts, impulse response functions, historical decomposition, and forecast error variance decomposition. ","archived":false,"fork":false,"pushed_at":"2022-11-07T01:27:17.000Z","size":8213,"stargazers_count":16,"open_issues_count":1,"forks_count":6,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-10-22T04:49:14.665Z","etag":null,"topics":["econometrics","forecasting","impulse-response","local-projection","macroeconomics","r","state-dependent","time-series","vector-autoregression"],"latest_commit_sha":null,"homepage":"","language":"R","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"gpl-3.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/tylerJPike.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2021-03-01T21:55:06.000Z","updated_at":"2025-08-30T06:27:03.000Z","dependencies_parsed_at":"2023-01-22T10:00:52.607Z","dependency_job_id":null,"html_url":"https://github.com/tylerJPike/sovereign","commit_stats":null,"previous_names":[],"tags_count":4,"template":false,"template_full_name":null,"purl":"pkg:github/tylerJPike/sovereign","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tylerJPike%2Fsovereign","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tylerJPike%2Fsovereign/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tylerJPike%2Fsovereign/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tylerJPike%2Fsovereign/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/tylerJPike","download_url":"https://codeload.github.com/tylerJPike/sovereign/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tylerJPike%2Fsovereign/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":280382976,"owners_count":26321423,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-10-22T02:00:06.515Z","response_time":63,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["econometrics","forecasting","impulse-response","local-projection","macroeconomics","r","state-dependent","time-series","vector-autoregression"],"created_at":"2025-10-22T04:49:37.353Z","updated_at":"2025-10-22T04:49:38.440Z","avatar_url":"https://github.com/tylerJPike.png","language":"R","funding_links":[],"categories":[],"sub_categories":[],"readme":"# sovereign: State-Dependent Empirical Analysis  \n\n\u003c!-- badges: start --\u003e\n[![License: GPL v3](https://img.shields.io/badge/License-GPL%20v3-blue.svg)](http://www.gnu.org/licenses/gpl-3.0)\n[![CRAN status](https://www.r-pkg.org/badges/version/sovereign)](https://CRAN.R-project.org/package=sovereign)\n[![Lifecycle: stable](https://img.shields.io/badge/lifecycle-stable-brightgreen.svg)](https://lifecycle.r-lib.org/articles/stages.html)\n[![codecov](https://codecov.io/gh/tylerJPike/sovereign/branch/main/graph/badge.svg?token=WXLWR6H93B)](https://app.codecov.io/gh/tylerJPike/sovereign)\n[![R-CMD-check](https://github.com/tylerJPike/sovereign/workflows/R-CMD-check/badge.svg)](https://github.com/tylerJPike/sovereign/actions)\n\u003c!-- badges: end --\u003e\n\nThe 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.    \n\nSee the sovereign package [website](https://tylerjpike.github.io/sovereign/) for examples and documentation. \n\n----\n\n## Installation \n\nOne may install `sovereign` through the package’s [GitHub](https://github.com/tylerJPike/sovereign) page with\n\n    devtools::install_github('tylerJPike/sovereign')\n\nor through [CRAN](https://CRAN.R-project.org/package=sovereign) via\n\n    install.packages(\"sovereign\")\n\n----\n\n## Available Tools  \n\nUnsupervised Regime Assignment\n1. random forest  \n2. k-means clustering  \n3. EM \n4. Bai \u0026 Perron (2003)  \n\nLocal Projections (LP)\n1. direct projection forecasting  \n1. impulse responses\n\nVector Auto-Regression (VAR), Structural VAR (SVAR),   \nand external instrument SVAR (Proxy-SVAR)  \n1. recursive forecasting\n2. impulse responses\n3. forecast error variance decomposition\n4. historical error decomposition\n\n\n----\n\n## Basic Workflow \n    # load packages\n    library(sovereign)         # analysis\n    library(dplyr)             # general cleaning\n    library(lubridate)         # date functions\n\n    #-------------------------------------------\n    # create data\n    #-------------------------------------------\n    # pull and prepare data from FRED\n    quantmod::getSymbols.FRED(\n        c('UNRATE','INDPRO','GS10'), \n        env = globalenv())\n\n    Data = cbind(UNRATE, INDPRO, GS10)\n\n    Data = data.frame(Data, date = zoo::index(Data)) %\u003e%\n        filter(lubridate::year(date) \u003e= 1990,\n               lubridate::year(date) \u003c  2020) %\u003e% \n        na.omit()\n\n    # create a regime explicitly   \n    Data.threshold = Data %\u003e%\n        mutate(mp = if_else(GS10 \u003e median(GS10), 1, 0))\n\n    #------------------------------------------\n    # learn regimes\n    #------------------------------------------\n    # assign regimes based on unsurpervised kmeans\n    #  (will not be used further)\n    regimes = \n        regimes(\n            data = Data, \n            regime.n = 3, \n            method = 'kmeans')\n\n    #------------------------------------------\n    # single-regime var\n    #------------------------------------------\n    # estimate VAR\n    # (using IC lag selection and short-term \n    #  impact restrictions, i.e. calculate structurals\n       errors via Cholesky decomposition)\n    var =\n        VAR(\n            data = Data,\n            horizon = 10,\n            freq = 'month',\n            lag.ic = 'BIC',\n            lag.max = 4)\n\n    # if impacts of the COVID shock are present,\n    #  in the user's data, then the user can \n    #  implement the Lenza-Primiceri COVID volatility\n    #  correction for VARs: \n    # var = covid_volatility_correction(var)\n\n    # plot forecasts\n    plot_forecast(var$forecasts[[1]])\n\n    # plot residuals\n    plot_error(var$residuals[[1]])\n\n    # estimate IRF\n    irf =\n        IRF(\n            var,\n            CI = c(0.05,0.95))\n\n    # plot IRF\n    plot_irf(irf)\n\n    # estimate forecast error variance decomposition\n    fevd =\n        FEVD(\n            var,\n            horizon = 10)\n\n    # plot FEVD\n    plot_fevd(fevd)\n\n    # estimate historical decomposition\n    hd = HD(var)\n    \n    # plot HD\n    plot_hd(hd)\n\n    #-------------------------------------------\n    # multi-regime var\n    #-------------------------------------------\n    # estimate multi-regime VAR\n    var =\n        RVAR(\n            data = Data.threshold,\n            regime = 'mp',\n            p = 1,\n            horizon = 1,\n            freq = 'month')\n    \n    # estimate IRF\n    rvar.irf =\n        IRF(\n            rvar,\n            horizon = 10,\n            CI = c(0.05,0.95))\n\n    # plot IRF\n    # regime 1: low interest rates\n    plot_irf(rvar.irf[[1]])\n    # regime 2: high interest rates\n    plot_irf(rvar.irf[[2]])\n\n    # estimate forecast error variance decomposition\n    rvar.fevd =\n        FEVD(\n            rvar,\n            horizon = 10)\n\n    # plot FEVD\n    # regime 1: low interest rates\n    plot_fevd(rvar.fevd[[1]])\n    # regime 2: high interest rates\n    plot_fevd(rvar.fevd[[2]])\n\n    # estimate HD\n    rvar.hd = HD(rvar)\n\n    #-------------------------------------------\n    # single-regime local projections\n    #-------------------------------------------\n    # estimate single-regime forecasts \n    #  (one or multiple horizons may be estimated)\n    lp = \n        LP(\n            data = Data,\n            p = 1,\n            horizon = 1,\n            freq = 'month')\n\n    # estimate single-regime IRF\n    lp.irf = IRF(lp)\n\n    # plot IRF\n    plot_irf(lp.irf)\n\n    #-------------------------------------------\n    # multi-regime local projections\n    #-------------------------------------------\n    # estimate multi-regime IRF\n    rlp = \n        RLP(\n            data = Data,\n            regime = 'mp',\n            p = 1,\n            horizon = 1,\n            freq = 'month')\n\n    # estimate multi-regime IRF\n    rlp.irf = IRF(rlp)\n\n    # plot IRF\n    # regime 1: low interest rates\n    plot_irf(rlp.irf[[1]])\n    # regime 2: high interest rates\n    plot_irf(rlp.irf[[2]])\n    \n\n---\n## Contact\nIf you should have questions, concerns, or wish to collaborate, please contact [Tyler J. Pike](https://tylerjpike.github.io/)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftylerjpike%2Fsovereign","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftylerjpike%2Fsovereign","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftylerjpike%2Fsovereign/lists"}