Skip to contents

The random_displacement_walk function generates a single random walk in 2 dimensions (x, y), where each step is a random displacement in both x and y directions, sampled from the provided displacement and distance spaces. The walk disregards steps where both x and y displacements are zero.

Usage

random_displacement_walk(
  .num_walks = 25,
  .seed = NULL,
  .n = 100,
  .distance_space = c(0, 1, 2, 3, 4),
  .displacement = c(-1, 1),
  .dimensions = 1
)

Arguments

.num_walks

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

.seed

An optional value to set the random seed. If NULL, no seed is set. Default is NULL.

.n

The number of steps in the walk. Must be >= 0. Default is 100.

.distance_space

A numeric vector of possible step distances. Default is c(0, 1, 2, 3, 4).

.displacement

A numeric vector of possible step directions. Default is c(-1, 1).

.dimensions

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

Value

A tibble with columns depending on the number of dimensions:

  • 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 tibble includes attributes for the function parameters.

Author

Steven P. Sanderson II, MPH

Examples

random_displacement_walk(.n = 10, .seed = Sys.Date())
#> # A tibble: 275 × 3
#>    walk_number step_number     y
#>    <fct>             <dbl> <dbl>
#>  1 1                     1     0
#>  2 1                     2     1
#>  3 1                     3    -2
#>  4 1                     4    -5
#>  5 1                     5    -9
#>  6 1                     6    -7
#>  7 1                     7   -10
#>  8 1                     8    -6
#>  9 1                     9    -2
#> 10 1                    10    -3
#> # ℹ 265 more rows