My tsbugs package has gone up on CRAN. The functions in the tsbugs package are aimed to automate the writing of time series models to run in WinBUGS (Lunn et al., 2000) or OpenBUGS (Lunn et al., 2009). I created these functions a while back when I was doing some work on model averaging for time series models. 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. I decided not to write a vignette for CRAN, as it would have involved doing some estimation in BUGS via R2WinBUGS or R2OpenBUGS and running into some problems when submitted the package. Instead I thought I would post a quick guide here.
The ar.bugs command builds a BUGS script for autoregressive (AR) models ready to use in R2OpenBUGS (Sturtz et al., 2005). For example, consider the LakeHuron data.
We can construct a AR(1) model for this data (after differencing the data to obtain a stationary mean) as such:
The ar.bugs function allows for alternative specifications for prior distributions, forecasts and the inclusion of mean term:
The 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:
Note, 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 coda package:
The 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:
Stochastic Volatility (SV) Models
The sv.bugs command builds a BUGS script for SV models ready to use in R2OpenBUGS. For example, consider the svpdx data.
We can construct a AR(0)-SV model for this data, and also obtain posterior simulations using the sv.bugs command:
This model closely matches those presented in Meyer and Yu (2002). 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,
The volatility and time-dependent standard deviations estimates can be easily derived,
Which allows us to directly view the estimated volatility process or the time-dependent standard deviation using the fanplot package ,
We can also plot the posterior simulations from the model:
Random Variance (RV) Shift Models
The rv.bugs command builds a BUGS script for random variance shift models, similar to that of McCulloch and Tsay (1993 ready to use in R2OpenBUGS. Consider the ew data.
We can create a BUGS script to fit a RV model to this data, including posterior simulations, using the rv.bugs command:
and then run the script in R2OpenBUGS:
We can plot the posterior simulations from the model using the fanplot package:
Alongside the posterior distributions of the volatility and standard deviations,
The posterior distributions of the multiplier effect of the shift in variance (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.
I hope to develop the package in the future with a few more functions to produce some MA and GARCH models. I am also thinking about ways to incorporate non-normal time series data.
Lunn, D. J., A. Thomas, N. Best, and D. Spiegelhalter (2000). WinBUGS – A Bayesian modelling framework: Concepts, structure, and extensibility. Statistics and Computing 10 (4), 325–337.
Lunn, D. et al. (2009). The BUGS project: Evolution, critique and future directions. Statistics in Medicine 28.25, 3049-3067
Meyer, R. and J. Yu (2002). BUGS for a Bayesian analysis of stochastic volatility models. Econometrics Journal, 3, 2, pp. 198–215.
McCulloch, R. E. and R. S. Tsay (1993). Bayesian Inference and Prediction for Mean and Variance Shifts in Autoregressive Time Series. Journal of the American Statistical Association 88 (423), 968-978.
Sturtz, S., U. Ligges, and A. Gelman (2005). R2WinBUGS: a package for running WinBUGS from R. Journal of Statistical Software 12 (3).