Something went wrong. Try again.
spatially-constrained clustering in R
Something went wrong. Try again.
spatialcluster README-src.Rmd
6.5 kB · 164 lines
at main
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165---output: github_document---
<!-- README.md is generated from README.Rmd. Please edit that file -->
```{r, echo = FALSE}knitr::opts_chunk$set ( collapse = TRUE, comment = "#>", fig.path = "man/figures/README-")```
[](https://github.com/UrbanAnalyst/spatialcluster/actions?query=workflow%3AR-CMD-check.yaml)[](https://www.repostatus.org/#wip)[](https://app.codecov.io/gh/UrbanAnalyst/spatialcluster)
# spatialcluster
An **R** package for spatially-constrained clustering using either distance orcovariance matrices. "*Spatially-constrained*" means that the data from whichclusters are to be formed also map on to spatial coordinates, and theconstraint is that clusters must be spatially contiguous.
The package includes both an implementation of theREDCAP collection of efficient yet approximate algorithms described in [D. Guo's2008 paper, "Regionalization with dynamically constrained agglomerativeclustering andpartitioning."](https://www.tandfonline.com/doi/abs/10.1080/13658810701674970)(pdf available[here](https://www.semanticscholar.org/paper/Regionalization-with-dynamically-constrained-and-Guo/ead17df8aaa1aed0e433b3ae1ec1ec5c7e785b2b),with extension to covariance matrices, and a new technique for computingclusters using complete data sets. The package is also designed to analysematrices of spatial interactions (counts, densities) between sets of origin anddestination points. The spatial structure of interaction matrices is able to bestatistically analysed to yield both global statistics for the overall spatialstructure, and local statistics for individual clusters.
## Installation
The easiest way to install `spatialcluster` is be enabling the [corresponding`r-universe`](https://mpadge.r-universe.dev/):
```{r r-univ, eval = FALSE}options (repos = c ( mpadge = "https://mpadge.r-universe.dev", CRAN = "https://cloud.r-project.org"))```
The package can then be installed as usual with,
```{r install, eval = FALSE}install.packages ("spatialcluster")```
Alternatively, the package can also be installed using any of the followingoptions:
```{r gh-installation, eval = FALSE}# install.packages("remotes")remotes::install_git ("https://codeberg.org/mpadge/spatialcluster")remotes::install_git ("https://git.sr.ht/~mpadge/spatialcluster")remotes::install_bitbucket ("mpadge/spatialcluster")remotes::install_gitlab ("mpadge/spatialcluster")remotes::install_github ("UrbanAnalyst/spatialcluster")```
## Usage
The two main functions, `scl_redcap()` and `scl_full()`, implement differentalgorithms for spatial clustering. The former implements the REDCAP collectionof efficient yet approximate algorithms described in [D. Guo's 2008 paper,"Regionalization with dynamically constrained agglomerative clustering andpartitioning."](https://www.tandfonline.com/doi/abs/10.1080/13658810701674970)(pdf available[here](https://www.semanticscholar.org/paper/Regionalization-with-dynamically-constrained-and-Guo/ead17df8aaa1aed0e433b3ae1ec1ec5c7e785b2b)),with extension here to apply clustering to covariance matrices. Thesealgorithms are computationally efficient yet generate only *approximate*estimates of underlying clusters. The second function, `scl_full()`, tradescomputational efficiency for accuracy, through generating clustering schemesusing all available data.
In short:
- `scl_full()` should always be preferred as long as it returns results within a reasonable amount of time- `scl_redcap()` should be used only where data are too large for `scl_full()` to be run in a reasonable time.
For clustering a group of `n` points, both of these functions require threemain arguments:
1. A rectangular matrix of spatial coordinates of points to be clustered (`n` rows; at least 2 columns);2. An `n`-by-`n` square matrix quantifying relationships between those points;3. A single value (`ncl`) specifying the desired number of clusters.
The following code demonstrates usage with randomly-generated data:```{r}set.seed (1)n <- 100xy <- matrix (runif (2 * n), ncol = 2)dmat <- matrix (runif (n^2), ncol = n)```
The load the package and call the function:
```{r full-single, echo = TRUE, eval = TRUE}library (spatialcluster)scl <- scl_full (xy, dmat, ncl = 8)plot (scl)```
Both functions return a `list` with the following components:
```{r list-components}names (scl)```
- `tree` details distances and cluster numbers for all pairwise comparisons between objects.- `merges` details increasing distances at which each pair of objects was merged into a single cluster.- `ord` provides the order of the merges (for `scl_full()` only).- `nodes` records the spatial coordinates of each point (node) of the input data.- `pars` retains the parameters used to call the clustering function.- `statsitics` returns the clustering statistics, both for individual clusters and an overall global statistic for the clustering scheme as a whole.
See the "_Get Started_" vignette for more details.
## A Cautionary Note
The following plot compares the results of applying four different clusteringalgorithms to the same data.
```{r cautionary, eval = TRUE, fig.width = 7, fig.height = 7}library (ggplot2)library (gridExtra)scl <- scl_full (xy, dmat, ncl = 8, linkage = "single")p1 <- plot (scl) + ggtitle ("full-single")scl <- scl_redcap (xy, dmat, ncl = 8, linkage = "single")p2 <- plot (scl) + ggtitle ("redcap-single")scl <- scl_redcap (xy, dmat, ncl = 8, linkage = "average")p3 <- plot (scl) + ggtitle ("redcap-average")scl <- scl_redcap (xy, dmat, ncl = 8, linkage = "complete")p4 <- plot (scl) + ggtitle ("redcap-complete")
grid.arrange (p1, p2, p3, p4, ncol = 2)```
This example illustrates the universal danger in all clustering algorithms: theycan not fail to produce results, even when the data fed to them are definitelydevoid of any information as in this example. Clustering algorithms should onlybe applied to reflect a very specific hypothesis for why data should beclustered in the first place; spatial clustering algorithms should only beapplied to reflect two very specific hypothesis for (i) why data should beclustered at all, and (ii) why those clusters should manifest a spatialpattern.