This function creates a raster where the cell values represent the number of times one or more random "walkers" traverse the cell. If you simulate multiple random walkers, you can do computation in parallel, which can be controlled by allowing fasterRaster to use multiple cores and more memory using the "cores" and "memory" arguments in the faster()
function.
Usage
# S4 method for class 'GRaster'
rWalkRast(
x,
n = 1,
steps = 1e+05,
directions = 8,
avoid = FALSE,
sameStart = FALSE,
seed = NULL,
check = TRUE
)
Arguments
- x
A
GRaster
to serve as a template.- n
Numeric: Number of walkers. Default is 1.
- steps
Numeric: Number of steps taken by each walker. Default is 100000.
- directions
Either 4 or 8: Directions in which a walker can turn at any point. If 4, then walks are confined to north/south/east/west directions (Rook's case). If 8, then the cardinal and subcardinal directions are allowed (Queen's case).
- avoid
Logical: If
FALSE
(default), then walkers can traverse their own walks. IfTRUE
, walkers avoid their own trails. A self-avoiding random walk can take much longer to compute.- sameStart
Logical: If
FALSE
(default), walkers can begin anywhere. IfTRUE
, then walkers start from the same place.- seed
Integer or
NULL
(default): IfNULL
, then a random seed is generated by the function. If numeric, results will be deterministic. In either case, the value will be rounded to the nearest integer.- check
Logical: If
TRUE
(default), function will check to see if the addonr.random.walk
has been installed. If it has not, it will attempt to install it.
Value
A GRaster
with cell values representing the number of times one or more walkers traversed the cell.
Details
This function needs the GRASS addon r.random.walk
. If it is not installed, it will try to install it.#'
Examples
if (grassStarted()) {
# Setup
library(sf)
library(terra)
# Elevation raster
madElev <- fastData("madElev")
# Convert a SpatRaster to a GRaster:
elev <- fast(madElev)
### Create a raster with values drawn from a uniform distribution:
unif <- rUnifRast(elev)
plot(unif)
### Create a raster with values drawn from a normal distribution:
norms <- rNormRast(elev, n = 2, mu = c(5, 10), sigma = c(2, 1))
plot(norms)
hist(norms, bins = 100)
# Create a raster with random, seemingly normally-distributed values:
rand <- rSpatialDepRast(elev, dist = 1000)
plot(rand)
# Values appear normal on first inspection:
hist(rand)
# ... but actually are patterned:
hist(rand, bins = 100)
# Create a fractal raster:
fractal <- fractalRast(elev, n = 2, dimension = c(2.1, 2.8))
plot(fractal)
hist(fractal)
### Random walker rasters
# One random walker
walk <- rWalkRast(elev)
plot(walk)
# Random walker with self-avoidance:
walkAvoid <- rWalkRast(elev, steps = 1000, avoid = TRUE, seed = 1)
plot(walkAvoid)
# 10 random walkers:
walk10 <- rWalkRast(elev, n = 10)
plot(walk10)
# 10 random walkers starting in same place:
walkSame10 <- rWalkRast(elev, n = 10, sameStart = TRUE)
plot(walkSame10)
}