Site icon R-bloggers

Spatial autocorrelation

[This article was first published on r.iresmi.net, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

One in a Million – CC-BY-NC by Thomas Hawk

Day 29 of 30DayMapChallenge: « Population » (previously).

< section id="setup" class="level2">

Setup

library(tidyverse)
library(sf)
library(glue)
library(sfdep)
< section id="data" class="level2">

Data

French administrative units (régions, départements, communes). Data is from an older post, based on IGN Admin Express.

# Keep only metropolitan France
com <- read_sf("~/data/adminexpress/adminexpress_cog_simpl_000_2022.gpkg",
               layer = "commune") |> 
  filter(insee_reg > "06") |> 
  st_transform("EPSG:2154") |> 
  mutate(densite = population / (surf_adminexpress_geo_ha / 100))

dep <- read_sf("~/data/adminexpress/adminexpress_cog_simpl_000_2022.gpkg", 
               layer = "departement_int") |> 
  st_transform("EPSG:2154") |> 
  st_join(com, left = FALSE)

reg <- read_sf("~/data/adminexpress/adminexpress_cog_simpl_000_2022.gpkg", 
               layer = "region_int") |> 
  st_transform("EPSG:2154") |> 
  st_join(com, left = FALSE)
< section id="compute-lisa" class="level2">

Compute LISA

We will map the local indicators of spatial association (LISA) (Anselin 1995): high-high means that the spatial autocorrelation is positive and the local Moran indice is high; thus it also means that the population density of the commune is high in a cluster of high density communes.

lisa <- com |> 
  mutate(nb = st_contiguity(geom),
         wt = st_weights(nb, allow_zero = TRUE),
         lm = local_moran(densite, nb = nb, wt = wt, zero.policy = TRUE))
< section id="map" class="level2">

Map

lisa |> 
  unnest(lm) |> 
  mutate(in_cluster = if_else(p_folded_sim <= 0.1, mean, "n.s.")) |>
  drop_na(in_cluster) |> # get rid of off coast single communes (a few islands)
  ggplot() +
  geom_sf(aes(fill = in_cluster), color = NA, lwd = 0.2) +
  geom_sf(data = dep, linewidth = 0.05, color = "grey60") +
  geom_sf(data = reg, linewidth = 0.15, color = "grey50") +
  scale_fill_manual(values = c("red", "pink", "lightblue", "blue", "white")) +
  labs(title = "Population spatial autocorrelation",
       subtitle = "Metropolitan France",
       fill = "density",
       #color = "density",
       caption = glue("Data: based on IGN Admin Express 2022
                       https://r.iresmi.net/ {Sys.Date()}")) +
  theme_void() +
  theme(plot.caption = element_text(size = 6),
        plot.background = element_rect(fill = "black"),
        text = element_text(color = "white"))

Figure 1: Population spatial autocorrelation
< section id="references" class="level2"> < !-- -->
< section class="quarto-appendix-contents">

References

Anselin, Luc. 1995. “Local Indicators of Spatial AssociationLISA.” Geographical Analysis 27 (2): 93–115. https://doi.org/10.1111/j.1538-4632.1995.tb00338.x.
To leave a comment for the author, please follow the link and comment on their blog: r.iresmi.net.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
Exit mobile version