From 37bb5fa5a41eaf831fa188346942c54470026429 Mon Sep 17 00:00:00 2001 From: dan9401 Date: Sat, 7 Dec 2024 17:36:41 +0000 Subject: [PATCH 1/8] add `coef` method --- R/robustGARCH-summary.R | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/R/robustGARCH-summary.R b/R/robustGARCH-summary.R index df3ce0e..0bd68ca 100644 --- a/R/robustGARCH-summary.R +++ b/R/robustGARCH-summary.R @@ -103,6 +103,12 @@ plot.robustGARCH <- function(x, digits = 3, estimation_pos = "topleft", line_nam } +#' @rdname robustGarch-summary +#' @export +coef.robustGARCH = function(x) { + x$fitted_pars +} + #' @rdname robustGARCH-summary aef <- function(fit, nu=5){ From a5d38667f6add5730842a41827457e9ed5e4145b Mon Sep 17 00:00:00 2001 From: dan9401 Date: Sat, 7 Dec 2024 17:37:36 +0000 Subject: [PATCH 2/8] update `print.robustGARCH` to follow `print.lm` --- R/robustGARCH-summary.R | 24 +++++++----------------- 1 file changed, 7 insertions(+), 17 deletions(-) diff --git a/R/robustGARCH-summary.R b/R/robustGARCH-summary.R index 0bd68ca..8bbbe11 100644 --- a/R/robustGARCH-summary.R +++ b/R/robustGARCH-summary.R @@ -73,25 +73,15 @@ summary.robustGARCH <- function(object, digits = 3, ...){ #' @rdname robustGARCH-summary #' @export -print.robustGARCH <- function(x, digits = 3, ...){ +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)") - - res[1,1] <- gsub(" ","",paste(res[1,1],'( ', round(fit$standard_error[1], digits), ' )')) - res[1,2] <- gsub(" ","",paste(res[1,2],'( ', round(fit$standard_error[2], digits), ' )')) - res[1,3] <- gsub(" ","",paste(res[1,3],'( ', round(fit$standard_error[3], digits), ' )')) - if (fit$fitMethod == "MLE"){ - res[1,4] <- gsub(" ","",paste(res[1,4],'( ', round(fit$standard_error[4], digits), ' )')) - } + cat("\n") + cat("Model: ", fit$methods, "\n") + cat("\n") + cat("Coefficients:\n") + print.default(format(coef(x), digits = digits), print.gap = 2L, quote = FALSE) - cat("Model: ", fit$fitMethod, "\n") - cat("Data: ", fit$data_name, "\n") - cat("Result:\n") - noquote(res) + invisible(x) } #' @rdname robustGARCH-summary From 89e1c1c68fcfc74fe1a22a3b9c8916fd352ed905 Mon Sep 17 00:00:00 2001 From: dan9401 Date: Sat, 7 Dec 2024 17:37:36 +0000 Subject: [PATCH 3/8] adjust format --- R/robustGARCH-summary.R | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/R/robustGARCH-summary.R b/R/robustGARCH-summary.R index 8bbbe11..b4209c4 100644 --- a/R/robustGARCH-summary.R +++ b/R/robustGARCH-summary.R @@ -75,13 +75,16 @@ summary.robustGARCH <- function(object, digits = 3, ...){ #' @export print.robustGARCH <- function(x, digits = 3, ...) { + fit = x cat("\n") - cat("Model: ", fit$methods, "\n") + cat("Model: ", fit$methods) cat("\n") - cat("Coefficients:\n") - print.default(format(coef(x), digits = digits), print.gap = 2L, quote = FALSE) + cat("\n") + cat("Coefficients:") + cat("\n") + print.default(format(coef(fit), digits = digits), print.gap = 2L, quote = FALSE) - invisible(x) + invisible(fit) } #' @rdname robustGARCH-summary From d7d963c207521da7fbaa52829ebb250ad964eae9 Mon Sep 17 00:00:00 2001 From: dan9401 Date: Sat, 7 Dec 2024 17:38:06 +0000 Subject: [PATCH 4/8] update `summary.robustGARCH` to have similar output as `summary.lm` --- R/robustGARCH-summary.R | 94 +++++++++++++++++++++++++++++++---------- 1 file changed, 72 insertions(+), 22 deletions(-) diff --git a/R/robustGARCH-summary.R b/R/robustGARCH-summary.R index b4209c4..3f9cd83 100644 --- a/R/robustGARCH-summary.R +++ b/R/robustGARCH-summary.R @@ -44,31 +44,81 @@ summary.robustGARCH <- function(object, digits = 3, ...){ # See chapter ‘Writing R documentation files’ in the ‘Writing R # Extensions’ manual. fit <- object + method = fit$methods + fixed_pars = fit$fixed_pars + fitted_pars = fit$fitted_pars - 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") - - cat("Model: ", fit$fitMethod, " ") - if (fit$fitMethod == "BM"){ - cat("with div = ", fit$robTunePars[1], ", k = ", fit$robTunePars[2]) - } - if (fit$fitMethod == "M"){ - cat("with div = ", fit$robTunePars[1]) + # model spec + model_str = paste0("Model: ", method, ", ") + if (method == "BM") { + pars_str = paste0("div = ", fixed_pars[1], ", k = ", fixed_pars[2]) + } else if (method == "M") { + pars_str = paste0("div = ", fixed_pars[1]) } - cat("\nData: ", fit$data_name, "\n") - cat("Observations: ", length(fit$data), "\n") - cat("\nResult:\n") - print(res) - cat("\nLog-likelihood: ", fit$objective) - cat("\n\nOptimizer: ", fit$optChoice) - if (fit$fitMethod == "MLE"){ - cat("\nInitial parameter estimates: ", fit$initialPars[1:3], fit$initialPars[5]) - } else{ - cat("\nInitial parameter estimates: ", fit$initialPars[1:3]) + data_str = paste0("Data: ", fit$data_name) + obs_str = paste0("Observations: ", length(fit$data)) + cat("\n") + cat(model_str) + cat(pars_str) + cat("\n") + cat(data_str) + cat("\n") + cat(obs_str) + cat("\n") + + # fit result + significance = sapply(fit$p_value, function(p) { + if (is.na(p)) "" + else if (p < 0.001) "***" + else if (p < 0.01) "**" + else if (p < 0.05) "*" + else if (p < 0.1) "." + else "" + }) + res <- rbind(round(fitted_pars, digits), + round(fit$standard_error, digits), + round(fit$t_value, digits), + round(fit$p_value, digits), + significance) + colnames(res) <- names(fitted_pars) + rownames(res) <- c("Estimate", "Std.Error", "t-statistic", "p-value", "") + res = t(res) + cat("\n") + cat("Result:") + cat("\n") + print.default(format(res, digits = digits), print.gap = 2L, quote = FALSE) + cat("---") + cat("\n") + cat("Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1") + cat("\n") + + # initial estimate + if(method == "MLE") { + idx_pars = c(1,2,3,5) + } else { + idx_pars = 1:3 } - cat("\nTime elapsed: ", fit$time_elapsed) - cat("\nConvergence Message: ", fit$message) + initial_estimate = c(fit$optimizer_x0[idx_pars]) + names(initial_estimate) = names(fitted_pars) + cat("\n") + cat("Initial parameter estimates:") + cat("\n") + print.default(format(initial_estimate, digits = digits), print.gap = 2L, quote = FALSE) + + # optimization + ll_str = paste0("Log-likelihood: ", format(fit$objective, digits=6)) + opt_str = paste0("Optimizer: ", fit$optimizer) + time_str = paste0("Time elapsed: ", format(fit$time_elapsed, digits=6)) + conv_str = paste0("Convergence Message: ", fit$message) + cat("\n") + cat(ll_str) + cat("\n") + cat(opt_str) + cat("\n") + cat(time_str) + cat("\n") + cat(conv_str) + cat("\n") } #' @rdname robustGARCH-summary From ed51cf3b85783346b3114b0535c086dba07e648f Mon Sep 17 00:00:00 2001 From: dan9401 Date: Sat, 7 Dec 2024 17:38:07 +0000 Subject: [PATCH 5/8] update summary method with return --- R/robustGARCH-summary.R | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/R/robustGARCH-summary.R b/R/robustGARCH-summary.R index 3f9cd83..2af8d4a 100644 --- a/R/robustGARCH-summary.R +++ b/R/robustGARCH-summary.R @@ -75,14 +75,14 @@ summary.robustGARCH <- function(object, digits = 3, ...){ else if (p < 0.1) "." else "" }) - res <- rbind(round(fitted_pars, digits), + coefficients <- rbind(round(fitted_pars, digits), round(fit$standard_error, digits), round(fit$t_value, digits), - round(fit$p_value, digits), - significance) - colnames(res) <- names(fitted_pars) - rownames(res) <- c("Estimate", "Std.Error", "t-statistic", "p-value", "") - res = t(res) + round(fit$p_value, digits)) + colnames(coefficients) <- names(fitted_pars) + rownames(coefficients) <- c("Estimate", "Std.Error", "t-statistic", "p-value") + coefficients = t(coefficients) + res = cbind(coefficients, significance) cat("\n") cat("Result:") cat("\n") @@ -119,6 +119,16 @@ summary.robustGARCH <- function(object, digits = 3, ...){ cat("\n") cat(conv_str) cat("\n") + + # summary object + converged = ifelse(fit$message == 0, TRUE, FALSE) # I'm not sure if this is true + summary_list = list( + method = method, + coefficients = coefficients, + loglikelihood = fit$objective, + converged = converged + ) + invisible(summary_list) } #' @rdname robustGARCH-summary From d6f8494859a33f3e280a7fb13187e1d8dc1b3559 Mon Sep 17 00:00:00 2001 From: dan9401 Date: Sat, 7 Dec 2024 17:38:17 +0000 Subject: [PATCH 6/8] fix document and check errors --- NAMESPACE | 2 ++ R/robustGARCH-summary.R | 54 ++++++++++++++++++++------------------ man/robustGARCH-summary.Rd | 2 ++ 3 files changed, 33 insertions(+), 25 deletions(-) diff --git a/NAMESPACE b/NAMESPACE index 2a219df..2a431c9 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -1,11 +1,13 @@ # Generated by roxygen2: do not edit by hand +S3method(coef,robustGARCH) S3method(plot,robustGARCH) S3method(print,robustGARCH) S3method(summary,robustGARCH) export(robGarch) importFrom(graphics,legend) importFrom(graphics,par) +importFrom(stats,coef) importFrom(stats,density) importFrom(stats,integrate) importFrom(stats,median) diff --git a/R/robustGARCH-summary.R b/R/robustGARCH-summary.R index 2af8d4a..f18ed26 100644 --- a/R/robustGARCH-summary.R +++ b/R/robustGARCH-summary.R @@ -1,7 +1,16 @@ #' @title Summary for robustGARCH class #' +#' @name robustGARCH-summary +#' @aliases summary.robustGARCH +#' @aliases print.robustGARCH +#' @aliases plot.robustGARCH +#' @aliases coef.robustGARCH +#' @aliases aef +#' #' @description Summary for robustGARCH S3 class #' +#' @importFrom stats coef +#' #' @param fit A robustGARCH fit object of class \code{\link{robGarch}} #' @param x Same as fit, for plot.robustGARCH and print.robustGARCH #' @param object Same as fit, for summary.robustGARCH @@ -16,12 +25,6 @@ #' @param ... # to be written #' @param nu degrees of freedom in a Student's t-distribution. #' -#' @name robustGARCH-summary -#' @aliases summary.robustGARCH -#' @aliases print.robustGARCH -#' @aliases plot.robustGARCH -#' @aliases aef -#' #' @examples #' #' data("gspc") @@ -30,19 +33,20 @@ #' summary(fit) #' print(fit) #' plot(fit) -#' +#' coef(fit) +#' #' @export summary.robustGARCH <- function(object, digits = 3, ...){ -# summary.robustGARCH <- function(fit, digits = 3){ - -# notes on why the name change -# Undocumented arguments in Rd file 'robustGARCH-summary.Rd' -# ‘object’ -# Functions with \usage entries need to have the appropriate \alias -# entries, and all their arguments documented. -# The \usage entries must correspond to syntactically valid R code. -# See chapter ‘Writing R documentation files’ in the ‘Writing R -# Extensions’ manual. + # summary.robustGARCH <- function(fit, digits = 3){ + + # notes on why the name change + # Undocumented arguments in Rd file 'robustGARCH-summary.Rd' + # ‘object’ + # Functions with \usage entries need to have the appropriate \alias + # entries, and all their arguments documented. + # The \usage entries must correspond to syntactically valid R code. + # See chapter ‘Writing R documentation files’ in the ‘Writing R + # Extensions’ manual. fit <- object method = fit$methods fixed_pars = fit$fixed_pars @@ -70,15 +74,15 @@ summary.robustGARCH <- function(object, digits = 3, ...){ significance = sapply(fit$p_value, function(p) { if (is.na(p)) "" else if (p < 0.001) "***" - else if (p < 0.01) "**" + else if (p < 0.01) "**" else if (p < 0.05) "*" else if (p < 0.1) "." else "" }) coefficients <- rbind(round(fitted_pars, digits), - round(fit$standard_error, digits), - round(fit$t_value, digits), - round(fit$p_value, digits)) + round(fit$standard_error, digits), + round(fit$t_value, digits), + round(fit$p_value, digits)) colnames(coefficients) <- names(fitted_pars) rownames(coefficients) <- c("Estimate", "Std.Error", "t-statistic", "p-value") coefficients = t(coefficients) @@ -89,7 +93,7 @@ summary.robustGARCH <- function(object, digits = 3, ...){ print.default(format(res, digits = digits), print.gap = 2L, quote = FALSE) cat("---") cat("\n") - cat("Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1") + cat("Signif. codes: 0 '***'' 0.001 '**'' 0.01 '*' 0.05 '.' 0.1 '' 1") cat("\n") # initial estimate @@ -104,7 +108,7 @@ summary.robustGARCH <- function(object, digits = 3, ...){ cat("Initial parameter estimates:") cat("\n") print.default(format(initial_estimate, digits = digits), print.gap = 2L, quote = FALSE) - + # optimization ll_str = paste0("Log-likelihood: ", format(fit$objective, digits=6)) opt_str = paste0("Optimizer: ", fit$optimizer) @@ -158,8 +162,8 @@ plot.robustGARCH <- function(x, digits = 3, estimation_pos = "topleft", line_nam #' @rdname robustGarch-summary #' @export -coef.robustGARCH = function(x) { - x$fitted_pars +coef.robustGARCH = function(object, ...) { + object$fitted_pars } #' @rdname robustGARCH-summary diff --git a/man/robustGARCH-summary.Rd b/man/robustGARCH-summary.Rd index ab6307e..ca0eb12 100644 --- a/man/robustGARCH-summary.Rd +++ b/man/robustGARCH-summary.Rd @@ -5,6 +5,7 @@ \alias{summary.robustGARCH} \alias{print.robustGARCH} \alias{plot.robustGARCH} +\alias{coef.robustGARCH} \alias{aef} \title{Summary for robustGARCH class} \usage{ @@ -65,5 +66,6 @@ fit <- robGarch(gspc, fitMethod="BM", robTunePars = c(0.8, 3.0), summary(fit) print(fit) plot(fit) +coef(fit) } From 3b25b38d709fb0476c38d1d341a4e726151d209e Mon Sep 17 00:00:00 2001 From: dan9401 Date: Sat, 7 Dec 2024 17:38:18 +0000 Subject: [PATCH 7/8] fix roxygen doc error --- R/robustGARCH-summary.R | 4 +++- man/robustGARCH-summary.Rd | 2 ++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/R/robustGARCH-summary.R b/R/robustGARCH-summary.R index f18ed26..c8ffe62 100644 --- a/R/robustGARCH-summary.R +++ b/R/robustGARCH-summary.R @@ -160,10 +160,12 @@ plot.robustGARCH <- function(x, digits = 3, estimation_pos = "topleft", line_nam } -#' @rdname robustGarch-summary +#' @rdname robustGARCH-summary #' @export coef.robustGARCH = function(object, ...) { + object$fitted_pars + } #' @rdname robustGARCH-summary diff --git a/man/robustGARCH-summary.Rd b/man/robustGARCH-summary.Rd index ca0eb12..868acdf 100644 --- a/man/robustGARCH-summary.Rd +++ b/man/robustGARCH-summary.Rd @@ -26,6 +26,8 @@ ... ) +\method{coef}{robustGARCH}(object, ...) + aef(fit, nu = 5) } \arguments{ From 9eba516bf1c10bbddd4a3c41c371d6037694e2dc Mon Sep 17 00:00:00 2001 From: dan9401 Date: Sat, 7 Dec 2024 17:49:08 +0000 Subject: [PATCH 8/8] update argument names --- R/robustGARCH-summary.R | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/R/robustGARCH-summary.R b/R/robustGARCH-summary.R index c8ffe62..d3bdd03 100644 --- a/R/robustGARCH-summary.R +++ b/R/robustGARCH-summary.R @@ -48,16 +48,16 @@ summary.robustGARCH <- function(object, digits = 3, ...){ # See chapter ‘Writing R documentation files’ in the ‘Writing R # Extensions’ manual. fit <- object - method = fit$methods - fixed_pars = fit$fixed_pars + method = fit$fitMethod + robTunePars = fit$robTunePars fitted_pars = fit$fitted_pars # model spec model_str = paste0("Model: ", method, ", ") if (method == "BM") { - pars_str = paste0("div = ", fixed_pars[1], ", k = ", fixed_pars[2]) + pars_str = paste0("div = ", robTunePars[1], ", k = ", robTunePars[2]) } else if (method == "M") { - pars_str = paste0("div = ", fixed_pars[1]) + pars_str = paste0("div = ", robTunePars[1]) } data_str = paste0("Data: ", fit$data_name) obs_str = paste0("Observations: ", length(fit$data)) @@ -102,7 +102,7 @@ summary.robustGARCH <- function(object, digits = 3, ...){ } else { idx_pars = 1:3 } - initial_estimate = c(fit$optimizer_x0[idx_pars]) + initial_estimate = c(fit$initialPars[idx_pars]) names(initial_estimate) = names(fitted_pars) cat("\n") cat("Initial parameter estimates:") @@ -111,7 +111,7 @@ summary.robustGARCH <- function(object, digits = 3, ...){ # optimization ll_str = paste0("Log-likelihood: ", format(fit$objective, digits=6)) - opt_str = paste0("Optimizer: ", fit$optimizer) + opt_str = paste0("Optimizer: ", fit$optChoice) time_str = paste0("Time elapsed: ", format(fit$time_elapsed, digits=6)) conv_str = paste0("Convergence Message: ", fit$message) cat("\n") @@ -141,7 +141,7 @@ print.robustGARCH <- function(x, digits = 3, ...) { fit = x cat("\n") - cat("Model: ", fit$methods) + cat("Model: ", fit$fitMethod) cat("\n") cat("\n") cat("Coefficients:")