### raster
r <- rast(nrows=10, ncols=10, xmin=0)
values(r) <- 1:ncell(r)
autocor(r)
# rook's case neighbors
f <- matrix(c(0,1,0,1,0,1,0,1,0), nrow=3)
autocor(r, f)
# local
rc <- autocor(r, w=f, global=FALSE)
### numeric (for vector data)
f <- system.file("ex/lux.shp", package="terra")
v <- vect(f)
w <- relate(v, relation="touches")
# global
autocor(v$AREA, w)
# local
v$Gi <- autocor(v$AREA, w, "Gi")
plot(v, "Gi")
Run the code above in your browser using DataLab