--- title: "Geographical Cross Mapping Cardinality (GCMC)" author: "Wenbo Lv" date: "2025-06-25" output: rmarkdown::html_vignette vignette: > %\VignetteIndexEntry{GCMC} %\VignetteEngine{knitr::rmarkdown} %\VignetteEncoding{UTF-8} --- ## πŸ“Œ Research Notice The `GCMC` algorithm provided in this package is a research method currently **under peer review in the *International Journal of Geographical Information Science (IJGIS)* **. We provide early access to its implementation and a detailed vignette for reproducibility. Users should note that **methodological details may evolve** before final publication. ## Methodological Background To measure causal associations in spatial cross-sectional data, GCMC (Geographical Convergent Cross Mapping) employs a three-stage procedure: **Stage one** involves reconstructing the state space. Given two spatial variables, $x$ and $y$, this step requires determining a suitable embedding dimension $E$ and a spatial lag interval $\tau$. For each spatial location (unit) $i$, the attribute values from its spatial neighbors at lag orders $1, 1+\tau, \dots, 1+(E-1)\tau$ are collected. These values are then summarizedβ€”commonly using the meanβ€”to construct an embedding vector for each unit. Aggregating these vectors across all spatial units results in the reconstructed state spaces, denoted as $E_x$ and $E_y$. **Stage two** constructs the Intersectional Cardinality (IC) curve, which serves to evaluate causal strength. To measure whether $y$ causally affects $x$, one computes, for each $k$, the overlap between the $k$ nearest neighbors of $E_y$ and the corresponding "projected" neighbors traced through $E_x$. Specifically, for each point in $E_x$, its $k$ nearest neighbors are identified, and their mapped neighbors in $E_y$ are compared with the direct neighbors of $E_y$. The IC curve is formed by recording the number of shared neighbors across a range of $k = 1, 2, \dots, n$. This process can also be reversed to test for causality from $x$ to $y$. **Stage three** involves quantifying and validating the strength of causal associations. The area under the IC curve (AUC) provides a numerical measure of causal strength. To determine whether the observed causal strength is statistically significant, a hypothesis test is performed: the null assumes no causality, while the alternative assumes its presence. The DeLong palcements method is applied to evaluate the difference in AUCs under these hypotheses. It also yields confidence intervals, supporting robust causal inference. ## Usage examples ### An example of spatial lattice data Load the `spEDM` package and its county-level population density data: ``` r library(spEDM) popd_nb = spdep::read.gal(system.file("case/popd_nb.gal",package = "spEDM")) ## Warning in spdep::read.gal(system.file("case/popd_nb.gal", package = "spEDM")): ## neighbour object has 4 sub-graphs popd = readr::read_csv(system.file("case/popd.csv",package = "spEDM")) ## Rows: 2806 Columns: 7 ## ── Column specification ──────────────────────────────────────────────────────────────────────────── ## Delimiter: "," ## dbl (7): x, y, popd, elev, tem, pre, slope ## ## β„Ή Use `spec()` to retrieve the full column specification for this data. ## β„Ή Specify the column types or set `show_col_types = FALSE` to quiet this message. popd_sf = sf::st_as_sf(popd, coords = c("x","y"), crs = 4326) popd_sf ## Simple feature collection with 2806 features and 5 fields ## Geometry type: POINT ## Dimension: XY ## Bounding box: xmin: 74.9055 ymin: 18.2698 xmax: 134.269 ymax: 52.9346 ## Geodetic CRS: WGS 84 ## # A tibble: 2,806 Γ— 6 ## popd elev tem pre slope geometry ## * ## 1 780. 8 17.4 1528. 0.452 (116.912 30.4879) ## 2 395. 48 17.2 1487. 0.842 (116.755 30.5877) ## 3 261. 49 16.0 1456. 3.56 (116.541 30.7548) ## 4 258. 23 17.4 1555. 0.932 (116.241 30.104) ## 5 211. 101 16.3 1494. 3.34 (116.173 30.495) ## 6 386. 10 16.6 1382. 1.65 (116.935 30.9839) ## 7 350. 23 17.5 1569. 0.346 (116.677 30.2412) ## 8 470. 22 17.1 1493. 1.88 (117.066 30.6514) ## 9 1226. 11 17.4 1526. 0.208 (117.171 30.5558) ## 10 137. 598 13.9 1458. 5.92 (116.208 30.8983) ## # β„Ή 2,796 more rows ``` The false nearest neighbours (FNN) method helps identify the appropriate embedding dimension for reconstructing the state space of a time series or spatial cross-sectional data. A low embedding dimension that minimizes false neighbours is considered optimal. ``` r fnn(popd_sf, "popd", E = 1:15, eps = stats::sd(popd_sf$popd) / 10) ## E:1 E:2 E:3 E:4 E:5 E:6 E:7 E:8 ## 0.9707769 0.6404134 0.4885959 0.4012830 0.3346401 0.3296507 0.3260870 0.3150392 ## E:9 E:10 E:11 E:12 E:13 E:14 ## 0.3150392 0.2957947 0.2875980 0.2915182 0.2701354 0.2494654 ``` The false nearest neighbours (FNN) ratio decreased to approximately 0.3 when the embedding dimension E reached 10, and remained relatively stable thereafter. Therefore, we adopted $E = 10$ as the embedding dimension for subsequent GCMC analysis. Then, run GCMC: ``` r # temperature and population density g1 = gcmc(popd_sf, "tem", "popd", E = 10, k = 200, nb = popd_nb, progressbar = FALSE) g1 ## neighbors tem->popd popd->tem ## 1 200 0.574825 0.0892 # elevation and population density g2 = gcmc(popd_sf, "elev", "popd", E = 10, k = 200, nb = popd_nb, progressbar = FALSE) g2 ## neighbors elev->popd popd->elev ## 1 200 0.267125 0.07675 # elevation and temperature g3 = gcmc(popd_sf, "elev", "tem", E = 10, k = 200, nb = popd_nb, progressbar = FALSE) g3 ## neighbors elev->tem tem->elev ## 1 200 0.224575 0.47705 ``` Here we define two functions to process the results and plot the causal association matrix. ``` r .process_xmap_result = \(g){ tempdf = g$xmap tempdf$x = g$varname[1] tempdf$y = g$varname[2] tempdf = dplyr::select(tempdf, 1, x, y, x_xmap_y_mean,x_xmap_y_sig, y_xmap_x_mean,y_xmap_x_sig, dplyr::everything()) g1 = tempdf |> dplyr::select(x,y,y_xmap_x_mean,y_xmap_x_sig)|> purrr::set_names(c("cause","effect","ca","sig")) g2 = tempdf |> dplyr::select(y,x,x_xmap_y_mean,x_xmap_y_sig) |> purrr::set_names(c("cause","effect","ca","sig")) return(rbind(g1,g2)) } plot_ca_matrix = \(.tbf,legend_title = "Causal Association"){ .tbf = .tbf |> dplyr::mutate(sig_marker = dplyr::case_when( sig < 0.001 ~ "***", sig < 0.01 ~ "**", sig < 0.05 ~ "*", .default = "" )) |> dplyr::mutate(sig_marker = paste0(round(ca,3),sig_marker)) fig = ggplot2::ggplot(data = .tbf, ggplot2::aes(x = effect, y = cause)) + ggplot2::geom_tile(color = "black", ggplot2::aes(fill = ca)) + ggplot2::geom_abline(slope = 1, intercept = 0, color = "black", linewidth = 0.25) + ggplot2::geom_text(ggplot2::aes(label = sig_marker), color = "black", family = "serif") + ggplot2::labs(x = "Effect", y = "Cause", fill = legend_title) + ggplot2::scale_x_discrete(expand = c(0, 0)) + ggplot2::scale_y_discrete(expand = c(0, 0)) + ggplot2::scale_fill_gradient(low = "#9bbbb8", high = "#256c68") + ggplot2::coord_equal() + ggplot2::theme_void() + ggplot2::theme( axis.text.x = ggplot2::element_text(angle = 0, family = "serif"), axis.text.y = ggplot2::element_text(color = "black", family = "serif"), axis.title.y = ggplot2::element_text(angle = 90, family = "serif"), axis.title.x = ggplot2::element_text(color = "black", family = "serif", margin = ggplot2::margin(t = 5.5, unit = "pt")), legend.text = ggplot2::element_text(family = "serif"), legend.title = ggplot2::element_text(family = "serif"), legend.background = ggplot2::element_rect(fill = NA, color = NA), legend.direction = "horizontal", legend.position = "bottom", legend.margin = ggplot2::margin(t = 1, r = 0, b = 0, l = 0, unit = "pt"), legend.key.width = ggplot2::unit(20, "pt"), panel.grid = ggplot2::element_blank(), panel.border = ggplot2::element_rect(color = "black", fill = NA) ) return(fig) } ``` Organize the results into a long table: ``` r res1 = list(g1,g2,g3) |> purrr::map(.process_xmap_result) |> purrr::list_rbind() res1 ## cause effect ca sig ## 1 tem popd 0.574825 1.953434e-02 ## 2 popd tem 0.089200 3.830104e-116 ## 3 elev popd 0.267125 8.221767e-16 ## 4 popd elev 0.076750 6.249810e-148 ## 5 elev tem 0.224575 1.628641e-23 ## 6 tem elev 0.477050 4.880176e-01 ``` Visualize the result: ``` r plot_ca_matrix(res1) ``` ![**Figure 1**. **Causal associations among elevation, temperature, and population density.**](../man/figures/gcmc/fig1-1.png) ### An example of spatial grid data Load the `spEDM` package and its farmland NPP data: ``` r library(spEDM) npp = terra::rast(system.file("case/npp.tif", package = "spEDM")) # To save the computation time, we will aggregate the data by 3 times npp = terra::aggregate(npp, fact = 3, na.rm = TRUE) npp ## class : SpatRaster ## size : 135, 161, 5 (nrow, ncol, nlyr) ## resolution : 30000, 30000 (x, y) ## extent : -2625763, 2204237, 1867078, 5917078 (xmin, xmax, ymin, ymax) ## coord. ref. : CGCS2000_Albers ## source(s) : memory ## names : npp, pre, tem, elev, hfp ## min values : 187.50, 390.3351, -47.8194, -110.1494, 0.04434316 ## max values : 15381.89, 23734.5330, 262.8576, 5217.6431, 42.68803711 # Inspect NA values terra::global(npp,"isNA") ## isNA ## npp 14815 ## pre 14766 ## tem 14766 ## elev 14760 ## hfp 14972 terra::ncell(npp) ## [1] 21735 nnamat = terra::as.matrix(npp[[1]], wide = TRUE) nnaindice = which(!is.na(nnamat), arr.ind = TRUE) dim(nnaindice) ## [1] 6920 2 # Select 1500 non-NA pixels to predict: set.seed(2025) indices = sample(nrow(nnaindice), size = 1500, replace = FALSE) libindice = nnaindice[-indices,] predindice = nnaindice[indices,] ``` Determining optimal embedding dimension: ``` r fnn(npp, "npp", E = 1:15, lib = predindice, pred = predindice, eps = stats::sd(terra::values(npp[["npp"]]),na.rm = TRUE) / 10) ## E:1 E:2 E:3 E:4 E:5 E:6 E:7 E:8 ## 0.4666667 0.4000000 0.1333333 0.5333333 0.4000000 0.4000000 0.4666667 0.4000000 ## E:9 E:10 E:11 E:12 E:13 E:14 ## 0.4666667 0.4000000 0.4666667 0.4000000 0.4000000 0.2666667 ``` At E = 3, the false nearest neighbor ratio decreases to approximately 0.13, whereas it remains above 0.25 for all other embedding dimensions. Hence, E = 3 is chosen for the subsequent GCMC analysis. ``` r # precipitation and npp g1 = gcmc(npp, "pre", "npp", E = 3, k = 350, lib = predindice, pred = predindice, progressbar = FALSE) g1 ## neighbors pre->npp npp->pre ## 1 350 0.2181796 0.1876816 # temperature and npp g2 = gcmc(npp, "tem", "npp", E = 3, k = 350, lib = predindice, pred = predindice, progressbar = FALSE) g2 ## neighbors tem->npp npp->tem ## 1 350 0.3869143 0.3799673 # precipitation and temperature g3 = gcmc(npp, "pre", "tem", E = 3, k = 350, lib = predindice, pred = predindice, progressbar = FALSE) g3 ## neighbors pre->tem tem->pre ## 1 350 0.3206776 0.2926694 ``` Organize the results into a long table: ``` r res2 = list(g1,g2,g3) |> purrr::map(.process_xmap_result) |> purrr::list_rbind() res2 ## cause effect ca sig ## 1 pre npp 0.2181796 4.990672e-48 ## 2 npp pre 0.1876816 7.390331e-67 ## 3 tem npp 0.3869143 1.716558e-06 ## 4 npp tem 0.3799673 3.234268e-07 ## 5 pre tem 0.3206776 1.429895e-15 ## 6 tem pre 0.2926694 2.796329e-21 ``` Visualize the result: ``` r plot_ca_matrix(res2) ``` ![**Figure 2**. **Causal associations among precipitation, temperature, and NPP.**](../man/figures/gcmc/fig2-1.png)