{"id":22424278,"url":"https://github.com/asael697/bayesforecast","last_synced_at":"2025-06-22T20:41:42.784Z","repository":{"id":45778856,"uuid":"319624614","full_name":"asael697/bayesforecast","owner":"asael697","description":"Automatic forecasting and Bayesian modeling for time series with Stan","archived":false,"fork":false,"pushed_at":"2024-01-16T15:36:16.000Z","size":10105,"stargazers_count":38,"open_issues_count":6,"forks_count":6,"subscribers_count":2,"default_branch":"master","last_synced_at":"2024-01-26T09:18:21.461Z","etag":null,"topics":["bayesian-inference","forecasting-models","mcmc","stan","time-series-analysis"],"latest_commit_sha":null,"homepage":"","language":"C++","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/asael697.png","metadata":{"files":{"readme":"README.Rmd","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null}},"created_at":"2020-12-08T11:57:52.000Z","updated_at":"2024-01-16T15:36:20.000Z","dependencies_parsed_at":"2023-10-11T07:56:02.041Z","dependency_job_id":null,"html_url":"https://github.com/asael697/bayesforecast","commit_stats":{"total_commits":89,"total_committers":3,"mean_commits":"29.666666666666668","dds":"0.022471910112359605","last_synced_commit":"605cbe4363cf84c622e258971a6f722ee8fb4cb7"},"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/asael697%2Fbayesforecast","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/asael697%2Fbayesforecast/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/asael697%2Fbayesforecast/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/asael697%2Fbayesforecast/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/asael697","download_url":"https://codeload.github.com/asael697/bayesforecast/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":228348311,"owners_count":17905899,"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","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":["bayesian-inference","forecasting-models","mcmc","stan","time-series-analysis"],"created_at":"2024-12-05T18:14:50.410Z","updated_at":"2024-12-05T18:14:51.215Z","avatar_url":"https://github.com/asael697.png","language":"C++","funding_links":[],"categories":[],"sub_categories":[],"readme":"---\noutput:\n  md_document:\n    variant: markdown_github\n---\n\n```{r setup, include=FALSE}\nknitr::opts_chunk$set(echo = TRUE)\nknitr::opts_chunk$set(\n  collapse = TRUE,\n  comment = \"#\u003e\",\n  fig.path = \"man/figures/\",\n  dev = \"png\",\n  dpi = 150,\n  fig.asp = 0.8,\n  fig.width = 10,\n  out.width = \"60%\",\n  fig.align = \"center\"\n)\n```\n\n\u003cimg src=\"man/figures/bflogo.png\" width = 120 alt=\"bayesforecast Logo\"/\u003e[\u003cimg src=\"https://raw.githubusercontent.com/stan-dev/logos/master/logo_tm.png\" align=\"right\" width=120 alt=\"Stan Logo\"/\u003e](https://mc-stan.org) \n\n# Automatic forecasting and Bayesian modeling for time series\n\n```{r,include=FALSE}\nset.seed(294)\nlibrary(astsa)\nlibrary(ggplot2)\nlibrary(gridExtra)\nlibrary(bayesforecast)\n\nlay1 =  matrix(c(1,1,2,3,4,5),ncol = 2,byrow = TRUE)\n```\n\nThe *bayesforecast* package implements Bayesian estimation of structured time series models, using the Hamiltonian Monte Carlo method, implemented with [Stan](https://mc-stan.org/), a probabilistic language model in C++. The aim of this package is to provide an interface for forecasting and Bayesian modelling of the most popular time series models.\n\nOn the beta version 1.0.0, the available models are:\n \n  + ARIMA [Box and Jenkins (1970)](https://www.wiley.com/en-us/Time+Series+Analysis%3A+Forecasting+and+Control%2C+5th+Edition-p-9781118675021).\n  + Multiplicative Seasonal ARIMA [Brockwell and Davis (2016)](https://www.amazon.com/dp/3319298526/ref=cm_sw_su_dp?tag=otexts-20)\n  + GARCH [Bollerslev (1986)](https://doi.org/10.1016/0304-4076(86)90063-1)\n  + Asymmetric GARCH [Fonseca et. al (2019)](https://arxiv.org/abs/1910.01398)\n  + ARMAX [Pankratz (1991)](https://www.amazon.com/dp/0471615285/ref=cm_sw_su_dp?tag=otexts-20)\n  + Dynamic Harmonic regression [Pankratz (1991)](https://www.amazon.com/dp/0471615285/ref=cm_sw_su_dp?tag=otexts-20)\n  + Stochastic Volatility models [Sangjoon et. al. (1998)](https://www.jstor.org/stable/2566931)\n  + Local global trend [Ng et. al. (2020)](https://arxiv.org/abs/2004.08492) \n  + Additive ETS models from the [forecast package](https://github.com/robjhyndman/forecast). [Rob and Khandakar (2008)](https://www.jstatsoft.org/article/view/v027i03)\n    * Local level models\n    * Holt's linear trend\n    * Holt-Winter's \n\nAdditionally the forecast function is implemented for automatic forecast, by default a Generalized additive model from the [prophet](https://facebook.github.io/prophet/docs/installation.html#r) package is used.\n\n### Installation \n\nThis is still a beta version package, so currently installing it could be challenging, we recommend to install the current R version (**R4.0**) and the **Rtools4.0**. After that, install the package [rstan](https://mc-stan.org/users/interfaces/rstan.html), you can follow the installation procedure [here](https://github.com/stan-dev/rstan/wiki/RStan-Getting-Started).\n\nTo install the latest release version from CRAN use\n\n```{r, eval=FALSE}\ninstall.packages(\"bayesforecast\")\n```\n\nThe current developmental version can be downloaded from github via:\n\n```{r, eval = FALSE}\nif (!requireNamespace(\"remotes\")) install.packages(\"remotes\")\n\nremotes::install_github(\"asael697/bayesforecast\",dependencies = TRUE)\n```\n \n### Case study: Analyzing the monthly live birth in U.S. an example\n\nAs an example, we provide a time series modelling for the monthly live births in the United States 1948-1979, published by [Stoffer (2019)](https://github.com/nickpoison/astsa). In **figure 1** , the data has a seasonal behaviour that repeats every year. The series *waves* in the whole 40 years period (*superior part*). In addition, the partial (*pacf*) and auto-correlation (*acf*) functions  are far from zero (*middle part*). After applying a difference to the data, the ACF and PACF plots still have some non-zero values every 12 lags (*inferior part*).\n\n```{r fig1,fig.cap=\"\\\\label{fig:fig1}Monthly live birth U.S.A\",echo=FALSE}\ndbirth = diff(birth)\n\ng1 = autoplot(birth)+labs(y= \"births (thousands)\",title =\"U.S. Monthly live births\")\ng2 = ggacf(birth)+labs(y =\"births\",title = \"ACF\")\ng3 = ggpacf(birth)+labs(y =\"births\",title = \"PACF\")\ng4 = ggacf(dbirth)+labs(y =\"differenced births\",title = \"ACF\")\ng5 = ggpacf(dbirth)+labs(y =\"differenced births\",title = \"PACF\")\n\n\ngrid.arrange(g1,g2,g3,g4,g5,layout_matrix = lay1)\nrm(g1,g2,g3,g4,g5,dbirth)\n```\n\nFor start, a multiplicative Seasonal ARIMA model could give a good fit to the data, following [Tsay (2010)](https://www.amazon.com/dp/047136164X/ref=cm_sw_su_dp?tag=otexts-20) recommendations for order selection using the auto-correlation functions,  we define (p = 1, d = 1, q = 1) and for the seasonal part (P= 1, D = 1 and Q = 1). The fitted model is defined as follows\n\n```{r,results='hide', message=FALSE}\nsf1 = stan_sarima(ts = birth,order = c(1,1,1),seasonal = c(1,1,1),\n                  prior_mu0 = student(mu = 0,sd = 1,df = 7))\n```\n\nAll fitted models are *varstan* objects, these  are S3 classes with the *stanfit* results provided by the [rstan](https://mc-stan.org/users/interfaces/rstan.html) package, and other useful elements that make the modeling process easier. \n\n```{r}\nsf1\n```\n\nAfter fitting our model we can make a visual diagnostic of our parameters, check residuals and fitted values using the plot method. The package provides the posterior sample of every residual, but checking all of them is an exhausting task. An alternative, is checking the process generated by the residuals posterior estimate.\n\n```{r fig3}\ncheck_residuals(sf1)\n```\n\nA white noise behavior indicates a good model fit. The model's residuals in figure 2, seems to follow a random noise, the auto-correlation in *acf plots* quickly falls to zero, indicating an acceptable model fit. Finally a forecast of the for the next year can be performed as:\n\n```{r}\nautoplot(forecast(object = sf1,h = 12))\n```\n\n### Automatic forecast with prophet\n\nAutomatic prediction is possible using the **forecast** function, by default the prediction is done\nusing Generalized additive models from the [prophet](https://facebook.github.io/prophet/docs/quick_start.html#python-api) package.\n\n```{r,message=FALSE}\nlibrary(astsa)\nautoplot(object = forecast(cardox,h = 12),include = 100)\n```\n\n\n### References \n\nFor further readings and references you can check: \n\n  \n  + Stan Development Team. 2018. Stan Modeling Language Users Guide and Reference Manual, Version 2.18.0. [http://mc-stan.org](https://mc-stan.org)\n  \n  + Forecasting: Principles and practice Monash University, Australia. [Forecasting principles](https://otexts.com/fpp2/)\n  \n  + Time Series Analysis and Its Applications, With R Examples — 4th Edition.[astsa](https://www.stat.pitt.edu/stoffer/tsa4/index.html)\n  \n  + facebook/prophet Quick start documentation. [prophet](https://facebook.github.io/prophet/docs/quick_start.html#python-api) \n  \n  + Orbit API Documentation and Examples. [Orbit](https://uber.github.io/orbit/)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fasael697%2Fbayesforecast","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fasael697%2Fbayesforecast","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fasael697%2Fbayesforecast/lists"}