R/do_MortalityForecasts.R

Defines functions print.MortalityForecasts do.MortalityForecasts

Documented in do.MortalityForecasts print.MortalityForecasts

# --------------------------------------------------- #
# Author: Marius D. Pascariu
# License: GNU General Public License v3.0
# Last update: Fri Nov 30 18:16:43 2018
# --------------------------------------------------- #


#' Perform Multiple Forecast of Mortality Using Various Models
#' 
#' @param object An object of class \code{MotalityModels} generated by the
#' \code{\link{do.MortalityModels}} function.
#' @param h Number of years to be forecast in the future.
#' @param level Significance level of the confidence interval.
#' @param jumpchoice Method used for computation of jumpchoice. 
#'  Possibilities: \code{"actual"} (use actual rates from final year) 
#'  and \code{"fit"} (use fitted rates).
#' @inheritParams do.MortalityModels
#' @examples # For examples go to ?do.MortalityModels
#' @export
do.MortalityForecasts <- function(object, 
                                  h, 
                                  level = 95, 
                                  jumpchoice = c("actual", "fit"), 
                                  verbose = TRUE, 
                                  ...) {
  
  jumpchoice <- match.arg(jumpchoice)
  input <- as.list(environment())
  call  <- match.call()
  x     <- object$x
  y     <- max(object$y) + 1:h
  Mn    <- object$input$models # Model names
  
  for (i in 1:length(Mn)) {
    cat(Mn[i], " ")
    M <- with(object, get(Mn[i]))
    
    if (Mn[i] %in% c("LC", "PLAT")) {
      P <- forecast(object = M, 
                    h = h, 
                    jumpchoice = jumpchoice, 
                    level = level)
      
    } else {
      P <- predict(object = M, 
                   h = h, 
                   jumpchoice = jumpchoice, 
                   level = level, 
                   ...)
    }
    assign(Mn[i], P)
  }
  
  # Save the environment and exit
  remove(object, h, level, jumpchoice, M, Mn, P, i)
  out <- as.list(environment())
  out <- structure(class = "MortalityForecasts", out)
  return(out)
}


#' Print function for do.MortalityForecasts
#' @param x An object of class \code{"MortalityForecasts"}
#' @param ... Further arguments passed to or from other methods.
#' @keywords internal
#' @export
print.MortalityForecasts <- function(x, ...) {
  cat("Stochastic Mortality Models - Forecast")
  cat("\nCall : "); print(x$call)
  cat("\nModels in forecast:", paste(x$input$object$input$models, collapse = ", "))
  cat("\nAges  in forecast :", paste(range(x$x), collapse = " - "))
  cat("\nYears in forecast :", paste(range(x$y), collapse = " - "))
  cat("\n")
}
mpascariu/MortalityForecast documentation built on Sept. 28, 2020, 2:40 p.m.