Description Usage Arguments Details Value Author(s) References See Also Examples
Create BUGS script of an Random Variance (RV) shift model similar to that of McCulloch and Tsay (1991). Options allow for the inclusion of a different lag orders for the mean term, forecasts, posterior simulations from the model and alternative specification of prior distributions on some parameters.
1 2 3 4 5 |
y |
Data to be used for the BUGS model. |
ar.order |
AR order of the mean process for BUGS model. |
k |
Length of forecast horizon to be included in the BUGS model. |
sim |
Enable posterior simulations to be included in the BUGS model. Default is |
mean.centre |
Include a term to centre the data on its mean value. Default is |
beg |
Starting value for which data are considered onwards (and including) in the likelihood of the BUGS model. Default is |
mean.prior |
Prior for mean term (not used if mean.centre is not set to |
ar.prior |
Prior for autoregressive terms. The distribution should be stated in BUGS syntax. By default this is set to a normal distribution with mean 0 and tolerance 1 ( |
rv.tol0.prior |
Prior distribution for the time-specific tolerance of the first data point, from which potential future shifts are based on. This must be a distribution of syntax recognisable to BUGS. By default this is set to a uninformative gamma distribution. |
rv.eps.prior |
Prior distribution for the epsilon term (the probability of the variance shift). This must be a distribution of syntax recognisable to BUGS and in theory restricted to generate values between 0 and 1, although no check is made to ensure users specify distributions as such. By default this argument is set to a beta distribution with a small probability for a variance shift ( |
rv.ilambda2.prior |
Prior for the inverse of the squared lambda term of a variance shift model. The lambda represents the magnitude (on the log scale) of average variance shifts, thus |
space |
Include some additional empty lines to seperate the likelihood, priors, forcasts and simulation components of the BUGS model. |
This function create BUGS scripts of an random variance shift model adapted from McCulloch and Tsay (1991). Prior distributions for the tolerance of the initial data point, the probability of the variance shift, and the magnitude of average variance shifts can be specified. by users to differ from their default values. User specified prior distributions should be set up using BUGS syntax. For example, dnorm
is a normal distribution with mean and tolerance (not variance) arguments.
The data y
, can contain missing values. Note, if missing values are close the beginning of the series when a high order model for the mean process is specified (i.e. the second data point is missing and a AR(4) is specified) the user with have to set a high starting point for model to be fitted on (beg
) for the BUGS model to function (i.e. 7).
bug |
A BUGS model of type |
data |
The data to be used with the model. This might extend the original data passed to the function with |
info |
Additional information on the length of the data, variance type and line numbers of certain parts of the BUGS model. |
Guy J. Abel
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 | # Create AR(0)-SV model for population growth rate
r <- ts(ew[2:167]/ew[1:166]-1, start=1841)
y <- diff(r)
plot(y, type = "l")
rv0 <- rv.bugs(y)
print(rv0)
# AR(2)-RV model with alternative priors
rv2 <- rv.bugs(y, rv.eps.prior = "dbeta(1,20)")
print(rv2)
# AR(0)-RV model with posterior simulations
rv0 <- rv.bugs(y, sim = TRUE)
print(rv0)
## Not run:
# Run in OpenBUGS
writeLines(rv0$bug, "rv0.txt")
library("R2OpenBUGS")
# Run model (can take some time, depending on data length)
rv0.bug <- bugs(data = rv0$data,
inits = list(inits(rv0)),
param = c(nodes(rv0, "prior")$name, "y.sim", "y.new"),
model = "rv0.txt",
n.iter = 11000, n.burnin = 1000, n.chains = 1)
# Plot the parameters posteriors and traces
library("coda")
param.mcmc <- as.mcmc(rv0.bug$sims.matrix[, nodes(rv0, "prior")$name])
plot(param.mcmc)
# Plot posterior simulations using fanplot
library("fanplot")
y.mcmc <- rv0.bug$sims.list$y.sim
y.pn <- pn(y.mcmc, st = start(y)[1])
plot(y, type = "n")
fan(y.pn)
lines(y)
# Plot volatility
h.mcmc <- rv0.bug$sims.list$h
h.pn <- pn(h.mcmc, st = start(y)[1])
sigma.pn <- pn(sims = sqrt(exp(h.mcmc)), st = start(y)[1])
par(mfrow = c(2, 1), mar = rep(2, 4))
plot(NULL, type = "n", xlim = tsp(h.pn)[1:2], ylim = range(h.pn[, 5:95]), main = "h_t")
fan(h.pn)
plot(NULL, type = "n", xlim = tsp(sigma.pn)[1:2], ylim = range(sigma.pn[, 1:95]), main = "sigma_t")
fan(sigma.pn)
## End(Not run)
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.