From 60116eadb01ac05da3b9e05ad6b73a6ff5cf1ab3 Mon Sep 17 00:00:00 2001 From: Kasper Schou Telkamp Date: Mon, 13 Nov 2023 16:05:14 +0100 Subject: [PATCH] Added a new function `epi_calendar()`. Fixes #15 --- NAMESPACE | 1 + NEWS.md | 4 +++ R/epi_calendar.R | 57 ++++++++++++++++++++++++++++++ man/aedseo-package.Rd | 5 +++ man/epi_calendar.Rd | 47 ++++++++++++++++++++++++ tests/testthat/test-epi_calendar.R | 28 +++++++++++++++ 6 files changed, 142 insertions(+) create mode 100644 R/epi_calendar.R create mode 100644 man/epi_calendar.Rd create mode 100644 tests/testthat/test-epi_calendar.R diff --git a/NAMESPACE b/NAMESPACE index 6041c55..0c58566 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -9,6 +9,7 @@ S3method(summary,aedseo) export("%>%") export(aedseo) export(autoplot) +export(epi_calendar) export(fit_growth_rate) export(tsd) importFrom(ggplot2,autoplot) diff --git a/NEWS.md b/NEWS.md index 233e5aa..9842f31 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,3 +1,7 @@ # aedseo (development version) * Added a `NEWS.md` file to track changes to the package. + + + +* Added a new function `epi_calendar()` that determines the epidemiological season based on a given date, allowing users to easily categorize dates within or outside specified seasons. diff --git a/R/epi_calendar.R b/R/epi_calendar.R new file mode 100644 index 0000000..467e827 --- /dev/null +++ b/R/epi_calendar.R @@ -0,0 +1,57 @@ +#' Determine Epidemiological Season +#' +#' @description +#' `r lifecycle::badge("stable")` +#' +#' This function identifies the epidemiological season to which a given date +#' belongs. +#' The epidemiological season is defined by a start and end week, where weeks +#' are numbered +#' according to the ISO week date system. +#' +#' @param date A date object representing the date to check. +#' @param start An integer specifying the start week of the epidemiological +#' season. +#' @param end An integer specifying the end week of the epidemiological season. +#' +#' @return A character vector indicating the season: +#' - "out_of_season" if the date is outside the specified season, +#' - If within the season, the function returns a character string indicating +#' the epidemiological season. +#' +#' @export +#' +#' @examples +#' # Check if a date is within the epidemiological season +#' epi_calendar(as.Date("2023-09-15"), start = 40, end = 20) +#' # Expected output: "2022/2023" +#' +#' epi_calendar(as.Date("2023-05-01"), start = 40, end = 20) +#' # Expected output: "out_of_season" +#' +#' epi_calendar(as.Date("2023-01-15"), start = 40, end = 20) +#' # Expected output: "2022/2023" +#' +#' epi_calendar(as.Date("2023-12-01"), start = 40, end = 20) +#' # Expected output: "2023/2024" +epi_calendar <- function(date, start = 40, end = 20) { + # Compute the current week + current_week <- as.integer(format(x = date, "%V")) + + if (current_week <= start && end <= current_week) { + return("out_of_season") + } + + # Compute the current year + current_year <- format(date, "%Y") + # ... and turn into integer + current_year_integer <- as.integer(current_year) + + if (current_week <= end) { + ans <- paste0(current_year_integer - 1, "/", current_year_integer) + } else { + ans <- paste0(current_year_integer, "/", current_year_integer + 1) + } + + return(ans) +} diff --git a/man/aedseo-package.Rd b/man/aedseo-package.Rd index a73615d..164188d 100644 --- a/man/aedseo-package.Rd +++ b/man/aedseo-package.Rd @@ -22,6 +22,11 @@ Useful links: \author{ \strong{Maintainer}: Kasper Schou Telkamp \email{ksst@ssi.dk} (\href{https://orcid.org/0009-0001-5126-0190}{ORCID}) +Authors: +\itemize{ + \item Lasse Engbo Christiansen \email{lsec@ssi.dk} (\href{https://orcid.org/0000-0001-5019-1931}{ORCID}) +} + Other contributors: \itemize{ \item Statens Serum Institut, SSI [copyright holder, funder] diff --git a/man/epi_calendar.Rd b/man/epi_calendar.Rd new file mode 100644 index 0000000..a2c18bd --- /dev/null +++ b/man/epi_calendar.Rd @@ -0,0 +1,47 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/epi_calendar.R +\name{epi_calendar} +\alias{epi_calendar} +\title{Determine Epidemiological Season} +\usage{ +epi_calendar(date, start = 40, end = 20) +} +\arguments{ +\item{date}{A date object representing the date to check.} + +\item{start}{An integer specifying the start week of the epidemiological +season.} + +\item{end}{An integer specifying the end week of the epidemiological season.} +} +\value{ +A character vector indicating the season: +\itemize{ +\item "out_of_season" if the date is outside the specified season, +\item If within the season, the function returns a character string indicating +the epidemiological season. +} +} +\description{ +\ifelse{html}{\href{https://lifecycle.r-lib.org/articles/stages.html#stable}{\figure{lifecycle-stable.svg}{options: alt='[Stable]'}}}{\strong{[Stable]}} + +This function identifies the epidemiological season to which a given date +belongs. +The epidemiological season is defined by a start and end week, where weeks +are numbered +according to the ISO week date system. +} +\examples{ +# Check if a date is within the epidemiological season +epi_calendar(as.Date("2023-09-15"), start = 40, end = 20) +# Expected output: "2022/2023" + +epi_calendar(as.Date("2023-05-01"), start = 40, end = 20) +# Expected output: "out_of_season" + +epi_calendar(as.Date("2023-01-15"), start = 40, end = 20) +# Expected output: "2022/2023" + +epi_calendar(as.Date("2023-12-01"), start = 40, end = 20) +# Expected output: "2023/2024" +} diff --git a/tests/testthat/test-epi_calendar.R b/tests/testthat/test-epi_calendar.R new file mode 100644 index 0000000..ef914f7 --- /dev/null +++ b/tests/testthat/test-epi_calendar.R @@ -0,0 +1,28 @@ +# Test if a date within the season returns the correct season +test_that("Within the season, correct season is returned", { + expect_equal( + epi_calendar(as.Date("2023-03-15"), start = 40, end = 20), "2022/2023" + ) + expect_equal( + epi_calendar(as.Date("2023-05-01"), start = 40, end = 20), "2022/2023" + ) + expect_equal( + epi_calendar(as.Date("2023-01-15"), start = 40, end = 20), "2022/2023" + ) + expect_equal( + epi_calendar(as.Date("2023-12-01"), start = 40, end = 20), "2023/2024" + ) +}) + +# Test if a date outside the season returns "out_of_season" +test_that("Outside the season, 'out_of_season' is returned", { + expect_equal( + epi_calendar(as.Date("2023-06-01"), start = 40, end = 20), "out_of_season" + ) + expect_equal( + epi_calendar(as.Date("2023-09-15"), start = 40, end = 20), "out_of_season" + ) + expect_equal( + epi_calendar(as.Date("2023-06-30"), start = 40, end = 20), "out_of_season" + ) +})