Skip to content

Commit

Permalink
Merge pull request #47 from spsanderson/development
Browse files Browse the repository at this point in the history
Fixes #44
  • Loading branch information
spsanderson authored Jul 29, 2024
2 parents 9f65f98 + 0dc39c4 commit cfae8c5
Show file tree
Hide file tree
Showing 48 changed files with 340 additions and 75 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export(cskewness)
export(cvar)
export(discrete_walk)
export(geometric_brownian_motion)
export(internal_rand_walk_helper)
export(kurtosis_vec)
export(random_normal_drift_walk)
export(random_normal_walk)
Expand Down
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ None
4. Fix #16 - Add Function `brownian_motion()` to generate Brownian Motion
5. Fix #13 - Add Function `random_normal_walk()` to generate Random Walk
6. Fix #30 - Add Function `discrete_walk()` to generate Discrete Random Walk
7. Fix #43 - Add vectorized functions
8. Fix #44 - Add Function `internal_rand_walk_helper()` to help generate common
columns for random walks.

## Minor Improvements and Fixes
None
File renamed without changes.
2 changes: 1 addition & 1 deletion R/gen-discrete-walk.R
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ discrete_walk <- function(.num_walks = 25, .n = 100, .upper_bound = 1,
x = c(upper_bound, lower_bound),
size = 1,
prob = c(upper_probability, lower_probability))
)
)
) |>
dplyr::mutate(cum_sum = initial_value + cumsum(y)) |>
dplyr::mutate(cum_prod = initial_value * cumprod(1 + y)) |>
Expand Down
File renamed without changes.
60 changes: 60 additions & 0 deletions R/helpers.R
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)
}
2 changes: 1 addition & 1 deletion R/vec-functions.R
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,7 @@ rw_range <- function(.x) {
#' @return
#' A numeric vector
#'
#' @name name
#' @name crange
NULL
#' @rdname crange
#' @export
Expand Down
4 changes: 4 additions & 0 deletions _pkgdown.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,9 @@ reference:
desc: Functions that generate random walks in vector form
contents:
- has_concept("Vector Function")
- title: Helpers
desc: Functions that help with random walks
contents:
- has_concept("Helper Functions")

search:
2 changes: 2 additions & 0 deletions docs/news/index.html

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

2 changes: 1 addition & 1 deletion docs/pkgdown.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ pkgdown: 2.1.0
pkgdown_sha: ~
articles:
getting-started: getting-started.html
last_built: 2024-07-29T14:42Z
last_built: 2024-07-29T16:17Z
urls:
reference: https://www.spsanderson.com/RandomWalker/reference
article: https://www.spsanderson.com/RandomWalker/articles
2 changes: 1 addition & 1 deletion docs/reference/cgmean.html

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

2 changes: 1 addition & 1 deletion docs/reference/chmean.html

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

2 changes: 1 addition & 1 deletion docs/reference/ckurtosis.html

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

2 changes: 1 addition & 1 deletion docs/reference/cmean.html

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

2 changes: 1 addition & 1 deletion docs/reference/cmedian.html

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

114 changes: 114 additions & 0 deletions docs/reference/crange.html

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

2 changes: 1 addition & 1 deletion docs/reference/csd.html

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

2 changes: 1 addition & 1 deletion docs/reference/cskewness.html

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

2 changes: 1 addition & 1 deletion docs/reference/cvar.html

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

2 changes: 1 addition & 1 deletion docs/reference/geometric_brownian_motion.html

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

Loading

0 comments on commit cfae8c5

Please sign in to comment.