Calculates the randomized shortest path distance between points.

rSPDistance(x, from, to, theta, totalNet = "net", method = 1)



TransitionLayer object


point locations coordinates (of SpatialPoints, matrix or numeric class)


point locations coordinates (of SpatialPoints, matrix or numeric class)


theta is the degree from which the path randomly deviates from the shortest path, 0 < theta < 20


total or net movements between cells


method 1 (as defined in Saerens et al.) or method 2 (a modified version, see below in Details)


distance matrix (S3 class dist or matrix)


The function implements the algorithm given by Saerens et al. (2009).

Method 1 implements the method as it is. Method 2 uses W = exp(-theta * ln(P)).


Saerens M., L. Yen, F. Fouss, and Y. Achbany. 2009. Randomized shortest-path problems: two related models. Neural Computation, 21(8):2363-2404.

See also


Jacob van Etten


#Create a new raster and set all its values to unity.
r <- raster(nrows=18, ncols=36)
r <- setValues(r,rep(1,ncell(raster)))

#Create a Transition object from the raster
tr <- transition(r,mean,4)
#> The extent and CRS indicate this raster is a global lat/lon raster. This means that transitions going off of the East or West edges will 'wrap' to the opposite edge.
#> Global lat/lon rasters are not supported under new optimizations for 4 and 8 directions with custom transition functions. Falling back to old method.

#Create two sets of coordinates
sP1 <- SpatialPoints(cbind(c(65,5,-65),c(55,35,-35)))
sP2 <- SpatialPoints(cbind(c(50,15,-40),c(80,20,-5)))

#Calculate the RSP distance between the points
rSPDistance(tr, sP1, sP2, 1)
#>           [,1]      [,2]      [,3]
#> [1,]  3.054101  9.061349 16.079315
#> [2,]  9.082282  3.048942  8.058728
#> [3,] 23.095346 13.073405  6.055039