diff --git a/DESCRIPTION b/DESCRIPTION index bbf9072..cbeb7ff 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -15,5 +15,5 @@ Imports: cli, tibble Suggests: - rtestim, tinytest, EpiNow2 + rtestim, tinytest, EpiNow2, EpiEstim Remotes: dajmcdon/rtestim diff --git a/NAMESPACE b/NAMESPACE index 6915f7d..b877b14 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -3,7 +3,9 @@ S3method(summarize_rtestimate,cv_poisson_rt) S3method(summarize_rtestimate,default) S3method(summarize_rtestimate,epinow) +S3method(summarize_rtestimate,estimate_R) S3method(summarize_rtestimate,poisson_rt) +export(new_summarize) export(summarize_rtestimate) importFrom(cli,cli_abort) importFrom(stats,median) diff --git a/R/summarize_rtestimate.R b/R/summarize_rtestimate.R index bb38a17..6e11fb9 100644 --- a/R/summarize_rtestimate.R +++ b/R/summarize_rtestimate.R @@ -1,3 +1,52 @@ + +#' Create a new summary object +#' +#' Creates a new summary object for the `summrt` package while validating the input. +#' +#' @param date Integer vector. vector of index dates. +#' @param median Double vector. vector of median values. +#' @param lb Double vector. vector of lower bounds. +#' @param ub Double vector. vector of upper bounds. +#' @param package String. Name of the package. +#' @export +#' @return A list of class `summrt_summary`. with the following components: +#' - `date`: Integer vector. vector of index dates. +#' - `median`: Double vector. vector of median values. +#' - `lb`: Double vector. vector of lower bounds. +#' - `ub`: Double vector. vector of upper bounds. +#' - `package`: String. Name of the package. +new_summarize <- function( + date, median, lb, ub, package +) { + + # Asserting the types + checkmate::assert_integer(date) + checkmate::assert_double(median) + checkmate::assert_double(lb) + checkmate::assert_double(up) + checkmate::assert_string(package) + + # Checking the length + len_date <- length(date) + len_median <- length(median) + len_lb <- length(lb) + len_up <- length(up) + if (len_date != len_median || len_date != len_lb || len_date != len_up) { + stop("The length of the date, median, lb, and ub should be the same.") + } + + structure( + list( + date = date, + median = median, + lb = lb, + ub = ub, + package = package + ), + class = "summrt_summary" + ) +} + #' Extract Rt estimation from a model fit #' @param x Object to extract Rt from. #' @param ... Additional arguments passed to methods. @@ -63,10 +112,28 @@ summarize_rtestimate.poisson_rt <- function(x, level = 0.95, lambda = NULL, ...) #' @export summarize_rtestimate.epinow <- function(x, level = 0.95, ...) { - if (!requireNamespace("epinow2", quietly = TRUE)) { - cli::cli_abort("You must install the {.pkg epinow2} package for this functionality.") + if (!requireNamespace("EpiNow2", quietly = TRUE)) { + cli::cli_abort("You must install the {.pkg EpiNow2} package for this functionality.") } checkmate::assert_number(level, lower = 0, upper = 1) # res <- x$estimates$summarized |> dplyr::select() } + +#' @export +#' @details The `estimate_R` method is for the `EpiEstim` package. +#' @rdname summarize_rtestimate +summarize_rtestimate.estimate_R <- function(x, ...) { + if (!requireNamespace("EpiEstim", quietly = TRUE)) { + cli::cli_abort("You must install the {.pkg EpiEstim} package for this functionality.") + } + checkmate::assert_number(level, lower = 0, upper = 1) + + new_summarize( + date = x$R$t_end, + median = x$R$`Median(R)`, + lb = x$R$`Quantile.0.025(R)`, + ub = x$R$`Quantile.0.975(R)`, + package = "EpiEstim" + ) +} \ No newline at end of file diff --git a/man/new_summarize.Rd b/man/new_summarize.Rd new file mode 100644 index 0000000..78015da --- /dev/null +++ b/man/new_summarize.Rd @@ -0,0 +1,32 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/summarize_rtestimate.R +\name{new_summarize} +\alias{new_summarize} +\title{Create a new summary object} +\usage{ +new_summarize(date, median, lb, up, package) +} +\arguments{ +\item{date}{Integer vector. vector of index dates.} + +\item{median}{Double vector. vector of median values.} + +\item{lb}{Double vector. vector of lower bounds.} + +\item{up}{Double vector. vector of upper bounds.} + +\item{package}{String. Name of the package.} +} +\value{ +A list of class \code{summrt_summary}. with the following components: +\itemize{ +\item \code{date}: Integer vector. vector of index dates. +\item \code{median}: Double vector. vector of median values. +\item \code{lb}: Double vector. vector of lower bounds. +\item \code{up}: Double vector. vector of upper bounds. +\item \code{package}: String. Name of the package. +} +} +\description{ +Creates a new summary object for the \code{summrt} package while validating the input. +} diff --git a/man/summarize_rtestimate.Rd b/man/summarize_rtestimate.Rd index 794117c..4f20a48 100644 --- a/man/summarize_rtestimate.Rd +++ b/man/summarize_rtestimate.Rd @@ -6,6 +6,7 @@ \alias{summarize_rtestimate.cv_poisson_rt} \alias{summarize_rtestimate.poisson_rt} \alias{summarize_rtestimate.epinow} +\alias{summarize_rtestimate.estimate_R} \title{Extract Rt estimation from a model fit} \usage{ summarize_rtestimate(x, ...) @@ -17,6 +18,8 @@ summarize_rtestimate(x, ...) \method{summarize_rtestimate}{poisson_rt}(x, level = 0.95, lambda = NULL, ...) \method{summarize_rtestimate}{epinow}(x, level = 0.95, ...) + +\method{summarize_rtestimate}{estimate_R}(x, ...) } \arguments{ \item{x}{Object to extract Rt from.} @@ -30,3 +33,6 @@ summarize_rtestimate(x, ...) \description{ Extract Rt estimation from a model fit } +\details{ +The \code{estimate_R} method is for the \code{EpiEstim} package. +}