Skip to content

Commit

Permalink
Styled and linted the package
Browse files Browse the repository at this point in the history
  • Loading branch information
telkamp7 committed Oct 20, 2023
1 parent 111b90b commit 0b400c7
Show file tree
Hide file tree
Showing 6 changed files with 140 additions and 53 deletions.
10 changes: 5 additions & 5 deletions R/aedseo.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#'
#' 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 (SoC).
#' consecutive time intervals and calculates the Sum of Cases (sum_of_cases).
#'
#' @param tsd A tsibble object containing time series data with 'time,'
#' 'observed,' and 'periodInYear.'
Expand All @@ -24,7 +24,7 @@
#' interval.
#' - 'growth_warning': Logical. Is the growth rate significantly higher than
#' zero?
#' - 'SoC': The Sum of Cases within the time window.
#' - 'sum_of_cases': The Sum of Cases within the time window.
#' - 'converged': Logical. Was the IWLS judged to have converged?
#'
#' @export
Expand Down Expand Up @@ -86,8 +86,8 @@ aedseo <- function(
# See if the growth rate is significantly higher than zero
growth_warning <- growth_rates$estimate[2] > 0

# Calculate Sum of Cases (SoC)
SoC <- base::sum(obs_iter$observed)
# Calculate Sum of Cases (sum_of_cases)
sum_of_cases <- base::sum(obs_iter$observed)

# Collect the results
res <- dplyr::bind_rows(
Expand All @@ -98,7 +98,7 @@ aedseo <- function(
lower_growth_rate = growth_rates$estimate[2],
upper_growth_rate = growth_rates$estimate[3],
growth_warning = growth_warning,
SoC = SoC,
sum_of_cases = sum_of_cases,
converged = growth_rates$fit$converged
)
)
Expand Down
15 changes: 10 additions & 5 deletions R/tsd.R
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
#' Create a tsibble (time-series data) object from observed data and corresponding dates.
#' Create a tsibble (time-series data) object from observed data and
#' corresponding dates.
#'
#' @description
#' `r lifecycle::badge("experimental")`
#'
#' This function takes observed data and the corresponding date vector and converts it into a tsibble object,
#' which is a time series data structure that can be used for time series analysis.
#' This function takes observed data and the corresponding date vector and
#' converts it into a tsibble 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 time A date vector containing the corresponding dates.
#' @param time_interval A character vector specifying the time interval, choose between "day," "week," or "month."
#' @param time_interval A character vector specifying the time interval.
#' Choose between "day," "week," or "month."
#'
#' @return A tsibble object containing time, the observations, and the periodInYear.
#' @return A tsibble object containing time, the observations, and the
#' periodInYear.
#'
#' @export
#'
Expand Down
14 changes: 7 additions & 7 deletions man/aedseo.Rd

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

34 changes: 21 additions & 13 deletions man/tsd.Rd

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

38 changes: 30 additions & 8 deletions vignettes/aedseo_introduction.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,15 @@ To assess the effectiveness of the `aedseo` function, we simulate some data. The
\end{equation}

```{r}
mu_t <- function(t, theta = 1, exp_beta = 1.001, gamma_sin = 1, gamma_cos = 1, trend = 1, j = 1, ...) {
mu_t <- function(
t,
theta = 1,
exp_beta = 1.001,
gamma_sin = 1,
gamma_cos = 1,
trend = 1,
j = 1,
...) {
# Start construction of linear predictor
linear_predictor <- theta
# ... add a trend if the scenario request it
Expand Down Expand Up @@ -74,7 +82,8 @@ simulate_from_nbinom <- function(...) {
# Match call
mc <- as.list(match.call())[-1]
# ... and change parameters relative to the call
mc <- append(mc, default_pars[!names(default_pars) %in% names(mc)])[names(default_pars)]
index_mc <- !names(default_pars) %in% names(mc)
mc <- append(mc, default_pars[index_mc])[names(default_pars)]
# Set the seed
set.seed(mc$seed)
Expand All @@ -99,20 +108,24 @@ weeks <- 52
# ... calculate the total number of observations
n <- years * weeks
# ... and a vector containing the overall time passed
timeOverall <- 1:n
time_overall <- 1:n
# Create arbitrary dates
dates <- as.POSIXct(
x = timeOverall * 86400 * 7,
x = time_overall * 86400 * 7,
origin = "2010-01-01",
tz = "UTC",
format = "%F"
)
# Simulate the data
simulation <- simulate_from_nbinom(t = timeOverall, theta = log(1000), phi = 5)
simulation <- simulate_from_nbinom(t = time_overall, theta = log(1000), phi = 5)
# Collect the data in a 'tibble'
sim_data <- tibble(Date = dates, mu_t = simulation$mu_t, y = simulation$simulation)
sim_data <- tibble(
Date = dates,
mu_t = simulation$mu_t,
y = simulation$simulation
)
```

A total of three years of weekly data are then simulated with the following set of parameters:
Expand Down Expand Up @@ -149,15 +162,24 @@ Next, the time series data object is passed to the `aedseo()` function. Here, a

```{r}
# Apply the 'aedseo' algorithm
aedseo_results <- aedseo(tsd = tsd_data, k = 5, level = 0.95, family = "quasipoisson")
aedseo_results <- aedseo(
tsd = tsd_data,
k = 5,
level = 0.95,
family = "quasipoisson"
)
```

In the figure below, the observed values from the simulations is visualized alongside the local estimate of the growth rate and its corresponding 95\% confidence interval.


```{r}
# Join the observations and estimated growth rates
full_data <- full_join(x = tsd_data, y = aedseo_results, by = join_by("time" == "reference_time"))
full_data <- full_join(
x = tsd_data,
y = aedseo_results,
by = join_by("time" == "reference_time")
)
# Data to add horizontal line in growth rate
ablines <- tibble(name = c("growth_rate", "observed"), x = c(0, NA))
Expand Down
Loading

0 comments on commit 0b400c7

Please sign in to comment.