-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
Fixes #44
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
#' Random Walk Helper | ||
#' | ||
#' @family Helper Functions | ||
#' | ||
#' @author Steven P. Sanderson II, MPH | ||
#' | ||
#' @details | ||
#' A function to help build random walks by mutating a data frame. This mutation | ||
#' adds the following columns to the data frame: `cum_sum`, `cum_prod`, `cum_min`, | ||
#' `cum_max`, and `cum_mean`. The function is used internally by certain functions | ||
#' that generate random walks. | ||
#' | ||
#' @description | ||
#' A function to help build random walks by mutating a data frame. | ||
#' | ||
#' @param .data The data frame to mutate. | ||
#' @param .value The .initial_value to use. This is passed from the random walk | ||
#' function being called by the end user. | ||
#' | ||
#' @examples | ||
#' df <- tibble( | ||
#' walk_number = factor(rep(1L:25L, each = 30L)), | ||
#' x = rep(1L:30L, 25L), | ||
#' y = rnorm(750L, 0L, 1L) | ||
#' ) | ||
#' | ||
#' internal_rand_walk_helper(df, 100) | ||
#' | ||
#' @return | ||
#' A modified data frame/tibble with the following columns added: | ||
#' \itemize{ | ||
#' \item `cum_sum`: Cumulative sum of `y`. | ||
#' \item `cum_prod`: Cumulative product of `y`. | ||
#' \item `cum_min`: Cumulative minimum of `y`. | ||
#' \item `cum_max`: Cumulative maximum of `y`. | ||
#' \item `cum_mean`: Cumulative mean of `y`. | ||
#' } | ||
#' | ||
#' @name internal_rand_walk_helper | ||
NULL | ||
#' @rdname internal_rand_walk_helper | ||
#' @export | ||
|
||
internal_rand_walk_helper <- function(.data, .value) { | ||
|
||
value = as.numeric(.value) | ||
|
||
df <- .data |> | ||
dplyr::group_by(walk_number) |> | ||
dplyr::mutate( | ||
cum_sum = initial_value + cumsum(y), | ||
cum_prod = initial_value * cumprod(1 + y), | ||
cum_min = initial_value + cummin(y), | ||
cum_max = initial_value + cummax(y), | ||
cum_mean = initial_value + cmean(y) | ||
) |> | ||
dplyr::ungroup() | ||
|
||
return(df) | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.