R/w.knn.R

Defines functions w.knn

Documented in w.knn

#' 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
}
funstatpackages/STARX documentation built on Jan. 30, 2021, 11:47 p.m.