forked from mpeeples2008/ArchNetSci
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrihll_wilson.R
82 lines (73 loc) · 2.53 KB
/
rihll_wilson.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
#' Rihll and Wilson "Retail" Gravity Model
#'
#' This function implements the "Retail" gravity model made popular in
#' archaeology by Rihll and Wilson (1987). This procedure assesses flows of
#' resources between geographic locations using a matrix of distances based on
#' some e measure of the cost of travel between those locations.
#'
#' @param Oi The estimate3d weight of flow or interactions out of the origins i.
#' @param Wj The estimated weight of flow or interaction into destinations j.
#' In most archaeological applications, this is used to represent some measure
#' of settlement size.
#' @param alpha This parameter defines the importance of resource flow into
#' destinations. Values greater than 1 indicate increasing returns to scale for
#' every unit of flow.
#' @param beta The decay parameter that describes the rate of decay in
#' interaction at increasing distance.
#' @param dist_mat A symmetric matrix of distances between all locations
#' considered in map units.
#' @param K This is the factor used to convert size W to the sum of flows Dj.
#' By default this is set to 1.
#' @param eps This is a control parameter that determines how quickly W can
#' change at each iterative step. The default value is 0.01
#'
#' @return This function returns a list which contains Wj or the final estimated
#' weights incident on each location; Tij which is the measure of flow between
#' all locations; iter which is the number of iterations the function went
#' through; and terminal_sites which includes the data for sites where the
#' total flow of inputs into the site Wj is bigger than the largest flow out.
#'
#'
#' @export
#'
rihll_wilson <-
function(Oi = 0,
Wj = 0,
alpha,
beta,
dist_mat,
K = 1,
eps = 0.01) {
if (Oi == 0) {
Oi <- rep(1, nrow(dist_mat))
}
if (Wj == 0) {
Wj <- rep(1, nrow(dist_mat))
}
Dj <- Wj
end_condition <- 1
iter <- 0
det <- exp(-beta * dist_mat)
while (!(end_condition < 1e-5) & iter < 10000) {
Wj <- Dj
Tij <-
apply(det * Oi %o% Wj ^ alpha, 2, '/',
(Wj ^ alpha %*% det))
delta_W <- eps * (colSums(Tij) - (K * Wj))
Dj <- delta_W + Wj
iter <- iter + 1
end_condition <- sum((Dj - Wj) ^ 2)
}
terminal_sites <- NULL
for (i in seq_len(length(Wj))) {
terminal_sites[i] <- sum(Tij[-i, i]) > max(Tij[i,])
}
out <-
list(
Wj = Wj,
Tij = Tij,
iter = iter,
terminals = terminal_sites
)
return(out)
}