#' STARX: spatiotemporal autoregressive model with exogenous variables
#'
#' This function generates weight matrix based on k nearest neighbor.
#' @import FNN
#' @importFrom Matrix sparseMatrix
w.knn <- function(coords, k = 10) {
coords <- as.matrix(coords)
n.coords <- nrow(coords)
i.vector <- rep(1:n.coords, each = k)
j.vector <- c(FNN::get.knn(coords, k = k)$nn.index)
W <- Matrix::sparseMatrix(
i = i.vector,
j = j.vector, x = 1 / k,
dims = c(n.coords, n.coords)
)
W
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.