Skip to contents

The custom_walk function generates multiple random walks in 1, 2, or 3 dimensions using a user-provided custom function. Each walk is a sequence of steps where each step is generated by calling the provided custom function. This allows users to create random walks with any type of step distribution.

Usage

custom_walk(
  .n = 100,
  .num_walks = 25,
  .initial_value = 0,
  .dimensions = 1,
  .custom_fns
)

Arguments

.n

An integer specifying the number of steps in each walk. Default is 100.

.num_walks

An integer specifying the number of random walks to generate. Default is 25.

.initial_value

A numeric value indicating the initial value of the walks. Default is 0.

.dimensions

An integer specifying the number of dimensions (1, 2, or 3). Default is 1.

.custom_fns

A function that generates a single random step. The function should take no parameters and return a single numeric value. This function will be called repeatedly to generate steps for the random walks.

Value

A tibble containing the generated random walks with columns depending on the number of dimensions:

  • walk_number: Factor representing the walk number.

  • step_number: Step index.

  • y: If .dimensions = 1, the value of the walk at each step.

  • x, y: If .dimensions = 2, the values of the walk in two dimensions.

  • x, y, z: If .dimensions = 3, the values of the walk in three dimensions.

The following are also returned based upon how many dimensions there are and could be any of x, y and or z:

  • cum_sum: Cumulative sum of dplyr::all_of(.dimensions).

  • cum_prod: Cumulative product of dplyr::all_of(.dimensions).

  • cum_min: Cumulative minimum of dplyr::all_of(.dimensions).

  • cum_max: Cumulative maximum of dplyr::all_of(.dimensions).

  • cum_mean: Cumulative mean of dplyr::all_of(.dimensions).

The tibble includes attributes for the function parameters.

Details

This function provides a flexible way to generate random walks using any custom random number generation function. The custom function should take no parameters and return a single numeric value representing the step displacement. The function will be called repeatedly to generate the required number of steps for each walk and dimension.

The resulting data structure follows the same pattern as other walk generation functions in this package, including cumulative statistics and proper attribute setting for further analysis and visualization.

Author

Steven P. Sanderson II, MPH

Examples

# Biased random walk with upward trend
biased_displacement <- function() {
  return(rnorm(1, mean = 0.1, sd = 1.0))
}

set.seed(123)
custom_walk(.n = 100, .num_walks = 5, .custom_fns = biased_displacement)
#> # A tibble: 500 × 8
#>    walk_number step_number      y cum_sum_y cum_prod_y cum_min_y cum_max_y
#>    <fct>             <int>  <dbl>     <dbl>      <dbl>     <dbl>     <dbl>
#>  1 1                     1 -0.130    -0.130          0    -0.130    -0.130
#>  2 1                     2  1.66      1.53           0    -0.130     1.66 
#>  3 1                     3  0.171     1.70           0    -0.130     1.66 
#>  4 1                     4  0.229     1.93           0    -0.130     1.66 
#>  5 1                     5  1.82      3.74           0    -0.130     1.82 
#>  6 1                     6  0.561     4.30           0    -0.130     1.82 
#>  7 1                     7 -1.17      3.14           0    -1.17      1.82 
#>  8 1                     8 -0.587     2.55           0    -1.17      1.82 
#>  9 1                     9 -0.346     2.21           0    -1.17      1.82 
#> 10 1                    10  1.32      3.53           0    -1.17      1.82 
#> # ℹ 490 more rows
#> # ℹ 1 more variable: cum_mean_y <dbl>

# Multi-dimensional custom walk
set.seed(123)
custom_walk(.n = 50, .num_walks = 3, .dimensions = 2, .custom_fns = biased_displacement)
#> # A tibble: 150 × 14
#>    walk_number step_number      x       y cum_sum_x cum_sum_y cum_prod_x
#>    <fct>             <int>  <dbl>   <dbl>     <dbl>     <dbl>      <dbl>
#>  1 1                     1 -0.130  0.0715    -0.130    0.0715          0
#>  2 1                     2  1.66   0.0571     1.53     0.129           0
#>  3 1                     3  0.171  1.47       1.70     1.60            0
#>  4 1                     4  0.229 -0.126      1.93     1.47            0
#>  5 1                     5  1.82   1.62       3.74     3.09            0
#>  6 1                     6  0.561 -1.45       4.30     1.64            0
#>  7 1                     7 -1.17   0.685      3.14     2.32            0
#>  8 1                     8 -0.587  0.224      2.55     2.55            0
#>  9 1                     9 -0.346  0.316      2.21     2.86            0
#> 10 1                    10  1.32   0.480      3.53     3.34            0
#> # ℹ 140 more rows
#> # ℹ 7 more variables: cum_prod_y <dbl>, cum_min_x <dbl>, cum_min_y <dbl>,
#> #   cum_max_x <dbl>, cum_max_y <dbl>, cum_mean_x <dbl>, cum_mean_y <dbl>