# NOT RUN {
# Create AR(0)-SV model for svpdx
y <- svpdx$pdx
plot(y, type = "l")
sv0 <- sv.bugs(y)
print(sv0)
# AR(1)-SV model with AR(2) structure in the volatility
sv1 <- sv.bugs(y, ar.order = 1, sv.order = 2)
print(sv1)
# AR(0)-SV model with alternative prior
sv0 <- sv.bugs(y, sv.ar.prior2 = "dunif(-1,1)")
print(sv0)
# AR(0)-SV model with forecast and posterior simulations
sv0 <- sv.bugs(y, k = 10, sim = TRUE)
print(sv0)
# }
# NOT RUN {
# Run in OpenBUGS
writeLines(sv0$bug, "sv0.txt")
library("R2OpenBUGS")
sv0.bug <- bugs(data = sv0$data,
inits = list(inits(sv0)),
param = c(nodes(sv0, "prior")$name, "y.sim", "y.new"),
model = "sv0.txt",
n.iter = 20000, n.burnin = 10000, n.chains = 1)
# Plot the parameters posteriors and traces
library("coda")
param.mcmc <- as.mcmc(sv0.bug$sims.matrix[, nodes(sv0, "prior")$name])
plot(param.mcmc)
# Plot posterior simulations using fanplot
library("fanplot")
y.mcmc <- sv0.bug$sims.list$y.sim
y.pn <- pn(y.mcmc, st = 1)
plot(y, type = "n")
fan(y.pn)
lines(y)
# Plot forecast using fanplot
ynew.mcmc <- sv0.bug$sims.list$y.new
ynew.pn <- pn(ynew.mcmc, st = sv0$info$n + 1)
plot(y, type = "n", xlim = sv0$info$n + c(-100, 20))
fan(ynew.pn)
lines(y)
# Plot volatility
h.mcmc <- sv0.bug$sims.list$h
h.pn <- pn(h.mcmc, st = 1)
sigma.pn <- pn(sims = sqrt(exp(h.mcmc)), st = 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)
abline(v = length(y))
plot(NULL, type = "n", xlim = tsp(sigma.pn)[1:2], ylim = range(sigma.pn[, 1:95]), main = "sigma_t")
fan(sigma.pn)
abline(v = length(y))
# }
Run the code above in your browser using DataLab