Title: | Automated and Early Detection of Seasonal Epidemic Onset and Burden Levels |
---|---|
Description: | A powerful tool for automating the early detection of seasonal epidemic onsets in time series data. It offers the ability to estimate growth rates across consecutive time intervals, calculate the sum of cases (SoC) within those intervals, and estimate seasonal onsets within user defined seasons. With use of a disease-specific threshold it also offers the possibility to estimate seasonal onset of epidemics. Additionally it offers the ability to estimate burden levels for seasons based on historical data. It is aimed towards epidemiologists, public health professionals, and researchers seeking to identify and respond to seasonal epidemics in a timely fashion. For reference on growth rate estimation, see Walling and Lipstich (2007) <doi:10.1098/rspb.2006.3754> and Obadia et al. (2012) <doi:10.1186/1472-6947-12-147>. Seasonal burden level calculations have been inspired by The Moving Epidemic Method (MEM), see Vega and Lozano (2012) <doi:10.1111/j.1750-2659.2012.00422.x>. |
Authors: | Sofia Myrup Otero [aut],
Kasper Schou Telkamp [aut] |
Maintainer: | Lasse Engbo Christiansen <[email protected]> |
License: | MIT + file LICENSE |
Version: | 0.1.2.9000 |
Built: | 2025-03-12 15:22:46 UTC |
Source: | https://github.com/ssi-dk/aedseo |
tsd
objectGenerates a complete 'ggplot' object suitable for visualizing time series data in a
tsd
, tsd_onset
or tsd_onset_and_burden
object.
autoplot(tsd)
Generates points for each observation and connects them with a line.
autoplot(tsd_onset)
The first plot generates a line connecting the observations. The transparency of the points reflects if seasonal onset has occurred.
The second plot presents the growth rate for each observation along with confidence intervals. The transparency of the points indicates whether a growth warning condition is met.
autoplot(tsd_onset_and_burden)
Generates a line connecting the observations in the current season, along with colored regions
representing different burdens levels and a vertical line indicating outbreak start.
The y-axis is scaled with ggplot2::scale_y_log10
to give better visualisation of the burden levels.
autoplot(object, ...) ## S3 method for class 'tsd' autoplot( object, line_width = 0.7, obs_size = 2, text_family = "sans", time_interval_step = "5 weeks", y_label = "Weekly observations", ... ) ## S3 method for class 'tsd_onset' autoplot( object, line_width = 0.7, obs_size = 2, alpha_warning = 0.2, alpha_ribbon = 0.1, text_family = "sans", legend_position = "bottom", time_interval_step = "5 weeks", y_label = "Weekly observations", ... ) ## S3 method for class 'tsd_onset_and_burden' autoplot( object, y_lower_bound = 5, factor_to_max = 2, disease_color = "royalblue", season_start = 21, season_end = season_start - 1, time_interval_step = "3 weeks", y_label = "Weekly observations", text_burden_size = 10/2.8, fill_alpha = c(0.45, 0.6, 0.75, 0.89, 1), text_family = "sans", line_color = "black", line_type = "solid", vline_color = "red", vline_linetype = "dashed", y_scale_labels = scales::label_comma(big.mark = ".", decimal.mark = ","), theme_custom = ggplot2::theme_bw(), legend_position = "right", ... )
autoplot(object, ...) ## S3 method for class 'tsd' autoplot( object, line_width = 0.7, obs_size = 2, text_family = "sans", time_interval_step = "5 weeks", y_label = "Weekly observations", ... ) ## S3 method for class 'tsd_onset' autoplot( object, line_width = 0.7, obs_size = 2, alpha_warning = 0.2, alpha_ribbon = 0.1, text_family = "sans", legend_position = "bottom", time_interval_step = "5 weeks", y_label = "Weekly observations", ... ) ## S3 method for class 'tsd_onset_and_burden' autoplot( object, y_lower_bound = 5, factor_to_max = 2, disease_color = "royalblue", season_start = 21, season_end = season_start - 1, time_interval_step = "3 weeks", y_label = "Weekly observations", text_burden_size = 10/2.8, fill_alpha = c(0.45, 0.6, 0.75, 0.89, 1), text_family = "sans", line_color = "black", line_type = "solid", vline_color = "red", vline_linetype = "dashed", y_scale_labels = scales::label_comma(big.mark = ".", decimal.mark = ","), theme_custom = ggplot2::theme_bw(), legend_position = "right", ... )
object |
a |
... |
Additional arguments (not used). |
line_width |
A numeric specifying the width of line connecting observations. |
obs_size |
A numeric, specifying the size of observational points. |
text_family |
A character specifying the font family for the text labels. |
time_interval_step |
A character vector specifying the time interval and how many time steps are desired on the x-axis, e.g. '10 days', '4 weeks', or '3 months'. |
y_label |
A character vector specifying the y label text. |
alpha_warning |
A numeric specifying the alpha (transparency) for the observations with a seasonal_onset_alarm (first plot) or significantly positive growth rate (second plot). |
alpha_ribbon |
A numeric specifying the alpha for the confidence intervals of the growth rate. |
legend_position |
A character specifying the position of the legend on the plot. |
y_lower_bound |
A numeric specifying the lower bound of the y-axis. |
factor_to_max |
A numeric specifying the factor to multiply the high burden level for extending the y-axis. |
disease_color |
A character specifying the base color for the disease level regions. |
season_start , season_end
|
Integers giving the start and end weeks of the seasons to stratify the observations by. |
text_burden_size |
A numeric specifying the size of the text labels. |
fill_alpha |
A numeric vector specifying the transparency levels for the fill colors of burden levels. Must match the number of levels. |
line_color |
A character specifying the color of the line connecting observations. |
line_type |
A character specifying the line type for observation line. |
vline_color |
A character specifying the color of the vertical outbreak start lines. |
vline_linetype |
A character specifying the line type for outbreak start lines. |
y_scale_labels |
A function to format y-axis labels. |
theme_custom |
A function with a ggplot2 theme, specifying the theme to apply to the plot. |
A 'ggplot' object for visualizing the tsd
data.
A 'ggplot' object for visualizing the tsd_onset
data.
A 'ggplot' object for visualizing the tsd_onset_and_burden
data for the current season.
set.seed(345) # Create an example `tsd` object time_series <- generate_seasonal_data() autoplot(time_series) # Create an `tsd_onset` object time_series_with_onset <- seasonal_onset( tsd = time_series, k = 3, level = 0.95, family = "quasipoisson" ) autoplot(time_series_with_onset) # Define `disease_threshold` disease_threshold <- 150 # Create a `tsd_onset_and_burden` object tsd_onset_burden <- combined_seasonal_output( tsd = time_series, disease_threshold = disease_threshold ) autoplot(tsd_onset_burden)
set.seed(345) # Create an example `tsd` object time_series <- generate_seasonal_data() autoplot(time_series) # Create an `tsd_onset` object time_series_with_onset <- seasonal_onset( tsd = time_series, k = 3, level = 0.95, family = "quasipoisson" ) autoplot(time_series_with_onset) # Define `disease_threshold` disease_threshold <- 150 # Create a `tsd_onset_and_burden` object tsd_onset_burden <- combined_seasonal_output( tsd = time_series, disease_threshold = disease_threshold ) autoplot(tsd_onset_burden)
This function performs automated and early detection of seasonal epidemic onsets and estimates the burden levels from time series dataset stratified by season. The seasonal onset estimates growth rates for consecutive time intervals and calculates the sum of cases. The burden levels use the previous seasons to estimate the levels of the current season.
combined_seasonal_output( tsd, disease_threshold = 20, family = c("poisson", "quasipoisson"), family_quant = c("lnorm", "weibull", "exp"), season_start = 21, season_end = season_start - 1, only_current_season = TRUE, ... )
combined_seasonal_output( tsd, disease_threshold = 20, family = c("poisson", "quasipoisson"), family_quant = c("lnorm", "weibull", "exp"), season_start = 21, season_end = season_start - 1, only_current_season = TRUE, ... )
tsd |
An object containing time series data with 'time' and 'observation.' |
disease_threshold |
An integer specifying the threshold for considering a disease outbreak. For seasonal onset it defines the per time-step disease threshold that has to be surpassed to possibly
trigger a seasonal onset alarm. If the total number of cases in a window of size k exceeds
|
family |
A character string specifying the family for modeling seasonal onset. |
family_quant |
A character string specifying the family for modeling burden levels. |
season_start , season_end
|
Integers giving the start and end weeks of the seasons to stratify the observations by. |
only_current_season |
Should the output only include results for the current season? |
... |
Arguments passed to |
An object containing two lists: onset_output and burden_output:
onset_output:
A seasonal_onset
object containing:
'reference_time': The time point for which the growth rate is estimated.
'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 interval.
'upper_growth_rate': The upper bound of the growth rate's confidence interval.
'growth_warning': Logical. Is the growth rate significantly higher than zero?
'sum_of_cases': The sum of cases within the time window.
'sum_of_cases_warning': Logical. Does the Sum of Cases exceed the disease threshold?
'seasonal_onset_alarm': Logical. Is there a seasonal onset alarm?
'skipped_window': Logical. Was the window skipped due to missing?
'converged': Logical. Was the IWLS judged to have converged? - 'seasonal_onset': Logical. The first detected seasonal onset in the season?
burden_output:
A list containing:
'season': The season that burden levels are calculated for.
'high_conf_level': (only for intensity_level method) The conf_level chosen for the high level.
'conf_levels': (only for peak_level method) The conf_levels chosen to fit the 'low', 'medium', 'high' levels.
'values': A named vector with values for 'very low', 'low', 'medium', 'high' levels.
'par': The fit parameters for the chosen family.
par_1:
For 'weibull': Shape parameter.
For 'lnorm': Mean of the log-transformed observations.
For 'exp': Rate parameter.
'par_2':
For 'weibull': Scale parameter.
For 'lnorm': Standard deviation of the log-transformed observations.
For 'exp': Not applicable (set to NA).
'obj_value': The value of the objective function - (negative log-likelihood), which represent the minimized objective function value from the optimisation. Smaller value equals better optimisation.
'converged': Logical. TRUE if the optimisation converged.
'family': The distribution family used for the optimization.
'weibull': Uses the Weibull distribution for fitting.
'lnorm': Uses the Log-normal distribution for fitting.
'exp': Uses the Exponential distribution for fitting.
'disease_threshold': The input disease threshold, which is also the very low level.
# Generate random flu season generate_flu_season <- function(start = 1, end = 1000) { random_increasing_obs <- round(sort(runif(24, min = start, max = end))) random_decreasing_obs <- round(rev(random_increasing_obs)) # Generate peak numbers add_to_max <- c(50, 100, 200, 100) peak <- add_to_max + max(random_increasing_obs) # Combine into a single observations sequence observations <- c(random_increasing_obs, peak, random_decreasing_obs) return(observations) } season_1 <- generate_flu_season() season_2 <- generate_flu_season() start_date <- as.Date("2022-05-29") end_date <- as.Date("2024-05-20") weekly_dates <- seq.Date(from = start_date, to = end_date, by = "week") tsd_data <- tsd( observation = c(season_1, season_2), time = as.Date(weekly_dates), time_interval = "week" ) # Run the main function combined_data <- combined_seasonal_output(tsd_data) # Print seasonal onset results print(combined_data$onset_output) # Print burden level results print(combined_data$burden_output)
# Generate random flu season generate_flu_season <- function(start = 1, end = 1000) { random_increasing_obs <- round(sort(runif(24, min = start, max = end))) random_decreasing_obs <- round(rev(random_increasing_obs)) # Generate peak numbers add_to_max <- c(50, 100, 200, 100) peak <- add_to_max + max(random_increasing_obs) # Combine into a single observations sequence observations <- c(random_increasing_obs, peak, random_decreasing_obs) return(observations) } season_1 <- generate_flu_season() season_2 <- generate_flu_season() start_date <- as.Date("2022-05-29") end_date <- as.Date("2024-05-20") weekly_dates <- seq.Date(from = start_date, to = end_date, by = "week") tsd_data <- tsd( observation = c(season_1, season_2), time = as.Date(weekly_dates), time_interval = "week" ) # Run the main function combined_data <- combined_seasonal_output(tsd_data) # Print seasonal onset results print(combined_data$onset_output) # Print burden level results print(combined_data$burden_output)
This function identifies the epidemiological season, (must span new year) 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.
epi_calendar(date, start = 21, end = 20)
epi_calendar(date, start = 21, end = 20)
date |
A date object representing the date to check. |
start |
An integer specifying the start week of the epidemiological season. |
end |
An integer specifying the end week of the epidemiological season. |
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.
# Check if a date is within the epidemiological season epi_calendar(as.Date("2023-09-15"), start = 21, end = 20) # Expected output: "2023/2024" epi_calendar(as.Date("2023-05-30"), start = 40, end = 20) # Expected output: "out_of_season" try(epi_calendar(as.Date("2023-01-15"), start = 1, end = 40)) # Expected error: "`start` must be greater than `end`!" epi_calendar(as.Date("2023-10-06"), start = 40, end = 11) # Expected output: "2023/2024"
# Check if a date is within the epidemiological season epi_calendar(as.Date("2023-09-15"), start = 21, end = 20) # Expected output: "2023/2024" epi_calendar(as.Date("2023-05-30"), start = 40, end = 20) # Expected output: "out_of_season" try(epi_calendar(as.Date("2023-01-15"), start = 1, end = 40)) # Expected error: "`start` must be greater than `end`!" epi_calendar(as.Date("2023-10-06"), start = 40, end = 11) # Expected output: "2023/2024"
This function fits a growth rate model to time series observations and provides parameter estimates along with confidence intervals.
fit_growth_rate( observations, level = 0.95, family = c("poisson", "quasipoisson") )
fit_growth_rate( observations, level = 0.95, family = c("poisson", "quasipoisson") )
observations |
A numeric vector containing the time series observations. |
level |
The confidence level for parameter estimates, a numeric value between 0 and 1. |
family |
A character string specifying the family for modeling. Choose between "poisson," or "quasipoisson". |
A list containing:
'fit': The fitted growth rate model.
'estimate': A numeric vector with parameter estimates, including the growth rate and its confidence interval.
'level': The confidence level used for estimating parameter confidence intervals.
# Fit a growth rate model to a time series of counts # (e.g., population growth) data <- c(100, 120, 150, 180, 220, 270) fit_growth_rate( observations = data, level = 0.95, family = "poisson" )
# Fit a growth rate model to a time series of counts # (e.g., population growth) data <- c(100, 120, 150, 180, 220, 270) fit_growth_rate( observations = data, level = 0.95, family = "poisson" )
This function estimates the percentiles of weighted time series observations. The output contains the percentiles from the fitted distribution.
fit_percentiles( weighted_observations, conf_levels = c(0.5, 0.9, 0.95), family = c("lnorm", "weibull", "exp"), optim_method = c("Nelder-Mead", "BFGS", "CG", "L-BFGS-B", "SANN", "Brent"), lower_optim = -Inf, upper_optim = Inf )
fit_percentiles( weighted_observations, conf_levels = c(0.5, 0.9, 0.95), family = c("lnorm", "weibull", "exp"), optim_method = c("Nelder-Mead", "BFGS", "CG", "L-BFGS-B", "SANN", "Brent"), lower_optim = -Inf, upper_optim = Inf )
weighted_observations |
A tibble containing two columns of length n; |
conf_levels |
A numeric vector specifying the confidence levels for parameter estimates. The values have to be unique and in ascending order, that is the lowest level is first and highest level is last. |
family |
A character string specifying the family for modeling |
optim_method |
A character string specifying the method to be used in the optimisation. Lookup |
lower_optim |
A numeric value for the optimisation. |
upper_optim |
A numeric value for the optimisation. |
A list containing:
'conf_levels': The conf_levels chosen to fit the percentiles.
'percentiles': The percentile results from the fit.
'par': The fit parameters for the chosen family.
par_1:
For 'weibull': Shape parameter (k).
For 'lnorm': Mean of the log-transformed observations.
For 'exp': Rate parameter (λ).
'par_2':
For 'weibull': Scale parameter (λ).
For 'lnorm': Standard deviation of the log-transformed observations.
For 'exp': Not applicable (set to NA).
'obj_value': The value of the objective function - (negative log-likelihood), which represent the minimized objective function value from the optimisation. Smaller value equals better optimisation.
'converged': Logical. TRUE if the optimisation converged.
'family': The distribution family used for the optimization.
'weibull': Uses the Weibull distribution for fitting.
'lnorm': Uses the Log-normal distribution for fitting.
'exp': Uses the Exponential distribution for fitting.
# Create three seasons with random observations obs <- 10 season <- c("2018/2019", "2019/2020", "2020/2021") season_num_rev <- rev(seq(from = 1, to = length(season))) observations <- rep(stats::rnorm(10, obs), length(season)) # Add into a tibble with decreasing weight for older seasons data_input <- tibble::tibble( observation = observations, weight = 0.8^rep(season_num_rev, each = obs) ) # Use the model fit_percentiles( weighted_observations = data_input, conf_levels = c(0.50, 0.90, 0.95), family= "weibull" )
# Create three seasons with random observations obs <- 10 season <- c("2018/2019", "2019/2020", "2020/2021") season_num_rev <- rev(seq(from = 1, to = length(season))) observations <- rep(stats::rnorm(10, obs), length(season)) # Add into a tibble with decreasing weight for older seasons data_input <- tibble::tibble( observation = observations, weight = 0.8^rep(season_num_rev, each = obs) ) # Use the model fit_percentiles( weighted_observations = data_input, conf_levels = c(0.50, 0.90, 0.95), family= "weibull" )
tsd
objectThis function generates a simulated dataset of seasonal waves with trend and noise. This function assumes 365 days, 52 weeks, and 12 months per year. Leap years are not included in the calculation.
generate_seasonal_data( years = 3, start_date = as.Date("2021-05-26"), amplitude = 100, mean = 100, phase = 0, trend_rate = NULL, noise_overdispersion = NULL, time_interval = c("week", "day", "month"), lower_bound = 1e-06 )
generate_seasonal_data( years = 3, start_date = as.Date("2021-05-26"), amplitude = 100, mean = 100, phase = 0, trend_rate = NULL, noise_overdispersion = NULL, time_interval = c("week", "day", "month"), lower_bound = 1e-06 )
years |
An integer specifying the number of years of data to simulate. |
start_date |
A date representing the start date of the simulated data. |
amplitude |
A number specifying the amplitude of the seasonal wave.
The output will fluctuate within the range |
mean |
A number specifying the mean of the seasonal wave. |
phase |
A numeric value (in radians) representing the horizontal shift of the sine wave, hence the phase shift of the seasonal wave. The phase must be between zero and 2*pi. |
trend_rate |
A numeric value specifying the exponential growth/decay rate. |
noise_overdispersion |
A numeric value specifying the overdispersion of the generated data. 0 means deterministic, 1 is pure poisson and for values > 1 a negative binomial is assumed. |
time_interval |
A character vector specifying the time interval. Choose between 'day', 'week', or 'month'. |
lower_bound |
A numeric value that can be used to ensure that intensities are always greater than zero,
which is needed when |
A tsd
object with simulated data containing:
'time': The time point for for when the observation is observed.
'observation': The observed value at the time point.
# Generate simulated data of seasonal waves #With default arguments default_sim <- generate_seasonal_data() plot(default_sim) #With an exponential growth rate trend trend_sim <- generate_seasonal_data(trend_rate = 1.001) plot(trend_sim) #With noise noise_sim <- generate_seasonal_data(noise_overdispersion = 2) plot(noise_sim) #With distinct parameters, trend and noise sim_data <- generate_seasonal_data( years = 2, start_date = as.Date("2022-05-26"), amplitude = 2000, mean = 3000, trend_rate = 1.002, noise_overdispersion = 1.1, time_interval = c("week") ) plot(sim_data, time_interval = "2 months")
# Generate simulated data of seasonal waves #With default arguments default_sim <- generate_seasonal_data() plot(default_sim) #With an exponential growth rate trend trend_sim <- generate_seasonal_data(trend_rate = 1.001) plot(trend_sim) #With noise noise_sim <- generate_seasonal_data(noise_overdispersion = 2) plot(noise_sim) #With distinct parameters, trend and noise sim_data <- generate_seasonal_data( years = 2, start_date = as.Date("2022-05-26"), amplitude = 2000, mean = 3000, trend_rate = 1.002, noise_overdispersion = 1.1, time_interval = c("week") ) plot(sim_data, time_interval = "2 months")
This function generates a complete 'ggplot' object suitable for visualizing time series data in
tsd
, tsd_onset
or tsd_onset_and_burden
objects.
## S3 method for class 'tsd' plot(x, ...) ## S3 method for class 'tsd_onset' plot(x, ...) ## S3 method for class 'tsd_onset_and_burden' plot(x, ...)
## S3 method for class 'tsd' plot(x, ...) ## S3 method for class 'tsd_onset' plot(x, ...) ## S3 method for class 'tsd_onset_and_burden' plot(x, ...)
x |
An |
... |
Additional arguments passed to |
A 'ggplot' object for visualizing output from desired method.
# set.seed(321) # Create and plot `tsd` object tsd_obj <- generate_seasonal_data( years = 3, phase = 1, start_date = as.Date("2021-10-18") ) plot(tsd_obj) disease_threshold <- 150 # Create and plot `tsd_onset` object tsd_onset_obj <- seasonal_onset( tsd = tsd_obj, k = 3, level = 0.95, disease_threshold = disease_threshold, family = "quasipoisson" ) plot(tsd_onset_obj) # Create a `tsd_onset_and_burden` object tsd_onset_burden_obj <- combined_seasonal_output( tsd = tsd_obj, disease_threshold = disease_threshold ) plot(tsd_onset_burden_obj, y_lower_bound = ifelse(disease_threshold < 10, 1, 5))
# set.seed(321) # Create and plot `tsd` object tsd_obj <- generate_seasonal_data( years = 3, phase = 1, start_date = as.Date("2021-10-18") ) plot(tsd_obj) disease_threshold <- 150 # Create and plot `tsd_onset` object tsd_onset_obj <- seasonal_onset( tsd = tsd_obj, k = 3, level = 0.95, disease_threshold = disease_threshold, family = "quasipoisson" ) plot(tsd_onset_obj) # Create a `tsd_onset_and_burden` object tsd_onset_burden_obj <- combined_seasonal_output( tsd = tsd_obj, disease_threshold = disease_threshold ) plot(tsd_onset_burden_obj, y_lower_bound = ifelse(disease_threshold < 10, 1, 5))
This function is used to predict future observations based on a tsd_onset
object.
It uses the time_interval
attribute from the tsd_onset
object to make predictions.
## S3 method for class 'tsd_onset' predict(object, n_step = 3, ...)
## S3 method for class 'tsd_onset' predict(object, n_step = 3, ...)
object |
A |
n_step |
An integer specifying the number of future time steps for which you want to predict observations. |
... |
Additional arguments (not used). |
A tibble-like object called tsd_predict
containing the predicted observations, including reference time,
lower confidence interval, and upper confidence interval for the specified number of future time steps.
# Generate predictions of time series data set.seed(123) time_series <- generate_seasonal_data( years = 1, time_interval = "day" ) # Apply `seasonal_onset` analysis time_series_with_onset <- seasonal_onset( tsd = time_series, k = 7 ) # Predict observations for the next 7 time steps predict(object = time_series_with_onset, n_step = 7)
# Generate predictions of time series data set.seed(123) time_series <- generate_seasonal_data( years = 1, time_interval = "day" ) # Apply `seasonal_onset` analysis time_series_with_onset <- seasonal_onset( tsd = time_series, k = 7 ) # Predict observations for the next 7 time steps predict(object = time_series_with_onset, n_step = 7)
This function estimates the burden levels of time series observations that are stratified by season. It uses the previous seasons to estimate the levels of the current season. The output is results regarding the current season in the time series observations. NOTE: The data must include data for a complete previous season to make predictions for the current season.
seasonal_burden_levels( tsd, family = c("lnorm", "weibull", "exp"), season_start = 21, season_end = season_start - 1, method = c("intensity_levels", "peak_levels"), conf_levels = 0.95, decay_factor = 0.8, disease_threshold = 20, n_peak = 6, only_current_season = TRUE, ... )
seasonal_burden_levels( tsd, family = c("lnorm", "weibull", "exp"), season_start = 21, season_end = season_start - 1, method = c("intensity_levels", "peak_levels"), conf_levels = 0.95, decay_factor = 0.8, disease_threshold = 20, n_peak = 6, only_current_season = TRUE, ... )
tsd |
An object containing time series data with 'time' and 'observation.' |
family |
A character string specifying the family for modeling |
season_start , season_end
|
Integers giving the start and end weeks of the seasons to stratify the observations by. |
method |
A character string specifying the model to be used in the level calculations. Both model predict the levels of the current series of observations.
|
conf_levels |
A numeric vector specifying the confidence levels for parameter estimates. The values have
to be unique and in ascending order, (i.e. the lowest level is first and highest level is last).
The
|
decay_factor |
A numeric value between 0 and 1, that specifies the weight applied to previous seasons in level
calculations. It is used as |
disease_threshold |
An integer specifying the threshold for considering a disease outbreak. It defines the per time-step disease threshold that has to be surpassed for the observation to be included in the level calculations. |
n_peak |
A numeric value specifying the number of peak observations to be selected from each season in the
level calculations. The |
only_current_season |
Should the output only include results for the current season? |
... |
Arguments passed to the |
A list containing:
'season': The season that burden levels are calculated for.
'high_conf_level': (only for intensity_level method) The conf_level chosen for the high level.
'conf_levels': (only for peak_level method) The conf_levels chosen to fit the 'low', 'medium', 'high' levels.
'values': A named vector with values for 'very low', 'low', 'medium', 'high' levels.
'par': The fit parameters for the chosen family.
par_1:
For 'weibull': Shape parameter.
For 'lnorm': Mean of the log-transformed observations.
For 'exp': Rate parameter.
'par_2':
For 'weibull': Scale parameter.
For 'lnorm': Standard deviation of the log-transformed observations.
For 'exp': Not applicable (set to NA).
'obj_value': The value of the objective function - (negative log-likelihood), which represent the minimized objective function value from the optimisation. Smaller value equals better optimisation.
'converged': Logical. TRUE if the optimisation converged.
'family': The distribution family used for the optimization.
'weibull': Uses the Weibull distribution for fitting.
'lnorm': Uses the Log-normal distribution for fitting.
'exp': Uses the Exponential distribution for fitting.
'disease_threshold': The input disease threshold, which is also the very low level.
# Generate random flu season generate_flu_season <- function(start = 1, end = 1000) { random_increasing_obs <- round(sort(runif(24, min = start, max = end))) random_decreasing_obs <- round(rev(random_increasing_obs)) # Generate peak numbers add_to_max <- c(50, 100, 200, 100) peak <- add_to_max + max(random_increasing_obs) # Combine into a single observations sequence observations <- c(random_increasing_obs, peak, random_decreasing_obs) return(observations) } season_1 <- generate_flu_season() season_2 <- generate_flu_season() start_date <- as.Date("2022-05-29") end_date <- as.Date("2024-05-20") weekly_dates <- seq.Date(from = start_date, to = end_date, by = "week") tsd_data <- tsd( observation = c(season_1, season_2), time = as.Date(weekly_dates), time_interval = "week" ) # Print seasonal burden results seasonal_burden_levels(tsd_data, family = "lnorm")
# Generate random flu season generate_flu_season <- function(start = 1, end = 1000) { random_increasing_obs <- round(sort(runif(24, min = start, max = end))) random_decreasing_obs <- round(rev(random_increasing_obs)) # Generate peak numbers add_to_max <- c(50, 100, 200, 100) peak <- add_to_max + max(random_increasing_obs) # Combine into a single observations sequence observations <- c(random_increasing_obs, peak, random_decreasing_obs) return(observations) } season_1 <- generate_flu_season() season_2 <- generate_flu_season() start_date <- as.Date("2022-05-29") end_date <- as.Date("2024-05-20") weekly_dates <- seq.Date(from = start_date, to = end_date, by = "week") tsd_data <- tsd( observation = c(season_1, season_2), time = as.Date(weekly_dates), time_interval = "week" ) # Print seasonal burden results seasonal_burden_levels(tsd_data, family = "lnorm")
This function performs automated and early detection of seasonal epidemic onsets on a time series dataset. It estimates growth rates for consecutive time intervals and calculates the sum of cases (sum_of_cases).
seasonal_onset( tsd, k = 5, level = 0.95, disease_threshold = NA_integer_, family = c("poisson", "quasipoisson"), na_fraction_allowed = 0.4, season_start = NULL, season_end = season_start - 1, only_current_season = NULL )
seasonal_onset( tsd, k = 5, level = 0.95, disease_threshold = NA_integer_, family = c("poisson", "quasipoisson"), na_fraction_allowed = 0.4, season_start = NULL, season_end = season_start - 1, only_current_season = NULL )
tsd |
An object containing time series data with 'time' and 'observation.' |
k |
An integer specifying the window size for modeling growth rates for the onset. |
level |
The confidence level for onset parameter estimates, a numeric value between 0 and 1. |
disease_threshold |
An integer specifying the threshold for considering a disease outbreak. It defines the per time-step disease threshold that has to be surpassed to possibly trigger a seasonal
onset alarm. If the total number of cases in a window of size k exceeds |
family |
A character string specifying the family for modeling |
na_fraction_allowed |
Numeric value between 0 and 1 specifying the fraction of observables in the window of size k that are allowed to be NA or zero, i.e. without cases, in onset calculations. |
season_start , season_end
|
Integers giving the start and end weeks of the seasons to
stratify the observations by. If set to |
only_current_season |
Should the output only include results for the current season? |
A seasonal_onset
object containing:
'reference_time': The time point for which the growth rate is estimated.
'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 interval.
'upper_growth_rate': The upper bound of the growth rate's confidence interval.
'growth_warning': Logical. Is the growth rate significantly higher than zero?
'sum_of_cases': The sum of cases within the time window.
'sum_of_cases_warning': Logical. Does the Sum of Cases exceed the disease threshold?
'seasonal_onset_alarm': Logical. Is there a seasonal onset alarm?
'skipped_window': Logical. Was the window skipped due to missing?
'converged': Logical. Was the IWLS judged to have converged? - 'seasonal_onset': Logical. The first detected seasonal onset in the season?
# Create a tibble object from sample data tsd_data <- tsd( observation = 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" ) # Estimate seasonal onset with a 3-day window and a Poisson family model seasonal_onset( tsd = tsd_data, k = 3, level = 0.95, disease_threshold = 20, family = "poisson", na_fraction_allowed = 0.4, season_start = NULL, season_end = NULL, only_current_season = NULL )
# Create a tibble object from sample data tsd_data <- tsd( observation = 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" ) # Estimate seasonal onset with a 3-day window and a Poisson family model seasonal_onset( tsd = tsd_data, k = 3, level = 0.95, disease_threshold = 20, family = "poisson", na_fraction_allowed = 0.4, season_start = NULL, season_end = NULL, only_current_season = NULL )
tsd_burden_levels
objectsSummarize key results from a seasonal burden levels analysis.
## S3 method for class 'tsd_burden_levels' summary(object, ...)
## S3 method for class 'tsd_burden_levels' summary(object, ...)
object |
An object of class 'tsd_burden_levels'
containing the results of a |
... |
Additional arguments (not used). |
This function is used for its side effect, which is printing the burden levels.
# Create a `tsd` object tsd_data <- generate_seasonal_data() # Create a `tsd_burden_levels` object tsd_burden_levels <- seasonal_burden_levels( tsd = tsd_data ) # Print the summary summary(tsd_burden_levels)
# Create a `tsd` object tsd_data <- generate_seasonal_data() # Create a `tsd_burden_levels` object tsd_burden_levels <- seasonal_burden_levels( tsd = tsd_data ) # Print the summary summary(tsd_burden_levels)
tsd_onset
objectsSummarize key results from a seasonal onset analysis.
## S3 method for class 'tsd_onset' summary(object, ...)
## S3 method for class 'tsd_onset' summary(object, ...)
object |
An object of class 'tsd_onset'
containing the results of a |
... |
Additional arguments (not used). |
This function is used for its side effect, which is printing a summary message to the console.
# Create a `tsd` object tsd_data <- generate_seasonal_data() # Create a `tsd_onset` object tsd_onset <- seasonal_onset( tsd = tsd_data, k = 3, disease_threshold = 100, season_start = 21, season_end = 20, level = 0.95, family = "poisson", only_current_season = TRUE ) # Print the summary summary(tsd_onset)
# Create a `tsd` object tsd_data <- generate_seasonal_data() # Create a `tsd_onset` object tsd_onset <- seasonal_onset( tsd = tsd_data, k = 3, disease_threshold = 100, season_start = 21, season_end = 20, level = 0.95, family = "poisson", only_current_season = TRUE ) # Print the summary summary(tsd_onset)
tsd
(time-series data) object from observed data and corresponding dates.This function takes observations and the corresponding date vector and converts it into a tsd
object, which is
a time series data structure that can be used for time series analysis.
to_time_series(observation, time, time_interval = c("day", "week", "month"))
to_time_series(observation, time, time_interval = c("day", "week", "month"))
observation |
A numeric vector containing the observations. |
time |
A date vector containing the corresponding dates. |
time_interval |
A character vector specifying the time interval. Choose between 'day', 'week', or 'month'. |
A tsd
object containing:
'time': The time point for for when the observation is observed.
'observation': The observed value at the time point.
# Create a `tsd` object from daily data daily_tsd <- to_time_series( observation = c(10, 15, 20, 18), time = as.Date( c("2023-01-01", "2023-01-02", "2023-01-03", "2023-01-04") ), time_interval = "day" ) # Create a `tsd` object from weekly data weekly_tsd <- to_time_series( observation = c(100, 120, 130), time = as.Date( c("2023-01-01", "2023-01-08", "2023-01-15") ), time_interval = "week" ) # Create a `tsd` object from monthly data monthly_tsd <- to_time_series( observation = c(500, 520, 540), time = as.Date( c("2023-01-01", "2023-02-01", "2023-03-01") ), time_interval = "month" )
# Create a `tsd` object from daily data daily_tsd <- to_time_series( observation = c(10, 15, 20, 18), time = as.Date( c("2023-01-01", "2023-01-02", "2023-01-03", "2023-01-04") ), time_interval = "day" ) # Create a `tsd` object from weekly data weekly_tsd <- to_time_series( observation = c(100, 120, 130), time = as.Date( c("2023-01-01", "2023-01-08", "2023-01-15") ), time_interval = "week" ) # Create a `tsd` object from monthly data monthly_tsd <- to_time_series( observation = c(500, 520, 540), time = as.Date( c("2023-01-01", "2023-02-01", "2023-03-01") ), time_interval = "month" )