diff --git a/NAMESPACE b/NAMESPACE index 2920b14..2a219df 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -3,7 +3,12 @@ S3method(plot,robustGARCH) S3method(print,robustGARCH) S3method(summary,robustGARCH) -export(.aValue) -export(.expFisherI) -export(aef) export(robGarch) +importFrom(graphics,legend) +importFrom(graphics,par) +importFrom(stats,density) +importFrom(stats,integrate) +importFrom(stats,median) +importFrom(stats,nlminb) +importFrom(stats,pnorm) +importFrom(xts,xts) diff --git a/R/data.R b/R/data.R index 2aaa2a8..a57f94e 100644 --- a/R/data.R +++ b/R/data.R @@ -16,4 +16,5 @@ #' #' @keywords datasets #' @source \url{https://finance.yahoo.com/quote/\%5EGSPC/history?period1=949363200&period2=1025222400&interval=1d&filter=history&frequency=1d} +#' @importFrom xts xts "gspc" diff --git a/R/robGarch.R b/R/robGarch.R index 9bf6d10..cae46a2 100644 --- a/R/robGarch.R +++ b/R/robGarch.R @@ -1,3 +1,5 @@ +#' @importFrom stats median pnorm density nlminb +#' @importFrom graphics par #' @title Robust Estimates for GARCH(1,1) Model #' #' @name robGarch @@ -59,9 +61,13 @@ robGarch <- function(data, methods = c("BM", "M", "QML", "MLE"), fixed_pars = c(0.8, 3.0), optimizer = c("Rsolnp", "nloptr", "nlminb"), optimizer_x0 = FALSE, optimizer_control = list(trace=0), stdErr_method = c("numDeriv", "optim", "sandwich")){ - # TODO: xts data takes a long time - if(!is.numeric(data) || length(data)==0) - stop("Data must be a numeric vector of non-zero length") + + # if(!is.numeric(data) || length(data)==0) + # stop("Data must be a numeric vector of non-zero length") + + # the calculation will use data_, which only has the return data and without dates + # but we will keep data to retain the original dates + data_ = zoo::coredata(data) methods = match.arg(methods) optimizer = match.arg(optimizer) @@ -99,7 +105,7 @@ robGarch <- function(data, methods = c("BM", "M", "QML", "MLE"), fixed_pars = c( stop("use Rsolnp optimizer for now") } - std_errors <- sqrt(diag(abs(solve(H)/length(data)))) + std_errors <- sqrt(diag(abs(solve(H)/length(data_)))) if(methods == "MLE"){ standard_error <- c(std_errors[2:4], std_errors[6]) fit$observed_I <- -H[c(2,3,4,6), c(2,3,4,6)] @@ -189,7 +195,7 @@ robGarchDistribution <- function(param = c(8.76e-04, 0.135, 0.686), methods = c( fixed <- param names(fixed) <- c("gamma", "alpha", "beta", "shape") fspec <- spec - setfixed(fspec) <- fixed + rugarch::setfixed(fspec) <- fixed y <- rugarch::ugarchpath(fspec, n.sim = n, m.sim = m, rseed = 42) y. <- y@path$seriesSim @@ -221,7 +227,7 @@ robGarchDistribution <- function(param = c(8.76e-04, 0.135, 0.686), methods = c( fixed <- param names(fixed) <- c("gamma", "alpha", "beta") fspec <- spec - setfixed(fspec) <- fixed + rugarch::setfixed(fspec) <- fixed y <- rugarch::ugarchpath(fspec, n.sim = n, m.sim = m, rseed = 42) y. <- y@path$seriesSim @@ -251,6 +257,10 @@ rgFit_local <- function(data, optimizer, optimizer_x0, optimizer_control, shared # unpack shared_vars methods <- shared_vars$methods + # for minimum code change, data_ and data naming are exchanged here + data_ = data # retain the dates and structure + data = zoo::coredata(data) # for calculation + start_time <- Sys.time() # optimizer/optimizer_control @@ -315,7 +325,7 @@ rgFit_local <- function(data, optimizer, optimizer_x0, optimizer_control, shared time_elapsed <- Sys.time() - start_time - list(data=data, + list(data=data_, methods = methods, optimizer=optimizer, optimizer_x0=res$x0, diff --git a/R/robustGARCH-infomat.R b/R/robustGARCH-infomat.R index 8b63c23..4c92899 100644 --- a/R/robustGARCH-infomat.R +++ b/R/robustGARCH-infomat.R @@ -1,4 +1,5 @@ -#' @export +# import methods from other packages +#' @importFrom stats integrate .aValue <- function(fit, top = FALSE, v=5){ if(top){ # Calculate a(Psi_0, g), the top value of AEF, where Psi_0 = rho_0'. @@ -158,11 +159,13 @@ g } -#' @export .expFisherI <- function(fit, true_pars){ # Big difference between expected and observed Fisher Information matrix. - T <- length(fit$data) + + data_ = zoo::coredata(fit$data) + + T <- length(data_) if(fit$methods == 'MLE'){ # to be added. @@ -175,17 +178,17 @@ # it seems like k is used here as an index # could any k was supposed to be the global one implemented before? for(k in 1:(T-1)){ - FS[2,1] <- FS[2,1] + true_pars[3]^(k-1) * (fit$data[T-k])^2 + FS[2,1] <- FS[2,1] + true_pars[3]^(k-1) * (data_[T-k])^2 } FS[3,1] <- true_pars[1]/(1-true_pars[3])^2 for(k in 1:(T-2)){ - FS[3,1] <- FS[3,1] + true_pars[2]*k*true_pars[3]^(k-1)*(fit$data[T-k-1])^2 + FS[3,1] <- FS[3,1] + true_pars[2]*k*true_pars[3]^(k-1)*(data_[T-k-1])^2 } ht <- true_pars[1]/(1-true_pars[3]) for(k in 1:(T-1)){ - ht <- ht + true_pars[2] * true_pars[3]^(k-1) * (fit$data[T-k])^2 + ht <- ht + true_pars[2] * true_pars[3]^(k-1) * (data_[T-k])^2 } } diff --git a/R/robustGARCH-plots.R b/R/robustGARCH-plots.R index 4a578fd..510394d 100644 --- a/R/robustGARCH-plots.R +++ b/R/robustGARCH-plots.R @@ -1,8 +1,10 @@ +#' @importFrom graphics legend par +#' @importFrom stats density integrate median nlminb pnorm .plot.garchsim <- function(fit, digits = 3, estimation_pos = "topleft", line_name_pos = "topright", par_ = par(no.readonly = TRUE), pctReturn_ = TRUE, abs_ = TRUE, original_ = FALSE, main_name = "Conditional Volatility (vs |pctReturns(%)|)") { - n <- length(fit$data) + 100 + n <- length(zoo::coredata(fit$data)) + 100 a0 <- fit$fitted_pars["gamma"] a1 <- fit$fitted_pars["alpha"] b1 <- fit$fitted_pars["beta"] @@ -21,11 +23,11 @@ #} #sigma2 <- sigma2[101:n] - returns <- xts::xts(fit$data) - sigma2 <- xts::xts(sqrt(fit$sigma), order.by = zoo::index(returns)) + returns <- zoo::coredata(fit$data) + sigma2 <- xts::xts(sqrt(fit$sigma), order.by = zoo::index(fit$data)) if(original_){ - zoo::plot.zoo(returns, type = "l", xlab = "", ylab = "Return", main = "Returns") + zoo::plot.zoo(fit$data, type = "l", xlab = "", ylab = "Return", main = "Returns") } if(pctReturn_){ @@ -34,14 +36,17 @@ } if(abs_){ - retAndVol <- cbind(abs(returns),sigma2) - } else{ - retAndVol <- cbind(returns,sigma2) + retAndVol <- cbind(abs(returns), sigma2) + } else { + retAndVol <- cbind(returns, sigma2) } + # Ensure retAndVol retains the dates from fit$data + retAndVol <- xts::xts(retAndVol, order.by = zoo::index(fit$data)) + par_ - zoo::plot.zoo(retAndVol, screens = "single", type = "l", xlab = "",ylab = "",main = main_name, - lty = c("dotted","solid"),col = c("blue","black"), lwd = c(.8,1.5)) + zoo::plot.zoo(retAndVol, screens = "single", type = "l", xlab = "", ylab = "", main = main_name, + lty = c("dotted", "solid"), col = c("blue", "black"), lwd = c(.8, 1.5)) exprss <- c(bquote(alpha[0]~'='~.(signif(a0, digits))), bquote(alpha[1]~'='~.(signif(a1, digits))), bquote(beta[1]~'='~.(signif(b1, digits)))) diff --git a/R/robustGARCH-summary.R b/R/robustGARCH-summary.R index 6e5fb02..1532a1c 100644 --- a/R/robustGARCH-summary.R +++ b/R/robustGARCH-summary.R @@ -14,8 +14,7 @@ #' @param pctReturn_ a logical argument. IF TRUE, the plot function will plot the returns in percentage instead of original. Default is TRUE. #' @param abs_ a logical argument, when TRUE, the plot function will plot abs(returns) with conditional standard deviation instead of returns, default to TRUE. #' @param ... # to be written -#' @param nu # to be written -#' @param v degrees of freedom in a Student's t-distribution. +#' @param nu degrees of freedom in a Student's t-distribution. #' #' @name robustGARCH-summary #' @aliases summary.robustGARCH @@ -31,7 +30,6 @@ #' summary(fit) #' print(fit) #' plot(fit) -#' aef(fit) #' #' @export summary.robustGARCH <- function(object, digits = 3, ...){ @@ -45,6 +43,8 @@ summary.robustGARCH <- function(object, digits = 3, ...){ # The \usage entries must correspond to syntactically valid R code. # See chapter ‘Writing R documentation files’ in the ‘Writing R # Extensions’ manual. + fit <- object + res <- rbind(round(fit$fitted_pars, digits), round(fit$standard_error, digits), round(fit$t_value, digits), round(fit$p_value, digits)) colnames(res) <- names(fit$fitted_pars) rownames(res) <- c("Estimates", "Std. Errors", "t-statistic", "p-value") @@ -75,6 +75,8 @@ summary.robustGARCH <- function(object, digits = 3, ...){ #' @export print.robustGARCH <- function(x, digits = 3, ...){ + fit <- x + res <- rbind(round(fit$fitted_pars, digits)) colnames(res) <- names(fit$fitted_pars) rownames(res) <- c("Estimates (Std. Errors)") @@ -96,12 +98,12 @@ print.robustGARCH <- function(x, digits = 3, ...){ #' @export plot.robustGARCH <- function(x, digits = 3, estimation_pos = "topleft", line_name_pos = "topright", par_ = par(no.readonly = TRUE), pctReturn_ = TRUE, abs_ = TRUE, original_ = FALSE, main_name = "Conditional Volatility (vs |pctReturns(%)|)", ...){ + fit <- x .plot.garchsim(fit, digits, estimation_pos, line_name_pos, par_, pctReturn_, abs_, original_, main_name) } #' @rdname robustGARCH-summary -#' @export aef <- function(fit, nu=5){ aTop <- .aValue(fit, TRUE, nu) diff --git a/data/gspc.rda b/data/gspc.rda index 5d36a11..8b848b1 100644 Binary files a/data/gspc.rda and b/data/gspc.rda differ diff --git a/man/robustGARCH-summary.Rd b/man/robustGARCH-summary.Rd index 1d3c771..5dc9c7a 100644 --- a/man/robustGARCH-summary.Rd +++ b/man/robustGARCH-summary.Rd @@ -52,8 +52,7 @@ aef(fit, nu = 5) \item{fit}{A robustGARCH fit object of class \code{\link{robGarch}}} -\item{nu}{# to be written} -\item{v}{degrees of freedom in a Student's t-distribution.} +\item{nu}{degrees of freedom in a Student's t-distribution.} } \description{ Summary for robustGARCH S3 class @@ -66,6 +65,5 @@ fit <- robGarch(gspc, methods="BM", fixed_pars = c(0.8, 3.0), summary(fit) print(fit) plot(fit) -aef(fit) }