R/printmethods.R

Defines functions print.hpfilter print.hfilter format_valuef

Documented in format_valuef print.hfilter print.hpfilter

#' @title Function factory for value formatting
#'
#' @description \code{format_valuef} is a function factory for
#'   formatting values with certain number of digits.
#'
#' @param digits the number of decimals to print
#' @return Returns a function that takes an atomic vector as argument
#'   and returns it formatted to character with \code{digits} decimals.

format_valuef <- function(digits) {
  function(x) format(round(x, digits), nsmall=digits)
}

#' @describeIn hfilter print method
#' @param x object of class 'hfilter' generated by function \code{hfilter}
#' @param digits number of digits to be printed.
#' @param ... currently not in use.
#' @export

print.hfilter <- function(x, ..., digits=3) {
  format_value <- format_valuef(digits)
  paste_se <- function(a) paste0("(", a[1], ", ", a[2], ")")
  cat("Separated trend and cyclical component:\n",
      paste0("h = ", x$h, ", p = ", x$p), "\n",
      paste0("T = ", length(x$cycle), ", freq = ", frequency(x$cycle)), "\n",
      paste0("start = ", paste_se(start(x$cycle)), ", end = ", paste_se(end(x$cycle))), "\n\n")

  coefs <- as.numeric(format_value(x$beta))
  names(coefs) <- paste0("beta_", 0:x$p)
  cat("Coefficients:\n")
  print(coefs)
}


#' @describeIn hpfilter print method
#' @param x object of class 'hpfilter' generated by function \code{hpfilter}
#' @param digits number of digits to be printed.
#' @param ... currently not in use.
#' @export

print.hpfilter <- function(x, ..., digits=3) {
  format_value <- format_valuef(digits)
  paste_se <- function(a) paste0("(", a[1], ", ", a[2], ")")
  cat("Separated trend and cyclical component:\n",
      paste0("lambda = ", x$lambda, ", type = ", x$type), "\n",
      paste0("T = ", length(x$cycle), ", freq = ", frequency(x$cycle)), "\n",
      paste0("start = ", paste_se(start(x$cycle)), ", end = ", paste_se(end(x$cycle))), "\n\n")
}
saviviro/tsfilters documentation built on July 16, 2025, 6:16 p.m.