-
Notifications
You must be signed in to change notification settings - Fork 0
/
dists.R
137 lines (128 loc) · 3.62 KB
/
dists.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
#' Generic function for calculating distance matrices
#'
#' @param x An **R** object.
#' @param ... Additional parameters or attributes.
#'
#' @return A list of signed distance matrices: `h` (Euclidean), `h1`
#' (horizontal), and `h2` (vertical) with the same dimensions.
#' @export
#' @family functions related to the class
dists <- function(x, ...) {
UseMethod("dists")
}
#' Calculating distance matrices for an `mcgf` object
#'
#' @name dists.mcgf
#' @aliases `dists<-`
#'
#' @param x An `mcgf` object.
#' @param ... Additional parameters or attributes.
#' @param return_grid Logical; used when `locations` in `x` are longitudes and
#' latitudes.
#'
#' @return A list of signed distance matrices: `h` (Euclidean), `h1`
#' (horizontal), and `h2` (vertical).
#' @export
#'
#' @details
#' If the `dists` attribute is available in `x`, it will be printed. Otherwise
#' `dists` will be calculated based on the `locations` attribute.
#'
#' @examples
#' data <- cbind(S1 = 1:5, S2 = 4:8, S3 = 5:9)
#' lon <- c(110, 120, 130)
#' lat <- c(50, 55, 60)
#' locations <- cbind(lon, lat)
#'
#' # if locations are longitudes and latitudes
#' obj <- mcgf(data = data, locations = locations)
#' obj
#' dists(obj)
#' dists(obj) <- dists(obj)
#' obj
#'
#' # if locations are just coordinates in a 2D plane:
#' obj <- mcgf(data = data, locations = locations, longlat = FALSE)
#' obj
#'
#' # calculate distances
#' dists(obj)
#'
#' # add distances to the `mcgf` object
#' dists(obj) <- dists(obj)
#' obj
dists.mcgf <- function(x, return_grid = FALSE, ...) {
if (return_grid && attr(x, "longlat", exact = TRUE)) {
locations <- attr(x, "locations", exact = TRUE)
origin <- attr(x, "origin", exact = TRUE)
dists <- find_dists(locations,
longlat = TRUE, origin = origin,
return_grid = TRUE, ...
)
} else {
dists <- attr(x, "dists", exact = TRUE)
}
if (is.null(dists)) {
locations <- attr(x, "locations", exact = TRUE)
longlat <- attr(x, "longlat", exact = TRUE)
origin <- attr(x, "origin", exact = TRUE)
if (return_grid) {
dists <- find_dists(locations,
longlat = longlat, origin = origin,
return_grid = TRUE, ...
)
} else {
dists <- find_dists(locations,
longlat = longlat, origin = origin,
...
)
}
}
return(dists)
}
#' @rdname dists.mcgf
#' @param value List of signed distance matrices, outputted from [dists()].
#' @export
`dists<-` <- function(x, value) {
dists <- check_dists(
dists = value, n_var = ncol(x),
names = colnames(x), name_dists = "value"
)
attr(x, "dists") <- value
return(x)
}
#' Generate random distance matrices
#'
#' @param N Number of locations.
#' @param names Names of locations.
#' @param scale Scale of the distance matrices. Default is 100.
#'
#' @return List of signed distances.
#' @export
#'
#' @details
#' This function generates random distance matrices using `rnorm`. `scale`
#' controls the scale of the distance matrices.
#'
#' @examples
#' set.seed(123)
#' rdists(3)
#' rdists(3, scale = 1)
#' rdists(3, names = LETTERS[1:3])
rdists <- function(N, names, scale = 100) {
if (!is_numeric_scalar(N)) {
stop("`N` must be an integer.")
}
x <- stats::rnorm(N) * scale
y <- stats::rnorm(N) * scale
grid <- cbind(x, y)
if (!missing(names)) {
if (length(names) != N) {
stop("Length of `names` must be `N`.")
}
} else {
names <- paste0("loc", 1:N)
}
dists <- .find_dists(grid, names = names, longlat = F)
return(dists)
}