Skip to content

Commit

Permalink
Merge pull request #44 from EchoRLiu/16-improve-print-and-summary
Browse files Browse the repository at this point in the history
16 improve print and summary
  • Loading branch information
dan9401 authored Dec 7, 2024
2 parents 831985c + 9eba516 commit 3f65aac
Show file tree
Hide file tree
Showing 3 changed files with 130 additions and 59 deletions.
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
183 changes: 124 additions & 59 deletions R/robustGARCH-summary.R
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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")
Expand All @@ -30,68 +33,122 @@
#' 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

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])
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 = ", robTunePars[1], ", k = ", robTunePars[2])
} else if (method == "M") {
pars_str = paste0("div = ", robTunePars[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 ""
})
coefficients <- rbind(round(fitted_pars, 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)
res = cbind(coefficients, significance)
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$initialPars[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$optChoice)
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")

# 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
#' @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)")

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("Model: ", fit$fitMethod, "\n")
cat("Data: ", fit$data_name, "\n")
cat("Result:\n")
noquote(res)
print.robustGARCH <- function(x, digits = 3, ...) {

fit = x
cat("\n")
cat("Model: ", fit$fitMethod)
cat("\n")
cat("\n")
cat("Coefficients:")
cat("\n")
print.default(format(coef(fit), digits = digits), print.gap = 2L, quote = FALSE)

invisible(fit)
}

#' @rdname robustGARCH-summary
Expand All @@ -103,6 +160,14 @@ plot.robustGARCH <- function(x, digits = 3, estimation_pos = "topleft", line_nam

}

#' @rdname robustGARCH-summary
#' @export
coef.robustGARCH = function(object, ...) {

object$fitted_pars

}

#' @rdname robustGARCH-summary
aef <- function(fit, nu=5){

Expand Down
4 changes: 4 additions & 0 deletions man/robustGARCH-summary.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 3f65aac

Please sign in to comment.