{"id":19590217,"url":"https://github.com/guyabel/tsbugs","last_synced_at":"2025-07-28T07:08:31.886Z","repository":{"id":21931582,"uuid":"25255965","full_name":"guyabel/tsbugs","owner":"guyabel","description":null,"archived":false,"fork":false,"pushed_at":"2021-08-14T12:03:53.000Z","size":69,"stargazers_count":4,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-07-14T09:43:02.989Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"R","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/guyabel.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}},"created_at":"2014-10-15T13:33:27.000Z","updated_at":"2021-08-14T12:03:56.000Z","dependencies_parsed_at":"2022-08-20T03:20:15.454Z","dependency_job_id":null,"html_url":"https://github.com/guyabel/tsbugs","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/guyabel/tsbugs","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/guyabel%2Ftsbugs","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/guyabel%2Ftsbugs/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/guyabel%2Ftsbugs/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/guyabel%2Ftsbugs/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/guyabel","download_url":"https://codeload.github.com/guyabel/tsbugs/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/guyabel%2Ftsbugs/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":267476115,"owners_count":24093435,"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-07-28T02:00:09.689Z","response_time":68,"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":[],"created_at":"2024-11-11T08:23:54.084Z","updated_at":"2025-07-28T07:08:31.863Z","avatar_url":"https://github.com/guyabel.png","language":"R","funding_links":[],"categories":[],"sub_categories":[],"readme":"---\noutput: github_document\n---\n\n\u003c!-- README.md is generated from README.Rmd. Please edit that file --\u003e\n\n```{r, include = FALSE}\nknitr::opts_chunk$set(\n  collapse = TRUE,\n  comment = \"#\u003e\",\n  fig.path = \"man/figures/README-\",\n  out.width = \"100%\"\n)\n```\n\n# tsbugs\n\n\u003c!-- badges: start --\u003e\n\u003c!-- badges: end --\u003e\n\n\u003c!-- The goal of tsbugs is to ... --\u003e\n\n## Installation\n\nYou can install the development version from [GitHub](https://github.com/) with:\n\n``` r\n# install.packages(\"devtools\")\ndevtools::install_github(\"guyabel/tsbugs\")\n```\n\nThe package is no longer on CRAN.\n\n## Details\n\nThe functions in the tsbugs package are aimed to automate the writing of time series models to run in \u003ca href=\"http://www.mrc-bsu.cam.ac.uk/bugs/welcome.shtml\"\u003eWinBUGS\u003c/a\u003e or \u003ca href=\"http://www.openbugs.net/w/FrontPage\"\u003eOpenBUGS\u003c/a\u003e. I created these functions when working on [model averaging for time series models](https://www.demographic-research.org/volumes/vol29/43/default.htm). I found it a lot easier to build R functions to write the BUGS models than the more error-inducing process of copy and pasting BUGS scripts, and then making slight alterations to create new models. It also allowed me to add arguments to specify different lag lengths, prior distributions, variance assumptions and data lengths. Below are examples for three types of time series models; autorgressive models with \n  \n  - [Constant variance](https://github.com/guyabel/tsbugs#autoregressive-models)\n  - [Stochastic volatility](https://github.com/guyabel/tsbugs#stochastic-volatility-models)\n  - [Random variance shift models](https://github.com/guyabel/tsbugs#random-variance-shift-models)\n\n## Autoregressive Models\n\nThe `ar.bugs` command builds a BUGS script for autoregressive (AR) models ready to use in \u003ca href=\"http://cran.r-project.org/web/packages/R2OpenBUGS\"\u003eR2OpenBUGS\u003c/a\u003e. For example, consider the `LakeHuron` data.\n```{r, fig.show='hide'}\nLH \u003c- LakeHuron\npar(mfrow=c(2,1))\nplot(LH, main=\"Level (ft)\")\nplot(diff(LH), main=\"Differenced Level\")\n```\n\u003ca href=\"http://gjabel.files.wordpress.com/2014/03/tsbugs1-cvdata.png\"\u003e\u003cimg class=\"aligncenter size-full wp-image-1219\" src=\"http://gjabel.files.wordpress.com/2014/03/tsbugs1-cvdata.png\" alt=\"tsbugs1-cvdata\" width=\"600\" height=\"600\" /\u003e\u003c/a\u003e\u003cbr /\u003e\nWe can construct a AR(1) model for this data (after differencing the data to obtain a stationary mean) as such:\n```{r}\nlibrary(tsbugs)\nar1 \u003c- ar.bugs(y=diff(LH), ar.order=1)\nprint(ar1$bug)\n```\nThe `ar.bugs` function allows for alternative specifications for prior distributions, forecasts and the inclusion of mean term:\n```{r}\nar2 \u003c- ar.bugs(y=diff(LH), ar.order=2, ar.prior=\"dunif(-1,1)\", var.prior=\"dgamma(0.001,0.001)\",\n               k = 10, mean.centre = TRUE)\nprint(ar2$bug)\n```\nThe tsbugs objects can be used with R2OpenBUGS to easily run models from R. This is made even easier using the `inits` and `nodes` functions (also in the tsbugs package). For example:\n```{r, eval = FALSE}\nwriteLines(ar2$bug, \"ar2.txt\")\nlibrary(\"R2OpenBUGS\")\nar2.bug \u003c- bugs(data = ar2$data,\n                inits = list(inits(ar2)),\n                param = c(nodes(ar2, \"prior\")$name, \"y.new\"),\n                model = \"ar2.txt\",\n                n.iter = 11000, n.burnin = 1000, n.chains = 1)\n```\nNote, 1) the model is written to a `.txt` file (as required by R2OpenBUGS), 2) the data used is part of the `tsbugs` object. The `ar.bugs` command cleans the data and adds missing values at the end of the series for foretasted values, 3) the initial values offered by the inits function are very crude, and with more complicated data or models, users might be better off specifying there own list of initial values. The parameter traces and posterior distributions can be plotted using the \u003ca href=\"http://cran.r-project.org/web/packages/coda\"\u003ecoda\u003c/a\u003e package:\n```{r, eval=FALSE}\nlibrary(coda)\nparam.mcmc \u003c- as.mcmc(ar2.bug$sims.matrix[,nodes(ar2, \"prior\")$name])\nplot(param.mcmc[,1:4])\n```\n\u003ca href=\"http://gjabel.files.wordpress.com/2014/03/tsbugs1-cvparam.png\"\u003e\u003cimg class=\"aligncenter size-full wp-image-1221\" src=\"http://gjabel.files.wordpress.com/2014/03/tsbugs1-cvparam.png\" alt=\"tsbugs1-cvparam\" width=\"600\" height=\"600\" /\u003e\u003c/a\u003e\u003cbr /\u003e\n\nThe fanplot package can be used to plot the entire series of posterior predictive distributions. We may also plot (after deriving using the `diffinv` function) the posterior predictive distributions of the lake level:\n```{r, eval=FALSE}\n# derive future level\nynew.mcmc \u003c- ar2.bug$sims.list$y.new\nlhnew.mcmc \u003c- apply(ynew.mcmc, 1, diffinv, xi = tail(LH,1))\nlhnew.mcmc \u003c- t(lhnew.mcmc[-1,])\n\n# plot differenced\npar(mfrow=c(2,1))\nplot(diff(LH), xlim = k0 + c(-50, 10), main=\"Differenced Level\")\n\n# add fan\nlibrary(\"fanplot\")\nk0 \u003c- end(LH)[1]\nfan(ynew.mcmc, start=k0+1, rcex=0.5)\n\n# plot undifferenced\nplot(LH, xlim=k0+c(-50,10), main=\"Level\")\nfan(lhnew.mcmc, start=k0+1, rcex=0.5)\n```\n\u003ca href=\"http://gjabel.files.wordpress.com/2014/03/tsbugs1-cvforc.png\"\u003e\u003cimg class=\"aligncenter size-full wp-image-1220\" src=\"http://gjabel.files.wordpress.com/2014/03/tsbugs1-cvforc.png\" alt=\"tsbugs1-cvforc\" width=\"600\" height=\"600\" /\u003e\u003c/a\u003e\n\n## Stochastic Volatility Models\n\nThe `sv.bugs` command builds a BUGS script for stochastic volatility SV models ready to use in R2OpenBUGS. For example, consider the `svpdx` data.\n```{r, eval=FALSE}\n# plot\nplot(svpdx$pdx, type = \"l\",\n     main = \"Return of Pound-Dollar exchange rate data from 2nd October 1981 to 28th June 1985\", \n     cex.main = 0.8)\n```\n\u003ca href=\"http://gjabel.files.wordpress.com/2014/03/tsbugs1-svdata.png\"\u003e\u003cimg class=\"aligncenter size-full wp-image-1216\" src=\"http://gjabel.files.wordpress.com/2014/03/tsbugs1-svdata.png\" alt=\"tsbugs1-svdata\" width=\"600\" height=\"600\" /\u003e\u003c/a\u003e\u003cbr /\u003e\nWe can construct a AR(0)-SV model for this data, and also obtain posterior simulations using the `sv.bugs` command:\n```{r}\ny \u003c- svpdx$pdx\nsv0 \u003c- sv.bugs(y, sim=TRUE)\nprint(sv0$bug)\n```\nThis model closely matches those presented in \u003ca href=\"http://onlinelibrary.wiley.com/doi/10.1111/1368-423X.00046/abstract\"\u003eMeyer and Yu (2002)\u003c/a\u003e. There are further options in the tsbugs package to incorporate different priors that do not involve transformations such as those for `psi1` above. Using R2OpenBUGS we can fit the model,\n```{r, eval=FALSE}\n# decent initial value for variance in first period\ninit \u003c- inits(sv0, warn=FALSE)\ninit$psi0 \u003c- log(var(y))\n# write bug\nwriteLines(sv0$bug, \"sv0.txt\")\n# might take a while to compile\nsv0.bug \u003c- bugs(data = sv0$data,\n                inits = list(init),\n                param = c(nodes(sv0, \"prior\")$name,\"y.sim\",\"h\"),\n                model = \"sv0.txt\",\n                n.iter = 11000, n.burnin = 1000, n.chains = 1)\n```\nThe volatility and estimates can be easily extracted,\n```{r, eval=FALSE}\nh.mcmc \u003c- sv0.bug$sims.list$h\n```\nWhich allows us to directly view the estimated volatility process or the time-dependent standard deviation using the \u003ca href=\"https://guyabel.github.io/fanplot/\"\u003efanplot\u003c/a\u003e package,\n```{r, eval=FALSE}\n# plot\nplot(NULL, xlim = c(1, 945)+c(0,40), ylim = c(-4,2), main=\"Estimated Volatility from SV Model\")\n# fan\nfan(h.mcmc, type = \"interval\")\n```\n\u003ca href=\"http://gjabel.files.wordpress.com/2014/03/tsbugs1-svh.png\"\u003e\u003cimg class=\"aligncenter size-full wp-image-1217\" src=\"http://gjabel.files.wordpress.com/2014/03/tsbugs1-svh.png\" alt=\"tsbugs1-svh\" width=\"600\" height=\"600\" /\u003e\u003c/a\u003e\u003cbr /\u003eWe can also plot the posterior simulations from the model:\n```{r, eval=FALSE}\n# derive percentiles\ny.mcmc \u003c- sv0.bug$sims.list$y.sim\n# plot\nplot(NULL, type = \"l\", xlim = c(1, 945)+c(0,20), ylim = range(y), \n     main = \"Posterior Model Simulations and Data\")\nfan(y.mcmc)\nlines(y)\n```\n\u003ca href=\"http://gjabel.files.wordpress.com/2014/03/tsbugs1-svysim.png\"\u003e\u003cimg class=\"aligncenter size-full wp-image-1218\" src=\"http://gjabel.files.wordpress.com/2014/03/tsbugs1-svysim.png\" alt=\"tsbugs1-svysim\" width=\"600\" height=\"600\" /\u003e\u003c/a\u003e\n\n## Random Variance Shift Models\n\nThe `rv.bugs` command builds a BUGS script for random variance (RV) shift models, similar to that of \u003ca href=\"http://www.tandfonline.com/doi/abs/10.1080/01621459.1993.10476364#.UygPt4U7bIc\"\u003eMcCulloch and Tsay (1993)\u003c/a\u003e ready to use in R2OpenBUGS. Consider the `ew` data.\n```{r, eval=FALSE}\nr \u003c- ts(ew[2:167]/ew[1:166]-1, start=1841)\ny \u003c- diff(r)\nplot(y, main=\"Difference in England and Wales Population Growth Rate\")\n```\n\u003ca href=\"http://gjabel.files.wordpress.com/2014/03/tsbugs1-rvdata.png\"\u003e\u003cimg class=\"aligncenter size-full wp-image-1223\" src=\"http://gjabel.files.wordpress.com/2014/03/tsbugs1-rvdata.png\" alt=\"tsbugs1-rvdata\" width=\"600\" height=\"600\" /\u003e\u003c/a\u003e\u003cbr /\u003eWe can create a BUGS script to fit a RV model to this data, including posterior simulations, using the `rv.bugs` command:\n```{r, eval=FALSE}\nrv0 \u003c- rv.bugs(y, sim=TRUE)\nprint(rv0)\n```\nand then run the script in R2OpenBUGS (this can take a couple of hours):\n```{r, eval=FALSE}\n# decent inital value for variance in first period\ninit \u003c- inits(rv0, warn=FALSE)\ninit$isig02\u003c-sd(y)^-2\n# write bug\nwriteLines(rv0$bug,\"rv0.txt\")\n# might take a while to compile\nrv0.bug \u003c- bugs(data = rv0$data,\n                inits = list(init),\n                param = c(nodes(rv0, \"prior\")$name,\"y.sim\",\n                          \"h\",\"delta\",\"beta\"),\n                model = \"rv0.txt\",\n                n.iter = 11000, n.burnin = 1000, n.chains = 1)\n```\nWe can plot the posterior simulations from the model using the fanplot package:\n```{r, eval=FALSE}\n# derive percentiles\ny0 \u003c- tsp(y)[1]\ny.mcmc \u003c- rv0.bug$sims.list$y.sim\n\n# plot\nplot(NULL, xlim=tsp(y)[1:2]+c(-5,5), ylim = range(y),\n     main=\"Posterior Simulations\")\nfan(y.mcmc, start = y0, rlab=c(10,50,90), llab=TRUE)\nlines(y)\n```\n\u003ca href=\"http://gjabel.files.wordpress.com/2014/03/tsbugs1-rvysim.png\"\u003e\u003cimg class=\"aligncenter size-full wp-image-1225\" src=\"http://gjabel.files.wordpress.com/2014/03/tsbugs1-rvysim.png\" alt=\"tsbugs1-rvysim\" width=\"600\" height=\"600\" /\u003e\u003c/a\u003e\u003cbr /\u003eAlongside the posterior distributions of the standard deviations,\n```{r, eval=FALSE}\n# derive sigma\nh.mcmc \u003c- rv0.bug$sims.list$h\nsigma.mcmc \u003c- sqrt(exp(h.mcmc))\n# plots\nplot(NULL, xlim =tsp(y)[1:2]+c(-5,5), ylim = c(0,0.008), main=\"Standard Deviation\")\nfan(sigma.mcmc, start = y0, rlab=c(5,50,95), llab = c(5,50,95))\n```\n\u003ca href=\"http://gjabel.files.wordpress.com/2014/03/tsbugs1-rvhsigma.png\"\u003e\u003cimg class=\"aligncenter size-full wp-image-1224\" src=\"http://gjabel.files.wordpress.com/2014/03/tsbugs1-rvhsigma.png\" alt=\"tsbugs1-rvhsigma\" width=\"600\" height=\"600\" /\u003e\u003c/a\u003e\u003cbr /\u003eThe posterior distributions of the probability of a variance shift and multiplier effect of the shift in variance (`delta[t]` and `beta[t]` in the BUGS model) can also be plotted. Note, when there is no variance shift, the posterior of the `beta[t]` is similar to the prior distribution.\n```{r, eval=FALSE}\n#extract data\ndelta.mcmc \u003c- rv0.bug$sims.list$delta\nbeta.mcmc \u003c- rv0.bug$sims.list$beta\n\n# plots\npar(mfrow=c(2,1))\nplot(NULL, xlim = tsp(y)[1:2]+c(-5,5), ylim = c(0,1), main=\"Probability of Variance Change Point\")\nfan(delta.mcmc, start=y0, ln = NULL, rlab = NULL)\n\nplot(NULL, xlim = tsp(y)[1:2]+c(-5,5), ylim = c(-2,2), main=\"Variance Multiplier\")\nfan(beta.mcmc, start=y0)\n```\n\u003ca href=\"http://gjabel.files.wordpress.com/2014/03/tsbugs1-rvbeta.png\"\u003e\u003cimg class=\"aligncenter size-full wp-image-1222\" src=\"http://gjabel.files.wordpress.com/2014/03/tsbugs1-rvbeta.png\" alt=\"tsbugs1-rvbeta\" width=\"600\" height=\"600\" /\u003e\u003c/a\u003e","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fguyabel%2Ftsbugs","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fguyabel%2Ftsbugs","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fguyabel%2Ftsbugs/lists"}