From d1ef276c09840d578443826401ad9501919498b4 Mon Sep 17 00:00:00 2001 From: "Steven Paul Sanderson II, MPH" Date: Thu, 1 Aug 2024 08:54:12 -0400 Subject: [PATCH 1/5] Update gen-brown-motion-geometric.R --- R/gen-brown-motion-geometric.R | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/R/gen-brown-motion-geometric.R b/R/gen-brown-motion-geometric.R index 4b08ce0..f3b0ce3 100644 --- a/R/gen-brown-motion-geometric.R +++ b/R/gen-brown-motion-geometric.R @@ -98,6 +98,22 @@ geometric_brownian_motion <- function(.num_walks = 25, .n = 100, ) } + # .mu and .sigma and .detla_time must be >= 0 + if (mu < 0 | sigma < 0 | delta_time < 0){ + rlang::abort( + message = "The parameters of `.mu`, `.sigma`, and `.delta_time` must be >= 0.", + use_cli_format = TRUE + ) + } + + # .num_walks and .n must be >= 1 + if (num_sims < 1 | t < 1){ + rlang::abort( + message = "The parameters of `.num_walks` and `.n` must be >= 1.", + use_cli_format = TRUE + ) + } + # matrix of random draws - one for each day for each simulation rand_matrix <- matrix(stats::rnorm(t * num_sims), ncol = num_sims, nrow = t) colnames(rand_matrix) <- 1:num_sims From a0826a274536d3a226474f7eb23cbf839e738eec Mon Sep 17 00:00:00 2001 From: "Steven Paul Sanderson II, MPH" Date: Thu, 1 Aug 2024 09:48:08 -0400 Subject: [PATCH 2/5] Update gen-brown-motion.R --- R/gen-brown-motion.R | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/R/gen-brown-motion.R b/R/gen-brown-motion.R index 060f534..f279d64 100644 --- a/R/gen-brown-motion.R +++ b/R/gen-brown-motion.R @@ -55,7 +55,7 @@ NULL #' @rdname brownian_motion brownian_motion <- function(.num_walks = 25, .n = 100, .delta_time = 1, - .initial_value = 0, .return_tibble = TRUE) { + .initial_value = 0, .return_tibble = TRUE) { # Tidyeval ---- num_sims <- as.numeric(.num_walks) @@ -73,6 +73,22 @@ brownian_motion <- function(.num_walks = 25, .n = 100, .delta_time = 1, ) } + # .num_walks and .n must be >= 1 + if (num_sims < 1 | t < 1){ + rlang::abort( + message = "The parameters of `.num_walks` and `.n` must be >= 1.", + use_cli_format = TRUE + ) + } + + # .delta_time must be > 0 + if (delta_time <= 0){ + rlang::abort( + message = "The parameter `.delta_time` must be > 0.", + use_cli_format = TRUE + ) + } + if (!is.logical(return_tibble)){ rlang::abort( message = "The parameter `.return_tibble` must be either TRUE/FALSE", From 339d4ec11b3653d9cbe18798df876b5a443e685a Mon Sep 17 00:00:00 2001 From: "Steven Paul Sanderson II, MPH" Date: Thu, 1 Aug 2024 09:59:15 -0400 Subject: [PATCH 3/5] Update gen-discrete-walk.R --- R/gen-discrete-walk.R | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/R/gen-discrete-walk.R b/R/gen-discrete-walk.R index 8c1fd98..d6d9dfe 100644 --- a/R/gen-discrete-walk.R +++ b/R/gen-discrete-walk.R @@ -62,6 +62,31 @@ discrete_walk <- function(.num_walks = 25, .n = 100, .upper_bound = 1, lower_probability <- 1 - upper_probability initial_value <- as.numeric(.initial_value) + # Checks + if (!is.integer(num_walks) | num_walks < 1) { + stop("The number of walks must be an integer greater than 0.") + } + + if (!is.integer(periods) | periods < 1) { + stop("The number of periods must be an integer greater than 0.") + } + + if (!is.numeric(upper_bound)) { + stop("The upper bound must be a numeric value.") + } + + if (!is.numeric(lower_bound)) { + stop("The lower bound must be a numeric value.") + } + + if (!is.numeric(upper_probability) | upper_probability < 0 | upper_probability > 1) { + stop("The upper probability must be a numeric value between 0 and 1.") + } + + if (!is.numeric(initial_value)) { + stop("The initial value must be a numeric value.") + } + res <- tidyr::expand_grid(walk_number = factor(1:num_walks), x = 1:periods) |> dplyr::group_by(walk_number) |> dplyr::mutate(y = replicate( From a01dae9f80f0ca39f5ba472a65e1f3f88793d705 Mon Sep 17 00:00:00 2001 From: "Steven Paul Sanderson II, MPH" Date: Thu, 1 Aug 2024 10:06:20 -0400 Subject: [PATCH 4/5] Update gen-discrete-walk.R --- R/gen-discrete-walk.R | 30 ++++++++++++++++++++++++------ 1 file changed, 24 insertions(+), 6 deletions(-) diff --git a/R/gen-discrete-walk.R b/R/gen-discrete-walk.R index d6d9dfe..9a161f5 100644 --- a/R/gen-discrete-walk.R +++ b/R/gen-discrete-walk.R @@ -64,27 +64,45 @@ discrete_walk <- function(.num_walks = 25, .n = 100, .upper_bound = 1, # Checks if (!is.integer(num_walks) | num_walks < 1) { - stop("The number of walks must be an integer greater than 0.") + rlang::abort( + message = "The number of walks must be an integer greater than 0.", + use_cli_format = TRUE + ) } if (!is.integer(periods) | periods < 1) { - stop("The number of periods must be an integer greater than 0.") + rlang::abort( + message = "The number of periods must be an integer greater than 0.", + use_cli_format = TRUE + ) } if (!is.numeric(upper_bound)) { - stop("The upper bound must be a numeric value.") + rlang::abort( + message = "The upper bound must be a numeric value.", + use_cli_format = TRUE + ) } if (!is.numeric(lower_bound)) { - stop("The lower bound must be a numeric value.") + rlang::abort( + message = "The lower bound must be a numeric value.", + use_cli_format = TRUE + ) } if (!is.numeric(upper_probability) | upper_probability < 0 | upper_probability > 1) { - stop("The upper probability must be a numeric value between 0 and 1.") + rlang::abort( + message = "The upper probability must be a numeric value between 0 and 1.", + use_cli_format = TRUE + ) } if (!is.numeric(initial_value)) { - stop("The initial value must be a numeric value.") + rlang::abort( + message = "The initial value must be a numeric value.", + use_cli_format = TRUE + ) } res <- tidyr::expand_grid(walk_number = factor(1:num_walks), x = 1:periods) |> From 2a76303693eaffd74de2c7c374551228721d5239 Mon Sep 17 00:00:00 2001 From: "Steven Paul Sanderson II, MPH" Date: Thu, 1 Aug 2024 10:06:24 -0400 Subject: [PATCH 5/5] Update gen-random-normal-walk-drift.R --- R/gen-random-normal-walk-drift.R | 44 ++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/R/gen-random-normal-walk-drift.R b/R/gen-random-normal-walk-drift.R index 2d956b5..6a497ae 100644 --- a/R/gen-random-normal-walk-drift.R +++ b/R/gen-random-normal-walk-drift.R @@ -47,6 +47,7 @@ NULL random_normal_drift_walk <- function(.num_walks = 25, .n = 100, .mu = 0, .sd = 1, .drift = 0.1, .initial_value = 0) { + # Convert inputs to appropriate types num_walks <- as.integer(.num_walks) num_steps <- as.integer(.n) mu <- as.numeric(.mu) @@ -55,6 +56,49 @@ random_normal_drift_walk <- function(.num_walks = 25, .n = 100, .mu = 0, initial_value <- as.numeric(.initial_value) dr <- seq(from = drift, to = drift * num_steps, length.out = num_steps) + # Checks + if (num_walks <= 0) { + rlang::abort( + message = "Number of walks must be a positive integer.", + use_cli = TRUE + ) + } + + if (num_steps <= 0) { + rlang::abort( + message = "Number of steps must be a positive integer.", + use_cli = TRUE + ) + } + + if (sd <= 0) { + rlang::abort( + message = "Standard deviation must be a positive number.", + use_cli = TRUE + ) + } + + if (is.na(mu)) { + rlang::abort( + message = "Mean must be a number.", + use_cli = TRUE + ) + } + + if (is.na(drift)) { + rlang::abort( + message = "Drift must be a number.", + use_cli = TRUE + ) + } + + if (is.na(initial_value)) { + rlang::abort( + message = "Initial value must be a number.", + use_cli = TRUE + ) + } + # Function to generate a single random walk single_random_walk_with_drift <- function(num_steps, mu, sd, drift) { wn <- stats::rnorm(n = num_steps, mean = mu, sd = sd)