/
probPassage.R
322 lines (283 loc) · 8.85 KB
/
probPassage.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
#' Probability of passage
#'
#' Calculates for each cell the number of passages of a random-walk
#' or randomised shortest paths with given origin(s) and destination(s).
#' Either the total or the net number of passages can be calculated.
#' In the case of multiple origins or destinations, each receives equal
#' weight.
#'
#' @name passage
#' @aliases passage
#' @aliases passage,TransitionLayer,Coords,Coords,missing-method
#' @aliases passage,TransitionLayer,Coords,Coords,numeric-method
#' @aliases passage,TransitionLayer,RasterLayer,RasterLayer,missing-method
#' @aliases passage,TransitionLayer,RasterLayer,RasterLayer,numeric-method
#' @keywords spatial
#' @keywords methods
#' @param x Object of class \code{Transition*}
#' @param origin \code{SpatialPoints}, matrix or numeric
#' object with coordinates or RasterLayer object with origin
#' cells set to TRUE
#' @param goal \code{SpatialPoints}, matrix or numeric object
#' with coordinates or RasterLayer object with origin
#' cells set to TRUE
#' @param theta If zero or missing, a random walk results.
#' If a numeric value 0 < theta < 20 is given, randomised
#' shortest paths are calculated; theta is the degree
#' from which the path randomly deviates from the shortest path
#' @param ... Additional arguments:
#' totalNet ("total" or "net"), and output ("RasterLayer" or "Transition")
#' @return RasterLayer or Transition object, depending on the output argument
#' @details
#' The net number of passages between i and j is defined as:
#' abs( passages from i to j - passages from j to i ).
#'
#' Defaults for additional argument \code{totalNet} is "net"
#' and for \code{output} it is "RasterLayer".
#'
#' Random walk requires a symmetric transition matrix.
#'
#' @references
#' McRae B.H., B.G. Dickson, and T. Keitt. 2008.
#' Using circuit theory to model connectivity in ecology,
#' evolution, and conservation.
#' \emph{Ecology} 89:2712-2724.
#'
#' Saerens M., L. Yen, F. Fouss, and Y. Achbany. 2009.
#' Randomized shortest-path problems: two related models.
#' \emph{Neural Computation}, 21(8):2363-2404.
#'
#' @author Jacob van Etten. Implementation of randomised shortest
#' paths based on Matlab code by Marco Saerens
#'
#' @seealso
#' \code{\link{commuteDistance}},
#' \code{\link{pathInc}}
#'
#' @examples
#' #create a new raster and set all its values to unity.
#' raster <- raster(nrows=18, ncols=36)
#' raster <- setValues(raster,rep(1,ncell(raster)))
#'
#' #create a Transition object from the raster
#' tr <- transition(raster,mean,4)
#' trC <- geoCorrection(tr, type="c", scl=TRUE)
#' trR <- geoCorrection(tr, type="r", scl=TRUE)
#'
#' #create two coordinates
#' sP1 <- SpatialPoints(cbind(-105,55))
#' sP2 <- SpatialPoints(cbind(105,-55))
#'
#' #randomised shortest paths with theta = 2
#' rSPraster <- passage(trC, sP1, sP2, 2)
#' plot(rSPraster)
#' points(sP1)
#' points(sP2)
#'
#' #randomised shortest paths with theta = 0.05
#' rSPraster <- passage(trC, sP1, sP2, 0.05)
#' plot(rSPraster)
#' points(sP1)
#' points(sP2)
#'
#' #randomised shortest paths with theta = 0.05
#' #and total
#' rSPraster <- passage(trC, sP1, sP2, 0.05, totalNet = "total")
#' plot(rSPraster)
#' points(sP1)
#' points(sP2)
#'
#' #random walk
#' rwraster <- passage(trR, sP1, sP2)
#' plot(rwraster)
#' points(sP1)
#' points(sP2)
#'
#' @exportMethod passage
setGeneric(
"passage",
function(x, origin, goal, theta, ...) {
standardGeneric("passage")
}
)
setMethod(
"passage",
signature(x = "TransitionLayer", origin = "Coords", goal = "Coords", theta = "missing"),
function(x, origin, goal, totalNet = "net", output = "RasterLayer") {
.checkInputsPassage(totalNet, output)
origin <- .coordsToMatrix(origin)
goal <- .coordsToMatrix(goal)
if (totalNet == "net" & output == "RasterLayer") {
x <- .transitionSolidify(x)
tc <- transitionCells(x)
cellnri <- cellFromXY(x, origin)
cellnrj <- cellFromXY(x, goal)
ci <- match(cellnri, tc)
cj <- match(cellnrj, tc)
result <- .flowMap(x, ci, cj, tc)
} else {
stop("no method available -- try a low value of theta instead")
}
return(result)
}
)
setMethod(
"passage",
signature(x = "TransitionLayer", origin = "RasterLayer", goal = "RasterLayer", theta = "missing"),
function(x, origin, goal, totalNet = "net", output = "RasterLayer") {
.checkInputsPassage(totalNet, output)
if (totalNet == "net" & output == "RasterLayer") {
x <- .transitionSolidify(x)
tc <- transitionCells(x)
ci <- which(getValues(origin))
cj <- which(getValues(goal))
result <- .flowMap(x, ci, cj, tc)
} else {
stop("no method available -- try a low value of theta instead")
}
return(result)
}
)
.checkInputsPassage <- function(totalNet, output) {
if (!(totalNet %in% c("total", "net"))) {
stop("totalNet should be either total or net")
}
if (!(output %in% c("RasterLayer", "TransitionLayer"))) {
stop("output should be either RasterLayer or TransitionLayer")
}
}
.flowMap <- function(x, indexOrigin, indexGoal, tc) {
L <- .Laplacian(x)
Lr <- L[-dim(L)[1], -dim(L)[1]]
A <- as(L, "lMatrix")
A <- as(A, "dMatrix")
n <- max(dim(Lr))
Current <- .currentR(L, Lr, A, n, indexOrigin, indexGoal)
result <- as(x, "RasterLayer")
dataVector <- rep(0, times = ncell(result))
dataVector[tc] <- Current
result <- setValues(result, dataVector)
return(result)
}
# Author: Jacob van Etten jacobvanetten@yahoo.com,
# based on Matlab code by Marco Saerens
# IE University
# Date : January 2010
# Version 1.0
# Licence GPL v3
setMethod(
"passage",
signature(x = "TransitionLayer", origin = "Coords", goal = "Coords", theta = "numeric"),
function(x, origin, goal, theta, totalNet = "net", output = "RasterLayer") {
cellnri <- cellFromXY(x, origin)
cellnrj <- cellFromXY(x, goal)
x <- .transitionSolidify(x)
tc <- transitionCells(x)
ci <- match(cellnri, tc)
cj <- match(cellnrj, tc)
result <- .randomShPaths(x, ci, cj, theta, tc, totalNet, output)
return(result)
}
)
setMethod(
"passage",
signature(x = "TransitionLayer", origin = "RasterLayer", goal = "RasterLayer", theta = "numeric"),
function(x, origin, goal, theta, totalNet = "net", output = "RasterLayer") {
#check if Transition and RasterLayers coincide
ci <- which(getValues(origin))
cj <- which(getValues(goal))
x <- .transitionSolidify(x)
tc <- transitionCells(x)
result <- .randomShPaths(x, ci, cj, theta, tc, totalNet, output)
return(result)
}
)
.randomShPaths <- function(x, ci, cj, theta, tc, totalNet, output) {
if(theta < 0 | theta > 20 ) {
stop("theta value out of range (between 0 and 20)")
}
tr <- transitionMatrix(x, inflate = FALSE)
trR <- tr
trR@x <- 1 / trR@x
nr <- dim(tr)[1]
Id <- Diagonal(nr)
rs <- rowSums(tr)
rs[rs>0] <- 1/rs[rs > 0]
P <- tr * rs
W <- trR
#zero values are not relevant because of next step exp(-theta * trR@x)
W@x <- exp(-theta * trR@x)
W <- W * P
return(.probPass(x, Id, W, nr, ci, cj, tc, totalNet, output))
}
.probPass <- function(x, Id, W, nr, ci, cj, tc, totalNet, output) {
nc <- ncell(x)
Ij <- Diagonal(nr)
Ij[cbind(cj, cj)] <- 1 - 1 / length(cj)
Wj <- Ij %*% W
ei <- rep(0, times = nr)
ei[ci] <- 1 / length(ci)
ej <- rep(0, times = nr)
ej[cj] <- 1 / length(cj)
IdMinusWj <- as((Id - Wj), "dgCMatrix")
zci <- solve(t(IdMinusWj), ei)
zcj <- solve(IdMinusWj, ej)
zcij <- sum(ei * zcj)
if(zcij < 1e-300) {
if(output == "RasterLayer") {
result <- as(x, "RasterLayer")
result[] <- rep(0, times = nc)
}
if(output == "TransitionLayer") {
result <- x
transitionMatrix(result) <- Matrix(0, nc, nc)
result@transitionCells <- 1:nc
}
} else {
# Computation of the cost dij between node i and node j
# dij <- (t(zci) %*% (trR * Wj) %*% zcj) / zcij
# Computation of the matrix N, containing the number of passages through
# each arc
N <- (Diagonal(nr, as.vector(zci)) %*% Wj %*% Diagonal(nr, as.vector(zcj))) / zcij
#N is here the NET number of passages, like McRae-random walk
if(output == "RasterLayer") {
if(totalNet == "total") {
# Computation of the vector n, containing the number of visits in
# each node
n <- pmax(rowSums(N), colSums(N)) #not efficient but effective
}
# Computation of the matrix Pr, containing the transition
# probabilities
#rn <- rep(0, times=length(n))
#rn[n>0] <- 1 / n[n>0]
#Pr <- N * rn
#Pr <- N * (1 / n)
#net visits
if(totalNet == "net") {
nNet <- abs(skewpart(N))
n <- pmax(rowSums(nNet), colSums(nNet))
n[c(ci, cj)] <- 2 * n[c(ci, cj)]
}
result <- as(x,"RasterLayer")
dataVector <- rep(NA, times = nc)
dataVector[tc] <- n
result <- setValues(result, dataVector)
}
if(output == "TransitionLayer") {
result <- x
if(totalNet == "total") {
tr <- Matrix(0, nc, nc)
tr[tc, tc] <- N
}
if(totalNet == "net") {
nNet <- skewpart(N) * 2
nNet@x[nNet@x<0] <- 0
tr <- Matrix(0, nc, nc)
tr[tc, tc] <- nNet
}
transitionMatrix(result) <- tr
result@transitionCells <- 1:nc
}
}
return(result)
}