Skip to content

Commit

Permalink
Merge pull request #6 from ssi-dk:telkamp7/issue5
Browse files Browse the repository at this point in the history
Telkamp7/issue5
  • Loading branch information
telkamp7 authored Oct 30, 2023
2 parents e1f4745 + bcb7e12 commit 86f2ca8
Show file tree
Hide file tree
Showing 3 changed files with 162 additions and 0 deletions.
83 changes: 83 additions & 0 deletions R/summary.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
#' Summary method for aedseo objects
#'
#' @description
#' `r lifecycle::badge("experimental")`
#'
#' Summarize the results of an aedseo analysis, including the latest growth rate
#' estimate, the confidence interval, and information about growth warnings.
#'
#' @param object An object of class 'aedseo' containing the results of an aedseo
#' analysis.
#' @param ... Additional arguments (not used).
#'
#' @return This function is used for its side effect, which is printing a
#' summary message to the console.
#'
#' @examples
#' # Create a tsibble object from sample data
#' tsd_data <- tsd(
#' observed = c(100, 120, 150, 180, 220, 270),
#' time = as.Date(c(
#' "2023-01-01",
#' "2023-01-02",
#' "2023-01-03",
#' "2023-01-04",
#' "2023-01-05",
#' "2023-01-06"
#' )),
#' time_interval = "day"
#' )
#'
#' # Calculate AEDSEO with a 3-day window and a Poisson family model
#' aedseo_results <- aedseo(
#' tsd = tsd_data,
#' k = 3,
#' level = 0.95,
#' family = "poisson"
#' )
#' # Print the summary of the aedseo_results to the console
#' summary(aedseo_results)
summary.aedseo <- function(object, ...) {
# Extract the last observation
last_observation <- dplyr::last(object)

# Extract the reference time
reference_time <- last_observation$reference_time

# Latest growth warning
latest_growth_warning <- object %>%
dplyr::filter(.data$growth_warning == TRUE) %>%
dplyr::summarise(latest_growth_warning = dplyr::last(reference_time)) %>%
dplyr::pull(latest_growth_warning)

# Calculate the total number of growth warnings
sum_of_growth_warnings <- object %>%
dplyr::summarise(sum_of_growth_warnings = sum(.data$growth_warning)) %>%
dplyr::pull(sum_of_growth_warnings)

# Generate the summary message
summary_message <- sprintf(
"Summary of aedseo Object
Reference time point:
%s
Growth rate estimate:
Estimate Lower Upper
%.3f %.3f %.3f
Total number of growth warnings in the series:
%d
Latest growth warning:
%s",
as.character(reference_time),
last_observation$growth_rate,
last_observation$lower_growth_rate,
last_observation$upper_growth_rate,
sum_of_growth_warnings,
as.character(latest_growth_warning)
)

# Print the summary message
cat(summary_message)
}
48 changes: 48 additions & 0 deletions man/summary.aedseo.Rd

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

31 changes: 31 additions & 0 deletions tests/testthat/test-summary.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
test_that("Summary prints without errors", {
# Set start and end dates
from_date <- as.Date("2021-01-01")
to_date <- as.Date("2021-01-31")

# Choose some time dates
time <- seq.Date(from = from_date, to = to_date, by = "day")

# Count the number of observations
n <- length(time)

# Data
tsd_data_poisson <- tsd(
observed = rpois(n = n, lambda = 1:n),
time = time,
time_interval = "day"
)
# Calculate AEDSEO with a 3-day window
aedseo_poisson <- aedseo(
tsd = tsd_data_poisson,
k = 3,
level = 0.95,
family = "poisson"
)

# Capture the output of the summary function
tmp <- capture_output(summary.aedseo(aedseo_poisson))

# Verify that the summary printed without errors
expect_true(grepl(pattern = "Summary of aedseo Object", x = tmp))
})

0 comments on commit 86f2ca8

Please sign in to comment.