/
roll_statistics.R
491 lines (457 loc) · 16.2 KB
/
roll_statistics.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
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
#' Apply Rolling Functions using Circular Statistics
#'
#' A generic function for applying a function to rolling margins of an array.
#'
#' @inheritParams circular_mean
#' @param width integer specifying the window width (in numbers of observations)
#' which is aligned to the original sample according to the `align` argument.
#' If `NULL`, an optimal width is calculated.
#' @param FUN the function to be applied
#' @param by.column logical. If `TRUE`, `FUN` is applied to each column
#' separately.
#' @param fill a three-component vector or list (recycled otherwise) providing
#' filling values at the left/within/to the right of the data range. See the
#' fill argument of [zoo::na.fill()] for details
#' @param partial logical or numeric. If `FALSE` then `FUN` is only
#' applied when all indexes of the rolling window are within the observed time
#' range. If `TRUE` (default), then the subset of indexes that are in range
#' are passed to `FUN`. A numeric argument to partial can be used to determine
#' the minimal window size for partial computations. See below for more details.
#' @param ... optional arguments passed to [zoo::rollapply()]
#'
#' @returns numeric vector with the results of the rolling function.
#'
#' @note If the rolling statistics are applied to values that are a function of
#' distance it is recommended to sort the values first.
#'
#' @importFrom zoo rollapply
#'
#' @export
#'
#' @examples
#' data("plates")
#' plate_boundary <- subset(plates, plates$pair == "na-pa")
#' data("san_andreas")
#' PoR <- subset(nuvel1, nuvel1$plate.rot == "na")
#' distance <- distance_from_pb(
#' x = san_andreas,
#' PoR = PoR,
#' pb = plate_boundary,
#' tangential = TRUE
#' )
#' dat <- san_andreas[order(distance), ]
#' roll_circstats(dat$azi, w = 1 / dat$unc, circular_mean, width = 51)
roll_circstats <- function(x, w = NULL,
FUN,
axial = TRUE, na.rm = TRUE,
width = NULL, by.column = FALSE,
partial = TRUE,
fill = NA,
...) {
FUN <- match.fun(FUN)
if (is.null(width)) {
width <- optimal_rollwidth(x)
}
zoo::rollapply(
cbind(x, w),
width = width,
FUN = function(x) {
FUN(x[, 1], w = x[, 2], axial, na.rm)
},
by.column = by.column,
partial = partial,
fill = fill,
...
)
}
#' Apply Rolling Functions using Circular Statistical Tests for Uniformity
#'
#' A generic function for applying a function to rolling margins of an array.
#'
#' @inheritParams norm_chisq
#' @param x,y numeric. Directions in degrees
#' @param w,w.y (optional) Weights of `x` and `y`, respectively. A vector of
#' positive numbers and of the same length as `x`.
#' @inheritParams circular_dispersion
#' @param conf.level Level of confidence: \eqn{(1 - \alpha \%)/100}.
#' (`0.95` by default).
#' @param R The number of bootstrap replicates.
#' @param width integer specifying the window width (in numbers of observations)
#' which is aligned to the original sample according to the `align` argument.
#' If `NULL`, an optimal width is estimated.
#' @param by.column logical. If `TRUE`, FUN is applied to each column
#' separately.
#' @param fill a three-component vector or list (recycled otherwise) providing
#' filling values at the left/within/to the right of the data range. See the
#' fill argument of [zoo::na.fill()] for details
#' @param partial logical or numeric. If `FALSE` then `FUN` is only
#' applied when all indexes of the rolling window are within the observed time
#' range. If `TRUE` (default), then the subset of indexes that are in range
#' are passed to `FUN`. A numeric argument to partial can be used to determine
#' the minimal window size for partial computations. See below for more details.
#' @param ... optional arguments passed to [zoo::rollapply()]
#'
#' @returns numeric vector with the test statistic of the rolling test.
#' `roll_dispersion_CI` returns a 2-column matrix with the lower and the upper
#' confidence limits
#'
#' @note If the rolling functions are applied to values that are a function of
#' distance it is recommended to sort the values first.
#'
#' @importFrom zoo rollapply
#'
#' @name rolling_test
#'
#' @examples
#' data("plates")
#' plate_boundary <- subset(plates, plates$pair == "na-pa")
#' data("san_andreas")
#' PoR <- subset(nuvel1, nuvel1$plate.rot == "na")
#' distance <- distance_from_pb(
#' x = san_andreas,
#' PoR = PoR,
#' pb = plate_boundary,
#' tangential = TRUE
#' )
#' dat <- san_andreas[order(distance), ]
#' dat.PoR <- PoR_shmax(san_andreas, PoR, "right")
#' roll_normchisq(dat.PoR$azi.PoR, 135, dat$unc)
#' roll_rayleigh(dat.PoR$azi.PoR, prd = 135, unc = dat$unc)
#' roll_dispersion(dat.PoR$azi.PoR, y = 135, w = 1 / dat$unc)
#' roll_confidence(dat.PoR$azi.PoR, w = 1 / dat$unc)
#' \donttest{
#' roll_dispersion_CI(dat.PoR$azi.PoR, y = 135, w = 1 / dat$unc, R = 10)
#' }
NULL
#' @rdname rolling_test
#' @export
roll_normchisq <- function(obs, prd, unc = NULL,
width = NULL, by.column = FALSE,
partial = TRUE,
fill = NA,
...) {
if (is.null(width)) width <- optimal_rollwidth(obs)
zoo::rollapply(
cbind(obs, prd, unc),
width = width,
FUN = function(x) {
norm_chisq(x[, 1], x[, 2], x[, 3])
},
by.column = by.column,
partial = partial,
fill = fill,
...
)
}
#' @rdname rolling_test
#' @export
roll_rayleigh <- function(obs, prd, unc = NULL,
width = NULL, by.column = FALSE,
partial = TRUE,
fill = NA,
...) {
if (is.null(width)) width <- optimal_rollwidth(obs)
zoo::rollapply(
cbind(obs, prd, 1 / unc),
width = width,
FUN = function(x) {
suppressMessages(weighted_rayleigh(x[, 1], x[, 2], x[, 3]))$statistic
},
by.column = by.column,
partial = partial,
fill = fill,
...
)
}
#' @rdname rolling_test
#' @export
roll_dispersion <- function(x, y, w = NULL, w.y = NULL,
width = NULL, by.column = FALSE,
partial = TRUE,
fill = NA,
...) {
if (is.null(width)) width <- optimal_rollwidth(x)
if (is.null(w)) w <- rep(1, length(x))
if (is.null(w.y)) w.y <- rep(1, length(x))
zoo::rollapply(
cbind(x, y, w, w.y),
width = width,
FUN = function(x) {
suppressMessages(
circular_dispersion(x[, 1], x[, 2], x[, 3], x[, 4], norm = TRUE)
)
},
by.column = by.column,
partial = partial,
fill = fill,
...
)
}
#' @rdname rolling_test
#' @export
roll_confidence <- function(x, conf.level = .95, w = NULL, axial = TRUE,
width = NULL, by.column = FALSE, partial = TRUE,
fill = NA,
...) {
if (is.null(width)) width <- optimal_rollwidth(x)
zoo::rollapply(
cbind(x, rep(conf.level, length(x)), w),
width = width,
FUN = function(x) {
confidence_angle(x[, 1], x[1, 2], x[, 3])
},
by.column = by.column,
partial = partial,
fill = fill,
...
)
}
#' @rdname rolling_test
#' @export
roll_dispersion_CI <- function(x, y, w = NULL, w.y = NULL, R, conf.level = .95,
width = NULL, by.column = FALSE, partial = TRUE, fill = NA, ...) {
if (is.null(width)) width <- optimal_rollwidth(x)
if (is.null(w)) w <- rep(1, length(x))
if (is.null(w.y)) w.y <- rep(1, length(x))
zoo::rollapply(
cbind(x, y, w, w.y),
width = width,
FUN = function(x) {
suppressMessages(circular_dispersion_boot(x[, 1], x[, 2], x[, 3], x[, 4], R = R, conf.level = conf.level, ...)$CI)
},
by.column = by.column,
partial = partial,
fill = fill,
...
)
}
#' @rdname rolling_test
#' @export
roll_dispersion_sde <- function(x, y, w = NULL, w.y = NULL, R, conf.level = .95,
width = NULL, by.column = FALSE, partial = TRUE, fill = NA, ...) {
if (is.null(width)) width <- optimal_rollwidth(x)
if (is.null(w)) w <- rep(1, length(x))
if (is.null(w.y)) w.y <- rep(1, length(x))
zoo::rollapply(
cbind(x, y, w, w.y),
width = width,
FUN = function(x) {
suppressMessages(
circular_dispersion_boot(
x[, 1], x[, 2], x[, 3], x[, 4],
R = R, conf.level = conf.level, ...
)$sde
)
},
by.column = by.column,
partial = partial,
fill = fill,
...
)
}
optimal_rollwidth <- function(x) {
round((2 * circular_IQR(x) / length(x)^(1 / 3)) * 2 * pi)
}
#' Apply Rolling Functions using Circular Statistics
#'
#' A generic function for applying a function to rolling margins of an array
#' along an additional value.
#'
#' @param x,y vectors of numeric values in degrees. `length(y)` is either 1 or
#' `length(x)`
#' @param distance numeric. the independent variable along the values in `x`
#' are sorted, e.g. the plate boundary distances
#' @param FUN the function to be applied
#' @param width numeric. the range across `distance` on which `FUN` should be
#' applied on `x`. If `NULL`, then width is a number that separates the
#' distances in 10 equal groups.
#' @param align specifies whether the index of the result should be left- or
#' right-aligned or centered (default) compared to the rolling window of
#' observations. This argument is only used if width represents widths.
#' @param w numeric. the weighting for `x`
#' @param w.y numeric. the weighting for `y`
#' @param sort logical. Should the values be sorted after `distance` prior to
#' applying the function (`TRUE` by default).
#' @param min_n integer. The minimum values that should be considered in `FUN`
#' (2 by default), otherwise `NA`.
#' @param ... optional arguments to `FUN`
#'
#' @return two-column vectors of (sorted) `x` and the rolled statistics along
#' `distance`.
#'
#' @name rolling_test_dist
#'
#' @importFrom dplyr first arrange filter between
#'
#' @examples
#' data("plates")
#' plate_boundary <- subset(plates, plates$pair == "na-pa")
#' data("san_andreas")
#' PoR <- subset(nuvel1, nuvel1$plate.rot == "na")
#' san_andreas$distance <- distance_from_pb(
#' x = san_andreas,
#' PoR = PoR,
#' pb = plate_boundary,
#' tangential = TRUE
#' )
#' dat <- san_andreas |> cbind(PoR_shmax(san_andreas, PoR, "right"))
#'
#' distroll_circstats(dat$azi.PoR, distance = dat$distance, w = 1 / dat$unc, FUN = circular_mean)
#' distroll_confidence(dat$azi.PoR, distance = dat$distance, w = 1 / dat$unc)
#' distroll_dispersion(dat$azi.PoR, y = 135, distance = dat$distance, w = 1 / dat$unc)
#' distroll_dispersion_sde(dat$azi.PoR, y = 135, distance = dat$distance, w = 1 / dat$unc, R = 100)
NULL
#' @rdname rolling_test_dist
#' @export
distroll_circstats <- function(x, distance, FUN, width = NULL, min_n = 2,
align = c("right", "center", "left"), w = NULL,
sort = TRUE, ...) {
align <- match.arg(align)
stopifnot(length(x) == length(distance))
if (is.null(w)) w <- rep(1, length(x))
if (is.null(width)) {
width <- seq(from = min(distance, na.rm = TRUE), to = max(distance, na.rm = TRUE), length.out = 10) |>
diff() |>
dplyr::first()
}
dat <- data.frame(x, d = distance, w)
if (sort) dat <- dplyr::arrange(dat, d)
d <- numeric()
d_sort <- dat$d
ds <- seq(from = min(d_sort, na.rm = TRUE), to = max(d_sort, na.rm = TRUE), width)
res <- c()
ns <- c()
for (i in seq_along(ds)) {
if (align == "left") {
sub <- dplyr::filter(dat, dplyr::between(d, ds[i] - width, ds[i]))
} else if (align == "center") {
sub <- dplyr::filter(dat, dplyr::between(d, ds[i] - width / 2, ds[i] + width / 2))
} else {
sub <- dplyr::filter(dat, dplyr::between(d, ds[i], ds[i] + width))
}
ns[i] <- nrow(sub)
if (length(na.omit(sub$x)) >= min_n) {
res[i] <- do.call(FUN, list(x = sub$x, w = sub$w, ...))
} else {
res[i] <- NA
}
}
cbind(distance = ds, x = res, n = ns)
}
#' @rdname rolling_test_dist
#' @export
distroll_confidence <- function(x, distance, w = NULL, width = NULL, min_n = 2,
align = c("right", "center", "left"),
sort = TRUE, ...) {
align <- match.arg(align)
stopifnot(length(x) == length(distance))
if (is.null(w)) w <- rep(1, length(x))
if (is.null(width)) {
width <- seq(min(distance, na.rm = TRUE), max(distance, na.rm = TRUE), length.out = 10) |>
diff() |>
dplyr::first()
}
dat <- data.frame(x, d = distance, w)
if (sort) dat <- dplyr::arrange(dat, d)
d <- numeric()
d_sort <- dat$d
ds <- seq(from = min(d_sort, na.rm = TRUE), to = max(d_sort, na.rm = TRUE), width)
res <- c()
ns <- c()
for (i in seq_along(ds)) {
if (align == "left") {
sub <- dplyr::filter(dat, dplyr::between(d, ds[i] - width, ds[i]))
} else if (align == "center") {
sub <- dplyr::filter(dat, dplyr::between(d, ds[i] - width / 2, ds[i] + width / 2))
} else {
sub <- dplyr::filter(dat, dplyr::between(d, ds[i], ds[i] + width))
}
ns[i] <- nrow(sub)
if (length(na.omit(sub$x)) >= min_n) {
res[i] <- do.call(confidence_angle, list(x = sub$x, w = sub$w, ...))
} else {
res[i] <- NA
}
}
return(cbind(distance = ds, x = res, n = ns))
}
#' @rdname rolling_test_dist
#' @export
distroll_dispersion <- function(x, y, w = NULL, w.y = NULL, distance,
width = NULL, min_n = 2,
align = c("right", "center", "left"),
sort = TRUE, ...) {
align <- match.arg(align)
stopifnot(length(x) == length(distance))
if (length(y) == 1) y <- rep(y, length(x))
if (is.null(w)) w <- rep(1, length(x))
if (is.null(w.y)) w.y <- rep(1, length(x))
if (is.null(width)) {
width <- seq(min(distance, na.rm = TRUE), max(distance, na.rm = TRUE), length.out = 10) |>
diff() |>
dplyr::first()
}
dat <- data.frame(d = distance, x, y, w, w.y)
if (sort) dat <- dplyr::arrange(dat, d)
d <- numeric()
d_sort <- dat$d
ds <- seq(from = min(d_sort, na.rm = TRUE), to = max(d_sort, na.rm = TRUE), width)
res <- c()
ns <- c()
for (i in seq_along(ds)) {
if (align == "left") {
sub <- dplyr::filter(dat, dplyr::between(d, ds[i] - width, ds[i]))
} else if (align == "center") {
sub <- dplyr::filter(dat, dplyr::between(d, ds[i] - width / 2, ds[i] + width / 2))
} else {
sub <- dplyr::filter(dat, dplyr::between(d, ds[i], ds[i] + width))
}
ns[i] <- nrow(sub)
if (length(na.omit(sub$x)) >= min_n) {
res[i] <- do.call(circular_dispersion, list(x = sub$x, y = sub$y, w = sub$w, w.y = sub$w.y, ...))
} else {
res[i] <- NA
}
}
return(cbind(distance = ds, x = res, n = ns))
}
#' @rdname rolling_test_dist
#' @export
distroll_dispersion_sde <- function(x, y, w = NULL, w.y = NULL, distance,
width = NULL, min_n = 2,
align = c("right", "center", "left"),
sort = TRUE, ...) {
align <- match.arg(align)
stopifnot(length(x) == length(distance))
if (length(y) == 1) y <- rep(y, length(x))
if (is.null(w)) w <- rep(1, length(x))
if (is.null(w.y)) w.y <- rep(1, length(x))
if (is.null(width)) {
width <- seq(min(distance, na.rm = TRUE), max(distance, na.rm = TRUE), length.out = 10) |>
diff() |>
dplyr::first()
}
dat <- data.frame(x, d = distance, y, w, w.y)
if (sort) dat <- dplyr::arrange(dat, d)
d <- numeric()
d_sort <- dat$d
ds <- seq(from = min(d_sort, na.rm = TRUE), to = max(d_sort, na.rm = TRUE), width)
res <- c()
ns <- c()
for (i in seq_along(ds)) {
if (align == "left") {
sub <- dplyr::filter(dat, dplyr::between(d, ds[i] - width, ds[i]))
} else if (align == "center") {
sub <- dplyr::filter(dat, dplyr::between(d, ds[i] - width / 2, ds[i] + width / 2))
} else {
sub <- dplyr::filter(dat, dplyr::between(d, ds[i], ds[i] + width))
}
ns[i] <- nrow(sub)
if (length(na.omit(sub$x)) >= min_n) {
res[i] <- do.call(circular_dispersion_boot, list(x = sub$x, y = sub$y, w = sub$w, w.y = sub$w.y, ...))$sde
} else {
res[i] <- NA
}
}
return(cbind(distance = ds, x = res, n = ns))
}