-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #10 from spsanderson/development
Fixes #9
- Loading branch information
Showing
8 changed files
with
120 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
# Generated by roxygen2: do not edit by hand | ||
|
||
export(rw30) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
globalVariables( | ||
names = c( | ||
"walk","x","value" | ||
) | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
#' Generate Random Walks | ||
#' | ||
#' @family Auto Random Walk | ||
#' | ||
#' @author Steven P. Sanderson II, MPH | ||
#' | ||
#' This function generates 30 random walks with 100 steps each and pivots the | ||
#' result into a long format tibble. | ||
#' | ||
#' @details | ||
#' The function generates random walks using the normal distribution with a | ||
#' specified mean (`mu`) and standard deviation (`sd`). | ||
#' Each walk is generated independently and stored in a tibble. The resulting | ||
#' tibble is then pivoted into a long format for easier analysis. | ||
#' | ||
#' @return A tibble in long format with columns `walk`, `x`, and `value`, | ||
#' representing the random walks. Additionally, attributes `num_walks`, | ||
#' `num_steps`, `mu`, and `sd` are attached to the tibble. | ||
#' | ||
#' @examples | ||
#' # Generate random walks and print the result | ||
#' rw30() | ||
#' | ||
#' @name rw30 | ||
NULL | ||
#' @rdname rw30 | ||
#' @export | ||
rw30 <- function() { | ||
|
||
num_walks <- 30L | ||
num_steps <- 100L | ||
mu <- 0L | ||
sd <- 1L | ||
|
||
# Function to generate a single random walk | ||
single_random_walk <- function(n, mu, sd) { | ||
cumsum(c(0, stats::rnorm(n - 1, mu, sd))) | ||
} | ||
|
||
# Generate the walks | ||
walks <- replicate( | ||
num_walks, | ||
single_random_walk(num_steps, mu, sd), | ||
simplify = FALSE | ||
) | ||
|
||
# Create a tibble with the walks | ||
walks_tibble <- dplyr::tibble( | ||
x = 1:num_steps, | ||
!!!stats::setNames(walks, paste0("walk_", 1:num_walks)) | ||
) | ||
|
||
# Pivot the tibble longer | ||
walks_long <- tidyr::pivot_longer( | ||
walks_tibble, | ||
cols = dplyr::starts_with("walk_"), | ||
names_to = "walk", | ||
values_to = "value" | ||
) |> | ||
dplyr::arrange(walk, x) |> | ||
dplyr::select(walk, x, value) |> | ||
dplyr::mutate(walk = factor(walk)) | ||
|
||
attr(walks_long, "num_walks") <- num_walks | ||
attr(walks_long, "num_steps") <- num_steps | ||
attr(walks_long, "mu") <- mu | ||
attr(walks_long, "sd") <- sd | ||
|
||
return(walks_long) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.