Skip to content

Commit

Permalink
Applied the styler package to all functions
Browse files Browse the repository at this point in the history
  • Loading branch information
telkamp7 committed Oct 20, 2023
1 parent 99ac67b commit 111b90b
Show file tree
Hide file tree
Showing 8 changed files with 115 additions and 110 deletions.
27 changes: 12 additions & 15 deletions R/aedseo.R
Original file line number Diff line number Diff line change
Expand Up @@ -39,18 +39,18 @@
#' "2023-01-03",
#' "2023-01-04",
#' "2023-01-05",
#' "2023-01-06")
#' ),
#' time_interval = "day"
#' )
#' "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 AEDSEO results
#' print(aedseo_results)
Expand All @@ -61,8 +61,8 @@ aedseo <- function(
level = 0.95,
family = c(
"poisson",
"quasipoisson")) {

"quasipoisson"
)) {
# Throw an error if any of the inputs are not supported
family <- rlang::arg_match(family)

Expand All @@ -72,8 +72,7 @@ aedseo <- function(
# Allocate space for growth rate estimates
res <- tibble::tibble()

for (i in k:n){

for (i in k:n) {
# Index observations for this iteration
obs_iter <- tsd[(i - k + 1):i, ]

Expand All @@ -82,7 +81,7 @@ aedseo <- function(
observations = obs_iter$observed,
level = level,
family = family
)
)

# See if the growth rate is significantly higher than zero
growth_warning <- growth_rates$estimate[2] > 0
Expand All @@ -96,16 +95,14 @@ aedseo <- function(
tibble::tibble(
reference_time = tsd$time[i],
growth_rate = growth_rates$estimate[1],
lower_growth_rate = growth_rates$estimate[2],
upper_growth_rate = growth_rates$estimate[3],
lower_growth_rate = growth_rates$estimate[2],
upper_growth_rate = growth_rates$estimate[3],
growth_warning = growth_warning,
SoC = SoC,
converged = growth_rates$fit$converged
)
)

)
}

return(res)

}
25 changes: 13 additions & 12 deletions R/tsd.R
Original file line number Diff line number Diff line change
Expand Up @@ -19,34 +19,35 @@
#' daily_tsd <- tsd(
#' observed = c(10, 15, 20, 18),
#' time = as.Date(
#' c("2023-01-01", "2023-01-02", "2023-01-03", "2023-01-04")),
#' c("2023-01-01", "2023-01-02", "2023-01-03", "2023-01-04")
#' ),
#' time_interval = "day"
#' )
#' )
#'
#' # Create a tsibble object from weekly data
#' weekly_tsd <- tsd(
#' observed = c(100, 120, 130),
#' time = as.Date(
#' c("2023-01-01", "2023-01-08", "2023-01-15")),
#' time_interval = "week"
#' )
#' c("2023-01-01", "2023-01-08", "2023-01-15")
#' ),
#' time_interval = "week"
#' )
#'
#' # Create a tsibble object from monthly data
#' monthly_tsd <- tsd(
#' observed = c(500, 520, 540),
#' time = as.Date(
#' c("2023-01-01", "2023-02-01", "2023-03-01")),
#' time_interval = "month"
#' )
#' 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(observed, time, time_interval = c("day", "week", "month")) {
# Throw an error if any of the inputs are not supported
time_interval <- rlang::arg_match(time_interval)

# Select the correct 'time_interval' and create the 'tsd' return object
ans <- switch(
time_interval,
ans <- switch(time_interval,
day = tibble(time = time, observed = observed) %>%
tsibble::build_tsibble(
index = time,
Expand Down
6 changes: 4 additions & 2 deletions man/aedseo.Rd

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

23 changes: 15 additions & 8 deletions man/fit_growth_rate.Rd

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

12 changes: 6 additions & 6 deletions tests/testthat/test-aedseo.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
test_that("The growth rate models converge", {

from_date <- as.Date("2021-01-01")
to_date <- as.Date("2021-01-31")

Expand All @@ -13,28 +12,29 @@ test_that("The growth rate models converge", {
tsd_data_poisson <- tsd(
observed = rpois(n = n, lambda = 1:n),
time = time,
time_interval = "day")
time_interval = "day"
)
tsd_data_nbinom <- tsd(
observed = rnbinom(n = n, mu = 1:n, size = 5),
time = time,
time_interval = "day")
time_interval = "day"
)

# Calculate AEDSEO with a 3-day window
aedseo_poisson <- aedseo(
tsd = tsd_data_poisson,
k = 3,
level = 0.95,
family = "poisson"
)
)
aedseo_quasipoisson <- aedseo(
tsd = tsd_data_nbinom,
k = 3,
level = 0.95,
family = "quasipoisson"
)
)

# Check if they all converge
expect_true(object = all(aedseo_poisson$converged))
expect_true(object = all(aedseo_quasipoisson$converged))

})
6 changes: 2 additions & 4 deletions tests/testthat/test-fit_growth_rate.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
test_that("The growth rate models converge", {

# Number of random data points to generate
n <- 1e3

Expand All @@ -15,15 +14,14 @@ test_that("The growth rate models converge", {
observations = data_poisson,
level = 0.95,
family = "poisson"
)
)
fit_quasipoisson <- fit_growth_rate(
observations = data_nbinom,
level = 0.95,
family = "quasipoisson"
)
)

# Check if they all converge
expect_true(object = fit_poisson$fit$converged)
expect_true(object = fit_quasipoisson$fit$converged)

})
Loading

0 comments on commit 111b90b

Please sign in to comment.