-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathW_fgsim.R
174 lines (149 loc) · 7.66 KB
/
W_fgsim.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
# Wrapper functions to be used with hhh4:
# Takes settings and parameters and returns functions that compute weights
# and their derivatives
# Misc functions
removeNAmat = function(x){
x[is.na(x)] = 0
x[is.infinite((x))] = 0
return(x)
}
removeNAlist = function(x){
x = lapply(x, removeNAmat)
return(x)
}
# Input:
# pars: List of parameters with elements mu, sigma, w, l, u
# truncPL: Truncated power law Y/N
# maxlag: Maximum spatial lag considered
# normalize: Row normalisation Y/N
# log: Log-transformation of parameters Y/N
# initial: Initial parameters
# from0: Consider intra-district weights Y/N
# popScale: Scale by population Y/N (if yes, pars should contain "pop")
# Should have determinant 1 nor numerical stability
W_fgsim = function(pars,
truncPL = FALSE,
gravity = FALSE,
maxlag = Inf,
normalize = TRUE,
log = FALSE,
initial = rep(ifelse(log, 0, 1), 1 + (truncPL | gravity)),
from0 = TRUE,
popScale = FALSE){
if(gravity & truncPL){
warning("Truncated power law not supported for gravity model, set to FALSE.")
truncPL = FALSE
}
if(gravity){
target = c("mu1", "mu2", "sigma1", "sigma12", "sigma2", "w")
}else{
target = c("mu", "sigma", "w", "l", "u")
}
pn = names(pars)
for(i in 1:length(target)){
if(!(target[i] %in% pn)) stop(paste("List pars must contain an element", target[i]))
}
if(popScale){
if(!("pop" %in% pn)) stop(paste("List pars must contain a matrix pop"))
}
if(truncPL & log){
warning("Truncation threshold for power law is already defined on log distances.")
}
# Initialise by computing raw weights
if(truncPL){
weights.call = call("WrawMTLtrunc", quote(nbmat), quote(pars), quote(d), ifelse(from0, 0, 1), maxlag, 0)
weights = as.function(c(alist(d=, nbmat=, ...=), call("{", weights.call)),
envir=getNamespace("surveillance"))
dweights.call = call("WrawMTLtrunc", quote(nbmat), quote(pars), quote(d), ifelse(from0, 0, 1), maxlag, 1)
dweights = as.function(c(alist(d=, nbmat=, ...=), call("{", dweights.call)),
envir=getNamespace("surveillance"))
d2weights.call = call("WrawMTLtrunc", quote(nbmat), quote(pars), quote(d), ifelse(from0, 0, 1), maxlag, 2)
d2weights = as.function(c(alist(d=, nbmat=, ...=), call("{", d2weights.call)),
envir=getNamespace("surveillance"))
}else{
if(gravity){
weights.call = call("WrawMN", quote(nbmat), quote(pars), quote(d), ifelse(from0, 0, 1), maxlag, 0)
weights = as.function(c(alist(d=, nbmat=, ...=), call("{", weights.call)),
envir=getNamespace("surveillance"))
dweights.call = call("WrawMN", quote(nbmat), quote(pars), quote(d), ifelse(from0, 0, 1), maxlag, 1)
dweights = as.function(c(alist(d=, nbmat=, ...=), call("{", dweights.call)),
envir=getNamespace("surveillance"))
d2weights.call = call("WrawMN", quote(nbmat), quote(pars), quote(d), ifelse(from0, 0, 1), maxlag, 2)
d2weights = as.function(c(alist(d=, nbmat=, ...=), call("{", d2weights.call)),
envir=getNamespace("surveillance"))
}else{
weights.call = call("WrawMTL", quote(nbmat), quote(pars), quote(d), ifelse(from0, 0, 1), maxlag, 0)
weights = as.function(c(alist(d=, nbmat=, ...=), call("{", weights.call)),
envir=getNamespace("surveillance"))
dweights.call = call("WrawMTL", quote(nbmat), quote(pars), quote(d), ifelse(from0, 0, 1), maxlag, 1)
dweights = as.function(c(alist(d=, nbmat=, ...=), call("{", dweights.call)),
envir=getNamespace("surveillance"))
d2weights.call = call("WrawMTL", quote(nbmat), quote(pars), quote(d), ifelse(from0, 0, 1), maxlag, 2)
d2weights = as.function(c(alist(d=, nbmat=, ...=), call("{", d2weights.call)),
envir=getNamespace("surveillance"))
}
}
# Assign weights to an object Wraw
body(weights) = as.call(c(as.name("{"),
substitute(out <- weights.call, list(weights.call=weights.call))
))
body(dweights) = as.call(c(as.name("{"),
substitute(out <- dweights.call, list(dweights.call=dweights.call))
))
body(d2weights) = as.call(c(as.name("{"),
substitute(out <- d2weights.call, list(d2weights.call=d2weights.call))
))
# Read in pars from global environment
# Slower than forwarding it from hhh4(), but feasible
nm = deparse(substitute(pars))
body(weights) <- as.call(append(as.list(body(weights)),
substitute(pars <- get(nameHere, globalenv()), list(nameHere = nm)), after = 1))
body(dweights) <- as.call(append(as.list(body(dweights)),
substitute(pars <- get(nameHere, globalenv()), list(nameHere = nm)), after = 1))
body(d2weights) <- as.call(append(as.list(body(d2weights)),
substitute(pars <- get(nameHere, globalenv()), list(nameHere = nm)), after = 1))
# Transform decay parameter if it is in logs
if (log){
body(weights) <- as.call(append(as.list(body(weights)),
quote(d <- exp(d)), after = 1))
body(dweights) <- as.call(append(as.list(body(dweights)),
quote(d <- exp(d)), after = 1))
body(d2weights) <- as.call(append(as.list(body(d2weights)),
quote(d <- exp(d)), after = 1))
}
# Scale by population
if(popScale){
body(weights) <- as.call(append(as.list(body(weights)),
quote(out <- helperSC(pars$pop, out$W, out$W1, out$W2))))
body(dweights) <- as.call(append(as.list(body(dweights)),
quote(out <- helperSC(pars$pop, out$W, out$W1, out$W2))))
body(d2weights) <- as.call(append(as.list(body(d2weights)),
quote(out <- helperSC(pars$pop, out$W, out$W1, out$W2))))
}
# Row normalisation
if(normalize){
body(weights) <- as.call(append(as.list(body(weights)),
quote(out <- helperRN(out$W, out$W1, out$W2))))
body(dweights) <- as.call(append(as.list(body(dweights)),
quote(out <- helperRN(out$W, out$W1, out$W2))))
body(d2weights) <- as.call(append(as.list(body(d2weights)),
quote(out <- helperRN(out$W, out$W1, out$W2))))
}
# Transform weights/derivatives if log-transformation of parameters
if(log){
body(weights) <- as.call(append(as.list(body(weights)),
quote(out <- helperLOG(d, out$W, out$W1, out$W2))))
body(dweights) <- as.call(append(as.list(body(dweights)),
quote(out <- helperLOG(d, out$W, out$W1, out$W2))))
body(d2weights) <- as.call(append(as.list(body(d2weights)),
quote(out <- helperLOG(d, out$W, out$W1, out$W2))))
}
# Remove NA and infinite values (just to be sure)
body(weights) <- as.call(append(as.list(body(weights)),
quote(removeNAmat(out$W))))
body(dweights) <- as.call(append(as.list(body(dweights)),
quote(removeNAlist(out$W1))))
body(d2weights) <- as.call(append(as.list(body(d2weights)),
quote(removeNAlist(out$W2))))
return(list(w = weights, dw = dweights, d2w = d2weights, initial = initial))
}