Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove badges from functions and rename #38

Merged
merged 2 commits into from
Nov 4, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 7 additions & 8 deletions R/aedseo.R
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
#' Automated and Early Detection of Seasonal Epidemic Onset
#'
#' @description
#' `r lifecycle::badge("stable")`
#'
#' This function performs automated and early detection of seasonal epidemic
#' onsets (aedseo) on a time series dataset. It estimates growth rates for
#' consecutive time intervals and calculates the sum of cases (sum_of_cases).
#'
#' @param tsd A `aedseo_tsd` object containing time series data with 'time' and
#' 'observed.'
#' 'observation.'
#' @param k An integer specifying the window size for modeling growth rates.
#' @param level The confidence level for parameter estimates, a numeric value
#' between 0 and 1.
Expand All @@ -28,7 +27,7 @@
#'
#' @return A `aedseo` object containing:
#' - 'reference_time': The time point for which the growth rate is estimated.
#' - 'observed': The observed value in the reference time point.
#' - 'observation': The observation in the reference time point.
#' - 'season': The stratification of observables in corresponding seasons.
#' - 'growth_rate': The estimated growth rate.
#' - 'lower_growth_rate': The lower bound of the growth rate's confidence
Expand All @@ -49,7 +48,7 @@
#' @examples
#' # Create a tibble object from sample data
#' tsd_data <- tsd(
#' observed = c(100, 120, 150, 180, 220, 270),
#' observation = c(100, 120, 150, 180, 220, 270),
#' time = as.Date(c(
#' "2023-01-01",
#' "2023-01-02",
Expand Down Expand Up @@ -90,7 +89,7 @@ aedseo <- function(
coll <- checkmate::makeAssertCollection()
checkmate::assert_data_frame(tsd, add = coll)
checkmate::assert_class(tsd, "aedseo_tsd", add = coll)
checkmate::assert_names(colnames(tsd), identical.to = c("time", "observed"), add = coll)
checkmate::assert_names(colnames(tsd), identical.to = c("time", "observation"), add = coll)
checkmate::assert_numeric(level, lower = 0, upper = 1, add = coll)
checkmate::assert_numeric(na_fraction_allowed, lower = 0, upper = 1,
add = coll)
Expand Down Expand Up @@ -130,7 +129,7 @@ aedseo <- function(
} else {
# Calculate growth rates
growth_rates <- fit_growth_rate(
observations = obs_iter$observed,
observations = obs_iter$observation,
level = level,
family = family
)
Expand All @@ -140,7 +139,7 @@ aedseo <- function(
growth_warning <- growth_rates$estimate[2] > 0

# Calculate Sum of Cases (sum_of_cases)
sum_of_cases <- base::sum(obs_iter$observed, na.rm = TRUE)
sum_of_cases <- base::sum(obs_iter$observation, na.rm = TRUE)

# Evaluate if sum_of_cases exceeds disease_threshold
sum_of_cases_warning <- sum_of_cases > (disease_threshold * k)
Expand All @@ -153,7 +152,7 @@ aedseo <- function(
res,
tibble::tibble(
reference_time = tsd$time[i],
observed = tsd$observed[i],
observation = tsd$observation[i],
season = tsd$season[i],
growth_rate = growth_rates$estimate[1],
lower_growth_rate = growth_rates$estimate[2],
Expand Down
7 changes: 3 additions & 4 deletions R/autoplot.R
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#' Create a complete 'ggplot' appropriate to a particular data type
#'
#' @description
#' `r lifecycle::badge("stable")`
#'
#' This function generates a complete 'ggplot' object suitable for
#' visualizing time series data in an `aedseo_tsd` object. It creates a line
Expand All @@ -23,7 +22,7 @@
#' @examples
#' # Create an example aedseo_tsd object
#' aedseo_tsd_object <- tsd(
#' observed = c(100, 120, 150, 180, 220, 270),
#' observation = c(100, 120, 150, 180, 220, 270),
#' time = as.Date(c(
#' "2023-01-01",
#' "2023-01-02",
Expand Down Expand Up @@ -62,7 +61,7 @@ autoplot.aedseo_tsd <- function(object, ...) {
ggplot2::ggplot(
mapping = ggplot2::aes(
x = .data$time,
y = .data$observed
y = .data$observation
)
) +
ggplot2::geom_point() +
Expand All @@ -87,7 +86,7 @@ autoplot.aedseo <- function(
ggplot2::ggplot(
mapping = ggplot2::aes(
x = .data$reference_time,
y = .data$observed
y = .data$observation
)
) +
ggplot2::geom_point(
Expand Down
1 change: 0 additions & 1 deletion R/epi_calendar.R
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#' Determine Epidemiological Season
#'
#' @description
#' `r lifecycle::badge("stable")`
#'
#' This function identifies the epidemiological season, (must span new year)
#' to which a given date belongs.
Expand Down
1 change: 0 additions & 1 deletion R/fit_growth_rate.R
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#' Fit a growth rate model to time series observations.
#'
#' @description
#' `r lifecycle::badge("stable")`
#'
#' This function fits a growth rate model to time series observations and
#' provides parameter estimates along with confidence intervals.
Expand Down
15 changes: 7 additions & 8 deletions R/fit_quantiles.R
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
#' Fits weighted observations to distribution and returns quantiles
#'
#' @description
#' `r lifecycle::badge("stable")`
#'
#' This function calculates the quantiles of weighted time series observations. The output contains the quantiles
#' from the fitted distribution.
#'
#' @param weighted_observations A tibble containing two columns of length n; `observed`, which contains the data
#' @param weighted_observations A tibble containing two columns of length n; `observation`, which contains the data
#' points, and `weight`, which is the importance assigned to the observation. Higher weights indicate that an
#' observation has more influence on the model outcome, while lower weights reduce its impact.
#' @param conf_levels A numeric vector specifying the confidence levels for parameter estimates. The values have
Expand Down Expand Up @@ -49,7 +48,7 @@
#'
#' # Add into a tibble with decreasing weight for older seasons
#' data_input <- tibble::tibble(
#' observed = observations,
#' observation = observations,
#' weight = 0.8^rep(season_num_rev, each = obs)
#' )
#'
Expand Down Expand Up @@ -78,7 +77,7 @@ fit_quantiles <- function(
checkmate::assert_numeric(conf_levels, lower = 0, upper = 1,
unique = TRUE, sorted = TRUE, add = coll)
checkmate::assert_names(colnames(weighted_observations),
identical.to = c("observed", "weight"), add = coll)
identical.to = c("observation", "weight"), add = coll)
checkmate::assert_numeric(lower_optim, add = coll)
checkmate::assert_numeric(upper_optim, add = coll)
checkmate::reportAssertions(coll)
Expand All @@ -99,16 +98,16 @@ fit_quantiles <- function(
# The weighted negative loglikelihood function
nll <- function(par, weighted_observations, family = family) {
log_probability <- switch(family,
weibull = stats::dweibull(weighted_observations$observed, shape = exp(par[1]), scale = exp(par[2]),
weibull = stats::dweibull(weighted_observations$observation, shape = exp(par[1]), scale = exp(par[2]),
log = TRUE),
lnorm = stats::dlnorm(weighted_observations$observed, meanlog = par[1], sdlog = par[2], log = TRUE),
exp = stats::dexp(weighted_observations$observed, rate = exp(par[1]), log = TRUE)
lnorm = stats::dlnorm(weighted_observations$observation, meanlog = par[1], sdlog = par[2], log = TRUE),
exp = stats::dexp(weighted_observations$observation, rate = exp(par[1]), log = TRUE)
)
return(-sum(log_probability * weighted_observations$weight))
}

# Run optimisation for weighted observations
optim_obj <- stats::optim(par = init_par_fun(family = family, observations = weighted_observations$observed),
optim_obj <- stats::optim(par = init_par_fun(family = family, observations = weighted_observations$observation),
fn = nll,
weighted_observations = weighted_observations,
family = family,
Expand Down
3 changes: 1 addition & 2 deletions R/plot.R
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#' Create a complete 'ggplot' appropriate to a particular data type
#'
#' @description
#' `r lifecycle::badge("stable")`
#'
#' This function generates a complete 'ggplot' object suitable for
#' visualizing time series data in an `aedseo_tsd` object. It creates a line
Expand All @@ -19,7 +18,7 @@
#' @examples
#' # Create an example aedseo_tsd object
#' aedseo_tsd_object <- tsd(
#' observed = c(100, 120, 150, 180, 220, 270),
#' observation = c(100, 120, 150, 180, 220, 270),
#' time = as.Date(c(
#' "2023-01-01",
#' "2023-01-02",
Expand Down
9 changes: 4 additions & 5 deletions R/predict.R
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#' Predict Growth Rates for Future Time Steps
#'
#' @description
#' `r lifecycle::badge("stable")`
#'
#' This function is used to predict future growth rates based on a model object
#' created using the 'aedseo' package. It takes the model object and the number
Expand All @@ -25,7 +24,7 @@
#' @examples
#' # Analyze the data using the aedseo package
#' tsd_data <- tsd(
#' observed = c(100, 120, 150, 180, 220, 270),
#' observation = c(100, 120, 150, 180, 220, 270),
#' time = as.Date(c(
#' "2023-01-01",
#' "2023-01-02",
Expand Down Expand Up @@ -56,9 +55,9 @@ predict.aedseo <- function(object, n_step = 3, ...) {
dplyr::reframe(
t = 0:n_step,
time = .data$reference_time + t,
estimate = exp(log(.data$observed) + .data$growth_rate * t),
lower = exp(log(.data$observed) + .data$lower_growth_rate * t),
upper = exp(log(.data$observed) + .data$upper_growth_rate * t)
estimate = exp(log(.data$observation) + .data$growth_rate * t),
lower = exp(log(.data$observation) + .data$lower_growth_rate * t),
upper = exp(log(.data$observation) + .data$upper_growth_rate * t)
)

# Extract the attributes from the object
Expand Down
19 changes: 9 additions & 10 deletions R/seasonal_burden_levels.R
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
#' Compute burden levels from seasonal time series observations.
#'
#' @description
#' `r lifecycle::badge("stable")`
#'
#' This function calculates the burden levels of time series of observations that are stratified by season.
#' It uses the previous seasons to calculate the levels of the newest season.
#'
#' @param tsd A `aedseo_tsd` object containing time series data with 'time' and 'observed'.
#' @param tsd A `aedseo_tsd` object containing time series data with 'time' and 'observation'.
#' @param season_weeks A numeric vector of length 2, `c(start, end)`, with the start and end weeks of the seasons to
#' stratify the observations by. Must span the new year; e.g.: `season_weeks = c(21, 20)`.
#' NOTE: The data must include data for a complete previous season to make predictions for the newest season.
Expand Down Expand Up @@ -67,10 +66,10 @@
#' add_to_max <- c(50, 100, 200, 100)
#' peak <- add_to_max + max(random_increasing_obs)
#'
#' # Combine into a single observed sequence
#' observed <- c(random_increasing_obs, peak, random_decreasing_obs)
#' # Combine into a single observations sequence
#' observations <- c(random_increasing_obs, peak, random_decreasing_obs)
#'
#' return(observed)
#' return(observations)
#' }
#'
#' season_1 <- generate_flu_season()
Expand All @@ -84,7 +83,7 @@
#' by = "week")
#'
#' tsd_data <- tsd(
#' observed = c(season_1, season_2),
#' observation = c(season_1, season_2),
#' time = as.Date(weekly_dates),
#' time_interval = "week"
#' )
Expand All @@ -106,7 +105,7 @@ seasonal_burden_levels <- function(
coll <- checkmate::makeAssertCollection()
checkmate::assert_data_frame(tsd, add = coll)
checkmate::assert_class(tsd, "aedseo_tsd", add = coll)
checkmate::assert_names(colnames(tsd), identical.to = c("time", "observed"), add = coll)
checkmate::assert_names(colnames(tsd), identical.to = c("time", "observation"), add = coll)
checkmate::assert_integerish(season_weeks, len = 2, lower = 1, upper = 53,
null.ok = FALSE, add = coll)
checkmate::assert_numeric(decay_factor, lower = 0, upper = 1, len = 1, add = coll)
Expand Down Expand Up @@ -139,12 +138,12 @@ seasonal_burden_levels <- function(
# Select n_peak highest observations and filter observations >= disease_threshold
season_observations_and_weights <- weighted_seasonal_tsd |>
dplyr::select(-c("year", "time")) |>
dplyr::filter(.data$observed >= disease_threshold) |>
dplyr::slice_max(.data$observed, n = n_peak, with_ties = FALSE, by = "season")
dplyr::filter(.data$observation >= disease_threshold) |>
dplyr::slice_max(.data$observation, n = n_peak, with_ties = FALSE, by = "season")

# Run quantiles_fit function
quantiles_fit <- season_observations_and_weights |>
dplyr::select("observed", "weight") |>
dplyr::select("observation", "weight") |>
fit_quantiles(weighted_observations = _, conf_levels = conf_levels, ...)

# If method intensity_levels was chosen; use the high level from the `fit_quantiles` function as the high
Expand Down
3 changes: 1 addition & 2 deletions R/summary.R
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#' Summary method for aedseo objects
#'
#' @description
#' `r lifecycle::badge("stable")`
#'
#' Summarize the results of an aedseo analysis, including the latest growth rate
#' estimate, the confidence interval, and information about growth warnings.
Expand All @@ -18,7 +17,7 @@
#' @examples
#' # Create a tsibble object from sample data
#' tsd_data <- tsd(
#' observed = c(100, 120, 150, 180, 220, 270),
#' observation = c(100, 120, 150, 180, 220, 270),
#' time = as.Date(c(
#' "2023-01-01",
#' "2023-01-02",
Expand Down
24 changes: 11 additions & 13 deletions R/tsd.R
Original file line number Diff line number Diff line change
@@ -1,28 +1,26 @@
#' Create a S3 `aedseo_tsd` (time-series data) object from observed data
#' and corresponding dates.
#' Create a S3 `aedseo_tsd` (time-series data) object from observed data and corresponding dates.
#'
#' @description
#' `r lifecycle::badge("stable")`
#'
#' This function takes observed data and the corresponding date vector and
#' This function takes observations and the corresponding date vector and
#' converts it into a `aedseo_tsd` object, which is a time series
#' data structure that can be used for time series analysis.
#'
#' @param observed A numeric vector containing the observations.
#' @param observation A numeric vector containing the observations.
#' @param time A date vector containing the corresponding dates.
#' @param time_interval A character vector specifying the time interval.
#' Choose between "day," "week," or "month."
#'
#' @return A `aedseo_tsd` object containing:
#' - 'time': The time point for for where the observations is observed.
#' - 'observed': The observed value at the time point.
#' - 'time': The time point for for when the observation is observed.
#' - 'observation': The observed value at the time point.
#'
#' @export
#'
#' @examples
#' # Create a `aedseo_tsd` object from daily data
#' daily_tsd <- tsd(
#' observed = c(10, 15, 20, 18),
#' observation = c(10, 15, 20, 18),
#' time = as.Date(
#' c("2023-01-01", "2023-01-02", "2023-01-03", "2023-01-04")
#' ),
Expand All @@ -31,7 +29,7 @@
#'
#' # Create a `aedseo_tsd` object from weekly data
#' weekly_tsd <- tsd(
#' observed = c(100, 120, 130),
#' observation = c(100, 120, 130),
#' time = as.Date(
#' c("2023-01-01", "2023-01-08", "2023-01-15")
#' ),
Expand All @@ -40,18 +38,18 @@
#'
#' # Create a `aedseo_tsd` object from monthly data
#' monthly_tsd <- tsd(
#' observed = c(500, 520, 540),
#' observation = c(500, 520, 540),
#' time = as.Date(
#' c("2023-01-01", "2023-02-01", "2023-03-01")
#' ),
#' time_interval = "month"
#' )
#'
tsd <- function(observed, time, time_interval = c("day", "week", "month")) {
tsd <- function(observation, time, time_interval = c("day", "week", "month")) {
# Check input arguments
coll <- checkmate::makeAssertCollection()
checkmate::assert_date(time, add = coll)
checkmate::assert_numeric(observed, add = coll)
checkmate::assert_numeric(observation, add = coll)
checkmate::reportAssertions(coll)

# Throw an error if any of the inputs are not supported
Expand All @@ -60,7 +58,7 @@ tsd <- function(observed, time, time_interval = c("day", "week", "month")) {
# Collect the input in a tibble
tbl <- tibble::tibble(
time = time,
observed = observed
observation = observation
)

# Create the time series data object
Expand Down
Loading
Loading
You are viewing a condensed version of this merge commit. You can view the full changes here.