View source: R/add-spatial-lags.R
add_spatial_lags | R Documentation |
Computes spatial lags for a given numeric variable in a dataset using a neighborhood list from the spdep package. It supports inverse distance weighting, exponential, and double power decay weighting methods along with various normalization procedures.
add_spatial_lags(nblist, y, .data, lags, type = NULL, parallel = FALSE, ...)
nblist |
An object of class "nb" from the spdep package which represents the neighborhood structure. |
y |
A character string indicating the name of the numeric variable in |
.data |
A data frame of class "sf" containing the variable specified by |
lags |
A numeric value specifying the number of spatial lags to compute. |
type |
A character string indicating the type of spatial weights to use.
Accepted values are |
parallel |
(default: FALSE) Logical indicating whether to use parallel processing.
Requires having purrr (>= 1.0.4.9000) installed, mirai (>= 2.1.0.9000) package installed and loaded, and setting |
... |
Additional arguments passed to |
To obtain a neighborhood list of class "nb," first, a neighborhood algorithm is fit to the geometry of sf dataset. Then, that object is coerced into a neighborhood list. For a workflow, see the Misc section in the Geospatial, Spatial Weights note of my Data Science notebook.
Valid Types: ("idw"
, "exp"
, "dpd"
).
Valid Styles: ("W"
, "B"
, "C"
, "S"
, "U"
, "minmax"
, "raw"
).
See the Spatial Weights section in the Geospatial, Spatial Weights note of my Data Science notebook for details
The spatial weights summary is extracted from the output of printing the spdep::nb2listw
or spdep::nb2listwdist
object. It contains characteristics such as the number of regions, number of nonzero links, percentage of nonzero weights, average number of links.
n: This refers to the number of regions (or spatial units) in your dataset.
nn: This refers to the total number of possible pairwise relationships between the regions. It is calculated as n × n. This represents the total number of possible links if every region were connected to every other region, including itself.
S0: This is the sum of all weights.
S1: This is related to the sum of the squares of the weights.
S2: This is related to the sum of the products of the weights for each pair of neighbors.
S0, S1, S2 are constants used in inference for global spatial autocorrelation statistics
A tibble containing the original dataset with additional columns for each
computed spatial lag. The spatial lag columns are named "spatlag_<lag>_<y>"
.
The output also includes weight summary attributes named "summ_wgts_spatlag_<lag>"
.
library(spdep, quietly = TRUE)
ny8_sf <-
st_read(system.file(
"shapes/NY8_bna_utm18.gpkg",
package = "spData"),
quiet = TRUE)
dplyr::glimpse(ny8_sf)
ny8_ct_sf <-
st_centroid(st_geometry(ny8_sf),
of_largest_polygon = TRUE)
ny88_nb_sf <-
knn2nb(knearneigh(ny8_ct_sf,
k = 4))
# Compute spatial lags
tib_spat_lags <-
add_spatial_lags(
nblist = ny88_nb_sf,
y = "PCTOWNHOME",
.data = ny8_sf,
lags = 2,
type = "dpd",
dmax = 25000,
style = "W",
zero.policy = TRUE
)
tib_spat_lags |>
dplyr::select(PCTOWNHOME,
spatlag_1_PCTOWNHOME,
spatlag_2_PCTOWNHOME) |>
dplyr::glimpse()
cat(attributes(tib_spat_lags)$summ_wgts_spatlag_1, sep = "\n")
rlang::check_installed(
"mirai (>= 2.1.0.9000)",
action = function(...) {
remotes::install_version('mirai',
version = ">= 2.1.0.9000",
repos = c('https://shikokuchuo.r-universe.dev',
'https://cloud.r-project.org'))
}
)
library(mirai)
daemons(2)
tib_spat_lags_para <-
add_spatial_lags(
nblist = ny88_nb_sf,
y = "PCTOWNHOME",
.data = ny8_sf,
lags = 2,
type = "exp",
zero.policy = TRUE,
parallel = TRUE
)
daemons(0)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.