Can anyone help me with the stochvol
package in R? I estimated the volatilities using this package but I am not being able to understand how to download the estimated volatilities. I used volplot
function to plot the volatilities but not understanding how to download that output.
I have used the following code. When I use the volplot
function, estimated volatilities look very different compared to the ones when I try to extract the median values in the m_export_oil
dataframe.
The data required is here: https://docs.google.com/spreadsheets/d/14r-_SxCvOG9-_tTNLgXWU-FPkx_8OYLh/edit?usp=sharing&ouid=118324139963698395675&rtpof=true&sd=true
library(stochvol)
library(xlsx)
set.seed(123)
oil_qtrly <- read.xlsx("oil_qtrly.xlsx", sheetIndex = 1L)
oil_qtrly$log_real_op <- log(oil_qtrly$real_price)
sv_model_oil <- svsample(oil_qtrly$log_real_op, priormu = c(-10, 1), priorphi = c(20, 1.1),
priorsigma = 0.1,designmatrix = "ar1")
summary(sv_model_oil, showlatent = FALSE)
sigma_oil <- sv_model_oil$latent;
sigma_matrix_oil <- matrix(unlist(sigma_oil), ncol = 100, byrow = TRUE)
num_draws_oil <- dim(sigma_matrix_oil)[1]
num_quarters_oil <- dim(sigma_matrix_oil)[2]
m_export_oil <- matrix(ncol = 4, nrow = num_quarters_oil)
for (i in 1:num_quarters_oil) {
qq_oil = quantile(100*exp(sigma_matrix_oil[,i]/2), probs = c(0.05, 0.5,0.95))
m_export_oil[i,1:3]<- qq_oil
m_export_oil[i,4] <- oil_qtrly$real_price[i]
}
m_export_oil<-data.frame(m_export_oil)
volplot(sv_model_oil, forecast = NULL, dates = oil_qtrly$date[-1])