# Pattern-based spatial analysis in R: an introduction

**rstats on Jakub Nowosad's website**, 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.

TLTR: **motif** is an R package aimed for pattern-based spatial analysis. It allows for spatial analysis such as search, change detection, and clustering to be performed on spatial patterns. This blog post introduces basic ideas behind the pattern-based spatial analysis, and shows the types of problems to which it can be applied.

# Spatial patterns

Discovering and describing patterns is a vital part of many spatial analysis. However, spatial data is gathered in many ways and forms, which requires different approaches to expressing spatial patterns. Other methods are applied when we work with numerical or categorical variables, also other methods are used to find patterns in point datasets, lines datasets, or raster datasets. Next, patterns and their relevance depend on a studied scale, with different patterns found on small or large scales, or data of different spatial resolutions. Finally, the way we describe patterns should depend on our main goal.

In this blog post, I only focus on a small subset of possible problems related to spatial patterns – I am only interested in categorical raster data. Categorical rasters, such as land cover maps, soil categories, or any other categorized images, express spatial patterns by two inter-related properties: composition and configuration. Composition shows how many different categories we have, and how much area they occupy, while configuration focuses on the spatial arrangement of the categories.

# Landscape metrics

Spatial patterns in categorical raster data are most often described by landscape metrics (landscape indices). A landscape metric is a single numerical value expressing some property of a raster, such as diversity of categories or spatial aggregation of classes. In the last 40 or so years, several hundred of different spatial metrics were developed. They are widely used in the field of landscape ecology, but their application can be also found in some other distant fields, even such as clinical pathology (laboratory medicine).

The **landscapemetrics** package allows calculating various landscape metrics in R. It contains a simple categorical raster named `landscape`

with three classes, which we can use to calculate some metrics. To learn more about these ideas and the **landscapemetrics** package visit https://r-spatialecology.github.io/landscapemetrics.

```
library(landscapemetrics)
library(raster)
plot(landscape)
```

For example, the `lsm_l_shdi()`

function calculates Shannon’s diversity index, which shows how many categories we have and what are they abundance. It is 0 when only one patch is present and increases, without limit, as the number of classes increases, while their proportions are similar.

```
lsm_l_shdi(landscape)
## # A tibble: 1 x 6
## layer level class id metric value
## <int> <chr> <int> <int> <chr> <dbl>
## 1 1 landscape NA NA shdi 1.01
```

The `lsm_l_ai()`

function focuses on the configuration of spatial patterns by calculating the aggregation index. It equals to 0 for maximally disaggregated areas and 100 for maximally aggregated ones.

```
lsm_l_ai(landscape)
## # A tibble: 1 x 6
## layer level class id metric value
## <int> <chr> <int> <int> <chr> <dbl>
## 1 1 landscape NA NA ai 81.1
```

# Spatial signatures

The above examples show how we condensed some information about raster data to just one number. It can be useful in a multitude of cases when we want to connect some aspect of a spatial pattern to external processes. However, what do to, if our goal is to find areas with similar spatial patterns?

In theory, we could calculate landscape metrics for many areas and then search for those which have the most similar values to our area of interest. This approach, however, leaves us with a number of problems, including which landscape metrics to use. Many landscape metrics are highly correlated, and their interrelations are hard to interpret.

An alternative approach, in this case, is to use a spatial signature. A spatial signature is a multi-number description that compactly stores information about the composition and configuration of a spatial pattern. Therefore, instead of having just one number representing a raster, we have several numbers that condense information about this location. We can calculate spatial signatures for many rasters, which allows us to find the most similar rasters, describe changes between rasters, or group (cluster) rasters based on the spatial patterns.

# motif

Search, change detection, and clustering of spatial patterns have been possible in GRASS GIS using the GeoPAT module or command-line tool GeoPAT 2. All of the above actions can also be now performed natively in R with the **motif** package. In a series of blog posts, I plan to show and explain several use cases. They include:

*A. Finding areas similar to the area of interest*

*B. Comparing changes between two times*

*C. Clustering areas with similar patterns of more than one layer of data*

If you do not want to wait for the next blog post, you can install the the **motif** package with:

```
remotes::install_github("nowosad/motif")
```

You can read more about it in the Landscape Ecology article or its preprint:

Nowosad, J. Motif: an open-source R tool for pattern-based spatial analysis. Landscape Ecol (2020). https://doi.org/10.1007/s10980-020-01135-0

You can also visit the package website at https://nowosad.github.io/motif and the GitHub repository with examples at https://github.com/Nowosad/motif-examples.

**leave a comment**for the author, please follow the link and comment on their blog:

**rstats on Jakub Nowosad's website**.

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.