-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtmbstan.R
389 lines (334 loc) · 10.5 KB
/
tmbstan.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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
#' Fit constant Small Area Estimation model using `tmbstan`.
#'
#' Simply fits a constant (the mean). This is useful as a benchmark for other models.
#'
#' @param sf A simple features object with some geometry.
#' @param nsim_warm Number of warmup samples, passed to `rstan`.
#' @param nsim_iter Number of samples, passed to `rstan`.
#' @param chains Number of chains, each of which gets `nsim_warm + nsim_iter` samples, passed to `rstan`.
#' @param cores Number of cores, passed to `rstan`, defaults to `parallel::detectCores()`.
#' @param ii The (zero-indexed) indices of the observations held-out.
#' @examples
#' constant_tmbstan(mw, nsim_warm = 0, nsim_iter = 100, cores = 2)
#' @export
constant_tmbstan <- function(sf, nsim_warm = 100, nsim_iter = 1000, chains = 4, cores = parallel::detectCores(), ii = NULL){
dat <- list(
n = nrow(sf),
y = sf$y,
m = sf$n_obs,
left_out = !is.null(ii),
ii = if(!is.null(ii)) ii else 0
)
param <- list(
beta_0 = 0
)
obj <- TMB::MakeADFun(
data = c(model = "constant", dat),
parameters = param,
DLL = "arealutils_TMBExports"
)
fit <- tmbstan::tmbstan(
obj = obj,
warmup = nsim_warm,
iter = nsim_iter,
chains = chains,
cores = cores
)
return(fit)
}
#' Fit IID Small Area Estimation model using `tmbstan`.
#'
#' Random effects are independent and identically distributed.
#'
#' @inheritParams constant_tmbstan
#' @examples
#' constant_tmbstan(mw, nsim_warm = 0, nsim_iter = 100, cores = 2)
#' @export
iid_tmbstan <- function(sf, nsim_warm = 100, nsim_iter = 1000, chains = 4, cores = parallel::detectCores(), ii = NULL){
dat <- list(
n = nrow(sf),
y = sf$y,
m = sf$n_obs,
left_out = !is.null(ii),
ii = if(!is.null(ii)) ii else 0
)
param <- list(
beta_0 = 0,
u = rep(0, nrow(sf)),
log_sigma_u = 0
)
obj <- TMB::MakeADFun(
data = c(model = "iid", dat),
parameters = param,
random = c("beta_0", "u"),
DLL = "arealutils_TMBExports"
)
fit <- tmbstan::tmbstan(
obj = obj,
warmup = nsim_warm,
iter = nsim_iter,
chains = chains,
cores = cores
)
return(fit)
}
#' Fit Besag Small Area Estimation model using `tmbstan`.
#'
#' Random effects have an improper conditional autoregressive (ICAR)
#' distribution with (generalised) precision matrix produced using
#' the [`nb_to_precision`] function with input `nb`,
#' the neighbourhood structure of `sf`.
#'
#' @inheritParams constant_tmbstan
#' @examples
#' besag_tmbstan(mw, nsim_warm = 0, nsim_iter = 100, cores = 2)
#' @export
besag_tmbstan <- function(sf, nsim_warm = 100, nsim_iter = 1000, chains = 4, cores = parallel::detectCores(), ii = NULL){
nb <- sf_to_nb(sf)
Q <- nb_to_precision(nb)
Q <- as(Q, "dgTMatrix")
dat <- list(n = nrow(sf),
y = sf$y,
m = sf$n_obs,
left_out = !is.null(ii),
ii = if(!is.null(ii)) ii else 0,
Q = Q,
Qrank = as.integer(Matrix::rankMatrix(Q)))
param <- list(beta_0 = 0,
u = rep(0, dat$n),
log_sigma_u = 0)
obj <- TMB::MakeADFun(
data = c(model = "besag", dat),
parameters = param,
random = c("beta_0", "u"),
DLL = "arealutils_TMBExports"
)
fit <- tmbstan::tmbstan(
obj = obj,
warmup = nsim_warm,
iter = nsim_iter,
chains = chains,
cores = cores
)
return(fit)
}
#' Fit BYM2 Small Area Estimation model using `tmbstan`.
#'
#' @inheritParams constant_tmbstan
#' @examples
#' bym2_tmbstan(mw, nsim_warm = 0, nsim_iter = 100, cores = 2)
#' @export
bym2_tmbstan <- function(sf, nsim_warm = 100, nsim_iter = 1000, chains = 4, cores = parallel::detectCores(), ii = NULL){
nb <- sf_to_nb(sf)
Q <- nb_to_precision(nb)
Q <- as(Q, "dgTMatrix")
dat <- list(n = nrow(sf),
y = sf$y,
m = sf$n_obs,
left_out = !is.null(ii),
ii = if(!is.null(ii)) ii else 0,
Q = Q)
param <- list(beta_0 = 0,
u = rep(0, dat$n),
w = rep(0, dat$n),
logit_phi = 0,
log_sigma_u = 0)
obj <- TMB::MakeADFun(
data = c(model = "bym2", dat),
parameters = param,
random = c("beta_0", "u", "w"),
DLL = "arealutils_TMBExports"
)
fit <- tmbstan::tmbstan(
obj = obj,
warmup = nsim_warm,
iter = nsim_iter,
chains = chains,
cores = cores
)
return(fit)
}
#' Fit Centroid MVN Small Area Estimation model using `tmbstan`.
#'
#' Random effects have a multivariate Gaussian distribution with covariance
#' matrix calculated using [`centroid_covariance`]. Kernel hyper-parameters
#' are fixed.
#'
#' @inheritParams constant_tmbstan
#' @inheritParams centroid_covariance
#' @examples
#' fck_tmbstan(mw, nsim_warm = 0, nsim_iter = 100, cores = 2)
#' @export
fck_tmbstan <- function(sf, nsim_warm = 100, nsim_iter = 1000, chains = 4, cores = parallel::detectCores(), kernel = matern, ii = NULL, ...){
cov <- centroid_covariance(sf, kernel, ...)
cov <- cov / riebler_gv(cov) # Standardise so tau prior is right
dat <- list(n = nrow(sf),
y = sf$y,
m = sf$n_obs,
left_out = !is.null(ii),
ii = if(!is.null(ii)) ii else 0,
Sigma = cov)
param <- list(beta_0 = 0,
u = rep(0, dat$n),
log_sigma_u = 0)
obj <- TMB::MakeADFun(
data = c(model = "mvn_covariance", dat),
parameters = param,
random = c("beta_0", "u"),
DLL = "arealutils_TMBExports"
)
fit <- tmbstan::tmbstan(
obj = obj,
warmup = nsim_warm,
iter = nsim_iter,
chains = chains,
cores = cores
)
return(fit)
}
#' Fit Integrated MVN Small Area Estimation model using `tmbstan`.
#'
#' Random effects have a multivariate Gaussian distribution with covariance
#' matrix calculated using [`integrated_covariance`].
#'
#' @inheritParams constant_tmbstan
#' @inheritParams integrated_covariance
#' @examples
#' fik_tmbstan(mw, nsim_warm = 0, nsim_iter = 100, cores = 2)
#' @export
fik_tmbstan <- function(sf, nsim_warm = 100, nsim_iter = 1000, chains = 4, cores = parallel::detectCores(), L = 10, type = "hexagonal", kernel = matern, ii = NULL, ...){
cov <- integrated_covariance(sf, L = L, type = type, kernel, ...)
cov <- cov / riebler_gv(cov) # Standardise so tau prior is right
dat <- list(n = nrow(sf),
y = sf$y,
m = sf$n_obs,
left_out = !is.null(ii),
ii = if(!is.null(ii)) ii else 0,
Sigma = cov)
param <- list(beta_0 = 0,
u = rep(0, dat$n),
log_sigma_u = 0)
obj <- TMB::MakeADFun(
data = c(model = "mvn_covariance", dat),
parameters = param,
random = c("beta_0", "u"),
DLL = "arealutils_TMBExports"
)
fit <- tmbstan::tmbstan(
obj = obj,
warmup = nsim_warm,
iter = nsim_iter,
chains = chains,
cores = cores
)
return(fit)
}
#' Fit Bayesian Centroid MVN Small Area Estimation model using `tmbstan`.
#'
#' Random effects have a multivariate Gaussian distribution with covariance
#' matrix calculated using [`centroid_covariance`]. Kernel hyperparameters
#' are given a prior and learnt.
#'
#' @inheritParams constant_tmbstan
#' @examples
#' ck_tmbstan(mw, nsim_warm = 0, nsim_iter = 100, cores = 2)
#' @export
ck_tmbstan <- function(sf, nsim_warm = 100, nsim_iter = 1000, chains = 4, cores = parallel::detectCores(), ii = NULL){
D <- centroid_distance(sf)
# Parameters of the length-scale prior
if(inherits(D, "units")) {
if(units(D)$numerator == "m") {
D <- units::set_units(D, "km")
}
}
D_nonzero <- as.vector(D)[as.vector(D) > 0]
lb <- quantile(D_nonzero, 0.05)
ub <- quantile(D_nonzero, 0.95)
param <- invgamma_prior(lb = lb, ub = ub, plb = 0.05, pub = 0.05)
dat <- list(n = nrow(sf),
y = sf$y,
m = sf$n_obs,
left_out = !is.null(ii),
ii = if(!is.null(ii)) ii else 0,
a = param$a,
b = param$b,
D = D)
param <- list(beta_0 = 0,
u = rep(0, dat$n),
log_sigma_u = 0,
log_l = 0)
obj <- TMB::MakeADFun(
data = c(model = "centroid", dat),
parameters = param,
random = c("beta_0", "u"),
DLL = "arealutils_TMBExports"
)
fit <- tmbstan::tmbstan(
obj = obj,
warmup = nsim_warm,
iter = nsim_iter,
chains = chains,
cores = cores
)
return(fit)
}
#' Fit Bayesian Integrated MVN Small Area Estimation model using `tmbstan`.
#'
#' Random effects have a multivariate Gaussian distribution with covariance
#' matrix calculated using [`integrated_covariance`]. Kernel hyperparameters
#' are given a prior and learnt.
#'
#' @inheritParams constant_tmbstan
#' @inheritParams integrated_covariance
#' @examples
#' ik_tmbstan(mw, nsim_warm = 0, nsim_iter = 100, cores = 2)
#' @export
ik_tmbstan <- function(sf, nsim_warm = 100, nsim_iter = 1000, chains = 4, cores = parallel::detectCores(), L = 10, type = "hexagonal", ii = NULL, ...){
n <- nrow(sf)
samples <- sf::st_sample(sf, type = type, exact = TRUE, size = rep(L, n))
S <- sf::st_distance(samples, samples)
# Parameters of the length-scale prior
if(inherits(S, "units")) {
if(units(S)$numerator == "m") {
S <- units::set_units(S, "km")
}
}
S_nonzero <- as.vector(S)[as.vector(S) > 0]
lb <- quantile(S_nonzero, 0.05)
ub <- quantile(S_nonzero, 0.95)
param <- invgamma_prior(lb = lb, ub = ub, plb = 0.05, pub = 0.05)
# Data structure for unequal number of points in each area
sample_index <- sf::st_intersects(sf, samples)
sample_lengths <- lengths(sample_index)
# Take one away due to zero indexing in C++
start_index <- sapply(sample_index, function(x) x[1] - 1)
dat <- list(n = nrow(sf),
y = sf$y,
m = sf$n_obs,
left_out = !is.null(ii),
ii = if(!is.null(ii)) ii else 0,
a = param$a,
b = param$b,
sample_lengths = sample_lengths,
total_samples = sum(sample_lengths),
start_index = start_index,
S = S)
param <- list(beta_0 = 0,
u = rep(0, dat$n),
log_sigma_u = 0,
log_l = 0)
obj <- TMB::MakeADFun(
data = c(model = "integrated", dat),
parameters = param,
random = c("beta_0", "u"),
DLL = "arealutils_TMBExports"
)
fit <- tmbstan::tmbstan(
obj = obj,
warmup = nsim_warm,
iter = nsim_iter,
chains = chains,
cores = cores
)
return(fit)
}