<?xml version="1.0" encoding="UTF-8"?>
<rss  xmlns:atom="http://www.w3.org/2005/Atom" 
      xmlns:media="http://search.yahoo.com/mrss/" 
      xmlns:content="http://purl.org/rss/1.0/modules/content/" 
      xmlns:dc="http://purl.org/dc/elements/1.1/" 
      version="2.0">
<channel>
<title>Thinking in spatial patterns</title>
<link>https://jakubnowosad.com/posts.html</link>
<atom:link href="https://jakubnowosad.com/posts.xml" rel="self" type="application/rss+xml"/>
<description>Blog posts by Jakub Nowosad on R, Python, spatial data science, geocomputation, and reproducible research.</description>
<generator>quarto-1.11.9</generator>
<lastBuildDate>Tue, 08 Sep 2026 00:00:00 GMT</lastBuildDate>
<item>
  <title>Navigating Challenges in Spatial Machine Learning</title>
  <dc:creator>Jakub Nowosad</dc:creator>
  <link>https://jakubnowosad.com/posts/2026-09-08-erdkunde/</link>
  <description><![CDATA[ 





<p>Spatial machine learning has become a standard tool for producing environmental and geographic prediction maps. It is now relatively (technically) easy to combine field observations with remote sensing, climate, terrain, or other predictor layers and fit a strong machine learning model. The harder question is whether the resulting map is reliable, transferable, and reproducible.</p>
<p>In our paper, <a href="https://doi.org/10.3112/erdkunde.2026.04.01"><em>Navigating challenges in spatial machine learning: validation, uncertainty, algorithms, and reproducibility</em></a>, we discuss this question from several connected angles. The paper was written by Jakub Nowosad, Carmelo Bonannella, Darius Görgen, Marta Jemeljanova, Teja Kattenborn, Jan Linnenbrink, Hanna Meyer, Madlene Nussbaum, Luca Patelli, Rolf Simoes, and Evelyn Uuemaa<sup>1</sup>, and published in <em>Erdkunde</em>.</p>
<p>The main argument is that spatial machine learning cannot simply reuse standard machine learning habits without modification. Spatial dependence, clustered and biased sampling, heterogeneous landscapes, and domain transfer all affect how models should be evaluated and interpreted. A model can appear accurate under a standard validation approach and still be unreliable where predictions are needed.</p>
<p>We structure the paper around six themes:</p>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://jakubnowosad.com/posts/2026-09-08-erdkunde/fig1.png" class="img-fluid figure-img"></p>
<figcaption>The main themes of the paper</figcaption>
</figure>
</div>
<p>Several practical messages follow from this. Validation should match the intended prediction scenario, and prediction-domain adaptive evaluation approaches can help when sticking just to simple random or spatial cross-validation is not appropriate. When training data do not represent the prediction domain, areas outside the model’s area of applicability should be identified, masked, or at least communicated clearly. Performance should not be reduced to a single global number: residual maps, spatial patterns of error, uncertainty, and thinking of the goal of the analysis are all important.</p>
<p>The paper also highlights that algorithmic progress alone is not enough. We need benchmark datasets with different spatial properties, clearer comparisons against baseline methods, better uncertainty frameworks, and software that makes robust spatial workflows easier to apply. R currently has several dedicated tools for spatial machine learning workflows, while Python has a strong general-purpose machine learning ecosystem but fewer mature implementations of spatial-specific methods. In both languages, reproducibility still depends heavily on careful reporting of data preparation, resampling, prediction, software versions, and computational choices.</p>
<p>The final part of the paper argues for standardized reporting protocols for spatial machine learning. Such protocols could help researchers document modeling aims, spatial characteristics of the data, validation design, uncertainty treatment, computational requirements, and reproducibility materials. This is also connected to ongoing work on the <a href="https://github.com/LOEK-RS/STeMP">Spatio-Temporal Modelling Protocol (STeMP)</a>.</p>
<blockquote class="blockquote">
<p>The short version is this: spatial machine learning needs to move beyond performance-driven mapping toward workflows that are spatially explicit, uncertainty-aware, and reproducible. Better models are useful, but better evaluation, clearer uncertainty communication, stronger software, and transparent reporting are equally important.</p>
</blockquote>




<div id="quarto-appendix" class="default"><section id="footnotes" class="footnotes footnotes-end-of-document"><h2 class="anchored quarto-appendix-heading">Footnotes</h2>

<ol>
<li id="fn1"><p>And builds partly on discussions from the <a href="https://jakubnowosad.com/advsml_2025/">Advances in Spatial Machine Learning 2025 workshop</a>. Many thanks to the workshop participants for their feedback and ideas!↩︎</p></li>
</ol>
</section><section class="quarto-appendix-contents" id="quarto-citation"><h2 class="anchored quarto-appendix-heading">Citation</h2><div><div class="quarto-appendix-secondary-label">BibTeX citation:</div><div class="code-copy-outer-scaffold"><pre class="sourceCode quarto-appendix-bibtex code-with-copy"><code class="sourceCode bibtex">@online{nowosad2026,
  author = {Nowosad, Jakub},
  title = {Navigating {Challenges} in {Spatial} {Machine} {Learning}},
  date = {2026-09-08},
  url = {https://jakubnowosad.com/posts/2026-09-08-erdkunde/},
  doi = {10.59350/kba53-aqt33},
  langid = {en}
}
</code></pre></div><div class="quarto-appendix-secondary-label">For attribution, please cite this work as:</div><div id="ref-nowosad2026" class="csl-entry quarto-appendix-citeas">
Nowosad, Jakub. 2026. <span>“Navigating Challenges in Spatial Machine
Learning.”</span> September 8. <a href="https://doi.org/10.59350/kba53-aqt33">https://doi.org/10.59350/kba53-aqt33</a>.
</div></div></section></div> ]]></description>
  <category>spatial</category>
  <category>spatial-machine-learning</category>
  <category>spatial-patterns</category>
  <category>rstats</category>
  <category>python</category>
  <category>paper</category>
  <guid>https://jakubnowosad.com/posts/2026-09-08-erdkunde/</guid>
  <pubDate>Tue, 08 Sep 2026 00:00:00 GMT</pubDate>
</item>
<item>
  <title>A world still to be mapped: reflections on geocomputation in R: takeaways from the talk and workshop at UseR! 2026</title>
  <dc:creator>Jakub Nowosad</dc:creator>
  <link>https://jakubnowosad.com/posts/2026-07-21-user/</link>
  <description><![CDATA[ 





<p>At <a href="https://user2026.r-project.org/">useR! 2026</a> in Warsaw, I contributed to a workshop on <em>Geocomputation with R</em> (July 6, 2026) and gave a keynote entitled <em>A world still to be mapped: reflections on geocomputation in R</em> (July 8, 2026). Both were opportunities to consider what the R spatial ecosystem already makes possible, its strengths, limitations, and what remains to be done.</p>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><a href="https://jakubnowosad.com/user2026/"><img src="https://jakubnowosad.com/posts/2026-07-21-user/user2026-title-slide.png" class="img-fluid figure-img"></a></p>
<figcaption>Title slide of the talk</figcaption>
</figure>
</div>
<p><strong>Keynote slides:</strong> <a href="https://jakubnowosad.com/user2026/" class="uri">https://jakubnowosad.com/user2026/</a></p>
<p><strong>Workshop materials:</strong> <a href="https://github.com/geocompx/user26" class="uri">https://github.com/geocompx/user26</a></p>
<section id="workshop" class="level2">
<h2 class="anchored" data-anchor-id="workshop">Workshop</h2>
<p>The workshop, led by Jannes Muenchow and based on the second edition of <a href="https://r.geocompx.org/"><em>Geocomputation with R</em></a>, introduced some of the core building blocks of spatial work in R. It covered vector data with the <strong>sf</strong> package, raster data with the <strong>terra</strong> package, and spatial data visualization with the <strong>tmap</strong> package. I presented the visualization part, showing the main ingredients of the mapping workflow and demonstrating how maps can be used to explore geographic data and communicate analytical results.</p>
<p>These tools represent a mature and increasingly interoperable ecosystem. They make it possible to import, manipulate, analyze, and visualize geographic data while drawing on R’s wider capabilities for data processing and statistics. The workshop combined presentations, live coding, and exercises to provide a practical starting point for working with these tools. You can try the exercises yourself by following the instructions in <a href="https://github.com/geocompx/user26">the workshop materials</a>.</p>
</section>
<section id="talk" class="level2">
<h2 class="anchored" data-anchor-id="talk">Talk</h2>
<p>At UseR! 2026, <a href="https://jakubnowosad.com/user2026/">I spoke about geocomputation in R</a> through the lens of my work as a computational geographer. I started by showing that geography is not only about drawing maps or attaching coordinates to data. For me, it is a way of asking questions about what is happening, where it is happening, why it is happening there, and how those patterns change over time. To answer those questions, I use a combination of spatial data, statistical models, and visualization.</p>
<p>I also reflected on how far the R spatial ecosystem has come. Over the last few decades, R has grown into a strong environment for spatial analysis, with tools for vector and raster data, visualization, reproducibility, and integration with broader geospatial infrastructure. At the same time, some of the most important gaps are no longer just technical. We can keep building better packages, but that alone will not solve problems like weak validation practices or misleading maps – the challenge is not only to make better maps, but to make predictions and maps that we can trust. For these purposes, I discussed ideas such as prediction-domain adaptive evaluation in the context of spatial machine learning and the importance of proper color palettes and projection choices in the context of spatial visualization.</p>
<p>My conclusion was that the future of geocomputation in R depends as much on community as on code. The R spatial ecosystem is strong in scientific rigor, geospatial infrastructure, visualization, and reproducible workflows, but these strengths depend on an active community. Good spatial work still requires data, domain knowledge, careful evaluation, and honest communication. We should, therefore, continue building tools while also supporting people who maintain software, share examples and expertise, mentor others, and connect methods to real-world problems.</p>


</section>

<div id="quarto-appendix" class="default"><section class="quarto-appendix-contents" id="quarto-citation"><h2 class="anchored quarto-appendix-heading">Citation</h2><div><div class="quarto-appendix-secondary-label">BibTeX citation:</div><div class="code-copy-outer-scaffold"><pre class="sourceCode quarto-appendix-bibtex code-with-copy"><code class="sourceCode bibtex">@online{nowosad2026,
  author = {Nowosad, Jakub},
  title = {A World Still to Be Mapped: Reflections on Geocomputation in
    {R:} Takeaways from the Talk and Workshop at {UseR!} 2026},
  date = {2026-07-21},
  url = {https://jakubnowosad.com/posts/2026-07-21-user/},
  doi = {10.59350/qvdka-a0c05},
  langid = {en}
}
</code></pre></div><div class="quarto-appendix-secondary-label">For attribution, please cite this work as:</div><div id="ref-nowosad2026" class="csl-entry quarto-appendix-citeas">
Nowosad, Jakub. 2026. <span>“A World Still to Be Mapped: Reflections on
Geocomputation in R: Takeaways from the Talk and Workshop at UseR!
2026.”</span> July 21. <a href="https://doi.org/10.59350/qvdka-a0c05">https://doi.org/10.59350/qvdka-a0c05</a>.
</div></div></section></div> ]]></description>
  <category>spatial</category>
  <category>spatial-machine-learning</category>
  <category>viz</category>
  <category>rstats</category>
  <category>rspatial</category>
  <category>talk</category>
  <category>workshop</category>
  <guid>https://jakubnowosad.com/posts/2026-07-21-user/</guid>
  <pubDate>Tue, 21 Jul 2026 00:00:00 GMT</pubDate>
</item>
<item>
  <title>Rethinking Validation for Spatial Machine Learning: Takeaways from the Talk</title>
  <dc:creator>Jakub Nowosad</dc:creator>
  <link>https://jakubnowosad.com/posts/2026-07-03-ml4eo/</link>
  <description><![CDATA[ 





<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://jakubnowosad.com/posts/2026-07-03-ml4eo/keynote-title-slide.png" class="img-fluid figure-img"></p>
<figcaption>Title slide of the talk</figcaption>
</figure>
</div>
<p><strong>Keynote slides:</strong> <a href="https://jakubnowosad.com/ml4eo2026/" class="uri">https://jakubnowosad.com/ml4eo2026/</a></p>
<p><strong>Workshop materials:</strong> <a href="https://jakubnowosad.com/ml4eo2026workshop/" class="uri">https://jakubnowosad.com/ml4eo2026workshop/</a></p>
<p>Machine learning is now deeply embedded<sup>1</sup> in Earth observation workflows, from mapping current environmental conditions to forecasting future change. However, the quality of a spatial prediction map cannot be judged only by how well a model performs on a convenient test sample. In spatial problems, the gap between where we have observations and where we want to make predictions is often a crucial factor in determining whether a model can be trusted.</p>
<p>At the <a href="https://ml4eo.org/">Machine Learning for Earth Observation 2026</a> conference in Exeter<sup>2</sup>, I gave a keynote talk entitled <em>Rethinking Validation for Spatial Machine Learning</em> (June 22, 2026). The next day, I showed some practical ways to implement these ideas in a workshop called <em>Where your models can be trusted: evaluating spatial machine learning reliably</em> (June 23, 2026). Both focused on the same general question: how can we evaluate spatial machine learning in a way that reflects the actual prediction task?</p>
<p>The keynote was structured around three assumptions that are easy to make, but often unsafe in spatial prediction:</p>
<ul>
<li>We can predict everywhere. In practice, we validate where we have data, but predict in places that may be poorly represented by the training sample. Tools such as Area of Applicability (AoA) and Local Point Density (LPD) help identify parts of the prediction domain where environmental conditions are more or less supported by the available data.</li>
<li>There is one “correct” validation approach. In reality, validation should follow the prediction task. Random cross-validation can be too optimistic when observations are spatially clustered, while spatial cross-validation can be too pessimistic when the intended prediction scenario is closer to interpolation. Adaptive strategies such as k-Nearest Neighbor Distance Matching (kNNDM) try to align validation folds with the distance structure of the prediction domain.</li>
<li>All validation points are equal. Prediction conditions are not equally common across a map, so a single unweighted average error can misrepresent the expected performance over the full prediction domain. This motivates thinking about how validation samples should be weighted by their prevalence in the places where predictions will be used.</li>
</ul>
<p>Together, these points lead to the idea of <strong>prediction-domain adaptive evaluation</strong>: first define the prediction domain, then construct validation folds that reflect it, and finally summarize performance in a way that accounts for how common different prediction conditions are. This is not a complete theory of spatial machine learning evaluation, but it is a useful step away from treating validation as a model-only problem. (To learn more about these ideas, read our preprint: <a href="https://arxiv.org/abs/2605.13689" class="uri">https://arxiv.org/abs/2605.13689</a>.)</p>
<p>The workshop turned these ideas into practical R workflows. Using synthetic and real-world-inspired examples, we used and discussed techniques for Area of Applicability, Local Point Density, compared random cross-validation, spatial cross-validation, and kNNDM cross-validation, and looked at error profiles. The hands-on materials also include exercises, where participants can compare validation strategies, map areas of applicability, and explore how expected error varies across space.</p>
<p>The main takeaway is simple: for spatial machine learning, the question is not only <em>How accurate is the model?</em> It is also <em>Where can the model be trusted?</em></p>




<div id="quarto-appendix" class="default"><section id="footnotes" class="footnotes footnotes-end-of-document"><h2 class="anchored quarto-appendix-heading">Footnotes</h2>

<ol>
<li id="fn1"><p>And embeddings are too, but that’s a story for another day↩︎</p></li>
<li id="fn2"><p>Many thanks to the organizers for inviting me to speak and for hosting a great event! The next edition of the conference will be in Exeter again in June 2027, and I highly recommend it to anyone interested in (broad) spatial machine learning.↩︎</p></li>
</ol>
</section><section class="quarto-appendix-contents" id="quarto-citation"><h2 class="anchored quarto-appendix-heading">Citation</h2><div><div class="quarto-appendix-secondary-label">BibTeX citation:</div><div class="code-copy-outer-scaffold"><pre class="sourceCode quarto-appendix-bibtex code-with-copy"><code class="sourceCode bibtex">@online{nowosad2026,
  author = {Nowosad, Jakub},
  title = {Rethinking {Validation} for {Spatial} {Machine} {Learning:}
    {Takeaways} from the {Talk}},
  date = {2026-07-03},
  url = {https://jakubnowosad.com/posts/2026-07-03-ml4eo/},
  doi = {10.59350/5xsxn-gab98},
  langid = {en}
}
</code></pre></div><div class="quarto-appendix-secondary-label">For attribution, please cite this work as:</div><div id="ref-nowosad2026" class="csl-entry quarto-appendix-citeas">
Nowosad, Jakub. 2026. <span>“Rethinking Validation for Spatial Machine
Learning: Takeaways from the Talk.”</span> July 3. <a href="https://doi.org/10.59350/5xsxn-gab98">https://doi.org/10.59350/5xsxn-gab98</a>.
</div></div></section></div> ]]></description>
  <category>spatial</category>
  <category>spatial-patterns</category>
  <category>spatial-machine-learning</category>
  <category>rstats</category>
  <category>rspatial</category>
  <category>talk</category>
  <category>workshop</category>
  <guid>https://jakubnowosad.com/posts/2026-07-03-ml4eo/</guid>
  <pubDate>Fri, 03 Jul 2026 00:00:00 GMT</pubDate>
</item>
<item>
  <title>PRISM project’s update: making spatial machine learning more reliable, plus related activities</title>
  <dc:creator>Jakub Nowosad</dc:creator>
  <link>https://jakubnowosad.com/posts/2026-02-13-msca-bp3/</link>
  <description><![CDATA[ 





<p>Time flies, and I am now much closer to the end of the PRISM (<a href="https://doi.org/10.3030/101147446"><em>PReservation and RecognItion of Spatial patterns using Machine learning</em></a>) project than to its beginning.<sup>1</sup>. The project’s primary goal is to develop methods for validating and including spatial patterns in machine learning. In this blog post, I summarize the project’s progress from April 2025 to February 2026 and share related outputs and activities.</p>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://jakubnowosad.com/posts/2026-02-13-msca-bp3/prism.png" class="img-fluid figure-img"></p>
<figcaption>PRISM project’s logo</figcaption>
</figure>
</div>
<div class="callout callout-style-simple callout-note">
<div class="callout-body d-flex">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-body-container">
<p>This blog post is the third part of the PRISM project’s progress report, covering the period from April 2025 to February 2026. <a href="https://jakubnowosad.com/posts/2024-07-22-msca-bp1/">The first part</a> explored my experiences during the application process, while <a href="https://jakubnowosad.com/posts/2025-04-13-msca-bp2/">the second part</a> focused on the project’s first eight months, including the initial research activities and other related events.</p>
</div>
</div>
</div>
<p>Before diving into the details of the research activities, I want to thank everyone who has been part of this journey so far!</p>
<section id="spatial-patterns-and-machine-learning" class="level2">
<h2 class="anchored" data-anchor-id="spatial-patterns-and-machine-learning">Spatial patterns and machine learning</h2>
<p>From April 2025 to February 2026, I continued working on spatial patterns and machine learning, which are the core topics of the PRISM project. One part of this work focused on writing and coordinating a perspective paper on spatial machine learning, which is currently under review. It is a collaborative effort following <a href="https://jakubnowosad.com/advsml_2025/">the Advances in Spatial Machine Learning 2025 workshop</a>, which I co-organized in March 2025. The paper provides an overview of the current state of spatial machine learning, identifies methodological challenges, and offers recommendations for improving robustness, transparency, and comparability in this field. I plan to share the paper once the review process is completed.</p>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://jakubnowosad.com/posts/2026-02-13-msca-bp3/moran.png" class="img-fluid figure-img"></p>
<figcaption>Some results related to Moran’s I of model errors</figcaption>
</figure>
</div>
<p>Another aspect of this work focused on better understanding the spatial autocorrelation of machine learning prediction errors. For that purpose, I focused on Moran’s I – a widely used measure of spatial autocorrelation – and its properties and interpretation in this context. I wrote preliminary results on this topic in <a href="https://doi.org/10.5194/agile-giss-6-40-2025">a short paper</a> and <a href="https://jakubnowosad.com/agile-gi2025/">presented them</a> at the AGILE 2025 conference in June 2025. Still, there is a lot to explore in this area, and I have been making steady progress – stay tuned for more updates.</p>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://jakubnowosad.com/posts/2026-02-13-msca-bp3/rome.png" class="img-fluid figure-img"></p>
<figcaption>Slide snapshot from the Rome R Users Group talk on spatial model validation</figcaption>
</figure>
</div>
<p>One of the biggest advantages of being part of the Remote Sensing and Spatial Modeling group in Muenster is the opportunity to interact with experts on spatial machine learning validation and evaluation. This quickly brought me up to speed with the state of the art and opened many opportunities for discussion. Much of this discussions focuses on robust validation of spatial machine learning models: how to design spatial cross-validation schemes depending on the available data and task, and for which locations predictions are reliable. I presented some of these ideas in a talk for the Rome R Users Group in November 2025 (slides: <a href="https://jakubnowosad.com/posts/2025-12-01-rome-talk/">slides</a>, recording: <a href="https://www.youtube.com/watch?v=uZe7thh80MI">recording</a>).</p>
<p>Together with colleagues from the research group, we also published a blog post series on spatial machine learning with R. It helped us better understand the current state of the R ecosystem for spatial machine learning and provided a useful resource for others interested in this topic. Read the full series at <a href="https://geocompx.org/post/2025/sml-bp1/" class="uri">https://geocompx.org/post/2025/sml-bp1/</a>.</p>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://jakubnowosad.com/posts/2026-02-13-msca-bp3/porto.png" class="img-fluid figure-img"></p>
<figcaption>Photo from ECML PKDD 2025 in Porto</figcaption>
</figure>
</div>
<p>During spring 2025, I also attended the Machine Learning for Visual Spatio-Temporal Data course at the Institute of Geoinformatics at the University of Muenster. The course deepened my understanding of deep learning for spatio-temporal data and clarified its potential and limitations for my research. To explore the current state of machine learning even more, I went to Porto, Portugal, to attend the ECML PKDD<sup>2</sup> 2025 conference. The conference has a strong computer science focus, but I found it very useful to take an outsider perspective on machine learning research, and it gave me a lot to think about. This event also allowed me to rethink some of my ideas for PRISM and other projects – I came back with many notes. New developments in machine learning are happening very fast, and this requires both constant learning and decisions on which topics to follow and which to ignore. In December 2025, I gave a talk for my research group on deep learning for spatial data to test and discuss related concepts and ideas. It included graph neural networks, embeddings, and foundation models – slides (and related code): <a href="https://jakubnowosad.com/posts/2025-12-15-agforum-talk/" class="uri">https://jakubnowosad.com/posts/2025-12-15-agforum-talk/</a>.</p>
</section>
<section id="geocompx" class="level2">
<h2 class="anchored" data-anchor-id="geocompx">geocomp<em>x</em></h2>
<p>The <a href="https://geocompx.org/">geocompx project</a> is a long-term initiative to create open educational resources for spatial data science and geocomputation. It is a collaborative effort of many people, and I am happy to be a part of it. In May 2025, the second edition of the Geocomputation with R book was published, which is a major milestone for the project. The book is a comprehensive resource for learning geocomputation with R, and it covers a wide range of topics, from data manipulation and visualization to spatial modeling and machine learning. The book is available online at <a href="https://r.geocompx.org/" class="uri">https://r.geocompx.org/</a> and in print from various retailers.</p>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://jakubnowosad.com/posts/2026-02-13-msca-bp3/geocompr.png" class="img-fluid figure-img"></p>
<figcaption>Cover image of the second edition of Geocomputation with R</figcaption>
</figure>
</div>
<p>A more recent addition to the geocomp<em>x</em> project is a book-in-progress, “Spatial Data Visualization with tmap: A Practical Guide to Thematic Mapping in R”. It focuses on the many tools and features of the R package tmap and, we hope, will be a great resource for anyone interested in maps with R. Currently, the first three out of five parts of the book are available online at <a href="https://tmap.geocompx.org/" class="uri">https://tmap.geocompx.org/</a>. We encourage you to check it out, and if you have any feedback or suggestions for improvement, please let us know by contributing to the GitHub repository at <a href="https://github.com/geocompx/tmap/" class="uri">https://github.com/geocompx/tmap/</a>.</p>
<p>To learn more about the geocompx project and its various outputs, visit <a href="https://geocompx.org/" class="uri">https://geocompx.org/</a>, read its <a href="https://geocompx.org/post/2025/updates-2025/">2025 annual report</a>, or follow updates on social media.</p>
</section>
<section id="other-research-outcomes" class="level2">
<h2 class="anchored" data-anchor-id="other-research-outcomes">Other research outcomes</h2>
<p>In December 2025, a paper on spatial data science (programming) languages was published in the Journal of Spatial Information Science (JOSIS). The paper is a result of a collaborative effort of discussions from <a href="https://spatial-data-science.github.io/">two workshops on spatial data science across languages</a>, organized in 2023 and 2024. It provides an overview of the current state of spatial data science languages, identifies commonalities and differences between them, and offers recommendations for improving interoperability and usability across languages. The paper is available at <a href="https://doi.org/10.5311/JOSIS.2025.31.462" class="uri">https://doi.org/10.5311/JOSIS.2025.31.462</a>.</p>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://jakubnowosad.com/posts/2026-02-13-msca-bp3/r-ecology.webp" class="img-fluid figure-img"></p>
<figcaption>Cover image of the book R Coding for Ecology</figcaption>
</figure>
</div>
<p>In early 2026, a book, <a href="https://doi.org/10.1007/978-3-031-99665-8">“R Coding for Ecology”</a>, was published that provides practical guides for using R in ecological research, including species distribution modeling, spatial ecology, and data visualization. It also includes three chapters that I co-authored:</p>
<ul>
<li><a href="https://doi.org/10.48550/arXiv.2405.06559">“The landscapemetrics and motif Packages for Measuring Landscape Patterns and Processes”</a>: It explores the usage of R packages, landscapemetrics and motif, for quantifying and analyzing landscape patterns using categorical maps.</li>
<li>“The cblindplotR Package: Building New Bridges to Visually Inclusive Science for Colourblind People”: It showcases the cblindplotR package to redo maps and plots in a way that is more accessible to people with color vision deficiencies. It also describes other R packages that may be useful for colourblind-friendly visualisations.</li>
<li>“The cartogram Package for Mapping Ecological Patterns”: It shows how cartograms can be useful for mapping ecological patterns, and how to create them with the cartogram package.</li>
</ul>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://jakubnowosad.com/posts/2026-02-13-msca-bp3/supercells.png" class="img-fluid figure-img"></p>
<figcaption>Illustration of supercells evaluation</figcaption>
</figure>
</div>
<p>In parallel, I also made several improvements and modifications to my other R packages. These include the patternogram package for quantifying and visualizing the spatial autocorrelation of multidimensional data. Its current features are described in the <a href="https://jakubnowosad.com/patternogram/articles/an-introduction-to-patternogram.html">introduction</a> vignette. Most changes, however, were made to the supercells package for grouping raster cells into meaningful, coherent regions. The package is based on the SLIC superpixels algorithm, adapted to work with arbitrary dissimilarity measures and multidimensional data. The upcoming version 2.0 provides new syntax and outputs, tools for parameter tuning, and functions to evaluate supercell quality across various aspects. More details about the changes are in the <a href="https://jakubnowosad.com/supercells/articles/v2-changes-since-v1.html">Main changes since version 1.0.0 vignette</a>. This work is still in progress – this is a perfect time to check the package out and share your feedback and suggestions at <a href="https://github.com/Nowosad/supercells/issues" class="uri">https://github.com/Nowosad/supercells/issues</a>.</p>
</section>
<section id="other-activities" class="level2">
<h2 class="anchored" data-anchor-id="other-activities">Other activities</h2>
<p>This period has also been full of other activities, including events, teaching, and outreach.</p>
<p>During the summer semester of 2025, I taught a two-day workshop on “R for automation and packaging” and gave a short class on “Version Control with Git and GitHub” for Landscape Ecology master’s students at the University of Muenster. Both activities focused on practical skills that students can directly apply in their research. In September 2025, I also went to the EON summer school and gave a workshop on “Understanding spatial patterns: how to measure and compare them” for participating students. My slides for this workshop are available <a href="https://jakubnowosad.com/eon2025/">online</a>.</p>
<p>In May, I gave an online presentation on tips for applying to the MSCA-PF program during an info session organized by KoWi<sup>3</sup>. It shared subjective advice based on my experience with the application process. Slides: <a href="https://jakubnowosad.com/kowi2025/" class="uri">https://jakubnowosad.com/kowi2025/</a>.</p>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://jakubnowosad.com/posts/2026-02-13-msca-bp3/barcelona.png" class="img-fluid figure-img"></p>
<figcaption>Photo from Barcelona</figcaption>
</figure>
</div>
<p>Moreover, I went to Barcelona to be part of the defense committee for Carles Milà’s PhD thesis on spatial data science and its applications to epidemiological research. The defense went very smoothly<sup>4</sup>, and it offered a useful perspective on spatial data science from another application domain.</p>
</section>
<section id="upcoming-months" class="level2">
<h2 class="anchored" data-anchor-id="upcoming-months">Upcoming months</h2>
<p>There are already many things planned for the upcoming months. I especially hope that I will be able to share some of the outputs of the PRISM project, including the perspective paper on spatial machine learning and the paper on spatial autocorrelation of machine learning errors. I also plan not to stop working on the supercells package and hopefully release version 2.0 in the next few months.</p>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://jakubnowosad.com/posts/2026-02-13-msca-bp3/user.png" class="img-fluid figure-img"></p>
<figcaption>UseR! 2026 conference visual</figcaption>
</figure>
</div>
<p>The summer months will also be full of various conferences and summer schools.<sup>5</sup> Here are some of the events that I am planning to attend, give talks at, or organize workshops for:</p>
<ul>
<li>EGU 2026 conference in Vienna, Austria (May 2026)<sup>6</sup></li>
<li><a href="https://ml4eo.org/">Machine Learning for Earth Observation (ML4EO) conference</a> in Exeter, UK (June 2026)</li>
<li><a href="https://user2026.r-project.org/">UseR! 2026 conference</a> in Warsaw, Poland (July 2026)</li>
<li><a href="https://opengeohub.org/2026/01/29/earth-observation-summer-school-2026/">Earth Observation Summer School</a> in Istanbul, Turkey (August 2026)</li>
</ul>
<p>As of the time of writing this post, the ML4EO and UseR! conferences have open calls for abstracts – I encourage you to check them out and submit if these events match your interests. The Earth Observation Summer School is also accepting student applications – details on the application process are at the link above.</p>
<p>See you at some of these events or online!</p>


</section>


<div id="quarto-appendix" class="default"><section id="footnotes" class="footnotes footnotes-end-of-document"><h2 class="anchored quarto-appendix-heading">Footnotes</h2>

<ol>
<li id="fn1"><p>The project started in August 2024 and will end in August 2026. During this period, I am working in the Remote Sensing and Spatial Modeling group at the University of Muenster, Germany.↩︎</p></li>
<li id="fn2"><p><em>European Conference on Machine Learning and Principles and Practice of Knowledge Discovery in Databases</em>↩︎</p></li>
<li id="fn3"><p>the German organization providing, among others, information on European research funding↩︎</p></li>
<li id="fn4"><p>Congratulations to Carles!↩︎</p></li>
<li id="fn5"><p>A more comprehensive list of geospatial conferences in 2026 is at <a href="https://github.com/Nowosad/conferences_2026" class="uri">https://github.com/Nowosad/conferences_2026</a>↩︎</p></li>
<li id="fn6"><p>First time for me at EGU.↩︎</p></li>
</ol>
</section><section class="quarto-appendix-contents" id="quarto-citation"><h2 class="anchored quarto-appendix-heading">Citation</h2><div><div class="quarto-appendix-secondary-label">BibTeX citation:</div><div class="code-copy-outer-scaffold"><pre class="sourceCode quarto-appendix-bibtex code-with-copy"><code class="sourceCode bibtex">@online{nowosad2026,
  author = {Nowosad, Jakub},
  title = {PRISM Project’s Update: Making Spatial Machine Learning More
    Reliable, Plus Related Activities},
  date = {2026-02-13},
  url = {https://jakubnowosad.com/posts/2026-02-13-msca-bp3/},
  doi = {10.59350/w20vw-k5814},
  langid = {en}
}
</code></pre></div><div class="quarto-appendix-secondary-label">For attribution, please cite this work as:</div><div id="ref-nowosad2026" class="csl-entry quarto-appendix-citeas">
Nowosad, Jakub. 2026. <span>“PRISM Project’s Update: Making Spatial
Machine Learning More Reliable, Plus Related Activities.”</span>
February 13. <a href="https://doi.org/10.59350/w20vw-k5814">https://doi.org/10.59350/w20vw-k5814</a>.
</div></div></section></div> ]]></description>
  <category>spatial</category>
  <category>landscape-ecology</category>
  <category>spatial-patterns</category>
  <category>spatial-machine-learning</category>
  <category>msca-pf</category>
  <guid>https://jakubnowosad.com/posts/2026-02-13-msca-bp3/</guid>
  <pubDate>Fri, 13 Feb 2026 00:00:00 GMT</pubDate>
</item>
<item>
  <title>Elephant(s) in the room: Graph neural networks, embeddings, and foundation models in spatial data science</title>
  <dc:creator>Jakub Nowosad</dc:creator>
  <link>https://jakubnowosad.com/posts/2025-12-15-agforum-talk/</link>
  <description><![CDATA[ 





<p><img src="https://jakubnowosad.com/posts/2025-12-15-agforum-talk/agforum2025.webp" class="img-fluid"></p>
<p><strong>Slides:</strong> <a href="https://jakubnowosad.com/agforum2025" class="uri">https://jakubnowosad.com/agforum2025</a></p>
<p>This presentation covered three interconnected deep learning concepts appearing in spatial data science work.</p>
<p><strong>Graph Neural Networks (GNNs)</strong> are a deep learning architecture that represents spatial data as graphs: nodes are spatial units (pixels, regions, locations) and edges are relationships (proximity, similarity, connectivity). Nodes aggregate information from neighbors through message passing, similar to spatial lag models. Common types include Graph Convolutional Networks (GCNs), Graph Attention Networks (GATs), GraphSAGE, and Graph Isomorphism Networks (GINs).</p>
<p><strong>Embeddings</strong> are compact numerical representations that compress high-dimensional spatial data. They’re used for similarity search, change detection, clustering, and classification with minimal labeled data. Google DeepMind’s AlphaEarth Foundations produces 64-dimensional, 10-meter resolution embeddings from diverse geospatial data (optical, thermal, radar, elevation, climate) for every year since 2017, requiring no preprocessing. Challenges in their use include interpretability and selecting appropriate embeddings for specific tasks.</p>
<p><strong>Foundation models</strong> are large pre-trained models that learn general representations from massive unlabeled Earth observation data through self-supervised learning (masked image modeling, multi-modal alignment, temporal modeling, contrastive learning). Examples include Terramind, AnySat, Prithvi, and AlphaEarth Foundations. They produce embeddings, work with minimal labeled data, and can be fine-tuned for tasks like land cover mapping and change detection. TabPFN is a foundation model for tabular data that can be applied to geospatial predictive mapping. Still, foundation models have limited transferability to new geographies, and traditional methods remain competitive when labeled data is abundant.</p>
<p>During the talk, I explained the basics of these concepts and, for each one, showed practical applications in R using reproducible examples. They included landform classification with GNNs, change detection using AlphaEarth embeddings, and species richness mapping with TabPFN.</p>



<div id="quarto-appendix" class="default"><section class="quarto-appendix-contents" id="quarto-citation"><h2 class="anchored quarto-appendix-heading">Citation</h2><div><div class="quarto-appendix-secondary-label">BibTeX citation:</div><div class="code-copy-outer-scaffold"><pre class="sourceCode quarto-appendix-bibtex code-with-copy"><code class="sourceCode bibtex">@online{nowosad2025,
  author = {Nowosad, Jakub},
  title = {Elephant(s) in the Room: {Graph} Neural Networks, Embeddings,
    and Foundation Models in Spatial Data Science},
  date = {2025-12-15},
  url = {https://jakubnowosad.com/posts/2025-12-15-agforum-talk/},
  doi = {10.59350/k9xq9-zwj76},
  langid = {en}
}
</code></pre></div><div class="quarto-appendix-secondary-label">For attribution, please cite this work as:</div><div id="ref-nowosad2025" class="csl-entry quarto-appendix-citeas">
Nowosad, Jakub. 2025. <span>“Elephant(s) in the Room: Graph Neural
Networks, Embeddings, and Foundation Models in Spatial Data
Science.”</span> December 15. <a href="https://doi.org/10.59350/k9xq9-zwj76">https://doi.org/10.59350/k9xq9-zwj76</a>.
</div></div></section></div> ]]></description>
  <category>spatial</category>
  <category>spatial-machine-learning</category>
  <category>geoai</category>
  <category>rstats</category>
  <category>rspatial</category>
  <category>talk</category>
  <guid>https://jakubnowosad.com/posts/2025-12-15-agforum-talk/</guid>
  <pubDate>Mon, 15 Dec 2025 00:00:00 GMT</pubDate>
</item>
<item>
  <title>R for Geospatial Predictive Mapping: Takeaways from the Talk</title>
  <dc:creator>Jakub Nowosad</dc:creator>
  <link>https://jakubnowosad.com/posts/2025-12-01-rome-talk/</link>
  <description><![CDATA[ 





<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://jakubnowosad.com/posts/2025-12-01-rome-talk/rome2025.webp" class="img-fluid figure-img"></p>
<figcaption>Title slide</figcaption>
</figure>
</div>
<p><strong>Slides:</strong> <a href="https://jakubnowosad.com/rome2025" class="uri">https://jakubnowosad.com/rome2025</a></p>
<p><strong>Video recording:</strong> <a href="https://youtu.be/uZe7thh80MI" class="uri">https://youtu.be/uZe7thh80MI</a></p>
<p><strong>Reproducible code:</strong> <a href="https://jakubnowosad.com/rome2025/index.R" class="uri">https://jakubnowosad.com/rome2025/index.R</a></p>
<p>Geospatial predictive mapping is a common task across many domains, aiming to produce continuous surfaces from point observations and spatial predictors. There are many algorithms available to perform this task, ranging from simple interpolation methods to complex machine learning models, and a variety of R packages implement these methods. Thus, producing a map from points is easy, but understanding how reliable that map is is much harder.</p>
<p>In my talk at the Rome R Users Group (Nov 27, 2025), I presented practical R workflows for generating and evaluating spatial predictions. Using plant species richness data across South America, I compared methods such as Inverse Distance Weighting, ordinary and universal Kriging, and Random Forests. These approaches often produce visually appealing maps, but they can be misleading. Common issues include unrealistic predictions outside the observed value ranges, predictions for environments not represented in the training data, and overly optimistic accuracy metrics when training and test points are spatially clustered.</p>
<p>To address these problems, I focused on two complementary tools:</p>
<ul>
<li>kNN Distance Matching (kNNDM), a prediction-domain adaptive cross-validation method that reshapes validation folds so that held-out data resemble “unseen” areas. This reduces evaluation bias caused by data sampling and yields more realistic performance estimates.</li>
<li>Area of Applicability (AoA), which identifies locations whose environmental conditions differ from the training data. Masking or highlighting these regions helps communicate where predictions are more or less trustworthy.</li>
</ul>
<p>Together, kNNDM and AoA shift the focus from model-centric accuracy to understanding the prediction domain: where the model can be trusted and how well its errors are quantified within that domain.</p>



<div id="quarto-appendix" class="default"><section class="quarto-appendix-contents" id="quarto-citation"><h2 class="anchored quarto-appendix-heading">Citation</h2><div><div class="quarto-appendix-secondary-label">BibTeX citation:</div><div class="code-copy-outer-scaffold"><pre class="sourceCode quarto-appendix-bibtex code-with-copy"><code class="sourceCode bibtex">@online{nowosad2025,
  author = {Nowosad, Jakub},
  title = {R for {Geospatial} {Predictive} {Mapping:} {Takeaways} from
    the {Talk}},
  date = {2025-12-01},
  url = {https://jakubnowosad.com/posts/2025-12-01-rome-talk/},
  doi = {10.59350/r7z67-4p318},
  langid = {en}
}
</code></pre></div><div class="quarto-appendix-secondary-label">For attribution, please cite this work as:</div><div id="ref-nowosad2025" class="csl-entry quarto-appendix-citeas">
Nowosad, Jakub. 2025. <span>“R for Geospatial Predictive Mapping:
Takeaways from the Talk.”</span> December 1. <a href="https://doi.org/10.59350/r7z67-4p318">https://doi.org/10.59350/r7z67-4p318</a>.
</div></div></section></div> ]]></description>
  <category>spatial</category>
  <category>spatial-patterns</category>
  <category>spatial-machine-learning</category>
  <category>rstats</category>
  <category>rspatial</category>
  <category>talk</category>
  <guid>https://jakubnowosad.com/posts/2025-12-01-rome-talk/</guid>
  <pubDate>Mon, 01 Dec 2025 00:00:00 GMT</pubDate>
</item>
<item>
  <title>The PRISM project’s progress report: 8 months into the MSCA-PF journey</title>
  <dc:creator>Jakub Nowosad</dc:creator>
  <link>https://jakubnowosad.com/posts/2025-04-13-msca-bp2/</link>
  <description><![CDATA[ 





<p>Last year, I received a grant from the Marie Skłodowska-Curie Actions Postdoctoral Fellowships (MSCA-PF) program for a project called PRISM: <a href="https://doi.org/10.3030/101147446"><em>PReservation and RecognItion of Spatial patterns using Machine learning</em></a>. Between August 2024 and August 2026, I am in the Remote Sensing and Spatial Modeling group at the University of Muenster, Germany. The project’s primary goal is to develop and compare methods for validating and including spatial patterns in machine learning. At the same time, MSCA-PF is an opportunity to meet new people, learn new things, and share my experiences with others.</p>
<p>Before starting the project, I wrote a <a href="https://jakubnowosad.com/posts/2024-07-22-msca-bp1/">blog post</a> about my insights from the application process. This post is a summary of my first eight months at the University of Muenster, including the project’s progress and various activities I took part in.</p>
<p><img src="https://jakubnowosad.com/posts/2025-04-13-msca-bp2/prism.png" class="img-fluid"></p>
<section id="starting-the-project" class="level2">
<h2 class="anchored" data-anchor-id="starting-the-project">Starting the project</h2>
<p>There is a significant overhead of moving to a new country (especially with a family and without knowing the language) and starting a position in another institution. Thus, the first several weeks are spent on settling in, getting to know the new environment, and meeting new people. It is also about getting the necessary paperwork done (including <em>anmeldung</em>, bank account, health insurance, and so on), registering the kid in kindergarten, furnishing the apartment, getting an internet connection, and so on. Gladly, people in my research group are friendly and very helpful (which matters a lot – thank you!) and Muenster is a beautiful city. Now, after a few months, those initial challenges feel more distant and less daunting…</p>
<p>I started my stay at the University of Muenster by giving two talks for my research group. The first was a general introduction to myself, my research interests, previous work, and the skills I can bring to the group. The second one was more about the PRISM project, where I presented two elements: a research project and other planned activities. <!-- The research project is focused on developing methods for validating and including spatial patterns in machine learning.
It also includes creating open-source software for the implementation of these methods.
Other activities include organizing workshops, teaching, and collaborating with local experts and partners. --></p>
<p><img src="https://jakubnowosad.com/posts/2025-04-13-msca-bp2/mun.webp" class="img-fluid"></p>
</section>
<section id="late-summer-meetings" class="level2">
<h2 class="anchored" data-anchor-id="late-summer-meetings">Late summer meetings</h2>
<p>Next, I took part in an Earth Observation Network (EON) summer school in 2024 in the Harz mountains in early September. It is a joint initiative of the University of Muenster, the University of Marburg, and the HAWK University of Applied Sciences and Arts in Göttingen. Students of landscape ecology, ecological informatics, and forestry learn about forest management (both from the perspective of managing areas inside and outside the national park), how to perform field measurements (including traditional methods, drones, sensors, and satellite data), and how to analyze the data using programming languages. During the summer school, I gave a short workshop about <a href="https://jakubnowosad.com/eon2024/#/title-slide">“Describing and comparing spatial patterns”</a>.</p>
<p><img src="https://jakubnowosad.com/posts/2025-04-13-msca-bp2/eon.webp" class="img-fluid"></p>
<p>Later that month, I participated in the Spatial Data Science Across Languages (SDSL) workshop in Prague. This was the second edition of this event that focuses on gathering researchers and developers from different programming languages used in data science for geographical applications, including R, Python, Julia, and Rust. It aims to bridge the gap between these communities, talk about their differences and similarities, and find ways to discuss, collaborate, and synchronize efforts.<sup>1</sup> There, I gave a talk <a href="https://jakubnowosad.com/sdsl2024/#/title-slide">“Learning resources and teaching methods”</a> about existing open-source resources for teaching spatial data science in R, Python, and Julia and how various methods can be used to teach spatial data science. I also introduced <a href="https://geocompx.org/">the geocomp<em>x</em> project</a>, which is a community-driven initiative to create a collection of open-source resources for learning and teaching geocomputation in multiple programming languages.</p>
</section>
<section id="autumn-months" class="level2">
<h2 class="anchored" data-anchor-id="autumn-months">Autumn months</h2>
<p>A lot of my time in autumn was spent directly on the PRISM project. I started by reading the literature on spatial patterns and machine learning, focusing on the existing methods for validating and including spatial patterns in machine learning. During that time, I also worked on a few deliverables required by the project, including a Data Management Plan (DMP) and a Career Development Plan (CDP).<sup>2</sup></p>
<p>I was also invited to give a talk at the “SAIL – Success Factors in Acquiring InternationaL Projects” program at the University of Muenster. The program is aimed at doctoral candidates and postdocs and is focused on providing them with the necessary skills to apply for international grants. There, I spoke about my experience with the MSCA-PF application process and how I prepared for it.<sup>3</sup></p>
<p>In autumn, I also published <a href="https://jakubnowosad.com/posts/2024-10-13-spatcomp-bp1/">a blog series on comparing spatial patterns in raster data</a> and made a list of <a href="https://github.com/Nowosad/conferences_2025">Geospatial Conferences that will take place in 2025</a>.</p>
</section>
<section id="new-r-packages" class="level2">
<h2 class="anchored" data-anchor-id="new-r-packages">New R packages</h2>
<p>During that time, I also started developing two R packages: <a href="https://github.com/Nowosad/simsam"><strong>simsam</strong></a> and <a href="https://github.com/Nowosad/spatialexplain"><strong>spatialexplain</strong></a>. Given my project’s goals, I needed a reproducible, flexible, and efficient way to simulate spatial data of a given structure and properties. Thus, the goal of <strong>simsam</strong> is to provide tools for simulating and sampling spatial data. It allows users to create sets of spatial raster data of specified properties (e.g., used then as covariates in machine learning) and also blend them with other data (e.g., used as a response variable). Then, the sampling function selects points from the raster data, which can be used to create training and testing datasets for machine learning. That function is a wrapper around some other existing functions, additionally providing the possibility to create clustered points. The <strong>simsam</strong> package also provides tools for creating various spatial proxies: coordinates, Euclidean Distance Fields (EDFs), and Oblique Geographic Coordinates (OGCs) based on the provided raster object. To learn more about the package, you can check the <a href="https://jakubnowosad.com/simsam/articles/">package’s vignettes</a>.</p>
<p><img src="https://jakubnowosad.com/posts/2025-04-13-msca-bp2/simsam.webp" class="img-fluid"></p>
<p>The <strong>spatialexplain</strong> package is, on the other hand, based on the needs of the people in my research group. Some of them are trying to model various environmental properties using machine learning, and they need a way to explain the models, particularly including the spatial context.<br>
This package provides model-agnostic tools for mapping how the input variables affect the model predictions based on the methods, such as SHAP, LIME, Break Down, and Oscillations. In short, it provides a wrapper around the <strong>DALEX</strong> package, which is a well-known package for model agnostic explanations of machine learning models, but then makes the explanation for each cell in the raster data.<sup>4</sup> The outcome of each function is a set of maps that show each predictor’s contribution to the model predictions. The package also provides <a href="https://jakubnowosad.com/spatialexplain/articles/An-introduction-to-spatial-explanations.html">a few vignettes</a> to help users understand how to use the package and how to interpret the results.</p>
<p><img src="https://jakubnowosad.com/posts/2025-04-13-msca-bp2/spatialexplain.webp" class="img-fluid"></p>
<p>Both packages are already used in our current research, but if you have any comments or suggestions on improving them, please let me know.</p>
</section>
<section id="talks-and-workshops" class="level2">
<h2 class="anchored" data-anchor-id="talks-and-workshops">Talks and workshops</h2>
<p>In November, I went to Seville to give a keynote talk and a workshop at the IIIRqueR conference (<em>3rd Spanish Congress of R Users</em>).<sup>5</sup> The conference was organized by the Spanish R community and was focused on the use of R in various fields, but it also excelled in making connections between the people from the R community. My talk <a href="https://jakubnowosad.com/IIIRqueR/#/title-slide">“R’s Geospatial Kaleidoscope: Exploring Perspectives, Strengths, and Challenges”</a> was a tour of different perspectives on spatial data analysis in R, including geographical, statistical, computational, visual, and a domain-specific one. Each of these perspectives has its own contexts, challenges, and achievements – I tried to show some of them and explain how they together create a mosaic of the R spatial landscape.</p>
<p>The workshop, <a href="https://jakubnowosad.com/IIIRqueR_workshop/#/title-slide">“Machine learning approaches for working with spatial data”</a>, started by giving a very simplified spatial machine learning workflow, including data preparation, model training, and prediction. Then, step by step, I included more and more details, focusing on how machine learning of spatial data differs from traditional machine learning. This included explicit spatial feature engineering, spatial hyperparameter tuning, feature selection, cross-validation, and spatial model explanations. The code examples were based on the <strong>mlr3</strong> workflow, but the general concepts can be applied to other machine learning frameworks.</p>
<p><img src="https://jakubnowosad.com/posts/2025-04-13-msca-bp2/sev.webp" class="img-fluid"></p>
<p>I started the following month by giving a talk at the GI-Forum – a seminar organized by the Institute for Geoinformatics at the University of Muenster. The talk entitled <a href="https://jakubnowosad.com/giforum2024/#/title-slide">“Exploration of spatial patterns in raster data”</a> was a summary of my previous work on describing, comparing, and analyzing spatial patterns in raster data. It was an opportunity to introduce my work to the local community and inform them about the PRISM project.<sup>6</sup></p>
</section>
<section id="winter-months" class="level2">
<h2 class="anchored" data-anchor-id="winter-months">Winter months</h2>
<p>The remaining winter months were spent working on the various ideas related to the PRISM project – I hope to share some of the results in the future. It also included reading related literature, writing software, and submitting abstracts to conferences.<sup>7</sup> We were also visited by guests from other institutions, including Lorena Abad from the University of Salzburg (who works on concepts of vector data cubes for multidimensional data) and Martijn Tennekes from Statistics Netherlands (of the <strong>tmap</strong> package fame). I also tried to be useful to my research group by helping with the ongoing projects, providing some feedback on their work, and advising the local students.<sup>8</sup></p>
<p><img src="https://jakubnowosad.com/posts/2025-04-13-msca-bp2/winter.webp" class="img-fluid"></p>
</section>
<section id="teaching-and-local-conferences" class="level2">
<h2 class="anchored" data-anchor-id="teaching-and-local-conferences">Teaching and local conferences</h2>
<p>After a few gray months, spring finally came to Muenster, and my family and I spent a lot of time outside, also attending various local events. I also had the first opportunity to teach at the University of Muenster. In early March, Hanna Meyer and I gave a week-long course on spatial data analysis in R to students from the Institute for Landscape Ecology and the Institute for Geoinformatics. It started by introducing the students to the basic concepts of spatial data (data models, sources, and visualization) and then moved to more advanced topics, such as spatial data processing (both vector and raster), spatial transformations, and automation of spatial data processing. The course was a mix of lectures and hands-on exercises, where students could practice the concepts they learned, and the last part of the course was dedicated to project work, where students could work on their own projects and present them at the end of the course. I was generally impressed by how much the students learned in such a short time and were able to apply the concepts they learned to their projects. You can find my slides about <a href="https://jakubnowosad.com/rgis2025/output/practices.html#/title-slide">“Best practices in code organization”</a> and <a href="https://jakubnowosad.com/rgis2025/output/landscape_metrics.html">“landscape metrics”</a> online.</p>
<p><img src="https://jakubnowosad.com/posts/2025-04-13-msca-bp2/lsm.webp" class="img-fluid"></p>
<p>The month of March was also marked by other events. The first one was the FOSSGIS-Konferenz 2025 – a conference focused on open-source software for geoinformatics aimed at the German-speaking community. There, we presented a poster <a href="https://loek-rs.github.io/FOSSGIS2025-examples/">“An Inventory of Spatial Machine Learning Packages in R”</a> – it was our group joint effort to summarize and compare the machine learning frameworks <strong>caret</strong>, <strong>mlr3</strong>, and <strong>tidymodels</strong> in R, and a few other packages that can be used for spatial machine learning tasks. The poster was accompanied by code examples demonstrating how to use these packages for spatial machine learning tasks. It was also a chance to meet the local community members and get the good energy from them.</p>
<p><img src="https://jakubnowosad.com/posts/2025-04-13-msca-bp2/poster_lr.webp" class="img-fluid"></p>
<p>Finally, there was an event that, I think, perfectly concludes my first eight months at the University of Muenster: <a href="https://jakubnowosad.com/advsml_2025/">the Advances in Spatial Machine Learning 2025 workshop</a> organized by Hanna Meyer and myself. It was a two-day event to which we invited leading researchers in spatial machine learning to talk about the current challenges and future directions of this evolving field. We focused on several key topics: validation and preservation of spatial patterns, analysis and communication of prediction uncertainties, comparability between different machine learning algorithms and frameworks, and standardized documentation protocols. The workshop was structured around a series of presentations and discussions, where participants shared their insights and current challenges in spatial machine learning. I learned a ton, and we plan to write down the workshop’s outcomes in a paper. The workshop was also an excellent opportunity to see some familiar faces and meet new people.</p>
<p><img src="https://jakubnowosad.com/posts/2025-04-13-msca-bp2/advsml2025.webp" class="img-fluid"></p>
</section>
<section id="looking-ahead" class="level2">
<h2 class="anchored" data-anchor-id="looking-ahead">Looking ahead</h2>
<p>The next few months will build on the progress already made. I plan to continue advancing the PRISM project, focusing on software development and writing reports summarizing my findings. I also plan to attend local courses and do some more teaching.</p>
<p>Moreover, I will attend two conferences in the summer. The first one is the Association of Geographic Information Laboratories in Europe (AGILE) Conference 2025 in Dresden, where I have a talk about spatial autocorrelation in machine learning. The second one is the Living Planet Symposium 2025 in Vienna, where my group has a poster on the standardized documentation of machine learning models. I look forward to meeting some of you there!</p>
<p>This blog post summarizes 1/3 of my MSCA-PF project. Thanks for reading it – I hope you found some of it interesting or useful.</p>


</section>


<div id="quarto-appendix" class="default"><section id="footnotes" class="footnotes footnotes-end-of-document"><h2 class="anchored quarto-appendix-heading">Footnotes</h2>

<ol>
<li id="fn1"><p>You can read more about the outcomes of these meetings at <a href="https://arxiv.org/pdf/2503.16686" class="uri">https://arxiv.org/pdf/2503.16686</a>.↩︎</p></li>
<li id="fn2"><p>DMP describes how I will manage the data generated during the project, while the CDP describes my career goals and how I plan to achieve them.↩︎</p></li>
<li id="fn3"><p>Again, you can read more about it in <a href="https://jakubnowosad.com/posts/2024-07-22-msca-bp1/">my previous blog post</a>.↩︎</p></li>
<li id="fn4"><p>This operation can be computationally expensive, thus, the package offers an option of making the explanations on an aggregated version of the raster data.↩︎</p></li>
<li id="fn5"><p>Many thanks to Francisco Rodriguez Sanchez and the rest of the organizers for the invitation and their hospitality.↩︎</p></li>
<li id="fn6"><p>In my opinion, it was my weakest talk that year, but I hope it was still useful for the audience.↩︎</p></li>
<li id="fn7"><p>And <a href="https://py.geocompx.org/">the Geocomputation with Python</a> book was published then.↩︎</p></li>
<li id="fn8"><p>Additionally, I was doing general academic work, including reviewing conference abstracts, software, and journal papers.↩︎</p></li>
</ol>
</section><section class="quarto-appendix-contents" id="quarto-citation"><h2 class="anchored quarto-appendix-heading">Citation</h2><div><div class="quarto-appendix-secondary-label">BibTeX citation:</div><div class="code-copy-outer-scaffold"><pre class="sourceCode quarto-appendix-bibtex code-with-copy"><code class="sourceCode bibtex">@online{nowosad2025,
  author = {Nowosad, Jakub},
  title = {The {PRISM} Project’s Progress Report: 8 Months into the
    {MSCA-PF} Journey},
  date = {2025-04-13},
  url = {https://jakubnowosad.com/posts/2025-04-13-msca-bp2/},
  doi = {10.59350/ht0fe-58388},
  langid = {en}
}
</code></pre></div><div class="quarto-appendix-secondary-label">For attribution, please cite this work as:</div><div id="ref-nowosad2025" class="csl-entry quarto-appendix-citeas">
Nowosad, Jakub. 2025. <span>“The PRISM Project’s Progress Report: 8
Months into the MSCA-PF Journey.”</span> April 13. <a href="https://doi.org/10.59350/ht0fe-58388">https://doi.org/10.59350/ht0fe-58388</a>.
</div></div></section></div> ]]></description>
  <category>spatial</category>
  <category>landscape-ecology</category>
  <category>spatial-patterns</category>
  <category>spatial-machine-learning</category>
  <category>msca-pf</category>
  <guid>https://jakubnowosad.com/posts/2025-04-13-msca-bp2/</guid>
  <pubDate>Sun, 13 Apr 2025 00:00:00 GMT</pubDate>
</item>
<item>
  <title>Understanding and extending the methods of comparing spatial patterns in raster data</title>
  <dc:creator>Jakub Nowosad</dc:creator>
  <link>https://jakubnowosad.com/posts/2024-11-17-spatcomp-bp6/</link>
  <description><![CDATA[ 





<div class="callout callout-style-simple callout-note callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Note</span>Methods for comparing spatial patterns in raster data
</div>
</div>
<div class="callout-body-container callout-body">
<p>This is the sixth part of a blog post series on comparing spatial patterns in raster data. More information about the whole series can be found <a href="../../posts/2024-10-13-spatcomp-bp1">in part one</a>.</p>
</div>
</div>
<p>The blog post series on comparing spatial patterns in raster data has covered a variety of methods for comparing spatial patterns in raster data, with a focus on (a) comparing two rasters at a time, and (b) using R for the analysis.</p>
<p>The <a href="../../posts/2024-10-13-spatcomp-bp1">first</a> post listed possible motivations for such comparison, and then described the general considerations when selecting a method for comparing spatial patterns in raster data. They include if the method works for arbitrary regions (or just for overlapping regions), if the method allows the integration of the spatial context of the analysis, and the type of data the method produces (single value, multiple values, or a raster). Next, it provided an inventory of methods for comparing continuous and categorical raster data adapted from <span class="citation" data-cites="Nowosad2024">Nowosad (2024)</span>.</p>
<p>The next four blog posts had a more applied focus – they showed how to use R to compare spatial patterns:</p>
<ul>
<li><a href="../../posts/2024-10-20-spatcomp-bp2">The second blog post</a> for overlapping regions of continuous raster data.</li>
<li><a href="../../posts/2024-10-27-spatcomp-bp3">The third blog post</a> for arbitrary regions on continuous raster data.</li>
<li><a href="../../posts/2024-11-03-spatcomp-bp4">The fourth blog post</a> for overlapping regions on categorical raster data.</li>
<li><a href="../../posts/2024-11-10-spatcomp-bp5">The fifth blog post</a> for arbitrary regions on categorical raster data.</li>
</ul>
<section id="implementation-of-the-methods" class="level2">
<h2 class="anchored" data-anchor-id="implementation-of-the-methods">Implementation of the methods</h2>
<p>What is important to note is that not all methods are implemented in R, and, thus, the methods presented in the blog posts are a subset of all possible methods for comparing spatial patterns in raster data. This includes Complex Wavelet Structural Similarity Index <span class="citation" data-cites="sampatComplexWaveletStructural2009">(Sampat et al. 2009)</span> or the Fuzzy Kappa index <span class="citation" data-cites="HagenZanker2009">(Hagen‐Zanker 2009)</span>.<sup>1</sup></p>
<p>Moreover, while comparing spatial patterns is a general concept, there may exist some methods developed for specific domains that are not covered in the blog posts. Please let me know if you know about such methods!</p>
</section>
<section id="discussion" class="level2">
<h2 class="anchored" data-anchor-id="discussion">Discussion</h2>
<p>The introductory blog post summarized many aspects of comparing spatial patterns in raster data, including the type of the rasters (continuous or categorical), the applicability of the method for arbitrary regions, the integration of the spatial context of the analysis, and the type of the data produced by the method.</p>
<p>Next, the presented examples of the use of R for comparing spatial patterns in raster data highlight several additional considerations.</p>
<p>First, a general approach for comparing rasters is calculating the difference between them or their derivatives (e.g., a metric in a moving window). The question here is whether such a difference should be calculated just by subtracting one raster from another or by calculating the absolute difference. The former usually highlights the direction of the change, while the latter highlights the magnitude of the change regardless of the direction. An additional related question is about how to interpret the results of such a comparison- we may easily understand the increase of temperature by 5 degrees, but how can we interpret the increase of the NDVI by 0.1?</p>
<p>Second, some methods are sensitive to the ranges of the values in the rasters, and thus, it is essential to scale the data before the comparison. Omitting this step may lead to incorrect results, as the method may be more sensitive to the range of the values than to the spatial pattern of the data.</p>
<p>Third, another way to look at the comparison methods is by considering the specific questions (aspects) they answer. Some of the methods are focused on specific characteristics of spatial patterns (e.g., roughness, diversity, etc.), and some are interested in encapsulating the general properties of spatial patterns. Thus, the choice of the method should depend on the specific question we want to answer.</p>
<p>Fourth, the topic that was not covered in the blog posts is the preprocessing of the rasters before the comparison. This may include reclassifying the values or clipping the rasters to some extent. The preprocessing may be crucial for the comparison, as it may significantly affect the results of the analysis. These decisions should be made based on the specific question we want to answer.</p>
<!-- new fifth -- the borders (consistency) of the patterns -->
<!-- also, distance measures -->
<!-- transformation of the data? -->
<p>Fifth, the blog posts showed some technical attributes of the methods and their implementations in R. They have different scalability – some may be applied to large rasters quickly, while some may be computationally expensive. Moreover, the methods require different R skill levels – some of them may be applied with just a few lines of code, while some may require more familiarity with R and numerical methods.</p>
</section>
<section id="applications" class="level2">
<h2 class="anchored" data-anchor-id="applications">Applications</h2>
<p>There are a variaty of reasons for comparing spatial patterns in raster data as listed in the first blog post (from <span class="citation" data-cites="longComparingSpatialPatterns2018a">Long and Robertson (2018)</span>):</p>
<ul>
<li>To study change</li>
<li>To study similarity</li>
<li>To study association</li>
<li>To assess spatial models</li>
</ul>
<p>The above reasons can be expanded based on the specific question we want to answer and how many rasters we wish to compare.</p>
<p>In general, we may think of the comparison of spatial patterns in raster data as one-to-one, one-to-many, or many-to-many. The whole blog post series focussed on the comparison of two rasters, but many methods of the two-raster comparison can be extended to the comparison of multiple rasters (one-to-many or many-to-many).</p>
<p>One-to-many analysis compares the spatial pattern of one raster to the spatial pattern of multiple rasters. For example, we may take a reference raster of a given variable and compare it to multiple rasters of the same variable from different years, regions, etc. Thus, it enables us to find the raster that is most similar to the reference raster.</p>
<p>Many-to-many analysis focuses on the comparison of spatial patterns of multiple rasters. The most basic way of thinking about it is as a way of clustering the rasters based on their spatial patterns. Our goal may be to group areas with similar spatial patterns, and thus, to merge the areas of the most similar rasters to each other.</p>
</section>
<section id="extensions-and-multi-dimensional-comparisons" class="level2">
<h2 class="anchored" data-anchor-id="extensions-and-multi-dimensional-comparisons">Extensions and multi-dimensional comparisons</h2>
<div class="cell">
<div class="cell-output-display">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://jakubnowosad.com/posts/2024-11-17-spatcomp-bp6/ts.png" class="img-fluid figure-img" width="576"></p>
<figcaption>Three approaches for extending the methods to compare multi-dimensional data (source: Abad, 2024)</figcaption>
</figure>
</div>
</div>
</div>
<p>Many of the presented methods can be extended to compare multi-dimensional data, for example, two raster time series. This, in general, may require one of the approaches:</p>
<ol type="1">
<li>Treating each layer as a separate raster and compare corresponding layers using the methods described above (a pairwise comparison)</li>
<li>Compressing the data into one dimension (e.g., one raster layer) and then comparing such compressed data</li>
<li>Create temporal or spatiotemporal signatures and compare them using a dissimilarity measure</li>
</ol>
</section>
<section id="looking-ahead" class="level2">
<h2 class="anchored" data-anchor-id="looking-ahead">Looking ahead</h2>
<p>This blog post series has explored various approaches to comparing spatial patterns in raster data, from foundational concepts to practical implementations in R. Whether you’re studying environmental change, validating spatial models, or analyzing land use patterns, I hope this series has provided you with practical tools and considerations for your spatial analysis work. As always, I welcome feedback and suggestions for improvements or extensions to these methods. I may also consider writing a follow-up series on the topic, so please let me know if you have any specific questions or issues you’d like to see covered in future posts.</p>
<!-- motif and spquery mentions? -->
<!-- statistical tests -->
<!-- recommendations? -->
<!-- future ideas: -->
<!-- - ts -- spquery -->
<!-- consider showing spquery (motifmw)? -->
</section>
<section id="references" class="level2">




</section>


<div id="quarto-appendix" class="default"><section class="quarto-appendix-contents" id="quarto-bibliography"><h2 class="anchored quarto-appendix-heading">References</h2><div id="refs" class="references csl-bib-body hanging-indent">
<div id="ref-HagenZanker2009" class="csl-entry">
Hagen‐Zanker, Alex. 2009. <span>“An Improved Fuzzy Kappa Statistic That Accounts for Spatial Autocorrelation.”</span> <em>International Journal of Geographical Information Science</em> 23 (1): 61–73. <a href="https://doi.org/10.1080/13658810802570317">https://doi.org/10.1080/13658810802570317</a>.
</div>
<div id="ref-longComparingSpatialPatterns2018a" class="csl-entry">
Long, Jed, and Colin Robertson. 2018. <span>“Comparing Spatial Patterns.”</span> <em>Geography Compass</em> 12 (2): e12356. <a href="https://doi.org/10.1111/gec3.12356">https://doi.org/10.1111/gec3.12356</a>.
</div>
<div id="ref-Nowosad2024" class="csl-entry">
Nowosad, Jakub. 2024. <span>“Comparing Spatial Patterns in Raster Data Using r.”</span> <em>The International Archives of the Photogrammetry, Remote Sensing and Spatial Information Sciences</em> XLVIII-4/W12-2024 (June): 127–33. <a href="https://doi.org/10.5194/isprs-archives-xlviii-4-w12-2024-127-2024">https://doi.org/10.5194/isprs-archives-xlviii-4-w12-2024-127-2024</a>.
</div>
<div id="ref-sampatComplexWaveletStructural2009" class="csl-entry">
Sampat, M. P., Zhou Wang, S. Gupta, A. C. Bovik, and M. K. Markey. 2009. <span>“Complex <span>Wavelet Structural Similarity</span>: <span>A New Image Similarity Index</span>.”</span> <em>IEEE Transactions on Image Processing</em> 18 (11): 2385–401. <a href="https://doi.org/10.1109/TIP.2009.2025923">https://doi.org/10.1109/TIP.2009.2025923</a>.
</div>
</div></section><section id="footnotes" class="footnotes footnotes-end-of-document"><h2 class="anchored quarto-appendix-heading">Footnotes</h2>

<ol>
<li id="fn1"><p>I tried my best to find possible R implementations, but there is still a chance that I missed some.↩︎</p></li>
</ol>
</section><section class="quarto-appendix-contents" id="quarto-reuse"><h2 class="anchored quarto-appendix-heading">Reuse</h2><div class="quarto-appendix-contents"><div><a rel="license" href="https://creativecommons.org/licenses/by/4.0/">CC BY 4.0</a></div></div></section><section class="quarto-appendix-contents" id="quarto-citation"><h2 class="anchored quarto-appendix-heading">Citation</h2><div><div class="quarto-appendix-secondary-label">BibTeX citation:</div><div class="code-copy-outer-scaffold"><pre class="sourceCode quarto-appendix-bibtex code-with-copy"><code class="sourceCode bibtex">@online{nowosad2024,
  author = {Nowosad, Jakub},
  title = {Understanding and Extending the Methods of Comparing Spatial
    Patterns in Raster Data},
  date = {2024-11-17},
  url = {https://jakubnowosad.com/posts/2024-11-17-spatcomp-bp6/},
  doi = {10.59350/qk14e-ctz80},
  langid = {en}
}
</code></pre></div><div class="quarto-appendix-secondary-label">For attribution, please cite this work as:</div><div id="ref-nowosad2024" class="csl-entry quarto-appendix-citeas">
Nowosad, Jakub. 2024. <span>“Understanding and Extending the Methods of
Comparing Spatial Patterns in Raster Data.”</span> November 17. <a href="https://doi.org/10.59350/qk14e-ctz80">https://doi.org/10.59350/qk14e-ctz80</a>.
</div></div></section></div> ]]></description>
  <category>spatial</category>
  <category>landscape-ecology</category>
  <category>spatial-patterns</category>
  <category>msca-pf</category>
  <category>rstats</category>
  <guid>https://jakubnowosad.com/posts/2024-11-17-spatcomp-bp6/</guid>
  <pubDate>Sun, 17 Nov 2024 00:00:00 GMT</pubDate>
</item>
<item>
  <title>Comparison of spatial patterns in categorical raster data for arbitrary regions using R</title>
  <dc:creator>Jakub Nowosad</dc:creator>
  <link>https://jakubnowosad.com/posts/2024-11-10-spatcomp-bp5/</link>
  <description><![CDATA[ 





<div class="callout callout-style-simple callout-note callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Note</span>Methods for comparing spatial patterns in raster data
</div>
</div>
<div class="callout-body-container callout-body">
<p>This is the fifth part of a blog post series on comparing spatial patterns in raster data. More information about the whole series can be found <a href="../../posts/2024-10-13-spatcomp-bp1">in part one</a>.</p>
</div>
</div>
<p>This blog post focuses on comparing spatial patterns in categorical raster data for arbitrary regions. This means that the methods shown can be applied to compare rasters of the same region (for example, with the same resolution) and rasters of different regions (extents), resolutions, etc. This implies that the outcome of such comparisons is a single value, which indicates the difference/similarity between the spatial patterns of the two rasters.</p>
<p>Comparisons of arbitrary regions allow us to perform various types of spatial pattern analysis, including the search for similar regions or the grouping of regions based on their spatial patterns.</p>
<p>For this blog post, we use three categorical raster datasets: the CORINE Land Cover (CLC) datasets for Tartu (Estonia) for the years 2000 and 2018 and Poznań (Poland) for the year 2018.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(terra)</span>
<span id="cb1-2">clc2000_tartu <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rast</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"https://github.com/Nowosad/comparing-spatial-patterns-2024/raw/refs/heads/main/data/clc2000_tartu.tif"</span>)</span>
<span id="cb1-3">clc2018_tartu <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rast</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"https://github.com/Nowosad/comparing-spatial-patterns-2024/raw/refs/heads/main/data/clc2018_tartu.tif"</span>)</span>
<span id="cb1-4">clc2018_poznan <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rast</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"https://github.com/Nowosad/comparing-spatial-patterns-2024/raw/refs/heads/main/data/clc2018_poznan.tif"</span>)</span>
<span id="cb1-5"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">plot</span>(clc2000_tartu, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">main =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Tartu (2000)"</span>)</span>
<span id="cb1-6"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">plot</span>(clc2018_tartu, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">main =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Tartu (2018)"</span>)</span>
<span id="cb1-7"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">plot</span>(clc2018_poznan, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">main =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Poznań (2018)"</span>)</span></code></pre></div></div>
<div class="cell column-page quarto-layout-panel" data-layout-ncol="3" data-time_it="false">
<div class="quarto-layout-row">
<div class="quarto-layout-cell" style="flex-basis: 33.3%;justify-content: center;">
<p><img src="https://jakubnowosad.com/posts/2024-11-10-spatcomp-bp5/index_files/figure-html/02-categorical-2-1.png" class="img-fluid" width="672"></p>
</div>
<div class="quarto-layout-cell" style="flex-basis: 33.3%;justify-content: center;">
<p><img src="https://jakubnowosad.com/posts/2024-11-10-spatcomp-bp5/index_files/figure-html/02-categorical-2-2.png" class="img-fluid" width="672"></p>
</div>
<div class="quarto-layout-cell" style="flex-basis: 33.3%;justify-content: center;">
<p><img src="https://jakubnowosad.com/posts/2024-11-10-spatcomp-bp5/index_files/figure-html/02-categorical-2-3.png" class="img-fluid" width="672"></p>
</div>
</div>
</div>
<p>The presented type of comparison can be done using information about the spatial context (e.g., the spatial arrangement of the categories; non-spatial context) or without it (e.g., non-spatial context). We start with the latter.</p>
<section id="non-spatial-context" class="level2">
<h2 class="anchored" data-anchor-id="non-spatial-context">Non-spatial context</h2>
<section id="single-value-outcome" class="level3">
<h3 class="anchored" data-anchor-id="single-value-outcome">Single value outcome</h3>
<section id="comparison-of-the-values-of-a-non-spatial-landscape-metric" class="level4">
<h4 class="anchored" data-anchor-id="comparison-of-the-values-of-a-non-spatial-landscape-metric">Comparison of the values of a non-spatial landscape metric</h4>
<p>Landscape metrics are a common way to quantify the spatial pattern of categorical raster data. Hundreds of such metrics exist, with each indicating a different aspect of the pattern of the landscape.<sup>1</sup> Importantly, some metrics are sensitive to the spatial arrangement of the categories, while others are not. An example of a non-spatial metric is Shannon’s diversity index (SHDI), which quantifies the diversity of the landscape based on the number of categories and their proportions. The larger the value of SHDI, the more diverse the landscape is.</p>
<p>Now, if we calculate the SHDI for the two rasters, we can compare the values to see how different the diversity of the landscapes is. The new value indicates the difference in the diversity of the two landscapes.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb2-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(landscapemetrics)</span>
<span id="cb2-2">clc2000_tartu_shdi <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lsm_l_shdi</span>(clc2000_tartu)    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 1.15</span></span>
<span id="cb2-3">clc2018_tartu_shdi <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lsm_l_shdi</span>(clc2018_tartu)    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 1.19</span></span>
<span id="cb2-4">clc2018_poznan_shdi <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lsm_l_shdi</span>(clc2018_poznan)  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 1.21</span></span>
<span id="cb2-5"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">abs</span>(clc2018_tartu_shdi<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>value <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> clc2000_tartu_shdi<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>value)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 0.03642529</code></pre>
</div>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb4-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">abs</span>(clc2018_poznan_shdi<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>value <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> clc2000_tartu_shdi<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>value)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 0.0662407</code></pre>
</div>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb6" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb6-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">abs</span>(clc2018_poznan_shdi<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>value <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> clc2018_tartu_shdi<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>value)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 0.02981541</code></pre>
</div>
</div>
<p>Here, the results show that: a) the diversity of the Tartu landscape has increased between 2000 and 2018, b) the diversity of the Poznań landscape is larger than the Tartu landscape in 2018, and c) at the same time, the most similar landscapes are Tartu in 2018 and Poznań in 2018.</p>
</section>
</section>
</section>
<section id="spatial-context" class="level2">
<h2 class="anchored" data-anchor-id="spatial-context">Spatial context</h2>
<section id="single-value-outcome-1" class="level3">
<h3 class="anchored" data-anchor-id="single-value-outcome-1">Single value outcome</h3>
<section id="comparison-of-the-values-of-a-landscape-metric" class="level4">
<h4 class="anchored" data-anchor-id="comparison-of-the-values-of-a-landscape-metric">Comparison of the values of a landscape metric</h4>
<p>Many existing landscape metrics can also be used to quantify the pattern of categorical raster data with the spatial context. For example, the edge density (ED) metric quantifies the amount of edge in the landscape (where, edge is a length of the border between two categories). The larger the value of ED, the more edges the landscape has – this can be interpreted as a more fragmented landscape.</p>
<p>The difference between ED for the two rasters indicates how differently fragmented the landscapes are.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb8" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb8-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(landscapemetrics)</span>
<span id="cb8-2">clc2000_tartu_ed <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lsm_l_ed</span>(clc2000_tartu)    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 24.7</span></span>
<span id="cb8-3">clc2018_tartu_ed <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lsm_l_ed</span>(clc2018_tartu)    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 24.6</span></span>
<span id="cb8-4">clc2018_poznan_ed <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lsm_l_ed</span>(clc2018_poznan)  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 18.9</span></span>
<span id="cb8-5"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">abs</span>(clc2018_tartu_ed<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>value <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> clc2000_tartu_ed<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>value)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 0.1701932</code></pre>
</div>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb10" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb10-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">abs</span>(clc2018_poznan_ed<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>value <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> clc2000_tartu_ed<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>value)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 5.801784</code></pre>
</div>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb12" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb12-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">abs</span>(clc2018_poznan_ed<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>value <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> clc2018_tartu_ed<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>value)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 5.63159</code></pre>
</div>
</div>
<p>These comparisons show a different perspective on the landscapes: a) the fragmentation of the Tartu landscape (almost) has not changed between 2000 and 2018, b) the Poznań landscape is less fragmented than the Tartu landscape in 2018, and c) the most different landscapes are Tartu in 2018 and Poznań in 2018.</p>
<p>The above examples use the <strong>landscapemetrics</strong> package <span class="citation" data-cites="hesselbarthLandscapemetricsOpenSource2019">(Hesselbarth et al. 2019)</span>.</p>
</section>
<section id="comparison-of-the-values-of-zhaos-entropy-for-landscape-mosaics" class="level4">
<h4 class="anchored" data-anchor-id="comparison-of-the-values-of-zhaos-entropy-for-landscape-mosaics">Comparison of the values of Zhao’s entropy for landscape mosaics</h4>
<p>Recently, several new metrics related to the complexity of the landscapes have been proposed.<sup>2</sup> For categorical rasters, it includes Zhao’s entropy metric <span class="citation" data-cites="zhaoCalculatingSpatialConfigurational2019">(Zhao and Zhang 2019)</span>. The larger the value of Zhao’s entropy, the more complex the landscape is. Thus, the absolute difference between the values of Zhao’s entropy for two rasters indicates how differently complex the landscapes are, with low values indicating similar complexity and high values indicating different complexity.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb14" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb14-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(bespatial)</span>
<span id="cb14-2">clc2000_tartu_bes <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">bes_m_zhao</span>(clc2000_tartu)   <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 0.0372</span></span>
<span id="cb14-3">clc2018_tartu_bes <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">bes_m_zhao</span>(clc2018_tartu)   <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 0.0385</span></span>
<span id="cb14-4">clc2018_poznan_bes <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">bes_m_zhao</span>(clc2018_poznan) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 0.0406</span></span>
<span id="cb14-5"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">abs</span>(clc2018_tartu_bes<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>value <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> clc2000_tartu_bes<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>value)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 0.001267469</code></pre>
</div>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb16" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb16-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">abs</span>(clc2018_poznan_bes<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>value <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> clc2000_tartu_bes<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>value)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 0.003338989</code></pre>
</div>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb18" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb18-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">abs</span>(clc2018_poznan_bes<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>value <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> clc2018_tartu_bes<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>value)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 0.00207152</code></pre>
</div>
</div>
<p>This complexity measure suggests that both rasters for Tartu have similar complexity, while the Poznań raster is more complex than the Tartu rasters. Moreover, the most different landscapes are Tartu in 2000 and Poznań in 2018.</p>
<p>This example uses the <strong>bespatial</strong> package <span class="citation" data-cites="nowosadBespatialBoltzmannEntropy2024">(Nowosad 2024)</span>.</p>
<!-- fractaldim for categorical?? -->
<!-- check! -->
</section>
<section id="dissimilarity-of-a-spatial-signature-between-two-rasters" class="level4">
<h4 class="anchored" data-anchor-id="dissimilarity-of-a-spatial-signature-between-two-rasters">Dissimilarity of a spatial signature between two rasters</h4>
<p>An alternative approach to the use of a single landscape metric for comparing spatial patterns in categorical raster data of arbitrary regions is to calculate a spatial signature of the landscape. A spatial signature is a vector of values that describe the spatial pattern of the landscape. Such a signature may be based on a co-occurrence vector (<em>cove</em>; a vector that shows how often each pair of categories is adjacent to each other). Next, the spatial signatures for two rasters can be compared using a distance measure, such as the Jensen-Shannon divergence <span class="citation" data-cites="jasiewiczExampleBasedRetrievalAlike2013">(Jasiewicz and Stepinski 2013)</span>. The smaller the outcome value, the more similar the spatial patterns of the two rasters are.</p>
<p>The spatial signature can be calculated using the <strong>motif</strong> package <span class="citation" data-cites="nowosadMotifOpensourceTool2021a">(Nowosad 2021)</span> and compared with the <strong>philentropy</strong> package <span class="citation" data-cites="hgPhilentropyInformationTheory2018">(HG 2018)</span>.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb20" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb20-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(motif)</span>
<span id="cb20-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(philentropy)</span>
<span id="cb20-3">clc2000_tartu_sign <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lsp_signature</span>(clc2000_tartu, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">type =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"cove"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">threshold =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span>
<span id="cb20-4">clc2018_tartu_sign <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lsp_signature</span>(clc2018_tartu, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">type =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"cove"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">threshold =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span>
<span id="cb20-5">clc2018_poznan_sign <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lsp_signature</span>(clc2018_poznan, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">type =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"cove"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">threshold =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span>
<span id="cb20-6"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">jensen_shannon</span>(clc2000_tartu_sign<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>signature[[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>]],</span>
<span id="cb20-7">               clc2018_tartu_sign<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>signature[[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>]], </span>
<span id="cb20-8">               <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">testNA =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">unit =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"log2"</span>)       <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 0.0011</span></span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 0.001113697</code></pre>
</div>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb22" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb22-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">jensen_shannon</span>(clc2000_tartu_sign<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>signature[[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>]],</span>
<span id="cb22-2">               clc2018_poznan_sign<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>signature[[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>]], </span>
<span id="cb22-3">               <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">testNA =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">unit =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"log2"</span>)       <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 0.0912</span></span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 0.09115045</code></pre>
</div>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb24" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb24-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">jensen_shannon</span>(clc2018_tartu_sign<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>signature[[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>]],</span>
<span id="cb24-2">               clc2018_poznan_sign<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>signature[[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>]], </span>
<span id="cb24-3">               <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">testNA =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">unit =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"log2"</span>)       <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 0.0826</span></span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 0.08261638</code></pre>
</div>
</div>
<p>Here, using this spatial signature, we can see that the Tartu landscapes are much more similar to each other than to the Poznań landscape.</p>
<!-- relative differences?? -->
</section>
</section>
</section>
<section id="references" class="level2">




</section>


<div id="quarto-appendix" class="default"><section class="quarto-appendix-contents" id="quarto-bibliography"><h2 class="anchored quarto-appendix-heading">References</h2><div id="refs" class="references csl-bib-body hanging-indent">
<div id="ref-hesselbarthLandscapemetricsOpenSource2019" class="csl-entry">
Hesselbarth, Maximilian H. K., Marco Sciaini, Kimberly A. With, Kerstin Wiegand, and Jakub Nowosad. 2019. <span>“<span><em>Landscapemetrics</em></span> : An Open-Source <span><em>R</em></span> Tool to Calculate Landscape Metrics.”</span> <em>Ecography</em> 42 (10): 1648–57. <a href="https://doi.org/10/gf4n9j">https://doi.org/10/gf4n9j</a>.
</div>
<div id="ref-hgPhilentropyInformationTheory2018" class="csl-entry">
HG, Drost. 2018. <span>“Philentropy: <span>Information</span> Theory and Distance Quantification with <span>R</span>.”</span> <em>Journal of Open Source Software</em> 3 (26): 765.
</div>
<div id="ref-jasiewiczExampleBasedRetrievalAlike2013" class="csl-entry">
Jasiewicz, Jaroslaw, and Tomasz F. Stepinski. 2013. <span>“Example-<span>Based Retrieval</span> of <span>Alike Land-Cover Scenes From NLCD2006 Database</span>.”</span> <em>IEEE Geoscience and Remote Sensing Letters</em> 10 (1): 155–59. <a href="https://doi.org/10/gjnqqx">https://doi.org/10/gjnqqx</a>.
</div>
<div id="ref-nowosadMotifOpensourceTool2021a" class="csl-entry">
Nowosad, Jakub. 2021. <span>“Motif: An Open-Source <span>R</span> Tool for Pattern-Based Spatial Analysis.”</span> <em>Landscape Ecology</em> 36: 26–43. <a href="https://doi.org/10.1007/s10980-020-01135-0">https://doi.org/10.1007/s10980-020-01135-0</a>.
</div>
<div id="ref-nowosadBespatialBoltzmannEntropy2024" class="csl-entry">
Nowosad, Jakub. 2024. <em>Bespatial: <span>Boltzmann</span> Entropy for Spatial Data</em>. Manual.
</div>
<div id="ref-zhaoCalculatingSpatialConfigurational2019" class="csl-entry">
Zhao, Y., and X. C. Zhang. 2019. <span>“Calculating Spatial Configurational Entropy of a Landscape Mosaic Based on the <span>Wasserstein</span> Metric.”</span> <em>Landscape Ecology</em> 34 (8): 1849–58. <a href="https://doi.org/10/gkb47d">https://doi.org/10/gkb47d</a>.
</div>
</div></section><section id="footnotes" class="footnotes footnotes-end-of-document"><h2 class="anchored quarto-appendix-heading">Footnotes</h2>

<ol>
<li id="fn1"><p>The term “landscape” here refers to categorical raster data.↩︎</p></li>
<li id="fn2"><p>Such complexity is meant to represent both the diversity and spatial arrangement of the categories. The ongoing discussion is on whether such complexity can be quantified with a single metric.↩︎</p></li>
</ol>
</section><section class="quarto-appendix-contents" id="quarto-reuse"><h2 class="anchored quarto-appendix-heading">Reuse</h2><div class="quarto-appendix-contents"><div><a rel="license" href="https://creativecommons.org/licenses/by/4.0/">CC BY 4.0</a></div></div></section><section class="quarto-appendix-contents" id="quarto-citation"><h2 class="anchored quarto-appendix-heading">Citation</h2><div><div class="quarto-appendix-secondary-label">BibTeX citation:</div><div class="code-copy-outer-scaffold"><pre class="sourceCode quarto-appendix-bibtex code-with-copy"><code class="sourceCode bibtex">@online{nowosad2024,
  author = {Nowosad, Jakub},
  title = {Comparison of Spatial Patterns in Categorical Raster Data for
    Arbitrary Regions Using {R}},
  date = {2024-11-10},
  url = {https://jakubnowosad.com/posts/2024-11-10-spatcomp-bp5/},
  doi = {10.59350/65v6p-7zs59},
  langid = {en}
}
</code></pre></div><div class="quarto-appendix-secondary-label">For attribution, please cite this work as:</div><div id="ref-nowosad2024" class="csl-entry quarto-appendix-citeas">
Nowosad, Jakub. 2024. <span>“Comparison of Spatial Patterns in
Categorical Raster Data for Arbitrary Regions Using R.”</span> November
10. <a href="https://doi.org/10.59350/65v6p-7zs59">https://doi.org/10.59350/65v6p-7zs59</a>.
</div></div></section></div> ]]></description>
  <category>spatial</category>
  <category>landscape-ecology</category>
  <category>spatial-patterns</category>
  <category>msca-pf</category>
  <category>rstats</category>
  <guid>https://jakubnowosad.com/posts/2024-11-10-spatcomp-bp5/</guid>
  <pubDate>Sun, 10 Nov 2024 00:00:00 GMT</pubDate>
</item>
<item>
  <title>Comparison of spatial patterns in categorical raster data for overlapping regions using R</title>
  <dc:creator>Jakub Nowosad</dc:creator>
  <link>https://jakubnowosad.com/posts/2024-11-03-spatcomp-bp4/</link>
  <description><![CDATA[ 





<div class="callout callout-style-simple callout-note callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Note</span>Methods for comparing spatial patterns in raster data
</div>
</div>
<div class="callout-body-container callout-body">
<p>This is the fourth part of a blog post series on comparing spatial patterns in raster data. More information about the whole series can be found <a href="../../posts/2024-10-13-spatcomp-bp1">in part one</a>.</p>
</div>
</div>
<p>This blog post focuses on the comparison of spatial patterns in categorical raster data for overlapping regions. In other words, here we have two rasters with the same number of rows and columns, and we want to compare their spatial patterns.</p>
<p>For this blog post, we use two categorical raster datasets: the CORINE Land Cover (CLC) datasets for Tartu (Estonia) for the years 2000 and 2018.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(terra)</span>
<span id="cb1-2">clc2000_tartu <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rast</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"https://github.com/Nowosad/comparing-spatial-patterns-2024/raw/refs/heads/main/data/clc2000_tartu.tif"</span>)</span>
<span id="cb1-3">clc2018_tartu <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rast</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"https://github.com/Nowosad/comparing-spatial-patterns-2024/raw/refs/heads/main/data/clc2018_tartu.tif"</span>)</span>
<span id="cb1-4"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">plot</span>(clc2000_tartu, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">main =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Tartu (2000)"</span>)</span>
<span id="cb1-5"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">plot</span>(clc2018_tartu, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">main =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Tartu (2018)"</span>)</span></code></pre></div></div>
<div class="cell column-page quarto-layout-panel" data-layout-ncol="2" data-fig-witdh="10" data-time_it="false">
<div class="quarto-layout-row">
<div class="quarto-layout-cell" style="flex-basis: 50.0%;justify-content: center;">
<p><img src="https://jakubnowosad.com/posts/2024-11-03-spatcomp-bp4/index_files/figure-html/02-categorical-2-1.png" class="img-fluid" width="672"></p>
</div>
<div class="quarto-layout-cell" style="flex-basis: 50.0%;justify-content: center;">
<p><img src="https://jakubnowosad.com/posts/2024-11-03-spatcomp-bp4/index_files/figure-html/02-categorical-2-2.png" class="img-fluid" width="672"></p>
</div>
</div>
</div>
<p>In short, the red areas in the rasters represent urban areas, the green areas represent forests, the blue areas represent water bodies, and the yellow areas represent agricultural land.</p>
<section id="non-spatial-context" class="level2">
<h2 class="anchored" data-anchor-id="non-spatial-context">Non-spatial context</h2>
<section id="raster-outcome" class="level3">
<h3 class="anchored" data-anchor-id="raster-outcome">Raster outcome</h3>
<section id="the-binary-difference-between-two-rasters" class="level4">
<h4 class="anchored" data-anchor-id="the-binary-difference-between-two-rasters">The binary difference between two rasters</h4>
<p>The simplest way to compare two categorical rasters is to create a binary raster that indicates whether the values of the two rasters are the same or different. Here, the output highlights the areas where the values of the two rasters differ (yellow) and where they are the same (purple).</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb2-1">clc_diff <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> clc2018_tartu <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">!=</span> clc2000_tartu</span>
<span id="cb2-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">plot</span>(clc_diff)</span></code></pre></div></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://jakubnowosad.com/posts/2024-11-03-spatcomp-bp4/index_files/figure-html/02-categorical-3-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
</section>
</section>
<section id="multiple-value-outcome" class="level3">
<h3 class="anchored" data-anchor-id="multiple-value-outcome">Multiple value outcome</h3>
<p>To get a more detailed comparison, we can calculate the confusion matrix (also known as, e.g., contingency table) of the two rasters. It shows the number of pixels that have the same value in both rasters (diagonal) and the number of pixels with different values (off-diagonal).</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb3-1">cm <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">table</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">values</span>(clc2000_tartu), <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">values</span>(clc2018_tartu))</span>
<span id="cb3-2">cm</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>   
        1     2     3     4     6     7
  1  4821   357     2    10     0     0
  2  1342 67915   684   389     0     0
  3    59   415 33670  2896     9    22
  4   122   638  1435  7040   229    24
  6     0     3    13    20  2177    37
  7     0     0     0     0     3  1995</code></pre>
</div>
</div>
<p>For example, the table shows that there are 4821 pixels with the value 1 in both rasters, and 357 pixels with the value 2 in the first raster and the value 1 in the second raster.</p>
<p>Confusion matrices is also a building block for <a href="https://en.wikipedia.org/wiki/Template:Diagnostic_testing_diagram">many other statistics</a>, including accuracy, that can be calculated to compare the two sets of categorical data.</p>
</section>
<section id="single-value-outcome" class="level3">
<h3 class="anchored" data-anchor-id="single-value-outcome">Single value outcome</h3>
<section id="the-proportion-of-changed-pixels" class="level4">
<h4 class="anchored" data-anchor-id="the-proportion-of-changed-pixels">The proportion of changed pixels</h4>
<p>A binary difference raster can be also used to calculate the proportion of changed pixels by dividing the number of changed pixels by the total number of non-NA pixels.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb5-1">clc_diff <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> clc2018_tartu <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">!=</span> clc2000_tartu</span>
<span id="cb5-2">changed_pixels <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">freq</span>(clc_diff)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>count[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>]</span>
<span id="cb5-3">na_pixels <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">freq</span>(clc_diff, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">value =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NA</span>)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>count</span>
<span id="cb5-4">total_pixels <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ncell</span>(clc_diff) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> na_pixels </span>
<span id="cb5-5">proportion_changed <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> changed_pixels <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> total_pixels</span>
<span id="cb5-6">proportion_changed</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 0.06894013</code></pre>
</div>
</div>
<p>This outcome shows that about 0.07 of the pixels have changed between the two rasters.</p>
</section>
<section id="the-overall-comparison" class="level4">
<h4 class="anchored" data-anchor-id="the-overall-comparison">The overall comparison</h4>
<!-- double check -->
<p>The overall comparison of the two rasters can be done by calculating the difference between the frequencies of the values of the two rasters <span class="citation" data-cites="pontiusStatisticalMethodsPartition2002">(Pontius 2002)</span>. <!-- improve? --></p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb7" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb7-1">clc2000_tartu_freq <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">freq</span>(clc2000_tartu)</span>
<span id="cb7-2">clc2018_tartu_freq <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">freq</span>(clc2018_tartu)</span>
<span id="cb7-3">freq <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">merge</span>(clc2000_tartu_freq, clc2018_tartu_freq, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">by =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"value"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">all =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>)</span>
<span id="cb7-4">freq<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>diff <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">abs</span>(freq<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>count.x <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> freq<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>count.y)</span>
<span id="cb7-5">sum_diff <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sum</span>(freq<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>diff)</span>
<span id="cb7-6">na_pixels <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">freq</span>(clc_diff, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">value =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NA</span>)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>count</span>
<span id="cb7-7">total_pixels <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ncell</span>(clc_diff) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> na_pixels </span>
<span id="cb7-8"><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> sum_diff <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> total_pixels</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 0.9640774</code></pre>
</div>
</div>
<p>The overall comparison tells what is the percentage of pixels that have the same class in both rasters (however, it does not consider if the pixels are in the same location).</p>
</section>
<section id="statistics-of-the-differences-between-rasters-values" class="level4">
<h4 class="anchored" data-anchor-id="statistics-of-the-differences-between-rasters-values">Statistics of the differences between rasters’ values</h4>
<p>More statistics of the differences between the values of the two rasters can be calculated using the <strong>diffeR</strong> package <span class="citation" data-cites="pontiusjr.DiffeRMetricsDifference2023">(Pontius Jr. and Santacruz 2023)</span>. These statistics are based on the confusion matrix and include the overall allocation disagreement, overall difference, overall exchange disagreement, overall quantity disagreement, and overall shift disagreement.<sup>1</sup></p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb9" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb9-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(diffeR)</span>
<span id="cb9-2">clc_ct <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">crosstabm</span>(clc2000_tartu, clc2018_tartu)</span>
<span id="cb9-3">diffeR_df <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">data.frame</span>(</span>
<span id="cb9-4">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">overallAllocD =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">overallAllocD</span>(clc_ct),</span>
<span id="cb9-5">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">overallDiff =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">overallDiff</span>(clc_ct),</span>
<span id="cb9-6">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">overallExchangeD =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">overallExchangeD</span>(clc_ct),</span>
<span id="cb9-7">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">overallQtyD =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">overallQtyD</span>(clc_ct),</span>
<span id="cb9-8">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">overallShiftD =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">overallShiftD</span>(clc_ct)</span>
<span id="cb9-9">)</span>
<span id="cb9-10">diffeR_df</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>  overallAllocD overallDiff overallExchangeD overallQtyD overallShiftD
1          6440        8709             5280        2269          1160</code></pre>
</div>
</div>
</section>
</section>
</section>
<section id="spatial-context" class="level2">
<h2 class="anchored" data-anchor-id="spatial-context">Spatial context</h2>
<section id="raster-outcome-1" class="level3">
<h3 class="anchored" data-anchor-id="raster-outcome-1">Raster outcome</h3>
<section id="the-difference-between-a-focal-measure-of-two-rasters-e.g.-selected-landscape-metric" class="level4">
<h4 class="anchored" data-anchor-id="the-difference-between-a-focal-measure-of-two-rasters-e.g.-selected-landscape-metric">The difference between a focal measure of two rasters (e.g., selected landscape metric)</h4>
<p>To include the spatial context in the comparison, we can calculate the difference between a focal measure of two rasters. An example of such a measure is the relative mutual information (<code>relmutinf</code>) metric, which quantifies the clumpiness of the landscape – the larger the value, the more clumped the landscape is [^See <a href="../../posts/2019-06-25-ent-bp1/#relative-mutual-information-u">a blog post about this metric</a>].</p>
<p>The below code chunk uses the <strong>landscapemetrics</strong> package <span class="citation" data-cites="hesselbarthLandscapemetricsOpenSource2019">(Hesselbarth et al. 2019)</span> to specify a moving window of 5 by 5 and calculate the <code>relmutinf</code> metric for the two rasters. Next, it calculates the absolute difference between the two rasters and plots the result.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb11" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb11-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(landscapemetrics)</span>
<span id="cb11-2">window <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">matrix</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">nrow =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ncol =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>)</span>
<span id="cb11-3">clc2000_tartu_relmutinf_mw <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">window_lsm</span>(clc2000_tartu, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">window =</span> window,</span>
<span id="cb11-4">                                        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">what =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"lsm_l_relmutinf"</span>)</span>
<span id="cb11-5">clc2018_tartu_relmutinf_mw <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">window_lsm</span>(clc2018_tartu, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">window =</span> window, </span>
<span id="cb11-6">                                        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">what =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"lsm_l_relmutinf"</span>)</span>
<span id="cb11-7">clc2018_tartu_relmutinf_diff <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">abs</span>(clc2018_tartu_relmutinf_mw[[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>]][[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>]] <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span></span>
<span id="cb11-8">                                   clc2000_tartu_relmutinf_mw[[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>]][[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>]])</span>
<span id="cb11-9"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">plot</span>(clc2018_tartu_relmutinf_diff)</span></code></pre></div></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://jakubnowosad.com/posts/2024-11-03-spatcomp-bp4/index_files/figure-html/02-categorical-4-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<p>The largest values in the output indicate the areas where the clumpiness of the landscape has changed the most between the two rasters.</p>
<p>Alternatively, if we calculate the regular difference, the output will show the areas where the clumpiness of the landscape has increased (positive values) and decreased (negative values).</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb12" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb12-1">plot_div <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(r, ...){</span>
<span id="cb12-2">  r_range <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">range</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">values</span>(r), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">na.rm =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">finite =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>)</span>
<span id="cb12-3">  max_abs <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">max</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">abs</span>(r_range))</span>
<span id="cb12-4">  new_range <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>max_abs, max_abs)</span>
<span id="cb12-5">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">plot</span>(r, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">col =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">hcl.colors</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">palette =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"prgn"</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">range =</span> new_range, ...)</span>
<span id="cb12-6">}</span>
<span id="cb12-7">clc2018_tartu_relmutinf_diff2 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> clc2018_tartu_relmutinf_mw[[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>]][[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>]] <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> </span>
<span id="cb12-8">                                clc2000_tartu_relmutinf_mw[[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>]][[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>]]</span>
<span id="cb12-9"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">plot_div</span>(clc2018_tartu_relmutinf_diff2)</span></code></pre></div></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://jakubnowosad.com/posts/2024-11-03-spatcomp-bp4/index_files/figure-html/unnamed-chunk-1-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<!-- should I include it? -->
</section>
<section id="cross-entropy-loss-function" class="level4">
<h4 class="anchored" data-anchor-id="cross-entropy-loss-function">Cross-entropy loss function</h4>
<p>The moving window approach is also used in the <code>raster.change()</code> function from the <strong>spatialEco</strong> package <span class="citation" data-cites="evansSpatialEco2023">(Evans and Murphy 2023)</span>. Its first two arguments are the two rasters to compare, the <code>s</code> argument specifies the size of the moving window, and the <code>stat</code> argument specifies the statistic to calculate. For example, <code>stat = "cross-entropy"</code> calculates the cross-entropy loss function, where the larger the value, the more different the two rasters are.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb13" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb13-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(spatialEco)</span>
<span id="cb13-2">clc_ce <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">raster.change</span>(clc2000_tartu, clc2018_tartu, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">s =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>, </span>
<span id="cb13-3">                       <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">stat =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"cross-entropy"</span>)</span>
<span id="cb13-4"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">plot</span>(clc_ce)</span></code></pre></div></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://jakubnowosad.com/posts/2024-11-03-spatcomp-bp4/index_files/figure-html/02-categorical-13-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<p><em>Note that the above calculation may take a few minutes to complete.</em></p>
</section>
</section>
<section id="multiple-value-outcome-1" class="level3">
<h3 class="anchored" data-anchor-id="multiple-value-outcome-1">Multiple value outcome</h3>
<p>Various statistics from categorical data can be calculated at multiple scales with the <strong>waywiser</strong> package <span class="citation" data-cites="mahoneyWaywiserErgonomicMethods2023">(Mahoney 2023)</span>. Here, the <code>ww_multi_scale()</code> function calculates the accuracy of the two rasters at different scales from 500 to 3000 meters (map units).</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb14" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb14-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(waywiser)</span>
<span id="cb14-2">cell_sizes <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">seq</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">500</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3000</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">by =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">500</span>)</span>
<span id="cb14-3">clc_multi_scale <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ww_multi_scale</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">truth =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.factor</span>(clc2000_tartu),</span>
<span id="cb14-4">                                 <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">estimate =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.factor</span>(clc2018_tartu),</span>
<span id="cb14-5">                                 <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">metrics =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(yardstick<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span>accuracy), </span>
<span id="cb14-6">                                 <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">cellsize =</span> cell_sizes,</span>
<span id="cb14-7">                                 <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">progress =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>)</span>
<span id="cb14-8">clc_multi_scale</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code># A tibble: 6 × 6
  .metric  .estimator .estimate .grid_args       .grid            .notes  
  &lt;chr&gt;    &lt;chr&gt;          &lt;dbl&gt; &lt;list&gt;           &lt;list&gt;           &lt;list&gt;  
1 accuracy multiclass     0.781 &lt;tibble [1 × 1]&gt; &lt;sf [6,400 × 5]&gt; &lt;tibble&gt;
2 accuracy multiclass     0.607 &lt;tibble [1 × 1]&gt; &lt;sf [1,600 × 5]&gt; &lt;tibble&gt;
3 accuracy multiclass     0.453 &lt;tibble [1 × 1]&gt; &lt;sf [729 × 5]&gt;   &lt;tibble&gt;
4 accuracy multiclass     0.358 &lt;tibble [1 × 1]&gt; &lt;sf [400 × 5]&gt;   &lt;tibble&gt;
5 accuracy multiclass     0.246 &lt;tibble [1 × 1]&gt; &lt;sf [256 × 5]&gt;   &lt;tibble&gt;
6 accuracy multiclass     0.270 &lt;tibble [1 × 1]&gt; &lt;sf [196 × 5]&gt;   &lt;tibble&gt;</code></pre>
</div>
</div>
<p>The output shows the accuracy of the two rasters at each scale: the largest value is at the scale of 500 meters, and the smallest value is at the scale of 3000 meters. It shows that with the increase of the scale, the agreement between the two rasters decreases – both rasters are similar at the local scale, but they differ at the regional one.</p>
</section>
<section id="single-value-outcome-1" class="level3">
<h3 class="anchored" data-anchor-id="single-value-outcome-1">Single value outcome</h3>
<section id="spatial-association-between-regionalizations-using-v-measure" class="level4">
<h4 class="anchored" data-anchor-id="spatial-association-between-regionalizations-using-v-measure">Spatial association between regionalizations using V-measure</h4>
<p>The <strong>sabre</strong> package <span class="citation" data-cites="nowosadSpatialAssociationRegionalizations2018">(Nowosad and Stepinski 2018)</span> provides a function to calculate a few measures of spatial association between two categorical maps.<sup>2</sup></p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb16" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb16-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(sabre)</span>
<span id="cb16-2">clc_sabre <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">vmeasure_calc</span>(clc2000_tartu, clc2018_tartu)</span>
<span id="cb16-3">clc_sabre</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>The SABRE results:

 V-measure: 0.77 
 Homogeneity: 0.78 
 Completeness: 0.76 

 The spatial objects can be retrieved with:
 $map1 - the first map
 $map2 - the second map </code></pre>
</div>
</div>
<p>Its output returns three values: the homogeneity, the completeness, and the V-measure. The homogeneity measures how well regions from the first map fit inside of regions from the second map, the completeness measures how well regions from the second map fit inside of regions from the first map, and the V-measure is the weighted harmonic mean of homogeneity and completeness. All of them range from 0 to 1, where larger values indicate better spatial agreement.</p>
<p>Additionally, the output contains two sets of maps of regions’ inhomogeneities (rih): the first set shows how the regions from the first map are inhomogenous in regard to the regions from the second map, and the second set shows how the regions from the second map are inhomogenous in regard to the regions from the first map.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb18" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb18-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">plot</span>(clc_sabre<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>map1[[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>]])</span>
<span id="cb18-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">plot</span>(clc_sabre<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>map2[[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>]])</span></code></pre></div></div>
<div class="cell quarto-layout-panel" data-layout-ncol="2">
<div class="quarto-layout-row">
<div class="quarto-layout-cell" style="flex-basis: 50.0%;justify-content: center;">
<p><img src="https://jakubnowosad.com/posts/2024-11-03-spatcomp-bp4/index_files/figure-html/unnamed-chunk-2-1.png" class="img-fluid" width="672"></p>
</div>
<div class="quarto-layout-cell" style="flex-basis: 50.0%;justify-content: center;">
<p><img src="https://jakubnowosad.com/posts/2024-11-03-spatcomp-bp4/index_files/figure-html/unnamed-chunk-2-2.png" class="img-fluid" width="672"></p>
</div>
</div>
</div>
<!-- Multiple resolution procedure [@costanzaModelGoodnessFit1989]; Expanding window approach [@kuhnertComparingRasterMap2005]; Fuzzy Kappa [@hagen-zankerImprovedFuzzyKappa2009] -->
</section>
</section>
</section>
<section id="references" class="level2">




</section>


<div id="quarto-appendix" class="default"><section class="quarto-appendix-contents" id="quarto-bibliography"><h2 class="anchored quarto-appendix-heading">References</h2><div id="refs" class="references csl-bib-body hanging-indent">
<div id="ref-evansSpatialEco2023" class="csl-entry">
Evans, Jeffrey S., and Melanie A. Murphy. 2023. <em><span class="nocase">spatialEco</span></em>. Manual.
</div>
<div id="ref-hesselbarthLandscapemetricsOpenSource2019" class="csl-entry">
Hesselbarth, Maximilian H. K., Marco Sciaini, Kimberly A. With, Kerstin Wiegand, and Jakub Nowosad. 2019. <span>“<span><em>Landscapemetrics</em></span> : An Open-Source <span><em>R</em></span> Tool to Calculate Landscape Metrics.”</span> <em>Ecography</em> 42 (10): 1648–57. <a href="https://doi.org/10/gf4n9j">https://doi.org/10/gf4n9j</a>.
</div>
<div id="ref-mahoneyWaywiserErgonomicMethods2023" class="csl-entry">
Mahoney, Michael J. 2023. <em>Waywiser: <span>Ergonomic</span> Methods for Assessing Spatial Models</em>. <a href="https://doi.org/10.48550/arXiv.2303.11312">https://doi.org/10.48550/arXiv.2303.11312</a>.
</div>
<div id="ref-nowosadSpatialAssociationRegionalizations2018" class="csl-entry">
Nowosad, J., and T. F. Stepinski. 2018. <span>“Spatial Association Between Regionalizations Using the Information-Theoretical <span><em>V</em></span> -Measure.”</span> <em>International Journal of Geographical Information Science</em> 32 (12): 2386–401. <a href="https://doi.org/10/gf283f">https://doi.org/10/gf283f</a>.
</div>
<div id="ref-pontiusjr.DiffeRMetricsDifference2023" class="csl-entry">
Pontius Jr., Robert Gilmore, and Ali Santacruz. 2023. <em><span class="nocase">diffeR</span>: <span>Metrics</span> of Difference for Comparing Pairs of Maps or Pairs of Variables</em>. Manual.
</div>
<div id="ref-pontiusStatisticalMethodsPartition2002" class="csl-entry">
Pontius, R Gil. 2002. <span>“Statistical <span>Methods</span> to <span>Partition Effects</span> of <span>Quantity</span> and <span>Location During Comparison</span> of <span>Categorical Maps</span> at <span>Multiple Resolutions</span>.”</span> <em>Photogrammetric Engineering</em>.
</div>
</div></section><section id="footnotes" class="footnotes footnotes-end-of-document"><h2 class="anchored quarto-appendix-heading">Footnotes</h2>

<ol>
<li id="fn1"><p>And more, as can be found in the package documentation – see <code>?diffeR::overallAllocD</code>.↩︎</p></li>
<li id="fn2"><p>Its output actually gives a few values and two maps, but the most important one is the V-measure.↩︎</p></li>
</ol>
</section><section class="quarto-appendix-contents" id="quarto-reuse"><h2 class="anchored quarto-appendix-heading">Reuse</h2><div class="quarto-appendix-contents"><div><a rel="license" href="https://creativecommons.org/licenses/by/4.0/">CC BY 4.0</a></div></div></section><section class="quarto-appendix-contents" id="quarto-citation"><h2 class="anchored quarto-appendix-heading">Citation</h2><div><div class="quarto-appendix-secondary-label">BibTeX citation:</div><div class="code-copy-outer-scaffold"><pre class="sourceCode quarto-appendix-bibtex code-with-copy"><code class="sourceCode bibtex">@online{nowosad2024,
  author = {Nowosad, Jakub},
  title = {Comparison of Spatial Patterns in Categorical Raster Data for
    Overlapping Regions Using {R}},
  date = {2024-11-03},
  url = {https://jakubnowosad.com/posts/2024-11-03-spatcomp-bp4/},
  doi = {10.59350/hbz1k-nvz12},
  langid = {en}
}
</code></pre></div><div class="quarto-appendix-secondary-label">For attribution, please cite this work as:</div><div id="ref-nowosad2024" class="csl-entry quarto-appendix-citeas">
Nowosad, Jakub. 2024. <span>“Comparison of Spatial Patterns in
Categorical Raster Data for Overlapping Regions Using R.”</span>
November 3. <a href="https://doi.org/10.59350/hbz1k-nvz12">https://doi.org/10.59350/hbz1k-nvz12</a>.
</div></div></section></div> ]]></description>
  <category>spatial</category>
  <category>landscape-ecology</category>
  <category>spatial-patterns</category>
  <category>msca-pf</category>
  <category>rstats</category>
  <guid>https://jakubnowosad.com/posts/2024-11-03-spatcomp-bp4/</guid>
  <pubDate>Sun, 03 Nov 2024 00:00:00 GMT</pubDate>
</item>
<item>
  <title>Comparison of spatial patterns in continuous raster data for arbitrary regions using R</title>
  <dc:creator>Jakub Nowosad</dc:creator>
  <link>https://jakubnowosad.com/posts/2024-10-27-spatcomp-bp3/</link>
  <description><![CDATA[ 





<div class="callout callout-style-simple callout-note callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Note</span>Methods for comparing spatial patterns in raster data
</div>
</div>
<div class="callout-body-container callout-body">
<p>This is the third part of a blog post series on comparing spatial patterns in raster data. More information about the whole series can be found <a href="../../posts/2024-10-13-spatcomp-bp1">in part one</a>.</p>
</div>
</div>
<p>This blog post focuses on the comparison of spatial patterns in continuous raster data for arbitrary regions. Thus, the shown methods require two continuous rasters, which may have different extents, resolutions, etc. The outcome of such comparisons is, most often, a single value, which indicates the difference/similarity between the spatial patterns of the two rasters.</p>
<p>Three continuous raster datasets are used in this blog post: the Normalized Difference Vegetation Index (NDVI) datasets for Tartu (Estonia) for the years 2018 and 2023, and Poznań (Poland) for the year 2023.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(terra)</span>
<span id="cb1-2">ndvi2018_tartu <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rast</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"https://github.com/Nowosad/comparing-spatial-patterns-2024/raw/refs/heads/main/data/ndvi2018_tartu.tif"</span>)</span>
<span id="cb1-3">ndvi2023_tartu <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rast</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"https://github.com/Nowosad/comparing-spatial-patterns-2024/raw/refs/heads/main/data/ndvi2023_tartu.tif"</span>)</span>
<span id="cb1-4">ndvi2023_poznan <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rast</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"https://github.com/Nowosad/comparing-spatial-patterns-2024/raw/refs/heads/main/data/ndvi2023_poznan.tif"</span>)</span>
<span id="cb1-5"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">plot</span>(ndvi2018_tartu, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">main =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Tartu (2018)"</span>)</span>
<span id="cb1-6"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">plot</span>(ndvi2023_tartu, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">main =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Tartu (2023)"</span>)</span>
<span id="cb1-7"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">plot</span>(ndvi2023_poznan, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">main =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Poznan (2023)"</span>)</span></code></pre></div></div>
<div class="cell column-page quarto-layout-panel" data-layout-ncol="3">
<div class="quarto-layout-row">
<div class="quarto-layout-cell" style="flex-basis: 33.3%;justify-content: center;">
<p><img src="https://jakubnowosad.com/posts/2024-10-27-spatcomp-bp3/index_files/figure-html/cont-1.png" class="img-fluid" width="672"></p>
</div>
<div class="quarto-layout-cell" style="flex-basis: 33.3%;justify-content: center;">
<p><img src="https://jakubnowosad.com/posts/2024-10-27-spatcomp-bp3/index_files/figure-html/cont-2.png" class="img-fluid" width="672"></p>
</div>
<div class="quarto-layout-cell" style="flex-basis: 33.3%;justify-content: center;">
<p><img src="https://jakubnowosad.com/posts/2024-10-27-spatcomp-bp3/index_files/figure-html/cont-3.png" class="img-fluid" width="672"></p>
</div>
</div>
</div>
<section id="non-spatial-context" class="level2">
<h2 class="anchored" data-anchor-id="non-spatial-context">Non-spatial context</h2>
<section id="single-value-outcome" class="level3">
<h3 class="anchored" data-anchor-id="single-value-outcome">Single value outcome</h3>
<section id="disimilarity-between-the-distributions-of-two-rasters-values" class="level4">
<h4 class="anchored" data-anchor-id="disimilarity-between-the-distributions-of-two-rasters-values">Disimilarity between the distributions of two rasters’ values</h4>
<p>Rasters consist of values, and thus, it seems possible to compare the distributions of these values. However, it may not be straightforward as they may have different lengths, ranges, etc. There are many possible ways to create distributions from rasters, but here I show just one:</p>
<ol type="1">
<li>Extract the non-missing values from the rasters.</li>
<li>Rescale the values to the range of 0 to 1.</li>
<li>Bin the values to create histograms.</li>
<li>Normalize the histogram counts to get probability distributions.</li>
</ol>
<p>Importantly, the above approach involves many decisions, for example, should we use the minimum and maximum values of both rasters or each separately; how many bins should we use; etc.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb2-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 1. Extract the non-missing values from the rasters</span></span>
<span id="cb2-2">values1 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">na.omit</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">values</span>(ndvi2023_tartu)[, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>])</span>
<span id="cb2-3">values2 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">na.omit</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">values</span>(ndvi2023_poznan)[, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>])</span>
<span id="cb2-4"></span>
<span id="cb2-5"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 2. Scale the values to the range of 0 to 1</span></span>
<span id="cb2-6">values1_rescaled <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> (values1 <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">min</span>(values1)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> (<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">max</span>(values1) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">min</span>(values1))</span>
<span id="cb2-7">values2_rescaled <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> (values2 <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">min</span>(values2)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> (<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">max</span>(values2) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">min</span>(values2))</span>
<span id="cb2-8"></span>
<span id="cb2-9"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 3. Bin the values to create histograms</span></span>
<span id="cb2-10">bin_edges <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">seq</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">length.out =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">33</span>)</span>
<span id="cb2-11">hist1 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">hist</span>(values1_rescaled, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">breaks =</span> bin_edges, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">plot =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>)</span>
<span id="cb2-12">hist2 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">hist</span>(values2_rescaled, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">breaks =</span> bin_edges, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">plot =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>)</span>
<span id="cb2-13"></span>
<span id="cb2-14"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 4. Normalize the histogram counts to get probability distributions</span></span>
<span id="cb2-15">prob1 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> hist1<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>counts <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sum</span>(hist1<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>counts)</span>
<span id="cb2-16">prob2 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> hist2<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>counts <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sum</span>(hist2<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>counts)</span></code></pre></div></div>
</div>
<p>Next, we can calculate the dissimilarity between the two distributions, for example, using the Kullback-Leibler divergence implemented in the <strong>philentropy</strong> package <span class="citation" data-cites="hgPhilentropyInformationTheory2018">(HG 2018)</span>.<sup>1</sup></p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb3-1">philentropy<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">distance</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rbind</span>(prob1, prob2), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">method =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"kullback-leibler"</span>)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>kullback-leibler 
       0.1033112 </code></pre>
</div>
</div>
<p>Lower values of the Kullback-Leibler divergence suggest that the distributions are more similar.</p>
<p>The above approach can be generalized with just one modification – the maximum and minimum values are external parameters.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb5-1">get_min_max <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(rast_list){</span>
<span id="cb5-2">    min_v <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">min</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">vapply</span>(rast_list, </span>
<span id="cb5-3">                       <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">FUN =</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(r) <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">min</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">na.omit</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">values</span>(r)[, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>])),</span>
<span id="cb5-4">                       <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">FUN.VALUE =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">numeric</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)))</span>
<span id="cb5-5">    max_v <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">max</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">vapply</span>(rast_list, </span>
<span id="cb5-6">                       <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">FUN =</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(r) <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">max</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">na.omit</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">values</span>(r)[, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>])),</span>
<span id="cb5-7">                       <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">FUN.VALUE =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">numeric</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)))</span>
<span id="cb5-8">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">return</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(min_v, max_v))</span>
<span id="cb5-9">}</span>
<span id="cb5-10">prepare_hist <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(r, min_v, max_v){</span>
<span id="cb5-11">    values_r <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">na.omit</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">values</span>(r)[, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>])</span>
<span id="cb5-12">    values_r_rescaled <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> (values_r <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> min_v) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> (max_v <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> min_v)</span>
<span id="cb5-13">    bin_edges <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">seq</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">length.out =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">33</span>)  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 32 bins</span></span>
<span id="cb5-14">    hist_r <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">hist</span>(values_r_rescaled, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">breaks =</span> bin_edges, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">plot =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>)</span>
<span id="cb5-15">    prob_r <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> hist_r<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>counts <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sum</span>(hist_r<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>counts)</span>
<span id="cb5-16">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">return</span>(prob_r)</span>
<span id="cb5-17">}</span>
<span id="cb5-18">min_max <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">get_min_max</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(ndvi2018_tartu, ndvi2023_tartu, ndvi2023_poznan))</span>
<span id="cb5-19"></span>
<span id="cb5-20">tartu2018_hist <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">prepare_hist</span>(ndvi2018_tartu, min_max[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>], min_max[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>])</span>
<span id="cb5-21">tartu2023_hist <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">prepare_hist</span>(ndvi2023_tartu, min_max[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>], min_max[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>])</span>
<span id="cb5-22">poznan2023_hist <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">prepare_hist</span>(ndvi2023_poznan, min_max[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>], min_max[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>])</span>
<span id="cb5-23"></span>
<span id="cb5-24">philentropy<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">distance</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rbind</span>(tartu2018_hist, tartu2023_hist, poznan2023_hist),</span>
<span id="cb5-25">                      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">method =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"kullback-leibler"</span>)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>         v1         v2         v3
v1 0.000000 2.48398816 2.65464473
v2 2.483988 0.00000000 0.08389761
v3 2.654645 0.08389761 0.00000000</code></pre>
</div>
</div>
<!-- v1         v2         v3
v1 0.000000 2.48398816 2.65464473
v2 2.483988 0.00000000 0.08389761
v3 2.654645 0.08389761 0.00000000 -->
<p>The results suggest that the distributions of NDVI values for Tartu and Poznan in 2023 are more similar to each other than to the distribution of NDVI values for Tartu in 2018.</p>
<p>As a bonus, we can visualize the histograms of the NDVI values for the three rasters.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb7" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb7-1">df_hist <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">data.frame</span>(</span>
<span id="cb7-2">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">values =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(tartu2018_hist, tartu2023_hist, poznan2023_hist),</span>
<span id="cb7-3">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">group =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rep</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Tartu 2018"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Tartu 2023"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Poznan 2023"</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">each =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">32</span>),</span>
<span id="cb7-4">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">bin =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rep</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">32</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>))</span>
<span id="cb7-5"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(ggplot2)</span>
<span id="cb7-6"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(df_hist, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> bin, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> values, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> group)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb7-7">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_line</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb7-8">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_minimal</span>()</span></code></pre></div></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://jakubnowosad.com/posts/2024-10-27-spatcomp-bp3/index_files/figure-html/unnamed-chunk-1-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
</section>
</section>
</section>
<section id="spatial-context" class="level2">
<h2 class="anchored" data-anchor-id="spatial-context">Spatial context</h2>
<section id="single-value-outcome-1" class="level3">
<h3 class="anchored" data-anchor-id="single-value-outcome-1">Single value outcome</h3>
<section id="the-difference-between-an-average-of-a-focal-measure-of-two-rasters" class="level4">
<h4 class="anchored" data-anchor-id="the-difference-between-an-average-of-a-focal-measure-of-two-rasters">The difference between an average of a focal measure of two rasters</h4>
<p>To include the spatial context in the comparison of continuous raster data, we can use focal measures.</p>
<p>The <strong>geodiv</strong> package <span class="citation" data-cites="smithGeodivMethodsCalculating2023">(Smith et al. 2023)</span> provides more than a dozen of functions to calculate surface metrics for continuous rasters. One of them is the surface roughness (<code>sa()</code>) function, which calculates the absolute deviation of raster value heights from the mean value.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb8" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb8-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(geodiv)</span>
<span id="cb8-2">ndvi2018_tartu_sa <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sa</span>(ndvi2018_tartu)    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 0.219</span></span>
<span id="cb8-3">ndvi2023_tartu_sa <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sa</span>(ndvi2023_tartu)    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 0.150</span></span>
<span id="cb8-4">ndvi2023_poznan_sa <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sa</span>(ndvi2023_poznan)  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 0.141</span></span>
<span id="cb8-5"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">abs</span>(ndvi2023_tartu_sa <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> ndvi2018_tartu_sa)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 0.06886273</code></pre>
</div>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb10" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb10-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">abs</span>(ndvi2023_poznan_sa <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> ndvi2018_tartu_sa)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 0.07695052</code></pre>
</div>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb12" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb12-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">abs</span>(ndvi2023_poznan_sa <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> ndvi2023_tartu_sa)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 0.008087792</code></pre>
</div>
</div>
<p>The absolute differences show that the NDVI values for Tartu in 2023 have much more similar variability to the NDVI values for Poznan in 2023 than to the NDVI values for Tartu in 2018. On the other hand, the calculation of the differences could reveal the direction of the change, e.g., if the NDVI values for Tartu in 2023 are more or less variable than in 2018.</p>
<p>Another example of calculating and then comparing focal measures is the calculation of one of the GLCM texture metrics and then comparing the outcome values. Here, we compute the average of the homogeneity metric, which values are higher for more homogeneous textures, using the <strong>GLCMTextures</strong> package <span class="citation" data-cites="ilichGLCMTextures2020">(Ilich 2020)</span>. Next, we calculate the absolute differences between the mean homogeneity values for the three rasters.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb14" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb14-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(GLCMTextures)</span>
<span id="cb14-2"></span>
<span id="cb14-3">ndvi2018_tartu_hom <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">glcm_textures</span>(ndvi2018_tartu, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">n_levels =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">16</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">shift =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>), </span>
<span id="cb14-4">                                    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">metric =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"glcm_homogeneity"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">quantization =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"equal prob"</span>)</span>
<span id="cb14-5">ndvi2023_tartu_hom <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">glcm_textures</span>(ndvi2023_tartu, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">n_levels =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">16</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">shift =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>), </span>
<span id="cb14-6">                                    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">metric =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"glcm_homogeneity"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">quantization =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"equal prob"</span>)</span>
<span id="cb14-7">ndvi2023_poznan_hom <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">glcm_textures</span>(ndvi2023_poznan, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">n_levels =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">16</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">shift =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>),</span>
<span id="cb14-8">                                    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">metric =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"glcm_homogeneity"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">quantization =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"equal prob"</span>)</span>
<span id="cb14-9"></span>
<span id="cb14-10">ndvi2018_tartu_homv <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">global</span>(ndvi2018_tartu_hom, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"mean"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">na.rm =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>)</span>
<span id="cb14-11">ndvi2023_tartu_homv <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">global</span>(ndvi2023_tartu_hom, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"mean"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">na.rm =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>)</span>
<span id="cb14-12">ndvi2023_poznan_homv <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">global</span>(ndvi2023_poznan_hom, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"mean"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">na.rm =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>)</span>
<span id="cb14-13"></span>
<span id="cb14-14"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">abs</span>(ndvi2023_tartu_homv <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> ndvi2018_tartu_homv)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>                       mean
glcm_homogeneity 0.01857073</code></pre>
</div>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb16" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb16-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">abs</span>(ndvi2023_poznan_homv <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> ndvi2018_tartu_homv)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>                       mean
glcm_homogeneity 0.02247696</code></pre>
</div>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb18" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb18-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">abs</span>(ndvi2023_poznan_homv <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> ndvi2023_tartu_homv)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>                        mean
glcm_homogeneity 0.003906225</code></pre>
</div>
</div>
<p>The results show that the NDVI rasters for Tartu in 2023 and Poznan in 2023 have more similar homogeneity than the NDVI raster for Tartu in 2018.</p>
</section>
<section id="comparison-of-the-values-of-the-boltzmann-entropy-of-a-landscape-gradient-gaoaggregationbasedmethodcomputing2019" class="level4">
<h4 class="anchored" data-anchor-id="comparison-of-the-values-of-the-boltzmann-entropy-of-a-landscape-gradient-gaoaggregationbasedmethodcomputing2019">Comparison of the values of the Boltzmann entropy of a landscape gradient <span class="citation" data-cites="gaoAggregationbasedMethodComputing2019">(Gao and Li 2019)</span></h4>
<p>Both, the surface roughness and the homogeneity metrics represent a given aspect of the spatial pattern of the continuous raster data. Alternatively, we can want to consider the complexity of the spatial pattern of the continuous raster data as a whole. This may be done using Gao’s entropy metric, which is based on aggregating the values of the input raster, and then calculating the possible ways to disaggregate the new raster into the original one <span class="citation" data-cites="gaoAggregationbasedMethodComputing2019">(Gao and Li 2019)</span>.</p>
<p>The below example uses the <strong>bespatial</strong> package <span class="citation" data-cites="nowosadBespatialBoltzmannEntropy2024">(Nowosad 2024)</span>.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb20" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb20-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(bespatial)</span>
<span id="cb20-2">ndvi2018_tartu_bes <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">bes_g_gao</span>(ndvi2018_tartu, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">method =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"hierarchy"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">relative =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>)</span>
<span id="cb20-3">ndvi2023_tartu_bes <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">bes_g_gao</span>(ndvi2023_tartu, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">method =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"hierarchy"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">relative =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>)</span>
<span id="cb20-4">ndvi2023_poznan_bes <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">bes_g_gao</span>(ndvi2023_poznan, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">method =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"hierarchy"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">relative =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>)</span>
<span id="cb20-5"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">abs</span>(ndvi2023_tartu_bes<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>value <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> ndvi2018_tartu_bes<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>value)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 15402.11</code></pre>
</div>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb22" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb22-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">abs</span>(ndvi2023_poznan_bes<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>value <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> ndvi2018_tartu_bes<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>value)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 26013.8</code></pre>
</div>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb24" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb24-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">abs</span>(ndvi2023_tartu_bes<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>value <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> ndvi2023_poznan_bes<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>value)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 10611.69</code></pre>
</div>
</div>
<p>The results show that the NDVI rasters for Tartu in 2023 and Poznan in 2023 have more similar Gao entropy than the NDVI raster for Tartu in 2018.</p>
</section>
<section id="comparison-of-deep-learning-based-feature-maps-using-a-dissimilarity-measure" class="level4">
<h4 class="anchored" data-anchor-id="comparison-of-deep-learning-based-feature-maps-using-a-dissimilarity-measure">Comparison of deep learning-based feature maps using a dissimilarity measure</h4>
<p>Fairly recent advances in deep learning have enabled the extraction of feature maps from pre-trained models. These feature maps can be used to compare the spatial patterns of continuous raster data <span class="citation" data-cites="malikLandscapeSimilarityAnalysis2021">(Malik and Robertson 2021)</span>.</p>
<div class="callout callout-style-simple callout-none no-icon">
<div class="callout-body d-flex">
<div class="callout-icon-container">
<i class="callout-icon no-icon"></i>
</div>
<div class="callout-body-container">
<p>The example below is very simplified as it uses only one feature map from the pre-trained VGG16 model. That model was trained on a different type of data (images) and might not be suitable for the analysis of NDVI values. Thus, in the real-world analysis, the model should be trained on the data similar to the one used in the analysis.<sup>2</sup></p>
</div>
</div>
</div>
<p>This example uses the <strong>keras3</strong> <span class="citation" data-cites="kalinowskiKeras3InterfaceKeras2024">(Kalinowski et al. 2024)</span> and <strong>philentropy</strong> <span class="citation" data-cites="hgPhilentropyInformationTheory2018">(HG 2018)</span> packages. First, the NDVI rasters are normalized to the range of 0 to 1 and converted to a matrix format. Then, they are reshaped to the format required by the VGG16 model: a 3D array with three channels. The VGG16 model is loaded in the next step, and the feature maps are extracted. Finally, the first feature maps are reshaped to a vector format and compared using the Euclidean distance.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb26" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb26-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(keras3)</span>
<span id="cb26-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(philentropy)</span>
<span id="cb26-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># keras3::install_keras(backend = "tensorflow")</span></span>
<span id="cb26-4">normalize_raster <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(r) {</span>
<span id="cb26-5">    min_val <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> terra<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">global</span>(r, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"min"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">na.rm =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>)[[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>]]</span>
<span id="cb26-6">    max_val <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> terra<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">global</span>(r, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"max"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">na.rm =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>)[[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>]]</span>
<span id="cb26-7">    r <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> terra<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">app</span>(r, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fun =</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(x) (x <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> min_val) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> (max_val <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> min_val))</span>
<span id="cb26-8">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">return</span>(r)</span>
<span id="cb26-9">}</span>
<span id="cb26-10"></span>
<span id="cb26-11">ndvi2023n_tartu <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">normalize_raster</span>(ndvi2023_tartu)</span>
<span id="cb26-12">ndvi2023n_poznan <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">normalize_raster</span>(ndvi2023_poznan)</span>
<span id="cb26-13"></span>
<span id="cb26-14">ndvi2023_tartu_mat <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.matrix</span>(ndvi2023n_tartu, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">wide =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>)</span>
<span id="cb26-15">ndvi2023_poznan_mat <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.matrix</span>(ndvi2023n_poznan, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">wide =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>)</span>
<span id="cb26-16"></span>
<span id="cb26-17">ndvi2023_tartu_mat <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">array</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rep</span>(ndvi2023_tartu_mat, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>), </span>
<span id="cb26-18">                      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">dim =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">nrow</span>(ndvi2023_tartu_mat), <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ncol</span>(ndvi2023_tartu_mat), <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>))</span>
<span id="cb26-19">ndvi2023_poznan_mat <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">array</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rep</span>(ndvi2023_poznan_mat, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>), </span>
<span id="cb26-20">                      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">dim =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">nrow</span>(ndvi2023_poznan_mat), <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ncol</span>(ndvi2023_poznan_mat), <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>))</span>
<span id="cb26-21"></span>
<span id="cb26-22">model <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> keras3<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">application_vgg16</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">weights =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"imagenet"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">include_top =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>,</span>
<span id="cb26-23">                                  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">input_shape =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">nrow</span>(ndvi2023_tartu_mat), </span>
<span id="cb26-24">                                                  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ncol</span>(ndvi2023_tartu_mat), <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>))</span>
<span id="cb26-25"></span>
<span id="cb26-26">ndvi2023_tartu_mat <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> keras3<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">array_reshape</span>(ndvi2023_tartu_mat, </span>
<span id="cb26-27">                                           <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">dim</span>(ndvi2023_tartu_mat)))</span>
<span id="cb26-28">ndvi2023_poznan_mat <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> keras3<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">array_reshape</span>(ndvi2023_poznan_mat,</span>
<span id="cb26-29">                                            <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">dim</span>(ndvi2023_poznan_mat)))</span>
<span id="cb26-30"></span>
<span id="cb26-31">features2023_tartu <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">predict</span>(model, ndvi2023_tartu_mat, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">verbose =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>)</span>
<span id="cb26-32">features2023_poznan <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">predict</span>(model, ndvi2023_poznan_mat, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">verbose =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>)</span>
<span id="cb26-33"></span>
<span id="cb26-34"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># [1, height, width, layer]</span></span>
<span id="cb26-35">feature_map_tartu_1 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.vector</span>(features2023_tartu[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, , , <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>])</span>
<span id="cb26-36">feature_map_poznan_1 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.vector</span>(features2023_poznan[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, , , <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>])</span>
<span id="cb26-37"></span>
<span id="cb26-38"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">distance</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rbind</span>(feature_map_tartu_1, feature_map_poznan_1))</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>euclidean 
 7.007962 </code></pre>
</div>
</div>
</section>
<section id="the-similarity-index-cq-based-on-the-co-dispersion-coefficient" class="level4">
<h4 class="anchored" data-anchor-id="the-similarity-index-cq-based-on-the-co-dispersion-coefficient">The similarity index (CQ) based on the co-dispersion coefficient</h4>
<p>The <strong>SpatialPack</strong> package <span class="citation" data-cites="vallejos2020spatial">(Vallejos et al. 2020)</span> provides the <code>CQ()</code> function, which calculates the similarity index based on the co-dispersion coefficient. However, this function (1) requires the input data to be in the matrix format, and (2) does not work with missing values. Thus, the code chunk below has not been evaluated, but it shows how to use the <code>CQ()</code> function for data fulfilling these requirements.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb28" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb28-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(SpatialPack)</span>
<span id="cb28-2">ndvi2023_tartu_mat <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.matrix</span>(ndvi2023_tartu, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">wide =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>)</span>
<span id="cb28-3">ndvi2023_poznan_mat <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.matrix</span>(ndvi2023_poznan, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">wide =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>)</span>
<span id="cb28-4">ndvi_CQ <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">CQ</span>(ndvi2023_tartu_mat, ndvi2023_poznan_mat)</span>
<span id="cb28-5">ndvi_CQ<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>CQ</span></code></pre></div></div>
</div>
</section>
</section>
</section>
<section id="references" class="level2">




</section>


<div id="quarto-appendix" class="default"><section class="quarto-appendix-contents" id="quarto-bibliography"><h2 class="anchored quarto-appendix-heading">References</h2><div id="refs" class="references csl-bib-body hanging-indent">
<div id="ref-gaoAggregationbasedMethodComputing2019" class="csl-entry">
Gao, Peichao, and Zhilin Li. 2019. <span>“Aggregation-Based Method for Computing Absolute <span>Boltzmann</span> Entropy of Landscape Gradient with Full Thermodynamic Consistency.”</span> <em>Landscape Ecology</em> 34 (8): 1837–47. <a href="https://doi.org/10/gkb47k">https://doi.org/10/gkb47k</a>.
</div>
<div id="ref-hgPhilentropyInformationTheory2018" class="csl-entry">
HG, Drost. 2018. <span>“Philentropy: <span>Information</span> Theory and Distance Quantification with <span>R</span>.”</span> <em>Journal of Open Source Software</em> 3 (26): 765.
</div>
<div id="ref-ilichGLCMTextures2020" class="csl-entry">
Ilich, Alexander R. 2020. <em><span>GLCMTextures</span></em>. <a href="https://doi.org/10.5281/zenodo.4310186">https://doi.org/10.5281/zenodo.4310186</a>.
</div>
<div id="ref-kalinowskiKeras3InterfaceKeras2024" class="csl-entry">
Kalinowski, Tomasz, JJ Allaire, and François Chollet. 2024. <em>Keras3: <span>R Interface</span> to ’<span>Keras</span>’</em>. Manual.
</div>
<div id="ref-malikLandscapeSimilarityAnalysis2021" class="csl-entry">
Malik, Karim, and Colin Robertson. 2021. <span>“Landscape <span>Similarity Analysis Using Texture Encoded Deep-Learning Features</span> on <span>Unclassified Remote Sensing Imagery</span>.”</span> <em>Remote Sensing</em> 13 (3): 492. <a href="https://doi.org/10.3390/rs13030492">https://doi.org/10.3390/rs13030492</a>.
</div>
<div id="ref-nowosadBespatialBoltzmannEntropy2024" class="csl-entry">
Nowosad, Jakub. 2024. <em>Bespatial: <span>Boltzmann</span> Entropy for Spatial Data</em>. Manual.
</div>
<div id="ref-smithGeodivMethodsCalculating2023" class="csl-entry">
Smith, Annie C., Phoebe Zarnetske, Kyla Dahlin, Adam Wilson, and Andrew Latimer. 2023. <em>Geodiv: <span>Methods</span> for Calculating Gradient Surface Metrics</em>. Manual.
</div>
<div id="ref-vallejos2020spatial" class="csl-entry">
Vallejos, Ronny, Felipe Osorio, and Moreno Bevilacqua. 2020. <em>Spatial Relationships Between Two Georeferenced Variables: <span>With</span> Applications in <span>R</span></em>. Springer Nature.
</div>
</div></section><section id="footnotes" class="footnotes footnotes-end-of-document"><h2 class="anchored quarto-appendix-heading">Footnotes</h2>

<ol>
<li id="fn1"><p>This is another example of a decision – which dissimilarity measure to use.↩︎</p></li>
<li id="fn2"><p>Any suggestions on how to improve this example are welcome – please let me know!↩︎</p></li>
</ol>
</section><section class="quarto-appendix-contents" id="quarto-reuse"><h2 class="anchored quarto-appendix-heading">Reuse</h2><div class="quarto-appendix-contents"><div><a rel="license" href="https://creativecommons.org/licenses/by/4.0/">CC BY 4.0</a></div></div></section><section class="quarto-appendix-contents" id="quarto-citation"><h2 class="anchored quarto-appendix-heading">Citation</h2><div><div class="quarto-appendix-secondary-label">BibTeX citation:</div><div class="code-copy-outer-scaffold"><pre class="sourceCode quarto-appendix-bibtex code-with-copy"><code class="sourceCode bibtex">@online{nowosad2024,
  author = {Nowosad, Jakub},
  title = {Comparison of Spatial Patterns in Continuous Raster Data for
    Arbitrary Regions Using {R}},
  date = {2024-10-27},
  url = {https://jakubnowosad.com/posts/2024-10-27-spatcomp-bp3/},
  doi = {10.59350/j9psx-q8w60},
  langid = {en}
}
</code></pre></div><div class="quarto-appendix-secondary-label">For attribution, please cite this work as:</div><div id="ref-nowosad2024" class="csl-entry quarto-appendix-citeas">
Nowosad, Jakub. 2024. <span>“Comparison of Spatial Patterns in
Continuous Raster Data for Arbitrary Regions Using R.”</span> October
27. <a href="https://doi.org/10.59350/j9psx-q8w60">https://doi.org/10.59350/j9psx-q8w60</a>.
</div></div></section></div> ]]></description>
  <category>spatial</category>
  <category>landscape-ecology</category>
  <category>spatial-patterns</category>
  <category>msca-pf</category>
  <category>rstats</category>
  <guid>https://jakubnowosad.com/posts/2024-10-27-spatcomp-bp3/</guid>
  <pubDate>Sun, 27 Oct 2024 00:00:00 GMT</pubDate>
</item>
<item>
  <title>Comparison of spatial patterns in continuous raster data for overlapping regions using R</title>
  <dc:creator>Jakub Nowosad</dc:creator>
  <link>https://jakubnowosad.com/posts/2024-10-20-spatcomp-bp2/</link>
  <description><![CDATA[ 





<div class="callout callout-style-simple callout-note callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Note</span>Methods for comparing spatial patterns in raster data
</div>
</div>
<div class="callout-body-container callout-body">
<p>This is the second part of a blog post series on comparing spatial patterns in raster data. More information about the whole series can be found <a href="../../posts/2024-10-13-spatcomp-bp1">in part one</a>.</p>
</div>
</div>
<p>This blog post shows various methods for comparing spatial patterns in continuous raster data for overlapping regions, i.e., how to compare two rasters for the same region, but in different moments in time (or, in some cases, with different variables)<sup>1</sup> using R programming language.</p>
<p>Two continuous raster datasets are used in this blog post: the Normalized Difference Vegetation Index (NDVI) for Tartu (Estonia) for the years 2000 and 2018.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(terra)</span>
<span id="cb1-2">ndvi2018_tartu <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rast</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"https://github.com/Nowosad/comparing-spatial-patterns-2024/raw/refs/heads/main/data/ndvi2018_tartu.tif"</span>)</span>
<span id="cb1-3">ndvi2023_tartu <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rast</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"https://github.com/Nowosad/comparing-spatial-patterns-2024/raw/refs/heads/main/data/ndvi2023_tartu.tif"</span>)</span>
<span id="cb1-4"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">plot</span>(ndvi2018_tartu, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">main =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Tartu (2000)"</span>)</span>
<span id="cb1-5"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">plot</span>(ndvi2023_tartu, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">main =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Tartu (2018)"</span>)</span></code></pre></div></div>
<div class="cell column-page quarto-layout-panel" data-layout-ncol="2">
<div class="quarto-layout-row">
<div class="quarto-layout-cell" style="flex-basis: 50.0%;justify-content: center;">
<p><img src="https://jakubnowosad.com/posts/2024-10-20-spatcomp-bp2/index_files/figure-html/cont-1.png" class="img-fluid" width="672"></p>
</div>
<div class="quarto-layout-cell" style="flex-basis: 50.0%;justify-content: center;">
<p><img src="https://jakubnowosad.com/posts/2024-10-20-spatcomp-bp2/index_files/figure-html/cont-2.png" class="img-fluid" width="672"></p>
</div>
</div>
</div>
<section id="non-spatial-context" class="level2">
<h2 class="anchored" data-anchor-id="non-spatial-context">Non-spatial context</h2>
<section id="raster-outcome" class="level3">
<h3 class="anchored" data-anchor-id="raster-outcome">Raster outcome</h3>
<section id="the-difference-between-values-of-two-rasters-for-each-cell" class="level4">
<h4 class="anchored" data-anchor-id="the-difference-between-values-of-two-rasters-for-each-cell">The difference between values of two rasters for each cell</h4>
<p>The most basic approach to compare two continuous rasters is to calculate the difference between their values for each cell.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb2-1">ndvi_diff <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> ndvi2023_tartu <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> ndvi2018_tartu</span>
<span id="cb2-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">plot</span>(ndvi_diff)</span></code></pre></div></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://jakubnowosad.com/posts/2024-10-20-spatcomp-bp2/index_files/figure-html/01-continuous-3-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<p>The output is a raster with positive values indicating that the values of the first raster are higher than the values of the second raster, while negative values indicate the opposite.<sup>2</sup></p>
<p>The differences may be even more highlighted by using a diverging color palette. The below function <code>plot_div()</code> visualizes a raster with a diverging color palette, where the middle color represents zero.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb3-1">plot_div <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(r, ...){</span>
<span id="cb3-2">  r_range <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">range</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">values</span>(r), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">na.rm =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">finite =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>)</span>
<span id="cb3-3">  max_abs <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">max</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">abs</span>(r_range))</span>
<span id="cb3-4">  new_range <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>max_abs, max_abs)</span>
<span id="cb3-5">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">plot</span>(r, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">col =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">hcl.colors</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">palette =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"prgn"</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">range =</span> new_range, ...)</span>
<span id="cb3-6">}</span>
<span id="cb3-7"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">plot_div</span>(ndvi_diff)</span></code></pre></div></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://jakubnowosad.com/posts/2024-10-20-spatcomp-bp2/index_files/figure-html/unnamed-chunk-1-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<p>It shows that most of the area has negative values, indicating that the NDVI values were lower in 2023 than in 2018.</p>
<div class="callout callout-style-simple callout-none no-icon">
<div class="callout-body d-flex">
<div class="callout-icon-container">
<i class="callout-icon no-icon"></i>
</div>
<div class="callout-body-container">
<p>The interpretation of the difference between the values of two rasters is not always straightforward as it depends, among other things, on the properties of the studied variable. Some variables are linear, and thus, the difference between the values has a clear meaning, while others are not, and the difference may be harder to interpret.<sup>3</sup></p>
</div>
</div>
</div>
</section>
</section>
<section id="multiple-value-outcome" class="level3">
<h3 class="anchored" data-anchor-id="multiple-value-outcome">Multiple value outcome</h3>
<section id="the-distribution-of-the-difference-between-values-of-two-rasters" class="level4">
<h4 class="anchored" data-anchor-id="the-distribution-of-the-difference-between-values-of-two-rasters">The distribution of the difference between values of two rasters</h4>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb4-1">ndvi_diff <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> ndvi2023_tartu <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> ndvi2018_tartu</span>
<span id="cb4-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">hist</span>(ndvi_diff)</span></code></pre></div></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://jakubnowosad.com/posts/2024-10-20-spatcomp-bp2/index_files/figure-html/01-continuous-22-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
</section>
</section>
<section id="single-value-outcome" class="level3">
<h3 class="anchored" data-anchor-id="single-value-outcome">Single value outcome</h3>
<section id="statistics-of-the-differences-between-rasters-values" class="level4">
<h4 class="anchored" data-anchor-id="statistics-of-the-differences-between-rasters-values">Statistics of the differences between rasters’ values</h4>
<p>A similar approach, also based on the values of each cell independently, is to calculate the statistics of the differences between the two rasters’ values (i.e., error metrics).</p>
<p>One of the most common error metrics is the Root Mean Square Error (RMSE), which quantifies the average difference between the values of the two rasters. Its lower value indicates a better agreement between the two rasters. RMSE is implemented in many R packages, and here we use the version from the <strong>yardstick</strong> package <span class="citation" data-cites="kuhnYardstickTidyCharacterizations2024">(Kuhn et al. 2024)</span>.<sup>4</sup></p>
<p>In general, error metrics require two sets of values; thus, we are using the <code>values()</code> function to extract the values of the rasters before calculating the RMSE.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb5-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(yardstick)</span>
<span id="cb5-2">ndvi_rmse <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rmse_vec</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">values</span>(ndvi2023_tartu)[, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>], <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">values</span>(ndvi2018_tartu)[, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>])</span>
<span id="cb5-3">ndvi_rmse</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 0.2191853</code></pre>
</div>
</div>
<p>The <strong>diffeR</strong> package <span class="citation" data-cites="pontiusjr.DiffeRMetricsDifference2023">(Pontius Jr. and Santacruz 2023)</span>, on the other hand, is directly aimed at comparing rasters. One of its functions, <code>MAD()</code>, calculates the Mean Absolute Difference (MAD) between two rasters.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb7" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb7-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(diffeR)</span>
<span id="cb7-2">ndvi_mad <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">MAD</span>(ndvi2023_tartu, ndvi2018_tartu)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] "The mean of grid1 is less than the mean of grid2"</code></pre>
</div>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb9" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb9-1">ndvi_mad<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>Total</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 0.184306</code></pre>
</div>
</div>
<p>This measure has a similar interpretation to RMSE, but it is less sensitive to outliers.</p>
</section>
</section>
</section>
<section id="spatial-context" class="level2">
<h2 class="anchored" data-anchor-id="spatial-context">Spatial context</h2>
<section id="raster-outcome-1" class="level3">
<h3 class="anchored" data-anchor-id="raster-outcome-1">Raster outcome</h3>
<p>The main general approach of incorporating spatial context into comparison of rasters is the use of calculations in a moving window. This way, we do not only consider the values of each cell independently, but also the values of the surrounding cells.<sup>5</sup></p>
<section id="correlation-coefficient-between-focal-regions-of-two-rasters" class="level4">
<h4 class="anchored" data-anchor-id="correlation-coefficient-between-focal-regions-of-two-rasters">Correlation coefficient between focal regions of two rasters</h4>
<p>The <code>focalPairs()</code> function from the <strong>terra</strong> package <span class="citation" data-cites="hijmansTerraSpatialData2024">(Hijmans 2024)</span> uses a moving window to extract values from the focal regions of two rasters and calculates the correlation coefficient between them. It requires two rasters with the same number of rows and columns, the size of the moving window, and the function to calculate the correlation coefficient.<sup>6</sup></p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb11" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb11-1">ndvi_cor <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">focalPairs</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(ndvi2023_tartu, ndvi2018_tartu), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">w =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>,</span>
<span id="cb11-2">                      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fun =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"pearson"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">na.rm =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>)</span>
<span id="cb11-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">plot_div</span>(ndvi_cor)</span></code></pre></div></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://jakubnowosad.com/posts/2024-10-20-spatcomp-bp2/index_files/figure-html/01-continuous-4-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<p>The values of the output raster range from -1 to 1, where 1 indicates a perfect positive correlation in the focal regions, 0 indicates no correlation (the focal values have no relationship to each other), and -1 indicates a perfect negative correlation.</p>
</section>
<section id="the-difference-between-a-focal-measure-of-two-rasters" class="level4">
<h4 class="anchored" data-anchor-id="the-difference-between-a-focal-measure-of-two-rasters">The difference between a focal measure of two rasters</h4>
<p>Similarly to the correlation coefficient, other statistics can be calculated in a moving window. This option is offered by several R packages, including <strong>geodiv</strong> <span class="citation" data-cites="smithGeodivMethodsCalculating2023">(Smith et al. 2023)</span>, <strong>GLCMTextures</strong> <span class="citation" data-cites="ilichGLCMTextures2020">(Ilich 2020)</span>, and <strong>rasterdiv</strong> <span class="citation" data-cites="rocchiniRasterdivInformationTheory2021">(Rocchini et al. 2021)</span>.</p>
<p>The <strong>geodiv</strong> package provides a function <code>focal_metrics()</code> that calculates more than 20 different focal statistics. In the below example, we calculate the average surface roughness (SA) in a moving window separately for the two rasters and then calculate the difference between them.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb12" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb12-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(geodiv)</span>
<span id="cb12-2">window <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">matrix</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">nrow =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ncol =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>)</span>
<span id="cb12-3">ndvi2018_tartu_sa_mw <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">focal_metrics</span>(ndvi2018_tartu, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">window =</span> window,</span>
<span id="cb12-4">                               <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">metric =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"sa"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">progress =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>)</span>
<span id="cb12-5">ndvi2023_tartu_sa_mw <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">focal_metrics</span>(ndvi2023_tartu, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">window =</span> window,</span>
<span id="cb12-6">                               <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">metric =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"sa"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">progress =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>)</span>
<span id="cb12-7">ndvi_diff_sa_mw <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> ndvi2023_tartu_sa_mw<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>sa <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> ndvi2018_tartu_sa_mw<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>sa</span>
<span id="cb12-8"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">plot_div</span>(ndvi_diff_sa_mw)</span></code></pre></div></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://jakubnowosad.com/posts/2024-10-20-spatcomp-bp2/index_files/figure-html/01-continuous-5-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<p>This outcome shows the difference in the average surface roughness between the two rasters in a moving window. The possitive values indicate that the surface roughness (in this case, the heterogeneity of NDVI) was higher in 2023 than in 2018, while the negative values indicate the opposite. Interestingly, the most extreme (negative) values are located in just a few small areas.</p>
<p>We can also compare various texture GLCM (Gray-Level Co-occurrence Matrix) metrics <span class="citation" data-cites="haralick1973textural">(Haralick et al. 1973)</span> in a moving window that characterizes many aspects of the spatial structure of the rasters. This example uses the <strong>GLCMTextures</strong> package <span class="citation" data-cites="ilichGLCMTextures2020">(Ilich 2020)</span>. We calculate the homogeneity of the NDVI values in a moving window for both rasters and calculate the difference between them.</p>
<!-- Here, we start by calculating the minimum and maximum values of the two rasters -- we will use this information to unify the quantization of the rasters.^[The quantization is the process of mapping the continuous values of the rasters to a discrete set of values; in this case, we use the "equal prob" method with 16 levels, which divides the range of values into 16 intervals.] -->
<!-- triple check if min_max is needed -->
<p>The positive values in the resulting map indicate that the homogeneity of the NDVI values was higher in 2023 than in 2018, while the negative values indicate the opposite.</p>
<p>The Rao’s quadratic entropy is a measure of diversity that takes into account not only the abundance of the classes (values), but also the dissimilarity between them <span class="citation" data-cites="raoDiversityDissimilarityCoefficients1982">(Rao 1982)</span>.</p>
<p>This measure can be calculated using the <code>paRao()</code> function from the <strong>rasterdiv</strong> package <span class="citation" data-cites="rocchiniRasterdivInformationTheory2021">(Rocchini et al. 2021)</span>. The function requires the raster with integer values (thus we multiply the NDVI values by 100) and the size of the moving window.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb13" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb13-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(rasterdiv)</span>
<span id="cb13-2">ndvi2018_tartu_int <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.int</span>(ndvi2018_tartu <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>)</span>
<span id="cb13-3">ndvi2023_tartu_int <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.int</span>(ndvi2023_tartu <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>)</span>
<span id="cb13-4">ndvi2018_tartu_rao <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">paRao</span>(ndvi2018_tartu_int, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">window =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">progBar =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>)</span>
<span id="cb13-5">ndvi2023_tartu_rao <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">paRao</span>(ndvi2023_tartu_int, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">window =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">progBar =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>)</span>
<span id="cb13-6">ndvi_rao_diff <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> ndvi2023_tartu_rao[[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>]][[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>]] <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> ndvi2018_tartu_rao[[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>]][[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>]]</span>
<span id="cb13-7"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">plot_div</span>(ndvi_rao_diff)</span></code></pre></div></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://jakubnowosad.com/posts/2024-10-20-spatcomp-bp2/index_files/figure-html/01-continuous-8b-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<p><em>Note that the above calculations may take a few minutes to complete.</em></p>
<p>The positive values in the resulting map indicate that the diversity (in this case, the heterogeneity of NDVI) was higher in 2023 than in 2018, while the negative values indicate the opposite.</p>
</section>
<section id="spatial-autocorrelation-analysis-of-the-differences" class="level4">
<h4 class="anchored" data-anchor-id="spatial-autocorrelation-analysis-of-the-differences">Spatial autocorrelation analysis of the differences</h4>
<p>An alternative approach for computing metrics in two rasters and then calculate the difference between them is to calculate the difference between the two rasters, and then compute the spatial autocorrelation of the differences <span class="citation" data-cites="cliffComputingSpatialCorrespondence1970">(Cliff 1970)</span>.</p>
<p>The <strong>terra</strong> package provides a function <code>autocor()</code> that calculates the spatial autocorrelation of a raster using a selected method. By default, it uses a three by three moving window (excluding the focal cell) to calculate the Moran’s I index. The <code>global = FALSE</code> argument specifies that the spatial autocorrelation should be calculated for each cell separately, and thus the output is a raster.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb14" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb14-1">ndvi_diff <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> ndvi2023_tartu <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> ndvi2018_tartu</span>
<span id="cb14-2">ndvi_diff_autocor <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">autocor</span>(ndvi_diff, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">method =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"moran"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">global =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>)</span>
<span id="cb14-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">plot_div</span>(ndvi_diff, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">main =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Difference"</span>)</span>
<span id="cb14-4"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">plot_div</span>(ndvi_diff_autocor, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">main =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Moran's I of the difference"</span>)</span></code></pre></div></div>
<div class="cell quarto-layout-panel" data-layout-ncol="2">
<div class="quarto-layout-row">
<div class="quarto-layout-cell" style="flex-basis: 50.0%;justify-content: center;">
<p><img src="https://jakubnowosad.com/posts/2024-10-20-spatcomp-bp2/index_files/figure-html/01-continuous-7-1.png" class="img-fluid" width="672"></p>
</div>
<div class="quarto-layout-cell" style="flex-basis: 50.0%;justify-content: center;">
<p><img src="https://jakubnowosad.com/posts/2024-10-20-spatcomp-bp2/index_files/figure-html/01-continuous-7-2.png" class="img-fluid" width="672"></p>
</div>
</div>
</div>
<p>The resulting map shows the spatial autocorrelation of the differences between the two rasters. Its high values indicate that the differences are spatially clustered (positive changes are close to positive changes, and negative changes are close to negative changes), while low values indicate that the differences are spatially random. Areas with no spatial autocorrelation (values around zero) indicate that the differences are spatially independent.</p>
</section>
<section id="structural-similarity-index-ssim-between-two-rasters" class="level4">
<h4 class="anchored" data-anchor-id="structural-similarity-index-ssim-between-two-rasters">Structural similarity index (SSIM) between two rasters</h4>
<p>Structural similarity index (SSIM) <span class="citation" data-cites="wangImageQualityAssessment2004">(Wang et al. 2004)</span> is a measure used to compare similarity between two images (no surprises here). It is based on the comparison of three aspects of the images: luminance, contrast, and structure, and its idea is to mimic the human perception of similarity.</p>
<p>The <strong>SSIMmap</strong> package <span class="citation" data-cites="haSSIMmapStructuralSimilarity2023">(Ha and Long 2023)</span> provides a function <code>ssim_raster()</code> that calculates the SSIM between two rasters.<sup>7</sup></p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb15" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb15-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(SSIMmap)</span>
<span id="cb15-2">ndvi_ssim <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ssim_raster</span>(ndvi2018_tartu, ndvi2023_tartu, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">global =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">w =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>)</span>
<span id="cb15-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">plot_div</span>(ndvi_ssim[[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>]])</span></code></pre></div></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://jakubnowosad.com/posts/2024-10-20-spatcomp-bp2/index_files/figure-html/01-continuous-8-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<p>The interpretation of the SSIM values is similar to the correlation coefficient: 1 indicates a perfect similarity, 0 indicates no similarity, and -1 indicates a perfect dissimilarity.</p>
<div class="callout callout-style-simple callout-none no-icon">
<div class="callout-body d-flex">
<div class="callout-icon-container">
<i class="callout-icon no-icon"></i>
</div>
<div class="callout-body-container">
<p>As of writing this blog post, the results from the <strong>SSIMmap</strong> package seem not to be consistent with other R implementations of SSIM. See <a href="https://github.com/Hailyee-Ha/SSIMmap/issues/1" class="uri">https://github.com/Hailyee-Ha/SSIMmap/issues/1</a> for more information.</p>
</div>
</div>
</div>
</section>
</section>
<section id="multiple-value-outcome-1" class="level3">
<h3 class="anchored" data-anchor-id="multiple-value-outcome-1">Multiple value outcome</h3>
<section id="statistics-of-the-differences-between-rasters-values-calculated-at-many-scales" class="level4">
<h4 class="anchored" data-anchor-id="statistics-of-the-differences-between-rasters-values-calculated-at-many-scales">Statistics of the differences between rasters’ values calculated at many scales</h4>
<p>All of the single value outcomes can be also calculated at multiple scales, i.e., instead of using each original cell value, the values are aggregated in windows of different sizes.</p>
<p>We have already seen the use of RMSE to compare two rasters based on the values of each cell independently. Now, using the <strong>waywiser</strong> package <span class="citation" data-cites="mahoneyWaywiserErgonomicMethods2023">(Mahoney 2023)</span>, we can calculate the RMSE at multiple scales.</p>
<p>Its function <code>ww_multi_scale()</code> requires the two rasters, the metrics to calculate, and the sizes of the windows (in cells) to aggregate the values. Here, we calculate the RMSE at six different scales, from 50 to 300 meters (map units).</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb16" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb16-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(waywiser)</span>
<span id="cb16-2">cell_sizes <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">seq</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">50</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">300</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">by =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">50</span>)</span>
<span id="cb16-3">ndvi_multi_scale <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ww_multi_scale</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">truth =</span> ndvi2018_tartu, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">estimate =</span> ndvi2023_tartu,</span>
<span id="cb16-4">                                 <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">metrics =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(yardstick<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span>rmse), </span>
<span id="cb16-5">                                 <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">cellsize =</span> cell_sizes,</span>
<span id="cb16-6">                                 <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">progress =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>)</span>
<span id="cb16-7">ndvi_multi_scale</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code># A tibble: 6 × 6
  .metric .estimator .estimate .grid_args       .grid             .notes  
  &lt;chr&gt;   &lt;chr&gt;          &lt;dbl&gt; &lt;list&gt;           &lt;list&gt;            &lt;list&gt;  
1 rmse    standard       0.205 &lt;tibble [1 × 1]&gt; &lt;sf [10,000 × 5]&gt; &lt;tibble&gt;
2 rmse    standard       0.198 &lt;tibble [1 × 1]&gt; &lt;sf [2,500 × 5]&gt;  &lt;tibble&gt;
3 rmse    standard       0.195 &lt;tibble [1 × 1]&gt; &lt;sf [1,156 × 5]&gt;  &lt;tibble&gt;
4 rmse    standard       0.193 &lt;tibble [1 × 1]&gt; &lt;sf [625 × 5]&gt;    &lt;tibble&gt;
5 rmse    standard       0.193 &lt;tibble [1 × 1]&gt; &lt;sf [400 × 5]&gt;    &lt;tibble&gt;
6 rmse    standard       0.193 &lt;tibble [1 × 1]&gt; &lt;sf [289 × 5]&gt;    &lt;tibble&gt;</code></pre>
</div>
</div>
<p>This result (note the <code>.estimate</code> column) shows the RMSE values at each scale: the largest value is at the scale of 50 meters, and the smallest value is at the scale of 200, 250, and 300 meters. It indicates that with the increase of the scale, the agreement between the two rasters increases.</p>
<p>A similar approach can be found in the <code>MAD()</code> function from the <strong>diffeR</strong> package, which calculates the Mean Absolute Difference (MAD) between two rasters at multiple scales. Here, these scales start at the original resolution of the rasters and increase by a factor of 2. <!-- ?Pontius Jr., R.G., Thontteh, O., Chen, H. 2008. Components of information for multiple resolution comparison between maps that share a real variable. Environmental and Ecological Statistics 15 (2), 111-142 --></p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb18" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb18-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(diffeR)</span>
<span id="cb18-2">ndvi_mad <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">MAD</span>(ndvi2023_tartu, ndvi2018_tartu, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">eval =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"multiple"</span>)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] "The mean of grid1 is less than the mean of grid2"</code></pre>
</div>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb20" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb20-1">ndvi_mad</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>   Multiples Resolution  Quantity Strata      Element     Total
1          1         10 0.1783152      0 0.0059907211 0.1843060
2          2         20 0.1783152      0 0.0038461843 0.1821614
3          4         40 0.1783152      0 0.0016917875 0.1800070
4          8         80 0.1783152      0 0.0003588876 0.1786741
5         16        160 0.1783152      0 0.0001143444 0.1784296
6         32        320 0.1783152      0 0.0001143444 0.1784296
7         64        640 0.1783152      0 0.0000000000 0.1783152
8        128       1280 0.1783152      0 0.0000000000 0.1783152
9        256       2560 0.1783152      0 0.0000000000 0.1783152
10       512       5120 0.1783152      0 0.0000000000 0.1783152</code></pre>
</div>
</div>
<p>Note the last column of the result, which shows the MAD values at each scale. It is similar to the RMSE result, with the largest value at the original resolution of the rasters and the smallest value at the largest scale.</p>
</section>
</section>
<section id="single-value-outcome-1" class="level3">
<h3 class="anchored" data-anchor-id="single-value-outcome-1">Single value outcome</h3>
<section id="average-of-structural-similarity-index" class="level4">
<h4 class="anchored" data-anchor-id="average-of-structural-similarity-index">Average of Structural Similarity Index</h4>
<p>The average (global) SSIM value can be calculated by averaging the SSIM values of all cells in the raster. It is a single value that indicates the overall similarity between the two rasters <span class="citation" data-cites="wangImageQualityAssessment2004 robertsonAssessingQualitySpatial2014">(Wang et al. 2004; Robertson et al. 2014)</span>.</p>
<p>It can be calculated using the <code>ssim_raster()</code> function from the <strong>SSIMmap</strong> package<span class="citation" data-cites="haSSIMmapStructuralSimilarity2023">(Ha and Long 2023)</span>.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb22" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb22-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(SSIMmap)</span>
<span id="cb22-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ssim_raster</span>(ndvi2018_tartu, ndvi2023_tartu, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">global =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>SSIM: 0.63845 SIM: 0.90432 SIV: 0.90778 SIP: 0.75671 </code></pre>
</div>
</div>
<p>The result is a single value that ranges from -1 to 1, where 1 indicates a perfect similarity, 0 indicates no similarity, and -1 indicates a perfect dissimilarity.</p>
<div class="callout callout-style-simple callout-none no-icon">
<div class="callout-body d-flex">
<div class="callout-icon-container">
<i class="callout-icon no-icon"></i>
</div>
<div class="callout-body-container">
<p>As of writing this blog post, the results from the <strong>SSIMmap</strong> package seem not to be consistent with other R implementations of SSIM. See <a href="https://github.com/Hailyee-Ha/SSIMmap/issues/1" class="uri">https://github.com/Hailyee-Ha/SSIMmap/issues/1</a> for more information.</p>
</div>
</div>
</div>
</section>
</section>
</section>
<section id="references" class="level2">




</section>


<div id="quarto-appendix" class="default"><section class="quarto-appendix-contents" id="quarto-bibliography"><h2 class="anchored quarto-appendix-heading">References</h2><div id="refs" class="references csl-bib-body hanging-indent">
<div id="ref-cliffComputingSpatialCorrespondence1970" class="csl-entry">
Cliff, Andrew D. 1970. <span>“Computing the <span>Spatial Correspondence</span> Between <span>Geographical Patterns</span>.”</span> <em>Transactions of the Institute of British Geographers</em>, no. 50 (July): 143. <a href="https://doi.org/10.2307/621351">https://doi.org/10.2307/621351</a>.
</div>
<div id="ref-haSSIMmapStructuralSimilarity2023" class="csl-entry">
Ha, Hui Jeong (Hailyee), and Jed Long. 2023. <em><span>SSIMmap</span>: <span>The</span> Structural Similarity Index Measure for Maps</em>. Manual.
</div>
<div id="ref-haralick1973textural" class="csl-entry">
Haralick, Robert M, Karthikeyan Shanmugam, and Its’ Hak Dinstein. 1973. <span>“Textural Features for Image Classification.”</span> <em>IEEE Transactions on Systems, Man, and Cybernetics</em>, no. 6: 610–21.
</div>
<div id="ref-hijmansTerraSpatialData2024" class="csl-entry">
Hijmans, Robert J. 2024. <em>Terra: <span>Spatial</span> Data Analysis</em>. Manual.
</div>
<div id="ref-ilichGLCMTextures2020" class="csl-entry">
Ilich, Alexander R. 2020. <em><span>GLCMTextures</span></em>. <a href="https://doi.org/10.5281/zenodo.4310186">https://doi.org/10.5281/zenodo.4310186</a>.
</div>
<div id="ref-kuhnYardstickTidyCharacterizations2024" class="csl-entry">
Kuhn, Max, Davis Vaughan, and Emil Hvitfeldt. 2024. <em>Yardstick: <span>Tidy</span> Characterizations of Model Performance</em>. Manual.
</div>
<div id="ref-mahoneyWaywiserErgonomicMethods2023" class="csl-entry">
Mahoney, Michael J. 2023. <em>Waywiser: <span>Ergonomic</span> Methods for Assessing Spatial Models</em>. <a href="https://doi.org/10.48550/arXiv.2303.11312">https://doi.org/10.48550/arXiv.2303.11312</a>.
</div>
<div id="ref-pontiusjr.DiffeRMetricsDifference2023" class="csl-entry">
Pontius Jr., Robert Gilmore, and Ali Santacruz. 2023. <em><span class="nocase">diffeR</span>: <span>Metrics</span> of Difference for Comparing Pairs of Maps or Pairs of Variables</em>. Manual.
</div>
<div id="ref-raoDiversityDissimilarityCoefficients1982" class="csl-entry">
Rao, C.Radhakrishna. 1982. <span>“Diversity and Dissimilarity Coefficients: <span>A</span> Unified Approach.”</span> <em>Theoretical Population Biology</em> 21 (1): 24–43. <a href="https://doi.org/10.1016/0040-5809(82)90004-1">https://doi.org/10.1016/0040-5809(82)90004-1</a>.
</div>
<div id="ref-robertsonAssessingQualitySpatial2014" class="csl-entry">
Robertson, Colin, Jed A. Long, Farouk S. Nathoo, Trisalyn A. Nelson, and Cameron C. F. Plouffe. 2014. <span>“Assessing <span>Quality</span> of <span>Spatial Models Using</span> the <span>Structural Similarity Index</span> and <span>Posterior Predictive Checks</span>.”</span> <em>Geographical Analysis</em> 46 (1): 53–74. <a href="https://doi.org/10.1111/gean.12028">https://doi.org/10.1111/gean.12028</a>.
</div>
<div id="ref-rocchiniRasterdivInformationTheory2021" class="csl-entry">
Rocchini, Duccio, Elisa Thouverai, Matteo Marcantonio, et al. 2021. <span>“Rasterdiv - <span>An Information Theory</span> Tailored <span>R</span> Package for Measuring Ecosystem Heterogeneity from Space: <span>To</span> the Origin and Back.”</span> <em>Methods in Ecology and Evolution</em> 12 (6): 2195. <a href="https://doi.org/10.1111/2041-210X.13583">https://doi.org/10.1111/2041-210X.13583</a>.
</div>
<div id="ref-smithGeodivMethodsCalculating2023" class="csl-entry">
Smith, Annie C., Phoebe Zarnetske, Kyla Dahlin, Adam Wilson, and Andrew Latimer. 2023. <em>Geodiv: <span>Methods</span> for Calculating Gradient Surface Metrics</em>. Manual.
</div>
<div id="ref-wangImageQualityAssessment2004" class="csl-entry">
Wang, Z., A. C. Bovik, H. R. Sheikh, and E. P. Simoncelli. 2004. <span>“Image <span>Quality Assessment</span>: <span>From Error Visibility</span> to <span>Structural Similarity</span>.”</span> <em>IEEE Transactions on Image Processing</em> 13 (4): 600–612. <a href="https://doi.org/10.1109/TIP.2003.819861">https://doi.org/10.1109/TIP.2003.819861</a>.
</div>
</div></section><section id="footnotes" class="footnotes footnotes-end-of-document"><h2 class="anchored quarto-appendix-heading">Footnotes</h2>

<ol>
<li id="fn1"><p>In fact, they may also be used for comparing rasters for other regions, but they still should have the same number of rows and columns.↩︎</p></li>
<li id="fn2"><p>Alternatively, the absolute difference can be calculated to show the difference in the magnitude of the values.↩︎</p></li>
<li id="fn3"><p>Read the comment on this by <a href="https://x.com/mathieuGravey/status/1820733783970091079">Mathieu Gravey</a>.↩︎</p></li>
<li id="fn4"><p>Implementation of RMSE is also straightforward: <code>rmse = function(x, y) sqrt(mean((x - y)^2))</code>.↩︎</p></li>
<li id="fn5"><p>This leaves a tough question of how to choose the size of the moving window.↩︎</p></li>
<li id="fn6"><p>The “pearson” method is the only one available by name, but other methods can be used by providing a custom function.↩︎</p></li>
<li id="fn7"><p>This function also returns three other rasters: SIM, SIV, and SIP that relate to the luminance, contrast, and structure, respectively.↩︎</p></li>
</ol>
</section><section class="quarto-appendix-contents" id="quarto-reuse"><h2 class="anchored quarto-appendix-heading">Reuse</h2><div class="quarto-appendix-contents"><div><a rel="license" href="https://creativecommons.org/licenses/by/4.0/">CC BY 4.0</a></div></div></section><section class="quarto-appendix-contents" id="quarto-citation"><h2 class="anchored quarto-appendix-heading">Citation</h2><div><div class="quarto-appendix-secondary-label">BibTeX citation:</div><div class="code-copy-outer-scaffold"><pre class="sourceCode quarto-appendix-bibtex code-with-copy"><code class="sourceCode bibtex">@online{nowosad2024,
  author = {Nowosad, Jakub},
  title = {Comparison of Spatial Patterns in Continuous Raster Data for
    Overlapping Regions Using {R}},
  date = {2024-10-20},
  url = {https://jakubnowosad.com/posts/2024-10-20-spatcomp-bp2/},
  doi = {10.59350/gm8md-88076},
  langid = {en}
}
</code></pre></div><div class="quarto-appendix-secondary-label">For attribution, please cite this work as:</div><div id="ref-nowosad2024" class="csl-entry quarto-appendix-citeas">
Nowosad, Jakub. 2024. <span>“Comparison of Spatial Patterns in
Continuous Raster Data for Overlapping Regions Using R.”</span> October
20. <a href="https://doi.org/10.59350/gm8md-88076">https://doi.org/10.59350/gm8md-88076</a>.
</div></div></section></div> ]]></description>
  <category>spatial</category>
  <category>landscape-ecology</category>
  <category>spatial-patterns</category>
  <category>msca-pf</category>
  <category>rstats</category>
  <guid>https://jakubnowosad.com/posts/2024-10-20-spatcomp-bp2/</guid>
  <pubDate>Sun, 20 Oct 2024 00:00:00 GMT</pubDate>
</item>
<item>
  <title>Inventory of methods for comparing spatial patterns in raster data</title>
  <dc:creator>Jakub Nowosad</dc:creator>
  <link>https://jakubnowosad.com/posts/2024-10-13-spatcomp-bp1/</link>
  <description><![CDATA[ 





<div class="callout callout-style-simple callout-note callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Note</span>Methods for comparing spatial patterns in raster data
</div>
</div>
<div class="callout-body-container callout-body">
<p>This is the first part of a blog post series on comparing spatial patterns in raster data.</p>
<!-- list all of the blog posts -->
<ul>
<li>Part 2: <a href="../../posts/2024-10-20-spatcomp-bp2">Comparison of spatial patterns in continuous raster data for overlapping regions using R</a></li>
<li>Part 3: <a href="../../posts/2024-10-27-spatcomp-bp3">Comparison of spatial patterns in continuous raster data for arbitrary regions using R</a></li>
<li>Part 4: <a href="../../posts/2024-11-03-spatcomp-bp4">Comparison of spatial patterns in categorical raster data for overlapping regions using R</a></li>
<li>Part 5: <a href="../../posts/2024-11-10-spatcomp-bp5">Comparison of spatial patterns in categorical raster data for arbitrary regions using R</a></li>
<li>Part 6: <a href="../../posts/2024-11-17-spatcomp-bp6">Understanding and extending the methods of comparing spatial patterns in raster data</a></li>
</ul>
</div>
</div>
<p>Comparison of spatial patterns in raster data is a part of many types of spatial analysis. With this task, we want to know how the physical arrangement of observations in one raster differs from the physical arrangement of observations in another raster.</p>
<p>This blog post series will explain the motivation for comparing spatial patterns in raster data, the general considerations when selecting a method for comparison, and the inventory of methods for comparing spatial patterns in raster data. Next, it will show how to use R to compare spatial patterns in continuous and categorical raster data. Lastly, it will discuss the methods’ properties, their applicability, and how they can be extended.</p>
<section id="motivation" class="level2">
<h2 class="anchored" data-anchor-id="motivation">Motivation</h2>
<p>In general, four main reasons for comparing spatial patterns in raster data were identified by Long and Robertson (2018, <a href="https://doi.org/10.1111/gec3.12356" class="uri">https://doi.org/10.1111/gec3.12356</a>):</p>
<ol type="1">
<li>Study of change (i.e., how the spatial pattern of a landscape has changed over time)</li>
<li>Study of similarity (i.e., how similar the spatial pattern of two landscapes is)</li>
<li>Study of association (i.e., how the spatial pattern of one theme is associated with the spatial pattern of another theme)</li>
<li>Spatial model assessment (i.e., how well a model output matches the spatial pattern of the reference data)</li>
</ol>
<p>For now, I want to focus on the situation in which only two rasters are compared at a time.<sup>1</sup> Then, we can think about comparing spatial patterns in raster data as the type of operation that is performed:</p>
<ol type="1">
<li>Comparison of the same variable for two different areas</li>
<li>Comparison of two different variables (or e.g., sensors) for the same area</li>
<li>Comparison of the same variable for the same area at two different times</li>
</ol>
</section>
<section id="visual-inspection" class="level2 page-columns page-full">
<h2 class="anchored" data-anchor-id="visual-inspection">Visual inspection</h2>
<p>To think about this topic, let’s consider the examples of the Corine Land Cover (CLC) data for Tartu, Estonia in 2000 and 2018, and for Poznan, Poland in 2018:</p>
<div class="cell column-page quarto-layout-panel" data-layout-ncol="3">
<div class="quarto-layout-row">
<div class="quarto-layout-cell" style="flex-basis: 33.3%;justify-content: center;">
<p><img src="https://jakubnowosad.com/posts/2024-10-13-spatcomp-bp1/index_files/figure-html/unnamed-chunk-2-1.png" class="img-fluid" width="672"></p>
</div>
<div class="quarto-layout-cell" style="flex-basis: 33.3%;justify-content: center;">
<p><img src="https://jakubnowosad.com/posts/2024-10-13-spatcomp-bp1/index_files/figure-html/unnamed-chunk-2-2.png" class="img-fluid" width="672"></p>
</div>
<div class="quarto-layout-cell" style="flex-basis: 33.3%;justify-content: center;">
<p><img src="https://jakubnowosad.com/posts/2024-10-13-spatcomp-bp1/index_files/figure-html/unnamed-chunk-2-3.png" class="img-fluid" width="672"></p>
</div>
</div>
</div>
<p>We can start comparing the spatial patterns in these rasters just by looking at them: the land cover for Tartu in 2000 and 2018 looks similar, but it is different for Poznan in 2018 (much more urban areas, less forests). A comparison of Tartu in 2000 and 2018 suggests that urban areas have expanded into areas that were previously mostly covered by agricultural land. Visual inspection is a good starting point, as the human eye can detect multiple patterns that are not easily quantifiable. At the same time, it is subjective and may not be suitable for large datasets.</p>
</section>
<section id="generalization-of-the-main-aspects-to-consider" class="level2 page-columns page-full">
<h2 class="anchored" data-anchor-id="generalization-of-the-main-aspects-to-consider">Generalization of the main aspects to consider</h2>
<p>Alternatively, we can use quantitative methods to compare the spatial patterns in these rasters. Figure&nbsp;1 shows general considerations when thinking about the properties of the methods for comparing spatial patterns in raster data.</p>
<!-- Arbitrary Regions -->
<!-- ├── Disjoint Regions (no overlap) -->
<!-- └── Overlapping Regions (with overlap) -->
<div class="cell page-columns page-full" data-layout-align="default">
<div class="cell-output-display column-page">
<div id="fig-flowchart" class="quarto-float quarto-figure quarto-figure-center anchored">
<figure class="quarto-float quarto-float-fig figure">
<div aria-describedby="fig-flowchart-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
<div>

<pre class="mermaid mermaid-js" data-label="fig-flowchart">flowchart TB
  A[Arbitrary regions] ---|Yes| B(Spatial context)
  A ---|No| C(Spatial context)
  B ---|Yes| D{{Outcome}}
  B ---|No| E{{Outcome}}
  C ---|Yes| F{{Outcome}}
  C ---|No| G{{Outcome}}
  D --&gt; H([Single value])
  E --&gt; I([Single value])
  F --&gt; J([Single value]) 
  F --&gt; K([Multiple values])
  F --&gt; L([Raster]) 
  G --&gt; M([Single value])
  G --&gt; N([Multiple values])
  G --&gt; O([Raster])
  
  style D stroke:transparent
  style E stroke:transparent
  style F stroke:transparent
  style G stroke:transparent
</pre>
</div>
</div>
<figcaption class="quarto-float-caption-bottom quarto-float-caption quarto-float-fig" id="fig-flowchart-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
Figure&nbsp;1: Generalization of the main aspects to consider when comparing spatial patterns in raster data
</figcaption>
</figure>
</div>
</div>
</div>
<p><strong>The first main aspect to consider when comparing spatial patterns in raster data is whether or not we are dealing with arbitrary regions.</strong> Working on overlapping (i.e., non-arbitrary) regions, e.g., CLC in Tartu in 2000 and 2018, allows for different approaches than working on arbitrary regions, e.g., CLC in Tartu in 2018 and Poznan in 2018. With non-arbitrary regions, each cell in one raster (or each cell in a moving window) can be compared to a corresponding cell in another raster. Thus, one possible outcome of the comparison is another raster, which highlights where the spatial patterns are similar or different. This is not possible with arbitrary regions, and the comparison usually includes spatial patterns of whole rasters.</p>
<p><strong>The second main aspect to consider when comparing spatial patterns in raster data is whether the method used allows the integration of the spatial context of the analysis.</strong> A difference between the values of two cells is straightforward to calculate and interpret, but it does not consider the other local values. Alternatively, some methods use the spatial context of the analysis, e.g., by comparing the values in a moving window or a local neighborhood.</p>
<p><strong>It is also worth noting that the comparison of spatial patterns in raster data can result in different types of data.</strong> For overlapping regions, the outcome can be a single value, multiple values, or a raster, and for arbitrary regions, it is usually a single value (multiple values are also possible, but often as a collection of single values’ results).</p>
<p>The above considerations can be applied to both continuous and categorical raster data, but the methods used for comparing spatial patterns in these two types of data are different.</p>
</section>
<section id="inventory-of-methods" class="level2">
<h2 class="anchored" data-anchor-id="inventory-of-methods">Inventory of methods</h2>
<p>An inventory of methods for comparing spatial patterns in raster data is presented in the following tables.<sup>2</sup> Methods for comparing two layers of spatial continuous raster data are shown in Table&nbsp;1, and methods for spatial categorical raster data are in Table&nbsp;2.</p>
<div class="cell">
<div id="tbl-continuous" class="cell quarto-float quarto-figure quarto-figure-center anchored">
<figure class="quarto-float quarto-float-tbl figure">
<figcaption class="quarto-float-caption-top quarto-float-caption quarto-float-tbl" id="tbl-continuous-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
Table&nbsp;1: Methods for comparing two layers of spatial continuous raster data.
</figcaption>
<div aria-describedby="tbl-continuous-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
<div class="cell-output-display">
<table class="cell caption-top table table-sm table-striped small">
<colgroup>
<col style="width: 4%">
<col style="width: 3%">
<col style="width: 4%">
<col style="width: 87%">
</colgroup>
<thead>
<tr class="header">
<th style="text-align: left;">Arbitrary regions</th>
<th style="text-align: left;">Context</th>
<th style="text-align: left;">Outcome</th>
<th style="text-align: left;">Examples of methods</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td style="text-align: left;">no</td>
<td style="text-align: left;">non-spatial</td>
<td style="text-align: left;">raster</td>
<td style="text-align: left;">The difference between values of two rasters for each cell</td>
</tr>
<tr class="even">
<td style="text-align: left;">no</td>
<td style="text-align: left;">spatial</td>
<td style="text-align: left;">raster</td>
<td style="text-align: left;">Correlation coefficient between focal regions of two rasters; The difference between a focal measure of two rasters (e.g., based on a GLCM-based texture measure (Haralick et al., 1973)); Spatial autocorrelation analysis of the differences (Cliff, 1970); Structural Similarity Index (Robertson et al., 2014; Wang et al., 2004)</td>
</tr>
<tr class="odd">
<td style="text-align: left;">no</td>
<td style="text-align: left;">non-spatial</td>
<td style="text-align: left;">single value</td>
<td style="text-align: left;">Statistics of the differences between rasters’ values (e.g., RMSE)</td>
</tr>
<tr class="even">
<td style="text-align: left;">yes</td>
<td style="text-align: left;">non-spatial</td>
<td style="text-align: left;">single value</td>
<td style="text-align: left;">Dissimilarity between the distributions of two rasters’ values</td>
</tr>
<tr class="odd">
<td style="text-align: left;">no</td>
<td style="text-align: left;">spatial</td>
<td style="text-align: left;">single value</td>
<td style="text-align: left;">Average of Structural Similarity Index (Robertson et al., 2014; Wang et al., 2004); Complex Wavelet Structural Similarity (Malik and Robertson, 2020; Sampat et al., 2009); Comparison of deep learning-based feature maps using a dissimilarity measure (Malik and Robertson, 2021)</td>
</tr>
<tr class="even">
<td style="text-align: left;">yes</td>
<td style="text-align: left;">spatial</td>
<td style="text-align: left;">single value</td>
<td style="text-align: left;">Comparison of deep learning-based feature maps using a dissimilarity measure (Malik and Robertson, 2021)</td>
</tr>
<tr class="odd">
<td style="text-align: left;">no</td>
<td style="text-align: left;">non-spatial</td>
<td style="text-align: left;">multiple values</td>
<td style="text-align: left;">The distribution of the difference between values of two rasters; Statistics of the differences between rasters’ values (e.g., RMSE) calculated at many scales</td>
</tr>
</tbody>
</table>
</div>
</div>
</figure>
</div>
</div>
<div class="cell">
<div id="tbl-categorical" class="cell quarto-float quarto-figure quarto-figure-center anchored">
<figure class="quarto-float quarto-float-tbl figure">
<figcaption class="quarto-float-caption-top quarto-float-caption quarto-float-tbl" id="tbl-categorical-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
Table&nbsp;2: Methods for comparing two layers of spatial categorical raster data.
</figcaption>
<div aria-describedby="tbl-categorical-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
<div class="cell-output-display">
<table class="cell caption-top table table-sm table-striped small">
<colgroup>
<col style="width: 6%">
<col style="width: 4%">
<col style="width: 5%">
<col style="width: 82%">
</colgroup>
<thead>
<tr class="header">
<th style="text-align: left;">Arbitrary regions</th>
<th style="text-align: left;">Context</th>
<th style="text-align: left;">Outcome</th>
<th style="text-align: left;">Examples of methods</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td style="text-align: left;">no</td>
<td style="text-align: left;">non-spatial</td>
<td style="text-align: left;">raster</td>
<td style="text-align: left;">The binary difference between two rasters</td>
</tr>
<tr class="even">
<td style="text-align: left;">no</td>
<td style="text-align: left;">spatial</td>
<td style="text-align: left;">raster</td>
<td style="text-align: left;">The difference between a focal measure of two rasters (e.g., selected landscape metric); Dissimilarity between spatial signatures of focal regions of two rasters</td>
</tr>
<tr class="odd">
<td style="text-align: left;">no</td>
<td style="text-align: left;">non-spatial</td>
<td style="text-align: left;">single value</td>
<td style="text-align: left;">The proportion of changed pixels; The overall comparison (Pontius, 2002), A statistic of the differences between rasters’ values</td>
</tr>
<tr class="even">
<td style="text-align: left;">yes</td>
<td style="text-align: left;">non-spatial</td>
<td style="text-align: left;">single value</td>
<td style="text-align: left;">Comparison of the values of a non-spatial landscape metric</td>
</tr>
<tr class="odd">
<td style="text-align: left;">no</td>
<td style="text-align: left;">spatial</td>
<td style="text-align: left;">single value</td>
<td style="text-align: left;">Multiple resolution procedure (Costanza, 1989); Expanding window approach (Kuhnert et al., 2005); Fuzzy Kappa (Hagen-Zanker, 2009); Spatial association between regionalizations using V-measure (Nowosad and Stepinski, 2018)</td>
</tr>
<tr class="even">
<td style="text-align: left;">yes</td>
<td style="text-align: left;">spatial</td>
<td style="text-align: left;">single value</td>
<td style="text-align: left;">Comparison of the values of a landscape metric (Turner et al., 1989) or fractal dimensions (Batty and Longley, 1994); Dissimilarity of a spatial signature between two rasters (Jasiewicz and Stepinski, 2013)</td>
</tr>
<tr class="odd">
<td style="text-align: left;">no</td>
<td style="text-align: left;">non-spatial</td>
<td style="text-align: left;">multiple values</td>
<td style="text-align: left;">The confusion matrix</td>
</tr>
<tr class="even">
<td style="text-align: left;">no</td>
<td style="text-align: left;">spatial</td>
<td style="text-align: left;">multiple values</td>
<td style="text-align: left;">Comparison of mutual information spectra (Remmel and Csillag, 2006)</td>
</tr>
</tbody>
</table>
</div>
</div>
</figure>
</div>
</div>
<p>Both tables are adapted from the “Comparing spatial patterns in raster data using R” paper published in the ISPRS Archives (Nowosad, 2024, <a href="https://doi.org/10.5194/isprs-archives-XLVIII-4-W12-2024-127-2024" class="uri">https://doi.org/10.5194/isprs-archives-XLVIII-4-W12-2024-127-2024</a>).<sup>3</sup> There you can find the complete list of the references for the methods presented in the tables.</p>
<!-- not all of the methods from the table are available in R -->
<p>In the next blog posts, I will focus on the practical aspects of comparing spatial patterns: how to use R to compare spatial patterns in continuous and categorical raster data. It will include the interpretation of the results and the discussion of the characteristics of the methods. Stay tuned!</p>


</section>


<div id="quarto-appendix" class="default"><section id="footnotes" class="footnotes footnotes-end-of-document"><h2 class="anchored quarto-appendix-heading">Footnotes</h2>

<ol>
<li id="fn1"><p>I may expand this to the comparison of multiple rasters in a future blog post.↩︎</p></li>
<li id="fn2"><p>The tables omit the visual inspection of the data, which is often the first step in the comparison of spatial patterns in raster data.↩︎</p></li>
<li id="fn3"><p>However, I decided to use the term “arbitrary regions” instead of “disjoint areas”.↩︎</p></li>
</ol>
</section><section class="quarto-appendix-contents" id="quarto-reuse"><h2 class="anchored quarto-appendix-heading">Reuse</h2><div class="quarto-appendix-contents"><div><a rel="license" href="https://creativecommons.org/licenses/by/4.0/">CC BY 4.0</a></div></div></section><section class="quarto-appendix-contents" id="quarto-citation"><h2 class="anchored quarto-appendix-heading">Citation</h2><div><div class="quarto-appendix-secondary-label">BibTeX citation:</div><div class="code-copy-outer-scaffold"><pre class="sourceCode quarto-appendix-bibtex code-with-copy"><code class="sourceCode bibtex">@online{nowosad2024,
  author = {Nowosad, Jakub},
  title = {Inventory of Methods for Comparing Spatial Patterns in Raster
    Data},
  date = {2024-10-13},
  url = {https://jakubnowosad.com/posts/2024-10-13-spatcomp-bp1/},
  doi = {10.59350/gpd60-zab66},
  langid = {en}
}
</code></pre></div><div class="quarto-appendix-secondary-label">For attribution, please cite this work as:</div><div id="ref-nowosad2024" class="csl-entry quarto-appendix-citeas">
Nowosad, Jakub. 2024. <span>“Inventory of Methods for Comparing Spatial
Patterns in Raster Data.”</span> October 13. <a href="https://doi.org/10.59350/gpd60-zab66">https://doi.org/10.59350/gpd60-zab66</a>.
</div></div></section></div> ]]></description>
  <category>spatial</category>
  <category>landscape-ecology</category>
  <category>spatial-patterns</category>
  <category>msca-pf</category>
  <category>rstats</category>
  <guid>https://jakubnowosad.com/posts/2024-10-13-spatcomp-bp1/</guid>
  <pubDate>Sun, 13 Oct 2024 00:00:00 GMT</pubDate>
</item>
<item>
  <title>Navigating the maze: Reflections on applying for the Marie Skłodowska-Curie Actions Postdoctoral Fellowship</title>
  <dc:creator>Jakub Nowosad</dc:creator>
  <link>https://jakubnowosad.com/posts/2024-07-22-msca-bp1/</link>
  <description><![CDATA[ 





<p>I received a grant from the Marie Skłodowska-Curie Actions Postdoctoral Fellowships (MSCA-PF) program: between August 2024 and August of 2026, I will be at the University of Muenster, Germany, working on a project named PRISM: <em>PReservation and RecognItion of Spatial patterns using Machine learning</em>. The project’s primary goal is to develop and compare methods for validating and including spatial patterns in machine learning. You can read a short description of the project <a href="https://doi.org/10.3030/101147446">here</a>.<sup>1</sup></p>
<p><img src="https://jakubnowosad.com/posts/2024-07-22-msca-bp1/prism.png" class="img-fluid"></p>
<p>I am excited about the project and looking forward to the next two years, but this blog post is not about the project itself. Instead, I want to share some thoughts on the process of applying for the MSCA-PF grant. I hope this post will be helpful for others applying for this grant in the future, but also for grant providers and reviewers who might be interested in improving the process.<sup>2</sup></p>
<section id="msca-pf" class="level2">
<h2 class="anchored" data-anchor-id="msca-pf">MSCA-PF</h2>
<p>In short, <a href="https://marie-sklodowska-curie-actions.ec.europa.eu/actions/postdoctoral-fellowships">the MSCA-PF grant</a> is a competitive grant aimed at early career researchers (up to 8 years after the PhD).<sup>3</sup> The European Postdoctoral Fellowship allows researchers to work on a project of their choice for up to two in a host institution in Europe or associated countries. The aim of this grant is not only to support the research project but also to develop new skills and promote knowledge transfer between the researcher and the host institution. <a href="https://marie-sklodowska-curie-actions.ec.europa.eu/actions/postdoctoral-fellowships">The MSCA-PF grant website</a> also lists the following benefits of the grant: a living allowance, a mobility allowance (plus possibly also family, long-term leave, and special needs allowances) and funding for research, training, and networking activities, and management and indirect costs.</p>
</section>
<section id="the-grant-proposal" class="level2">
<h2 class="anchored" data-anchor-id="the-grant-proposal">The grant proposal</h2>
<p>The grant proposal consists of <strong>Part A</strong> (the administrative part filled in the online portal) and <strong>Part B</strong> (the scientific part uploaded as a PDF). <strong>Part B</strong> is divided into two subparts: <strong>B1</strong> and <strong>B2</strong>.</p>
<p><strong>Part B1</strong> is the core part of the proposal and should contain details of the proposed research and training activities along with the practical arrangements proposed to implement them, etc. It is strictly restricted to 10 A4-sized pages, with font size and margin limitations. <strong>Part B2</strong> has no page limit and contains the researcher’s CV, the capacity of the participating organization(s), and other related information.</p>
<p>The grant process has extensive documentation, which includes <a href="https://marie-sklodowska-curie-actions.ec.europa.eu/actions/postdoctoral-fellowships">the main website</a>, <a href="https://mariesklodowskacurieactions.blogspot.com/">Q&amp;A Blog</a>, and <a href="https://rea.ec.europa.eu/funding-and-grants/horizon-europe-marie-sklodowska-curie-actions/horizon-europe-msca-how-apply_en#postdoctoral-fellowships--call-2024">many other documents</a>, such as “The guide for Applicants”, “PF Handbook”, “Evaluation Form”, “How to complete your ethics self-assessment”, and many more. The extensive documentation, while comprehensive, can be overwhelming for applicants, as it far exceeds the length of the proposal itself.</p>
</section>
<section id="application-process" class="level2">
<h2 class="anchored" data-anchor-id="application-process">Application process</h2>
<p>After reading the documentation, the application process seems straightforward: you just fill out the online form, write <strong>Part B</strong>, and submit everything. I initially thought that my main issue would be the page limit of <strong>Part B1</strong>: how to fit all the ideas about the project into just 10 pages? The reality turned out to be quite different–the main issue was understanding what was actually expected in the proposal.</p>
<p><strong>Part B1</strong> has three main sections: (1) “Excellence”, (2) “Impact”, and (3) “Quality and Efficiency of the Implementation”. Then, each section has a number of subsections. For example, the “Excellence” section has the following subsections:</p>
<ul>
<li>“Quality and pertinence of the project’s research and innovation objectives (and the extent to which they are ambitious, and go beyond the state of the art).”</li>
<li>“Soundness of the proposed methodology (including interdisciplinary approaches, consideration of the gender dimension and other diversity aspects if relevant for the research project, and the quality of open science practices).”</li>
<li>“Quality of the supervision, training and of the two-way transfer of knowledge between the researcher and the host.”</li>
<li>“Quality and appropriateness of the researcher’s professional experience, competences and skills.”</li>
</ul>
<p>Then, each subsection has several bullet points that you are supposed to address in your proposal. For example, the first subsection has two bullet points, and the second subsection has five bullet points, etc. <em>Spoiler alert: not addressing these bullet points may result in a lower reviewer’s score.</em></p>
<p>Moreover, throughout the grant proposal template, you may encounter various new terms and concepts that you need to understand and address. Some are probably well-known by seasoned grant writers but not by all early-career researchers. For example, you need to know the differences between dissemination, exploitation, and communication of the results; what’s “Data Management Plan (DMP)” or “Career Development Plan (CDP)”; what’s “Mobility declaration” or “Evaluation questionnaire”; how to address “Gender dimension and other diversity aspects”, “Environmental considerations in light of the MSCA Green Charter”, etc. These are defined in various documents available online, but it takes time to understand them and it is required to address them in the proposal.</p>
<p>To make things more complicated, the grant documentation also contains several hidden expectations. I got great help by talking to a previous MSCA-PF grant holder, local university advisors, and a <a href="https://www.kowi.de/en/kowi.aspx">KoWi</a> advisor.<sup>4</sup> Interestingly, they all pointed out different aspects (and hidden expectations) of the grant proposal. Thus, the process can feel like navigating a complex puzzle, with some elements not immediately apparent.</p>
<p>My rough estimation is that about 7 out of 10 pages of <strong>Part B1</strong> relate to the expected information: you must address the bullet points, explain the concepts, and meet the hidden expectations. The remaining three pages, scattered throughout the proposal, are for the actual ideas behind the project (and some references).</p>
<p>The proposal’s structure may inadvertently encourage applicants to focus more on meeting specific criteria than on fully elaborating their research ideas. <!-- This creates a significant problem: people are incentivized to write the grant proposal in a way that fills the expected boxes, rather than explaining the actual ideas behind the project. --> For example, there should be a part about “Open science practices” within the second subsection of <strong>Part B1</strong>. It does not matter if you are actually doing (or thinking about doing) open science<sup>5</sup> – you need to write about it to get points. I think it teaches the wrong behavior to young researchers.</p>
<p>Let’s say you wrote a first draft of the text and are happy with it. Now, you need to format it to meet all formal expectations, such as font size, margins, page limits, and more. You may spend many hours moving the text around, changing the font sizes, and so on. Thus, I suggest leaving the formatting as one of the last steps of the proposal writing process.</p>
<p>In total, I spent about three months of part-time work on the grant proposal.<sup>6</sup> I also suspect that the cumulative time investment across all applicants (of this single-person grant) is substantial, highlighting an opportunity to explore ways to streamline the application process.<sup>7</sup></p>
</section>
<section id="reviewing-process" class="level2">
<h2 class="anchored" data-anchor-id="reviewing-process">Reviewing process</h2>
<p>After you submit the grant proposal (the deadline is usually in September), it goes through a reviewing process and the results are announced the following March. You can find the evaluation form at <a href="https://ec.europa.eu/info/funding-tenders/opportunities/docs/2021-2027/horizon/temp-form/ef/ef_he-msca_en.pdf" class="uri">https://ec.europa.eu/info/funding-tenders/opportunities/docs/2021-2027/horizon/temp-form/ef/ef_he-msca_en.pdf</a>. The form mainly focuses on the expected information. On the one hand, it makes sense: the reviewers need to try to be objective and evaluate the proposals based on the same criteria. On the other hand, there’s a chance that proposals that best meet the formal criteria score highly, which may not always align perfectly with identifying the most innovative research ideas.<sup>8</sup></p>
</section>
<section id="celebration" class="level2">
<h2 class="anchored" data-anchor-id="celebration">Celebration?</h2>
<p>Now, let’s say that you got the grant. It is time for <del>celebration</del> filling out the grant agreement. This brings me to the topic of the online portals related to the MSCA-PF grant.<sup>9</sup> Yes, portal<strong>s</strong>. This is because there are several portals that you need to use during the application and project management phases. These portals are not very user-friendly, and each of them uses a different technology and visual style. It takes a lot of time to get used to them, understand which portal you need to use for which task, and how to navigate them. There seems to be room for improving the user experience and integration of these systems.</p>
</section>
<section id="the-fine-print" class="level2">
<h2 class="anchored" data-anchor-id="the-fine-print">The fine print</h2>
<p>Let’s end up with one important piece of information for those who are considering applying for the MSCA-PF grant. The grant promotes itself with a list of benefits: a living allowance, a mobility allowance (plus possibly also family, long-term leave, and special needs allowances), funding for research, training, networking activities, and management and indirect costs. The living allowance (that is supposed to cover your salary) depends on the country where you will be working, based on a correction factor for the cost of living, while the rest of the allowances are fixed. For example, in the 2023 edition of the grant, the living allowance for Germany was about 5,000 EUR per month, plus the mobility allowance of 600 EUR per month, and the family allowance of 660 EUR per month (plus some money for the research, training, and networking activities, and management and indirect costs).</p>
<p>What is not directly mentioned, however, is that every country (and even institutions in one country) has different rules about salary, taxes, and other benefits. For example, some universities in Germany will just hire you as a regular employee, and you will get a salary based on the pay scale. Thus, you won’t get the mobility and family allowances.<sup>10</sup> Moreover, the grant funding can be treated in some (?) institutions as gross gross (<em>brutto brutto</em>), which means that the money will be first used to cover the employer’s costs, and then you will need to pay the taxes on the remaining money. Thus, the actual amount the researcher receives (netto) is significantly less than the total amount granted.</p>
<p>Given that the way the grant is treated may vary between countries and institutions, I think it is essential to ask about the details before applying for the grant. The best way is probably to contact a previous MSCA-PF grant holder from the institution where you plan to work and ask about all these details.</p>
</section>
<section id="conclusions" class="level2">
<h2 class="anchored" data-anchor-id="conclusions">Conclusions</h2>
<p>This may not be obvious after reading this blog post, but I am very happy I got the grant and excited about the project. If I could go back in time knowing all of the above, I would still apply for the MSCA-PF grant. This grant format is an excellent opportunity for researchers to move to a different environment, learn new skills, and develop new ideas.</p>
<p>That being said, I think the grant proposal process could be greatly improved. <!-- Something needs to be fixed if you need hundreds (or more?) of pages to explain the process of writing a ten-page proposal. --> The complexity of the application process (evidenced by the extensive documentation required for a relatively short proposal) suggests that there may be opportunities to improve the procedure. Currently, it puts a lot of pressure and a high time burden on the applicants and may lead to a situation where the best proposals are not funded.<sup>11</sup> This, combined with many hidden expectations, document formatting, and user-unfriendly online portals, makes the whole process even more time-consuming: it could require thousands of hours of young researchers’ work.</p>
<p>I think the grant proposal process should be much simplified and streamlined. While I understand the need for evaluation criteria and objectivity, I think the current system is not the best way to achieve this. In my opinion, the focus of the proposal should be on the research ideas, the potential of the applicants, and the transfer of knowledge, not on the ability to fill in the expected information. A good example of a grant proposal process that I think is much better<sup>12</sup> is <a href="https://www.humboldt-foundation.de/en/apply/sponsorship-programmes/humboldt-research-fellowship">the Humboldt Research Fellowship</a>, which has one simple online portal and fairly straightforward expectations – the focus is on the research ideas and the potential of the applicants, and the whole application process is less surprising and much less time-consuming.</p>
<p>I hope you found this post helpful – if you have any questions or comments, feel free to email me. And now, it’s time to pack my bags.</p>


</section>


<div id="quarto-appendix" class="default"><section id="footnotes" class="footnotes footnotes-end-of-document"><h2 class="anchored quarto-appendix-heading">Footnotes</h2>

<ol>
<li id="fn1"><p>I also plan to write a few blog posts about the project, so stay tuned!↩︎</p></li>
<li id="fn2"><p>It will be useful for me to clear my thoughts and reflect on the process.↩︎</p></li>
<li id="fn3"><p>The acceptance rate in 2024 was about 15%.↩︎</p></li>
<li id="fn4"><p>Thank you!↩︎</p></li>
<li id="fn5"><p>I strongly encourage it.↩︎</p></li>
<li id="fn6"><p>Of course, I was also working on several other projects, teaching, etc., at the same time.↩︎</p></li>
<li id="fn7"><p>About 8,000 applicants multiplied by 3 months is 2,000 years of part-time work of a highly educated person.↩︎</p></li>
<li id="fn8"><p>As pointed out by a peer, these types of reviews are still very good to filter out bad proposals. I agree with this statement. At the same time, I also think that science is <a href="https://www.experimental-history.com/p/science-is-a-strong-link-problem">a strong link problem</a>, and we should focus our efforts on finding the best ideas.↩︎</p></li>
<li id="fn9"><p>And, I assume, to other EU grants as well.↩︎</p></li>
<li id="fn10"><p>This creates a situation where a person with a family gets the same salary as a person without a family.↩︎</p></li>
<li id="fn11"><p>And possibly not even written when the applicants do not have enough privilege to spend three months on a single-person grant proposal.↩︎</p></li>
<li id="fn12"><p>But still not perfect.↩︎</p></li>
</ol>
</section><section class="quarto-appendix-contents" id="quarto-citation"><h2 class="anchored quarto-appendix-heading">Citation</h2><div><div class="quarto-appendix-secondary-label">BibTeX citation:</div><div class="code-copy-outer-scaffold"><pre class="sourceCode quarto-appendix-bibtex code-with-copy"><code class="sourceCode bibtex">@online{nowosad2024,
  author = {Nowosad, Jakub},
  title = {Navigating the Maze: {Reflections} on Applying for the
    {Marie} {Skłodowska-Curie} {Actions} {Postdoctoral} {Fellowship}},
  date = {2024-07-22},
  url = {https://jakubnowosad.com/posts/2024-07-22-msca-bp1/},
  doi = {10.59350/bwdj0-xzw64},
  langid = {en}
}
</code></pre></div><div class="quarto-appendix-secondary-label">For attribution, please cite this work as:</div><div id="ref-nowosad2024" class="csl-entry quarto-appendix-citeas">
Nowosad, Jakub. 2024. <span>“Navigating the Maze: Reflections on
Applying for the Marie Skłodowska-Curie Actions Postdoctoral
Fellowship.”</span> July 22. <a href="https://doi.org/10.59350/bwdj0-xzw64">https://doi.org/10.59350/bwdj0-xzw64</a>.
</div></div></section></div> ]]></description>
  <category>spatial</category>
  <category>landscape-ecology</category>
  <category>spatial-patterns</category>
  <category>spatial-machine-learning</category>
  <category>msca-pf</category>
  <guid>https://jakubnowosad.com/posts/2024-07-22-msca-bp1/</guid>
  <pubDate>Mon, 22 Jul 2024 00:00:00 GMT</pubDate>
</item>
<item>
  <title>Optimizing the parameters of the spatial kinetic Ising model to simulate spatial patterns</title>
  <dc:creator>Jakub Nowosad</dc:creator>
  <link>https://jakubnowosad.com/posts/2024-01-07-spatialising-bp2/</link>
  <description><![CDATA[ 





<p>The spatial kinetic Ising model is <a href="../../posts/2023-12-17-spatialising-bp1/">a simple model of spatial patterns that can be used to simulate the evolution of spatial patterns over time</a>. Its two main parameters are <code>B</code> and <code>J</code>, which control the external pressure and the local autocorrelation tendency, respectively. Both of them have a strong effect on the results of the spatial kinetic Ising model. Thus, the question is how to find the best values of these parameters for a given situation.</p>
<p>This blog post shows how to use the simulated annealing algorithm to find the best values of <code>B</code> and <code>J</code> that minimize the difference between the metrics of the expected map and the metrics of the last simulation.</p>
<section id="data-preparation" class="level1">
<h1>Data preparation</h1>
<p>To reproduce the calculations in the following post, you need to attach the following packages:</p>
<div class="cell" data-layout-align="center">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(terra)</span>
<span id="cb1-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(spatialising)</span>
<span id="cb1-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(ggplot2)</span></code></pre></div></div>
</div>
<!-- update with url -->
<p>We will also use and process the same input data as in <a href="../../posts/2023-12-17-spatialising-bp1/">the previous blog post</a>.</p>
<div class="cell" data-layout-align="center" data-crop="true">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb2-1">twomaps <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rast</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"/vsicurl/https://github.com/Nowosad/bp-data/raw/main/spatialising-bp/twomaps.tif"</span>)</span>
<span id="cb2-2">rcl <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">matrix</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">byrow =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ncol =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>)</span>
<span id="cb2-3">twomaps <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">classify</span>(twomaps, rcl)</span>
<span id="cb2-4">map1 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> twomaps[[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>]]</span>
<span id="cb2-5">map2 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> twomaps[[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>]]</span>
<span id="cb2-6"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">plot</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(map1, map2))</span></code></pre></div></div>
<div class="cell-output-display">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://jakubnowosad.com/posts/2024-01-07-spatialising-bp2/index_files/figure-html/unnamed-chunk-4-1.png" class="img-fluid quarto-figure quarto-figure-center figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
</section>
<section id="optimizing-the-parameters" class="level1">
<h1>Optimizing the parameters</h1>
<p>In this case, we have two maps: the first map (<code>map1</code>) is the initial map, and the second map (<code>map2</code>) is the expected map. Our main goal is to find the best values of <code>B</code> and <code>J</code> that, given the initial map (<code>map1</code>), will result in a simulated map that is as similar as possible to the expected map (<code>map2</code>).</p>
<p>One possible approach to find these best values of <code>B</code> and <code>J</code> is to use an optimization algorithm. This algorithm’s goal is to minimize the difference between the metrics of the expected map (e.g., <code>map2</code>) and the metrics of the last simulation.</p>
<p>For that purpose, we can use the <code>optim_sa()</code> function from the <strong>optimization</strong> package that implements the simulated annealing algorithm. Firstly, we need to define a function that takes one variable (<code>x</code>) and returns a single numeric value. Here, we created the <code>optimize_model()</code> function that takes the <code>x</code> variable, which is a vector of two values: <code>B</code> and <code>J</code>. The function then runs the spatial kinetic Ising model with the given values of <code>B</code> and <code>J</code>, calculates the composition and texture indexes for the expected map (<code>map2</code>) and the last simulation, and returns the distance between the metrics of the expected map (<code>map2</code>) and the metrics of the last simulation.<sup>1</sup></p>
<div class="cell" data-layout-align="center">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb3-1">optimize_model <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(x){</span>
<span id="cb3-2">  sim <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> spatialising<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">kinetic_ising</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> map1, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">B =</span> x[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>], <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">J =</span> x[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>], </span>
<span id="cb3-3">                                    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">updates =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>)  </span>
<span id="cb3-4">  map2_metrics <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">composition_index</span>(map2), <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">texture_index</span>(map2))</span>
<span id="cb3-5">  sim_metrics <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">composition_index</span>(sim[[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>]]), <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">texture_index</span>(sim[[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>]]))             </span>
<span id="cb3-6">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">dist</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rbind</span>(map2_metrics, sim_metrics))[[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>]]</span>
<span id="cb3-7">}</span></code></pre></div></div>
</div>
<p>Secondly, we need to use the <code>optim_sa()</code> function, which takes the <code>optimize_model()</code> function, the initial values of <code>B</code> and <code>J</code>, and the lower and upper bounds for <code>B</code> and <code>J</code>. Here, we set the bounds for <code>B</code> to be between <code>0</code> and <code>0.9</code> as we know that the proportion of the <code>1</code> (forest) values should increase over time. This operation may take about one minute in this case.</p>
<div class="cell" data-layout-align="center">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb4-1">optim_params <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> optimization<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">optim_sa</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fun =</span> optimize_model, </span>
<span id="cb4-2">                                      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">start =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.4</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.4</span>),</span>
<span id="cb4-3">                                      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">lower =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>), </span>
<span id="cb4-4">                                      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">upper =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.9</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.9</span>))</span></code></pre></div></div>
</div>
<p>The output of the <code>optim_sa()</code> function is a list with several elements, including the best values of <code>B</code> and <code>J</code> in the <code>par</code> element.</p>
<div class="cell" data-layout-align="center">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb5-1">optim_params<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>par</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 0.78 0.51</code></pre>
</div>
</div>
<p>Here, our optimal values of <code>B</code> and <code>J</code> are 0.78 and 0.51, respectively. Now, we can use the newly derived values to simulate the spatial kinetic Ising model similar to the second map (<code>map2</code>).</p>
<div class="cell" data-layout-align="center" data-crop="true">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb7" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb7-1">sim_optim <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">kinetic_ising</span>(map1, </span>
<span id="cb7-2">                          <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">B =</span> optim_params<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>par[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>], <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">J =</span> optim_params<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>par[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>], </span>
<span id="cb7-3">                          <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">updates =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>)</span>
<span id="cb7-4"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">plot</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(map2, sim_optim[[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>]]))</span></code></pre></div></div>
<div class="cell-output-display">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://jakubnowosad.com/posts/2024-01-07-spatialising-bp2/index_files/figure-html/unnamed-chunk-8-1.png" class="img-fluid quarto-figure quarto-figure-center figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<p>The map on the left is the expected, true map, and the map on the right is our simulation. While both maps are not identical, they have a similar spatial pattern with a dominance of forest (<code>1</code>) (especially in the northeast part of the map) and a similar configuration of the patches.</p>
</section>
<section id="exploring-the-simulation-results" class="level1">
<h1>Exploring the simulation results</h1>
<p>In addition to looking at just the final map, we can also retrace the entire simulation process and its effect on the metrics of spatial patterns. Firstly, we can plot all of the simulated rasters:</p>
<div class="cell" data-layout-align="center" data-crop="true">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb8" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb8-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">names</span>(sim_optim) <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">paste0</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"sim_year"</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>)</span>
<span id="cb8-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">plot</span>(sim_optim, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">nr =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span></code></pre></div></div>
<div class="cell-output-display">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://jakubnowosad.com/posts/2024-01-07-spatialising-bp2/index_files/figure-html/unnamed-chunk-10-1.png" class="img-fluid quarto-figure quarto-figure-center figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<p>Secondly, we can calculate their metrics of spatial patterns and compare their changes over time.</p>
<div class="cell" data-layout-align="center">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb9" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb9-1">ci_df <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">data.frame</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">year =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">metric =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"composition index"</span>,</span>
<span id="cb9-2">                   <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">value =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">composition_index</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(map1, sim_optim)))</span>
<span id="cb9-3">ti_df <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">data.frame</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">year =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">metric =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"texture index"</span>,</span>
<span id="cb9-4">                   <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">value =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">texture_index</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(map1, sim_optim)))</span>
<span id="cb9-5">pred_df <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rbind</span>(ci_df, ti_df)</span>
<span id="cb9-6"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(pred_df, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(year, value)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb9-7">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_line</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb9-8">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">facet_wrap</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span>metric, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">scales =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"free_y"</span>)</span></code></pre></div></div>
<div class="cell-output-display">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://jakubnowosad.com/posts/2024-01-07-spatialising-bp2/index_files/figure-html/unnamed-chunk-11-1.png" class="img-fluid quarto-figure quarto-figure-center figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<p>As expected, the composition index increases over time, from negative values indicating a dominance of the <code>-1</code> values to positive values indicating a dominance of the <code>1</code> values. The texture index, on the other hand, decreases for the first two simulations and then increases for the last two simulations. There is a good (and well-known) reason for that: the composition of values has an impact on the spatial texture. The larger the dominance of one category, the more clustered the values are, and thus the higher the texture index is.</p>
</section>
<section id="predicting-the-future-changes" class="level1">
<h1>Predicting the future changes</h1>
<p>Given the assumption that the external pressure and the local autocorrelation tendency will remain the same, we can use the <code>kinetic_ising()</code> function to predict future spatial patterns.</p>
<div class="cell" data-layout-align="center" data-crop="true">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb10" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb10-1">map2_pred <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">kinetic_ising</span>(map2, </span>
<span id="cb10-2">                          <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">B =</span> optim_params<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>par[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>], <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">J =</span> optim_params<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>par[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>], </span>
<span id="cb10-3">                          <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">updates =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>)</span>
<span id="cb10-4"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">names</span>(map2_pred) <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">paste0</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"sim_year"</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">6</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">9</span>)</span>
<span id="cb10-5"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">plot</span>(map2_pred, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">nr =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span></code></pre></div></div>
<div class="cell-output-display">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://jakubnowosad.com/posts/2024-01-07-spatialising-bp2/index_files/figure-html/unnamed-chunk-12-1.png" class="img-fluid quarto-figure quarto-figure-center figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<p>The above map shows the predicted spatial patterns for the years 6, 7, 8, and 9.</p>
</section>
<section id="conclusions" class="level1">
<h1>Conclusions</h1>
<p>This blog post showed how to use the simulated annealing algorithm to find the best values of <code>B</code> and <code>J</code> that minimize the difference between the metrics of the expected map and the metrics of the last simulation. It allows not only the simulation of spatial patterns similar to the expected map but also the analysis of the simulation process and prediction of future spatial patterns.</p>
<p>Of course, there are many caveats and limitations of this approach. For example, the spatial kinetic Ising model assumes that the external pressure and the local autocorrelation tendency are constant over time over the entire area. Moreover, the model is not able to simulate patterns that create or modify linear features (e.g., rivers or roads).</p>
<p>To learn more about the spatial kinetic Ising model, its background, possible applications, and limitations, I encourage you to read <span class="citation" data-cites="Stepinski2023">Stepinski (2023)</span> and <span class="citation" data-cites="spatialisingpaper">Stepinski and Nowosad (2023)</span>.</p>



</section>


<div id="quarto-appendix" class="default"><section class="quarto-appendix-contents" id="quarto-bibliography"><h2 class="anchored quarto-appendix-heading">References</h2><div id="refs" class="references csl-bib-body hanging-indent">
<div id="ref-Stepinski2023" class="csl-entry">
Stepinski, Tomasz F. 2023. <span>“Spatially Explicit Simulation of Deforestation Using the Ising-Like Neutral Model.”</span> <em>Environmental Research: Ecology</em> 2 (2): 025003. <a href="https://doi.org/10.1088/2752-664x/acdbd2">https://doi.org/10.1088/2752-664x/acdbd2</a>.
</div>
<div id="ref-spatialisingpaper" class="csl-entry">
Stepinski, Tomasz F., and Jakub Nowosad. 2023. <span>“The Kinetic Ising Model Encapsulates Essential Dynamics of Land Pattern Change.”</span> <em>Royal Society Open Science</em> 10 (10): 231005. <a href="https://doi.org/10.1098/rsos.231005">https://doi.org/10.1098/rsos.231005</a>.
</div>
</div></section><section id="footnotes" class="footnotes footnotes-end-of-document"><h2 class="anchored quarto-appendix-heading">Footnotes</h2>

<ol>
<li id="fn1"><p>The optimization is also explained at https://jakubnowosad.com/spatialising/articles/Optimizing_spatialising_parameters.html.↩︎</p></li>
</ol>
</section><section class="quarto-appendix-contents" id="quarto-reuse"><h2 class="anchored quarto-appendix-heading">Reuse</h2><div class="quarto-appendix-contents"><div><a rel="license" href="https://creativecommons.org/licenses/by/4.0/">CC BY 4.0</a></div></div></section><section class="quarto-appendix-contents" id="quarto-citation"><h2 class="anchored quarto-appendix-heading">Citation</h2><div><div class="quarto-appendix-secondary-label">BibTeX citation:</div><div class="code-copy-outer-scaffold"><pre class="sourceCode quarto-appendix-bibtex code-with-copy"><code class="sourceCode bibtex">@online{nowosad2024,
  author = {Nowosad, Jakub},
  title = {Optimizing the Parameters of the Spatial Kinetic {Ising}
    Model to Simulate Spatial Patterns},
  date = {2024-01-07},
  url = {https://jakubnowosad.com/posts/2024-01-07-spatialising-bp2/},
  doi = {10.59350/m3r98-ehb46},
  langid = {en}
}
</code></pre></div><div class="quarto-appendix-secondary-label">For attribution, please cite this work as:</div><div id="ref-nowosad2024" class="csl-entry quarto-appendix-citeas">
Nowosad, Jakub. 2024. <span>“Optimizing the Parameters of the Spatial
Kinetic Ising Model to Simulate Spatial Patterns.”</span> January 7. <a href="https://doi.org/10.59350/m3r98-ehb46">https://doi.org/10.59350/m3r98-ehb46</a>.
</div></div></section></div> ]]></description>
  <category>spatial</category>
  <category>geocompr</category>
  <category>rstats</category>
  <category>simulations</category>
  <category>landscape-ecology</category>
  <category>spatial-patterns</category>
  <category>ising</category>
  <category>sil</category>
  <guid>https://jakubnowosad.com/posts/2024-01-07-spatialising-bp2/</guid>
  <pubDate>Sun, 07 Jan 2024 00:00:00 GMT</pubDate>
</item>
<item>
  <title>Simulating spatial patterns with the spatial kinetic Ising model</title>
  <dc:creator>Jakub Nowosad</dc:creator>
  <link>https://jakubnowosad.com/posts/2023-12-17-spatialising-bp1/</link>
  <description><![CDATA[ 





<p>A two-dimensional Ising model is an idealized physical system that consists of a lattice of binary variables (<em>magnetic spins</em>) that can be in one of two states: up or down. Each spin’s state is influenced by its neighbors: the more neighbors in the same state, the more likely the spin will be in the same state. Thus, the change in the state of a spin impacts the state of its neighbors, which in turn affects the state of their neighbors, and so on. <!-- When the system's temperature is high, the spins are more likely to change their state, while when the temperature is low, they are more likely to keep their state. --> It is a simple model that can be used in physics to study, for example, phase transitions given by the temperature of the system.</p>
<p>The above idea can be, in principle, applied to other two-dimensional systems, such as geographical (<em>spatial</em>) data. For example, we can think of a binary spatial raster as a two-dimensional system, where each cell can have one of two states: <code>-1</code> or <code>1</code>. These numbers can represent, for example, two land cover categories: forest and non-forest. Next, we can introduce two parameters influencing the state of each cell: <code>B</code> and <code>J</code>. <code>B</code> is an external pressure: it tries to align cells’ values with its sign: are we more likely to have more <code>-1</code> or <code>1</code> values? <code>J</code> is a strength of the local autocorrelation tendency—it tries to align signs of neighboring cells: are we more likely to have more cells of the same value in the neighborhood? The spatial kinetic Ising model is a simulation of such a system, where each cell is given <em>an opportunity</em> to flip its value (<code>1</code> to <code>-1</code> or <code>-1</code> to <code>1</code>). The probability of a flip depends on the value of the cell, the values of its four neighbors (top, left, bottom, and right), and the values of <code>B</code> and <code>J</code>.</p>
<p>This blog post shows how to use the spatial kinetic Ising model to simulate the change in the spatial pattern of a binary raster using the <strong>spatialising</strong> R package. The package is available on GitHub at <a href="https://github.com/Nowosad/spatialising/" class="uri">https://github.com/Nowosad/spatialising/</a>.</p>
<section id="setup" class="level1">
<h1>Setup</h1>
<p>To reproduce the calculations in the following post, you need to attach the following packages:</p>
<div class="cell" data-layout-align="center">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(terra)</span>
<span id="cb1-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(spatialising)</span>
<span id="cb1-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(ggplot2)</span></code></pre></div></div>
</div>
</section>
<section id="data-preparation" class="level1">
<h1>Data preparation</h1>
<p>The <code>twomaps.tif</code> file contains two maps of the same area, but for different years. The first map represents the year 1, and the second map represents the year 5. Both of them have only two values: <code>1</code> for non-forest and <code>2</code> for forest:</p>
<div class="cell" data-layout-align="center" data-crop="true">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb2-1">twomaps <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rast</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"/vsicurl/https://github.com/Nowosad/bp-data/raw/main/spatialising-bp/twomaps.tif"</span>)</span>
<span id="cb2-2">map1 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> twomaps[[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>]]</span>
<span id="cb2-3">map2 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> twomaps[[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>]]</span>
<span id="cb2-4"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">plot</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(map1, map2))</span></code></pre></div></div>
<div class="cell-output-display">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://jakubnowosad.com/posts/2023-12-17-spatialising-bp1/index_files/figure-html/unnamed-chunk-5-1.png" class="img-fluid quarto-figure quarto-figure-center figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<p>The spatial kinetic Ising model requires binary raster data with just two values: <code>-1</code> and <code>1</code>. Thus, the first step in our case is to reclassify the original binary (<code>1</code>, <code>2</code>) map into new (<code>-1</code>, <code>1</code>) values. This can be done with the <code>classify()</code> function from the <strong>terra</strong> package.<sup>1</sup> Here, we replace the <code>1</code> value with <code>-1</code> and the <code>2</code> value with <code>1</code>:</p>
<div class="cell" data-layout-align="center" data-crop="true">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb3-1">rcl <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">matrix</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">byrow =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ncol =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>)</span>
<span id="cb3-2">map1 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">classify</span>(map1, rcl)</span>
<span id="cb3-3">map2 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">classify</span>(map2, rcl)</span>
<span id="cb3-4"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">plot</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(map1, map2))</span></code></pre></div></div>
<div class="cell-output-display">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://jakubnowosad.com/posts/2023-12-17-spatialising-bp1/index_files/figure-html/unnamed-chunk-6-1.png" class="img-fluid quarto-figure quarto-figure-center figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
</section>
<section id="running-the-spatial-kinetic-ising-model" class="level1">
<h1>Running the spatial kinetic Ising model</h1>
<p>Now, our data is ready for use in the <strong>spatialising</strong> package. Its main function is <code>kinetic_ising()</code>, which simulates the spatial kinetic Ising model. It requires the input raster (<code>x</code>), the strength of the external pressure (<code>B</code>), the strength of the local autocorrelation tendency (<code>J</code>), and also has some optional arguments, such as the number of updates.</p>
<p>Our goal is to simulate the change in the spatial pattern of the first map (<code>map1</code>) to make it similar to the second map (<code>map2</code>). The code below simulates the spatial kinetic Ising model for the first map (<code>map1</code>) with the value of <code>B</code> of 0.3 (meaning that the external pressure is toward increased forest cover) and the value of <code>J</code> of 0.7 (meaning that the local autocorrelation tendency is strong). We also set the number of updates to 4, which means that it will create four simulations (rasters), each of which will be based on the previous one.<sup>2</sup></p>
<div class="cell" data-layout-align="center" data-crop="true">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb4-1">sim1 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">kinetic_ising</span>(map1, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">B =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.3</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">J =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.7</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">updates =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>)</span>
<span id="cb4-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">plot</span>(sim1, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">nr =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span></code></pre></div></div>
<div class="cell-output-display">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://jakubnowosad.com/posts/2023-12-17-spatialising-bp1/index_files/figure-html/unnamed-chunk-7-1.png" class="img-fluid quarto-figure quarto-figure-center figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<p>The result consists of four simulated rasters, which are stored in the <code>sim1</code> object. Each of them represents successive simulations of the spatial kinetic Ising model.</p>
<p>We can also compare the final simulation (<code>sim1[[4]]</code>) with the first (<code>map1</code>) and the second map (<code>map2</code>).</p>
<div class="cell" data-layout-align="center" data-crop="true">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb5-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">plot</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(map1, map2, sim1[[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>]]), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">nr =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span></code></pre></div></div>
<div class="cell-output-display">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://jakubnowosad.com/posts/2023-12-17-spatialising-bp1/index_files/figure-html/unnamed-chunk-8-1.png" class="img-fluid quarto-figure quarto-figure-center figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<p>Compared to the first map, the last simulation has slightly more forest cover, which is in line with the provided external pressure (<code>B</code>) toward increased forest cover. The forest category also tends to be spatially clustered, which is in line with the set local autocorrelation tendency (<code>J</code>). On the other hand, the simulation is still quite different from the second map (<code>map2</code>), possibly indicating that we should increase the value of <code>B</code> to make the simulation more like the second map.</p>
</section>
<section id="metrics-of-spatial-patterns" class="level1">
<h1>Metrics of spatial patterns</h1>
<p>The <strong>spatialising</strong> package also provides two functions to calculate metrics of spatial patterns of binary rasters: <code>composition_index()</code> and <code>texture_index()</code>. The composition imbalance index (<code>composition_index()</code>) is a sum of cell’s values over the entire site divided by the number of cells in the site. It has a range from -1 (site completely dominated by the -1 values) to 1 (site completely dominated by the 1 values). The value of 0 indicates that the site is equally divided between the two values.</p>
<div class="cell" data-layout-align="center">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb6" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb6-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">composition_index</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(map1, map2, sim1[[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>]]))</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] -0.5000  0.5000 -0.4184</code></pre>
</div>
</div>
<p>In our case, <code>map1</code> has a dominance of the <code>-1</code> values, <code>map2</code> has a dominance of the <code>1</code> values, and <code>sim1[[4]]</code> has a dominance of the <code>-1</code> values, but it is less pronounced than in <code>map1</code>.</p>
<p>The texture index (<code>texture_index()</code>) is a measure of the spatial autocorrelation of the values of a raster. Its value is between 0 (fine texture), and 1 (coarse texture).</p>
<div class="cell" data-layout-align="center">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb8" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb8-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">texture_index</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(map1, map2, sim1[[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>]]))</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 0.6477551 0.6216327 0.8387755</code></pre>
</div>
</div>
<p>In our examples, <code>map1</code> and <code>map2</code> have a rather similar texture, while <code>sim1[[4]]</code> has a slightly coarser texture (its values have stronger spatial autocorrelation).</p>
</section>
<section id="spatial-kinetic-ising-model-explained" class="level1">
<h1>Spatial kinetic Ising model explained</h1>
<p>How does the spatial kinetic Ising model work? The simulation starts with the input binary (<code>-1</code>, <code>1</code>) raster and proceeds with one randomly selected cell at a time. The selected cell is given <em>an opportunity</em> to flip its value (<code>1</code> to <code>-1</code> or <code>-1</code> to <code>1</code>). The probability of a flip depends on the value of the cell and the values of its four neighbors (top, left, bottom, and right). It also depends on the values of <code>B</code> and <code>J</code>. <code>B</code> (positive or negative) is an external pressure: it tries to align cells’ values with its sign. <code>J</code> (always positive) is a strength of the local autocorrelation tendency: it tries to align signs of neighboring cells.</p>
<p>We can also control the model using a few additional arguments. The <code>iter</code> argument controls the number of iterations—how many times the flip of a cell value is attempted before a new simulated raster is returned. By default, its value equals to the number of cells in the input raster. Next, <code>updates</code> controls the number of simulated rasters returned—each of which is based on the previous one. The <code>inertia</code> parameter (<code>0</code>, by default), when positive makes it less likely for a cell of <code>-1</code> to change its value to <code>1</code> when surrounded by other <code>-1</code> cells. As the effect, it minimizes the possibility of a “salt and pepper” effect, where cells of different values are mixed together. The last important argument is <code>rule</code>, which controls how the probability of a flip is calculated: either using the <code>"glauber"</code> (default) or <code>"metropolis"</code> rule.</p>
<p>The code below compares the results of the spatial kinetic Ising model for different values of <code>B</code> and <code>J</code>.</p>
<div class="cell" data-layout-align="center">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb10" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb10-1">sim2 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">kinetic_ising</span>(map1, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">B =</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.7</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">J =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.1</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">updates =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">inertia =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span>
<span id="cb10-2">sim3 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">kinetic_ising</span>(map1, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">B =</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.7</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">J =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.7</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">updates =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">inertia =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span>
<span id="cb10-3">sim4 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">kinetic_ising</span>(map1, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">B =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">J =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.1</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">updates =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">inertia =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span>
<span id="cb10-4">sim5 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">kinetic_ising</span>(map1, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">B =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">J =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.7</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">updates =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">inertia =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span>
<span id="cb10-5">sim6 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">kinetic_ising</span>(map1, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">B =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.7</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">J =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.1</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">updates =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">inertia =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span>
<span id="cb10-6">sim7 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">kinetic_ising</span>(map1, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">B =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.7</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">J =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.7</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">updates =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">inertia =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span>
<span id="cb10-7">all_sims <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(sim2[[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>]], sim3[[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>]], sim4[[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>]], sim5[[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>]], sim6[[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>]], sim7[[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>]])</span>
<span id="cb10-8"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">names</span>(all_sims) <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"B: -0.7, J: 0.1"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"B: -0.7, J: 0.7"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"B: 0, J: 0.1"</span>, </span>
<span id="cb10-9">                    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"B: 0, J: 0.7"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"B: 0.7, J: 0.1"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"B: 0.7, J: 0.7"</span>)</span>
<span id="cb10-10"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">plot</span>(all_sims, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">nc =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>)</span></code></pre></div></div>
<div class="cell-output-display">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://jakubnowosad.com/posts/2023-12-17-spatialising-bp1/index_files/figure-html/unnamed-chunk-11-1.png" class="img-fluid quarto-figure quarto-figure-center figure-img" width="480"></p>
</figure>
</div>
</div>
</div>
<p>The top row shows the results for <code>B</code> values equal to <code>-0.7</code>, the middle row shows the results for <code>B</code> values equal to <code>0</code>, and the bottom row shows the results for <code>B</code> values equal to <code>0.7</code>. The left column shows the results of the spatial kinetic Ising model for values of <code>J</code> equal to <code>0.1</code>, while the right column shows the results for values of <code>J</code> equal to <code>0.7</code>.</p>
<p>Quick visual comparison underlines that both parameters have a strong effect on the results of the spatial kinetic Ising model. Negative values of <code>B</code> tend to decrease the forest cover, while positive values of <code>B</code> tend to increase the forest cover. The effect of <code>J</code> is, on the other hand, more related to the configuration of the values, with lower values of <code>J</code> leading to more dispersed values, and higher values of <code>J</code> leading to more clustered values.</p>
</section>
<section id="summary" class="level1">
<h1>Summary</h1>
<p>This blog post showed how to use the <strong>spatialising</strong> package to simulate the spatial kinetic Ising model, how selected parameters influence the results, and how to calculate metrics of spatial patterns. However, it leaves one important question unanswered: how do you find the best values of <code>B</code> and <code>J</code> to make the simulation more like the second map? That is the topic of <a href="../../posts/2024-01-07-spatialising-bp2/">the next blog post</a>.</p>
<p>To learn more about the spatial kinetic Ising model, I encourage you to read <span class="citation" data-cites="Stepinski2023">Stepinski (2023)</span> and <span class="citation" data-cites="spatialisingpaper">Stepinski and Nowosad (2023)</span>.</p>



</section>


<div id="quarto-appendix" class="default"><section class="quarto-appendix-contents" id="quarto-bibliography"><h2 class="anchored quarto-appendix-heading">References</h2><div id="refs" class="references csl-bib-body hanging-indent">
<div id="ref-Stepinski2023" class="csl-entry">
Stepinski, Tomasz F. 2023. <span>“Spatially Explicit Simulation of Deforestation Using the Ising-Like Neutral Model.”</span> <em>Environmental Research: Ecology</em> 2 (2): 025003. <a href="https://doi.org/10.1088/2752-664x/acdbd2">https://doi.org/10.1088/2752-664x/acdbd2</a>.
</div>
<div id="ref-spatialisingpaper" class="csl-entry">
Stepinski, Tomasz F., and Jakub Nowosad. 2023. <span>“The Kinetic Ising Model Encapsulates Essential Dynamics of Land Pattern Change.”</span> <em>Royal Society Open Science</em> 10 (10): 231005. <a href="https://doi.org/10.1098/rsos.231005">https://doi.org/10.1098/rsos.231005</a>.
</div>
</div></section><section id="footnotes" class="footnotes footnotes-end-of-document"><h2 class="anchored quarto-appendix-heading">Footnotes</h2>

<ol>
<li id="fn1"><p>This function can also be used to binarize continuous data or data with many categories.↩︎</p></li>
<li id="fn2"><p>Here, we can think of each simulation as a year, and the number of updates as the number of years.↩︎</p></li>
</ol>
</section><section class="quarto-appendix-contents" id="quarto-reuse"><h2 class="anchored quarto-appendix-heading">Reuse</h2><div class="quarto-appendix-contents"><div><a rel="license" href="https://creativecommons.org/licenses/by/4.0/">CC BY 4.0</a></div></div></section><section class="quarto-appendix-contents" id="quarto-citation"><h2 class="anchored quarto-appendix-heading">Citation</h2><div><div class="quarto-appendix-secondary-label">BibTeX citation:</div><div class="code-copy-outer-scaffold"><pre class="sourceCode quarto-appendix-bibtex code-with-copy"><code class="sourceCode bibtex">@online{nowosad2023,
  author = {Nowosad, Jakub},
  title = {Simulating Spatial Patterns with the Spatial Kinetic {Ising}
    Model},
  date = {2023-12-17},
  url = {https://jakubnowosad.com/posts/2023-12-17-spatialising-bp1/},
  doi = {10.59350/6chwk-cq915},
  langid = {en}
}
</code></pre></div><div class="quarto-appendix-secondary-label">For attribution, please cite this work as:</div><div id="ref-nowosad2023" class="csl-entry quarto-appendix-citeas">
Nowosad, Jakub. 2023. <span>“Simulating Spatial Patterns with the
Spatial Kinetic Ising Model.”</span> December 17. <a href="https://doi.org/10.59350/6chwk-cq915">https://doi.org/10.59350/6chwk-cq915</a>.
</div></div></section></div> ]]></description>
  <category>spatial</category>
  <category>geocompr</category>
  <category>rstats</category>
  <category>simulations</category>
  <category>landscape-ecology</category>
  <category>spatial-patterns</category>
  <category>ising</category>
  <category>sil</category>
  <guid>https://jakubnowosad.com/posts/2023-12-17-spatialising-bp1/</guid>
  <pubDate>Sun, 17 Dec 2023 00:00:00 GMT</pubDate>
</item>
<item>
  <title>Finding the most unique land cover spatial pattern</title>
  <dc:creator>Jakub Nowosad</dc:creator>
  <link>https://jakubnowosad.com/posts/2023-12-03-motif-bp8/</link>
  <description><![CDATA[ 





<p>Spatial signatures represent spatial patterns of land cover in a given area. Thus, they can be used to <a href="https://jakubnowosad.com/posts/2021-02-17-motif-bp3/">search for areas with similar spatial patterns to a query region</a> or to <a href="https://jakubnowosad.com/posts/2021-02-24-motif-bp4">quantify changes in spatial patterns</a>. The approaches above are implemented as <code>lsp_search()</code> and <code>lsp_compare()</code> functions of the <strong>motif</strong> R package, respectively.</p>
<p>At the same time, it is possible to create other, more customized workflows. Here, I will show how to compare spatial patterns of two different areas and find the most unique land cover spatial pattern in the process.</p>
<section id="spatial-data" class="level1">
<h1>Spatial data</h1>
<p>To reproduce the calculations in the following post, you need to download all of the relevant datasets using the code below:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(osfr)</span>
<span id="cb1-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">dir.create</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"data"</span>)</span>
<span id="cb1-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">osf_retrieve_node</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"xykzv"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb1-4">        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">osf_ls_files</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">n_max =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">Inf</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb1-5">        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">osf_download</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">path =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"data"</span>,</span>
<span id="cb1-6">                     <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">conflicts =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"skip"</span>)</span></code></pre></div></div>
</div>
<p>You should also attach the following packages:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb2-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(sf)</span>
<span id="cb2-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(terra)</span>
<span id="cb2-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(motif)</span>
<span id="cb2-4"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(dplyr)</span>
<span id="cb2-5"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(readr)</span>
<span id="cb2-6"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(cluster)</span></code></pre></div></div>
</div>
</section>
<section id="land-cover-in-africa" class="level1">
<h1>Land cover in Africa</h1>
<p>The <code>data/land_cover.tif</code> contains land cover data for Africa. It is a categorical raster of the 300-meter resolution that can be read into R using the <code>rast()</code> function.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb3-1">lc <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rast</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"data/land_cover.tif"</span>)</span></code></pre></div></div>
</div>
<p>Additionally, the <code>data/lc_palette.csv</code> file contains information about the labels and colors of each land cover category.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb4-1">lc_palette_df <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">read.csv</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"data/lc_palette.csv"</span>)</span></code></pre></div></div>
</div>
<p>We will use this file to integrate labels and colors into the raster object:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb5-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">levels</span>(lc) <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> lc_palette_df[<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"value"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"label"</span>)]</span>
<span id="cb5-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">coltab</span>(lc) <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> lc_palette_df[<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"value"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"color"</span>)]</span>
<span id="cb5-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">plot</span>(lc)</span></code></pre></div></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://jakubnowosad.com/posts/2023-12-03-motif-bp8/index_files/figure-html/unnamed-chunk-5-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
</section>
<section id="comparing-spatial-patterns-of-two-areas" class="level1">
<h1>Comparing spatial patterns of two areas</h1>
<p>First, we need to define the areas for which we want to compare spatial patterns. For the example purpose, we use two African countries: Cameroon and Congo. We can download their areas using the <strong>rnaturalearth</strong> package, and use them to crop the <code>lc</code> raster object to their borders:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb6" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb6-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(rnaturalearth)</span>
<span id="cb6-2"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># download</span></span>
<span id="cb6-3">cameroon <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ne_countries</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">country =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Cameroon"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">returnclass =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"sf"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb6-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">select</span>(name) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb6-5">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">st_transform</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">crs =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">st_crs</span>(lc))</span>
<span id="cb6-6">congo <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ne_countries</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">country =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Republic of the Congo"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">returnclass =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"sf"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb6-7">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">select</span>(name) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb6-8">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">st_transform</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">crs =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">st_crs</span>(lc))</span>
<span id="cb6-9"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># crop</span></span>
<span id="cb6-10">lc_cameroon <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">crop</span>(lc, cameroon, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mask =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>)</span>
<span id="cb6-11">lc_congo <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">crop</span>(lc, congo, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mask =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>)</span>
<span id="cb6-12"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># plot</span></span>
<span id="cb6-13"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">plot</span>(lc_cameroon)</span>
<span id="cb6-14"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">plot</span>(lc_congo)</span></code></pre></div></div>
<div class="cell quarto-layout-panel" data-layout-ncol="2">
<div class="quarto-layout-row">
<div class="quarto-layout-cell" style="flex-basis: 50.0%;justify-content: center;">
<p><img src="https://jakubnowosad.com/posts/2023-12-03-motif-bp8/index_files/figure-html/unnamed-chunk-6-1.png" class="img-fluid" width="672"></p>
</div>
<div class="quarto-layout-cell" style="flex-basis: 50.0%;justify-content: center;">
<p><img src="https://jakubnowosad.com/posts/2023-12-03-motif-bp8/index_files/figure-html/unnamed-chunk-6-2.png" class="img-fluid" width="672"></p>
</div>
</div>
</div>
<p>Both countries have similar shares of land cover categories, with the domination of forests and some agricultural and grassland areas, as we can see by calculating their <code>"composition"</code> signatures.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb7" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb7-1">lc_cameroon_composition <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lsp_signature</span>(lc_cameroon, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">type =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"composition"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">classes =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">9</span>)</span>
<span id="cb7-2">lc_congo_composition <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lsp_signature</span>(lc_congo, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">type =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"composition"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">classes =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">9</span>)</span>
<span id="cb7-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">round</span>(lc_cameroon_composition<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>signature[[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>]], <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>        1    2    3 4 5    6 7 8    9
[1,] 0.15 0.77 0.03 0 0 0.04 0 0 0.01</code></pre>
</div>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb9" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb9-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">round</span>(lc_congo_composition<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>signature[[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>]], <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>       1    2    3 4 5    6 7 8 9
[1,] 0.1 0.82 0.04 0 0 0.03 0 0 0</code></pre>
</div>
</div>
<p>We can also look at their spatial patterns (both composition and configuration) by calculating the <code>"cove"</code> signature.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb11" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb11-1">lc_cameroon_cove <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lsp_signature</span>(lc_cameroon, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">type =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"cove"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">classes =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">9</span>)</span>
<span id="cb11-2">lc_congo_cove <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lsp_signature</span>(lc_congo, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">type =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"cove"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">classes =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">9</span>)</span></code></pre></div></div>
</div>
<p>Next, these signatures can be compared using dissimilarity measures. The <strong>philentropy</strong> package provides a wide range of such measures, including the Jensen-Shannon divergence. Here, we use this measure to calculate the dissimilarity between the spatial patterns (as represented with <code>"cove"</code>) of Cameroon and Congo.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb12" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb12-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(philentropy)</span>
<span id="cb12-2">dist_cove <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">dist_one_one</span>(lc_cameroon_cove<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>signature[[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>]], </span>
<span id="cb12-3">                         lc_congo_cove<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>signature[[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>]], </span>
<span id="cb12-4">                         <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">method =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"jensen-shannon"</span>)</span>
<span id="cb12-5">dist_cove</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 0.008919291</code></pre>
</div>
</div>
<p>This value is small (approximately 0.009), which means that, in general, Cameroon’s and Congo’s spatial patterns are fairly similar.</p>
</section>
<section id="comparing-local-spatial-patterns" class="level1">
<h1>Comparing local spatial patterns</h1>
<p>We can also look at the local spatial patterns of Cameroon and Congo, here on a scale of 100 by 100 cells (i.e., 30 by 30 km):</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb14" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb14-1">lc_cameroon_cove100 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lsp_signature</span>(lc_cameroon, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">type =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"cove"</span>,</span>
<span id="cb14-2">                                    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">window =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">classes =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">9</span>)</span>
<span id="cb14-3">lc_congo_cove100 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lsp_signature</span>(lc_congo, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">type =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"cove"</span>, </span>
<span id="cb14-4">                                 <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">window =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">classes =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">9</span>)</span></code></pre></div></div>
</div>
<p>To compare these signatures, we can calculate the Jensen-Shannon divergence for each pair of signatures in both datasets. This can be done using the <code>dist_many_many()</code> function from the <strong>philentropy</strong> package, which expects two matrices as input.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb15" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb15-1">lc_cameroon_cove100_mat <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">do.call</span>(rbind, lc_cameroon_cove100<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>signature)</span>
<span id="cb15-2">lc_congo_cove100_mat <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">do.call</span>(rbind, lc_congo_cove100<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>signature)</span>
<span id="cb15-3">dist_cove_100 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">dist_many_many</span>(lc_cameroon_cove100_mat, </span>
<span id="cb15-4">                               lc_congo_cove100_mat, </span>
<span id="cb15-5">                               <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">method =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"jensen-shannon"</span>)</span></code></pre></div></div>
</div>
<p>The result is a matrix with the Jensen-Shannon divergence between each pair of areas in both countries, in which rows represent areas in Cameroon and columns represent areas in Congo. Lower values indicate more similar spatial patterns, while higher values indicate more dissimilar spatial patterns. This matrix shows that there are some areas with similar spatial patterns in both countries, and some are even identical (given the source data scale/resolution and scope/number and variety of categories):</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb16" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb16-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">summary</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(dist_cove_100))</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
0.00000 0.03228 0.08722 0.16375 0.22835 0.69315 </code></pre>
</div>
</div>
<p>Identifiers of the identical areas can be found using the <code>which()</code> function. For example, area <code>341</code> in Cameroon and area <code>4</code> in Congo have the same spatial pattern:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb18" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb18-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">head</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">which</span>(dist_cove_100 <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">arr.ind =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>))</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>     row col
[1,] 341   4
[2,] 418   4
[3,] 423   4
[4,] 440   4
[5,] 462   4
[6,] 477   4</code></pre>
</div>
</div>
<p>We can add spatial information to the <code>lc_cameroon_cove100</code> and <code>lc_congo_cove100</code> objects using the <code>lsp_add_sf()</code> function. Then, we are able to visualize these areas by cropping the land cover data using the new objects. In this case, both areas are fully covered by forest (although the second one is located at the border, and thus contains some NA values).</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb20" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb20-1">lc_cameroon_cove100_sf <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lsp_add_sf</span>(lc_cameroon_cove100)</span>
<span id="cb20-2">lc_congo_cove100_sf <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lsp_add_sf</span>(lc_congo_cove100)</span>
<span id="cb20-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">plot</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">crop</span>(lc_cameroon, lc_cameroon_cove100_sf[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">341</span>, ]), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">main =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Cameroon"</span>)</span>
<span id="cb20-4"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">plot</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">crop</span>(lc_congo, lc_congo_cove100_sf[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>, ]), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">main =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Congo"</span>)</span></code></pre></div></div>
<div class="cell quarto-layout-panel" data-layout-ncol="2">
<div class="quarto-layout-row">
<div class="quarto-layout-cell" style="flex-basis: 50.0%;justify-content: center;">
<p><img src="https://jakubnowosad.com/posts/2023-12-03-motif-bp8/index_files/figure-html/unnamed-chunk-14-1.png" class="img-fluid" width="672"></p>
</div>
<div class="quarto-layout-cell" style="flex-basis: 50.0%;justify-content: center;">
<p><img src="https://jakubnowosad.com/posts/2023-12-03-motif-bp8/index_files/figure-html/unnamed-chunk-14-2.png" class="img-fluid" width="672"></p>
</div>
</div>
</div>
</section>
<section id="grouping-areas-with-similar-local-spatial-patterns" class="level1">
<h1>Grouping areas with similar local spatial patterns</h1>
<p>We can group areas with similar spatial patterns of land cover using the <code>pam()</code> function from the <strong>cluster</strong> package. For this example, we will divide the areas into six groups.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb21" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb21-1">my_pam <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">pam</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rbind</span>(lc_cameroon_cove100_mat, lc_congo_cove100_mat), <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">6</span>)</span></code></pre></div></div>
</div>
<p>Next, we can add the clustering results to the spatial object by naming both existing <code>sf</code> objects, combining them into one, and adding the clustering results as a new column.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb22" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb22-1">lc_cameroon_cove100_sf<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>name <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Cameroon"</span></span>
<span id="cb22-2">lc_congo_cove100_sf<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>name <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Congo"</span></span>
<span id="cb22-3">lc_cove100_sf <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rbind</span>(lc_cameroon_cove100_sf, lc_congo_cove100_sf)</span>
<span id="cb22-4">lc_cove100_sf<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>k <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.factor</span>(my_pam<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>clustering)</span></code></pre></div></div>
</div>
<p>Visualization of the results is shown below:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb23" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb23-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">plot</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">subset</span>(lc_cove100_sf, name <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Cameroon"</span>)[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"k"</span>], <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">pal =</span> palette.colors, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">main =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Cameroon"</span>)</span>
<span id="cb23-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">plot</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">subset</span>(lc_cove100_sf, name <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Congo"</span>)[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"k"</span>],  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">pal =</span> palette.colors, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">main =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Congo"</span>)</span></code></pre></div></div>
<div class="cell quarto-layout-panel" data-layout-ncol="2">
<div class="quarto-layout-row">
<div class="quarto-layout-cell" style="flex-basis: 50.0%;justify-content: center;">
<p><img src="https://jakubnowosad.com/posts/2023-12-03-motif-bp8/index_files/figure-html/unnamed-chunk-17-1.png" class="img-fluid" width="672"></p>
</div>
<div class="quarto-layout-cell" style="flex-basis: 50.0%;justify-content: center;">
<p><img src="https://jakubnowosad.com/posts/2023-12-03-motif-bp8/index_files/figure-html/unnamed-chunk-17-2.png" class="img-fluid" width="672"></p>
</div>
</div>
</div>
<p>You may quickly notice that the sixth and fifth clusters exist prominently in both countries. On the other hand, cluster 2 only exists in Cameroon.</p>
<p>We can look at each cluster representative by subsetting the <code>lc_cove100_sf</code> object using the <code>id.med</code> column from the <code>my_pam</code> object.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb24" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb24-1">lc_cove100_sf_subset <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> lc_cove100_sf[my_pam<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>id.med, ]</span>
<span id="cb24-2"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> (i <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">seq_len</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">nrow</span>(lc_cove100_sf_subset))){</span>
<span id="cb24-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">plot</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">crop</span>(lc, lc_cove100_sf_subset[i, ]), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">main =</span> i)</span>
<span id="cb24-4">}</span></code></pre></div></div>
<div class="cell quarto-layout-panel" data-layout-ncol="2">
<div class="quarto-layout-row">
<div class="quarto-layout-cell" style="flex-basis: 50.0%;justify-content: center;">
<p><img src="https://jakubnowosad.com/posts/2023-12-03-motif-bp8/index_files/figure-html/unnamed-chunk-18-1.png" class="img-fluid" width="672"></p>
</div>
<div class="quarto-layout-cell" style="flex-basis: 50.0%;justify-content: center;">
<p><img src="https://jakubnowosad.com/posts/2023-12-03-motif-bp8/index_files/figure-html/unnamed-chunk-18-2.png" class="img-fluid" width="672"></p>
</div>
</div>
<div class="quarto-layout-row">
<div class="quarto-layout-cell" style="flex-basis: 50.0%;justify-content: center;">
<p><img src="https://jakubnowosad.com/posts/2023-12-03-motif-bp8/index_files/figure-html/unnamed-chunk-18-3.png" class="img-fluid" width="672"></p>
</div>
<div class="quarto-layout-cell" style="flex-basis: 50.0%;justify-content: center;">
<p><img src="https://jakubnowosad.com/posts/2023-12-03-motif-bp8/index_files/figure-html/unnamed-chunk-18-4.png" class="img-fluid" width="672"></p>
</div>
</div>
<div class="quarto-layout-row">
<div class="quarto-layout-cell" style="flex-basis: 50.0%;justify-content: center;">
<p><img src="https://jakubnowosad.com/posts/2023-12-03-motif-bp8/index_files/figure-html/unnamed-chunk-18-5.png" class="img-fluid" width="672"></p>
</div>
<div class="quarto-layout-cell" style="flex-basis: 50.0%;justify-content: center;">
<p><img src="https://jakubnowosad.com/posts/2023-12-03-motif-bp8/index_files/figure-html/unnamed-chunk-18-6.png" class="img-fluid" width="672"></p>
</div>
</div>
</div>
<p>Cluster 6 represents forest areas, and cluster 4 consists of areas predominantly covered by forests and some agricultural and grassland areas. Cluster 5 is represented by forest, but with a substantial share of agriculture and grasslands and cluster 1 is a mix of highly aggregated agriculture and forest. Cluster 3 are areas with a large share of shrublands, agricultural and forest areas. Finally, cluster 2, which only exists in Cameroon, represents large (30 by 30 km) areas of agriculture.</p>
</section>
<section id="finding-the-most-unique-land-cover-spatial-pattern" class="level1">
<h1>Finding the most unique land cover spatial pattern</h1>
<p>The <code>dist_cove_100</code> object contains the Jensen-Shannon divergence between each pair of areas in both countries, where rows represent areas in Cameroon and columns represent areas in Congo. Usually, it may be used to find the most similar areas (areas with the smallest divergence), but here, we will look for the most unique areas.</p>
<p>This can be done in two steps. First, we need to calculate the smallest value in each row and column, which can be done using the <code>apply()</code> function. This allows us to find what is the smallest divergence between each area in Cameroon and Congo; in other words, how dissimilar is an area in one country to the most similar area in the other country.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb25" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb25-1">lc_cameroon_cove100_sf<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>min_dist <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">apply</span>(dist_cove_100, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, min)</span>
<span id="cb25-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">plot</span>(lc_cameroon_cove100_sf[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"min_dist"</span>], <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">main =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Cameroon"</span>)</span>
<span id="cb25-3">lc_congo_cove100_sf<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>min_dist <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">apply</span>(dist_cove_100, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, min)</span>
<span id="cb25-4"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">plot</span>(lc_congo_cove100_sf[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"min_dist"</span>], <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">main =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Congo"</span>)</span></code></pre></div></div>
<div class="cell quarto-layout-panel" data-layout-ncol="2">
<div class="quarto-layout-row">
<div class="quarto-layout-cell" style="flex-basis: 50.0%;justify-content: center;">
<p><img src="https://jakubnowosad.com/posts/2023-12-03-motif-bp8/index_files/figure-html/unnamed-chunk-19-1.png" class="img-fluid" width="672"></p>
</div>
<div class="quarto-layout-cell" style="flex-basis: 50.0%;justify-content: center;">
<p><img src="https://jakubnowosad.com/posts/2023-12-03-motif-bp8/index_files/figure-html/unnamed-chunk-19-2.png" class="img-fluid" width="672"></p>
</div>
</div>
</div>
<p>Second, we can find the area with the largest value in the <code>lc_cameroon_cove100_sf$min_dist</code> column, which is the most unique area in Cameroon, and the area with the largest value in <code>lc_congo_cove100_sf$min_dist</code>, which is the most unique area in Congo. In other words, these areas are the most dissimilar to any area in the other country.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb26" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb26-1">most_unique_cameroon <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> lc_cameroon_cove100_sf[<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">which.max</span>(lc_cameroon_cove100_sf<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>min_dist), ]</span>
<span id="cb26-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">plot</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">crop</span>(lc_cameroon, most_unique_cameroon), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">main =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Cameroon"</span>)</span>
<span id="cb26-3">most_unique_congo <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> lc_congo_cove100_sf[<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">which.max</span>(lc_congo_cove100_sf<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>min_dist), ]</span>
<span id="cb26-4"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">plot</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">crop</span>(lc_congo, most_unique_congo), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">main =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Congo"</span>)</span></code></pre></div></div>
<div class="cell preview-image quarto-layout-panel" data-layout-ncol="2">
<div class="quarto-layout-row">
<div class="quarto-layout-cell" style="flex-basis: 50.0%;justify-content: center;">
<p><img src="https://jakubnowosad.com/posts/2023-12-03-motif-bp8/index_files/figure-html/unnamed-chunk-20-1.png" class="img-fluid" width="672"></p>
</div>
<div class="quarto-layout-cell" style="flex-basis: 50.0%;justify-content: center;">
<p><img src="https://jakubnowosad.com/posts/2023-12-03-motif-bp8/index_files/figure-html/unnamed-chunk-20-2.png" class="img-fluid" width="672"></p>
</div>
</div>
</div>
<p>In the case of Cameroon, such an area is a mosaic of agriculture and grasslands; for Congo, it is a complex area with grasslands, agriculture, forest, and some shrublands. Interestingly, both areas are located at the border of the countries.<sup>1</sup></p>
</section>
<section id="summary" class="level1">
<h1>Summary</h1>
<p>In this post, we have seen how to compare spatial patterns of land cover in two different areas. It also showed how to find the most unique land cover spatial pattern (try to find the most unique area in your country as compared to the rest of the world!) This approach can be used to find areas with unique spatial land cover patterns or any other categorical rasters. To learn more about the <strong>motif</strong> package, see the other blog posts in the <a href="https://jakubnowosad.com/posts.html#category=motif">“motif” category</a>.</p>


</section>


<div id="quarto-appendix" class="default"><section id="footnotes" class="footnotes footnotes-end-of-document"><h2 class="anchored quarto-appendix-heading">Footnotes</h2>

<ol>
<li id="fn1"><p>You could change the <code>threshold</code> parameter in <code>lsp_signature()</code> to 0 to only include areas completely inside the countries’ borders.↩︎</p></li>
</ol>
</section><section class="quarto-appendix-contents" id="quarto-reuse"><h2 class="anchored quarto-appendix-heading">Reuse</h2><div class="quarto-appendix-contents"><div><a rel="license" href="https://creativecommons.org/licenses/by/4.0/">CC BY 4.0</a></div></div></section><section class="quarto-appendix-contents" id="quarto-citation"><h2 class="anchored quarto-appendix-heading">Citation</h2><div><div class="quarto-appendix-secondary-label">BibTeX citation:</div><div class="code-copy-outer-scaffold"><pre class="sourceCode quarto-appendix-bibtex code-with-copy"><code class="sourceCode bibtex">@online{nowosad2023,
  author = {Nowosad, Jakub},
  title = {Finding the Most Unique Land Cover Spatial Pattern},
  date = {2023-12-03},
  url = {https://jakubnowosad.com/posts/2023-12-03-motif-bp8/},
  doi = {10.59350/jdft5-8q15},
  langid = {en}
}
</code></pre></div><div class="quarto-appendix-secondary-label">For attribution, please cite this work as:</div><div id="ref-nowosad2023" class="csl-entry quarto-appendix-citeas">
Nowosad, Jakub. 2023. <span>“Finding the Most Unique Land Cover Spatial
Pattern.”</span> December 3. <a href="https://doi.org/10.59350/jdft5-8q15">https://doi.org/10.59350/jdft5-8q15</a>.
</div></div></section></div> ]]></description>
  <category>spatial</category>
  <category>geocompr</category>
  <category>rstats</category>
  <category>landscape-ecology</category>
  <category>spatial-patterns</category>
  <category>sil</category>
  <category>motif</category>
  <guid>https://jakubnowosad.com/posts/2023-12-03-motif-bp8/</guid>
  <pubDate>Sun, 03 Dec 2023 00:00:00 GMT</pubDate>
</item>
<item>
  <title>Extracting information about spatial patterns from spatial signatures</title>
  <dc:creator>Jakub Nowosad</dc:creator>
  <link>https://jakubnowosad.com/posts/2023-11-18-motif-bp7/</link>
  <description><![CDATA[ 





<p>The spatial signatures of categorical rasters are a set of numbers that <a href="https://jakubnowosad.com/posts/2021-02-10-motif-bp2/">describe the spatial patterns of the provided variables</a>. Next, they allow for further operations such as <a href="https://jakubnowosad.com/posts/2021-03-10-motif-bp6/">searching, comparing, or clustering</a>. Less known is that they can also be used to extract information about the composition and configuration of spatial patterns. This blog post shows how to do it using the <strong>motif</strong> R package.</p>
<section id="spatial-data" class="level1">
<h1>Spatial data</h1>
<p>To reproduce the calculations in the following post, you need to download all of the relevant datasets using the code below:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(osfr)</span>
<span id="cb1-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">dir.create</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"data"</span>)</span>
<span id="cb1-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">osf_retrieve_node</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"xykzv"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb1-4">        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">osf_ls_files</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">n_max =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">Inf</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb1-5">        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">osf_download</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">path =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"data"</span>,</span>
<span id="cb1-6">                     <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">conflicts =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"skip"</span>)</span></code></pre></div></div>
</div>
<p>You should also attach the following packages:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb2-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(sf)</span>
<span id="cb2-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(terra)</span>
<span id="cb2-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(motif)</span>
<span id="cb2-4"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(dplyr)</span>
<span id="cb2-5"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(readr)</span>
<span id="cb2-6"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(cluster)</span>
<span id="cb2-7"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(ggplot2)</span></code></pre></div></div>
</div>
</section>
<section id="land-cover-in-africa" class="level1">
<h1>Land cover in Africa</h1>
<p>The <code>data/land_cover.tif</code> contains land cover data for Africa. It is a categorical raster of the 300-meter resolution that can be read into R using the <code>rast()</code> function.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb3-1">lc <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rast</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"data/land_cover.tif"</span>)</span></code></pre></div></div>
</div>
<p>Additionally, the <code>data/lc_palette.csv</code> file contains information about the labels and colors of each land cover category.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb4-1">lc_palette_df <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">read.csv</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"data/lc_palette.csv"</span>)</span></code></pre></div></div>
</div>
<p>We will use this file to integrate labels and colors into the raster object:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb5-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">levels</span>(lc) <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> lc_palette_df[<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"value"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"label"</span>)]</span>
<span id="cb5-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">coltab</span>(lc) <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> lc_palette_df[<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"value"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"color"</span>)]</span>
<span id="cb5-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">plot</span>(lc)</span></code></pre></div></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://jakubnowosad.com/posts/2023-11-18-motif-bp7/index_files/figure-html/unnamed-chunk-5-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
</section>
<section id="extracting-information-from-spatial-signatures" class="level1">
<h1>Extracting information from spatial signatures</h1>
<p>As already shown in the previous blog posts about <strong>motif</strong>, the <code>lsp_signature()</code> function can be used to extract spatial signatures from a categorical raster object that can be used to describe spatial patterns of land cover. The most fundamental signature is <em>co</em>-occurrence <em>ma</em>trix (<em>coma</em>), which is a matrix of co-occurrence frequencies of each pair of land cover categories. The <code>lsp_signature()</code> function can be used to extract the coma signature in 300 by 300 cells non-overlapping windows (i.e., 90 by 90 km) as follows:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb6" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb6-1">lc_coma <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lsp_signature</span>(lc, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">type =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"coma"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">window =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">300</span>)</span></code></pre></div></div>
</div>
<p>The output is a data frame with 3,843 rows and 3 columns. The most important one is the <code>signature</code> column, which contains <em>coma</em> signatures in each window.</p>
<p>The co-occurrence matrix can be thought of as a compression of information about the composition and configuration of land cover categories in a given window. However, as it consists of many numbers (here, 81), it is not easy to directly analyze, visualize, or interpret. Gladly, we can further extract information from this signature using metrics from the information theory, such as marginal entropy and relative mutual information (for more details, see <a href="http://dx.doi.org/10.1007/s10980-019-00830-x"><span class="citation" data-cites="Nowosad2019">Nowosad and Stepinski (<span>2019</span>)</span></a> and the <a href="https://jakubnowosad.com/posts/2019-06-25-ent-bp1/">“Information theory provides a consistent framework for the analysis of spatial patterns”</a> blog post).</p>
<p>The <code>it_metric()</code> function from the <strong>comat</strong> package can be used to calculate these metrics for each <em>coma</em> signature. Here, we calculate marginal entropy (<code>"ent"</code>) and relative mutual information (<code>"relmutinf"</code>), and add them to the <code>lc_coma</code> data frame.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb7" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb7-1">lc_coma<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>ent <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">vapply</span>(lc_coma<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>signature, comat<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span>it_metric,</span>
<span id="cb7-2">       <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">FUN.VALUE =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">numeric</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">metric =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ent"</span>)</span>
<span id="cb7-3">lc_coma<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>relmutinf <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">vapply</span>(lc_coma<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>signature, comat<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span>it_metric,</span>
<span id="cb7-4">       <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">FUN.VALUE =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">numeric</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">metric =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"relmutinf"</span>)</span></code></pre></div></div>
</div>
<p>In short, the marginal entropy is a measure of diversity (thematic complexity, composition) of spatial categories — the larger the entropy, the more diverse the categories in the window. The relative mutual information is a measure of spatial autocorrelation (configuration) of spatial categories – the larger the relative mutual information, the more autocorrelated the categories in the window are.</p>
<p>Importantly, both metrics are uncorrelated, which means that they describe different aspects of spatial patterns of land cover:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb8" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb8-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">plot</span>(lc_coma<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>ent, lc_coma<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>relmutinf)</span></code></pre></div></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://jakubnowosad.com/posts/2023-11-18-motif-bp7/index_files/figure-html/unnamed-chunk-8-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
</section>
<section id="visualizing-spatial-patterns-metrics" class="level1">
<h1>Visualizing spatial patterns’ metrics</h1>
<p>We can visualize the spatial distribution of these metrics’ values by removing the <code>signature</code> column and converting the <code>lc_coma</code> object to an <code>sf</code> class:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb9" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb9-1">lc_coma<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>signature <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NULL</span></span>
<span id="cb9-2">lc_coma_sf <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lsp_add_sf</span>(lc_coma)</span>
<span id="cb9-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">plot</span>(lc_coma_sf[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ent"</span>], <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">border =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NA</span>)</span>
<span id="cb9-4"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">plot</span>(lc_coma_sf[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"relmutinf"</span>], <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">border =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NA</span>)</span></code></pre></div></div>
<div class="cell quarto-layout-panel" data-layout-ncol="2">
<div class="quarto-layout-row">
<div class="quarto-layout-cell" style="flex-basis: 50.0%;justify-content: center;">
<p><img src="https://jakubnowosad.com/posts/2023-11-18-motif-bp7/index_files/figure-html/unnamed-chunk-9-1.png" class="img-fluid" width="672"></p>
</div>
<div class="quarto-layout-cell" style="flex-basis: 50.0%;justify-content: center;">
<p><img src="https://jakubnowosad.com/posts/2023-11-18-motif-bp7/index_files/figure-html/unnamed-chunk-9-2.png" class="img-fluid" width="672"></p>
</div>
</div>
</div>
</section>
<section id="representative-examples" class="level1">
<h1>Representative examples</h1>
<p>We are also able to look at some examples of areas with representative values of these metrics. For that purpose, we use the <code>pam()</code> method (<em>Partitioning Around Medoids</em>) to cluster the <code>lc_coma</code> data frame into six groups based on the scaled values of <code>ent</code> and <code>relmutinf</code>.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb10" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb10-1">pam <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">pam</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale</span>(lc_coma[, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ent"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"relmutinf"</span>)]), <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">6</span>)</span></code></pre></div></div>
</div>
<p>We can see all of the groups on the map by adding a new column with cluster labels to the <code>lc_coma_sf</code> object and plotting it:</p>
<div class="cell preview-image">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb11" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb11-1">lc_coma_sf<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>cluster <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> pam<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>clustering</span>
<span id="cb11-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">plot</span>(lc_coma_sf[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"cluster"</span>], <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">border =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NA</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">pal =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">palette.colors</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">6</span>))</span></code></pre></div></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://jakubnowosad.com/posts/2023-11-18-motif-bp7/index_files/figure-html/unnamed-chunk-11-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<p>Then, we can select one representative from each cluster in a loop using the <code>crop()</code> function and visualize it using <code>plot()</code>.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb12" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb12-1">lc_coma_sf_subset <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> lc_coma_sf[pam<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>id.med, ]</span>
<span id="cb12-2"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> (i <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">seq_len</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">nrow</span>(lc_coma_sf_subset))){</span>
<span id="cb12-3">  ent_sel <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">round</span>(lc_coma_sf_subset[i, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ent"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">drop =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>], <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>)</span>
<span id="cb12-4">  relmutinf_sel <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">round</span>(lc_coma_sf_subset[i, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"relmutinf"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">drop =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>], <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>)</span>
<span id="cb12-5">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">plot</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">crop</span>(lc, lc_coma_sf_subset[i, ]), </span>
<span id="cb12-6">       <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">main =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">paste0</span>(i, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">" ent: "</span>,  ent_sel, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">" relmutinf: "</span>, relmutinf_sel))</span>
<span id="cb12-7">}</span></code></pre></div></div>
<div class="cell quarto-layout-panel" data-layout-ncol="2">
<div class="quarto-layout-row">
<div class="quarto-layout-cell" style="flex-basis: 50.0%;justify-content: center;">
<p><img src="https://jakubnowosad.com/posts/2023-11-18-motif-bp7/index_files/figure-html/unnamed-chunk-12-1.png" class="img-fluid" width="672"></p>
</div>
<div class="quarto-layout-cell" style="flex-basis: 50.0%;justify-content: center;">
<p><img src="https://jakubnowosad.com/posts/2023-11-18-motif-bp7/index_files/figure-html/unnamed-chunk-12-2.png" class="img-fluid" width="672"></p>
</div>
</div>
<div class="quarto-layout-row">
<div class="quarto-layout-cell" style="flex-basis: 50.0%;justify-content: center;">
<p><img src="https://jakubnowosad.com/posts/2023-11-18-motif-bp7/index_files/figure-html/unnamed-chunk-12-3.png" class="img-fluid" width="672"></p>
</div>
<div class="quarto-layout-cell" style="flex-basis: 50.0%;justify-content: center;">
<p><img src="https://jakubnowosad.com/posts/2023-11-18-motif-bp7/index_files/figure-html/unnamed-chunk-12-4.png" class="img-fluid" width="672"></p>
</div>
</div>
<div class="quarto-layout-row">
<div class="quarto-layout-cell" style="flex-basis: 50.0%;justify-content: center;">
<p><img src="https://jakubnowosad.com/posts/2023-11-18-motif-bp7/index_files/figure-html/unnamed-chunk-12-5.png" class="img-fluid" width="672"></p>
</div>
<div class="quarto-layout-cell" style="flex-basis: 50.0%;justify-content: center;">
<p><img src="https://jakubnowosad.com/posts/2023-11-18-motif-bp7/index_files/figure-html/unnamed-chunk-12-6.png" class="img-fluid" width="672"></p>
</div>
</div>
</div>
<p>As you can see above, the <code>ent</code> and <code>relating</code> metrics can be used to describe various spatial patterns of land cover. The last group, <code>6</code>, is the simplest one with just one land cover category. Next, groups <code>4</code> and <code>5</code> represent areas with a low diversity of land cover categories but with different spatial autocorrelation: Group <code>4</code> has lower spatial autocorrelation (is more fragmented), while group <code>5</code> has higher spatial autocorrelation (is less fragmented). Groups <code>2</code> and <code>3</code> are areas with medium diversity of land cover categories, but with different spatial autocorrelation: group <code>2</code> has higher spatial autocorrelation, while group <code>3</code> has lower spatial autocorrelation. Finally, group <code>1</code> is an area with a high diversity of land cover categories and a medium spatial autocorrelation.</p>
</section>
<section id="additional-possibilities" class="level1">
<h1>Additional possibilities</h1>
<p>Importantly, these metrics do not provide any information about the actual land cover categories. Thus, to look at the results in more depth, we can add information about land cover shares in each window to the <code>lc_coma_sf</code> data frame and use it in further analysis.</p>
<p>Here, we can use the <code>"composition"</code> type of signature to extract information about land cover shares in each window, restructure it from a list column to a set of columns, and add it to the <code>lc_coma_sf</code> data frame.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb13" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb13-1">lc_composition <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lsp_signature</span>(lc, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">type =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"composition"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">window =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">300</span>)</span>
<span id="cb13-2">lc_composition <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lsp_restructure</span>(lc_composition)</span>
<span id="cb13-3">lc_coma_sf <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">left_join</span>(lc_coma_sf, lc_composition)</span></code></pre></div></div>
</div>
<p>Now, you are able to subset your data frame based on the land cover shares and analyze the spatial patterns for various types of areas. You can also repeat the above calculations for two time periods or two areas and compare the results.</p>
<!-- show some examples -->
<!-- lc2 = c(lc, lc)
lc2 = stars::st_as_stars(lc2)
lc_coma2 = lsp_signature(lc2, type = "incoma", window = 300)
lc_coma2$signature[[1]] -->
</section>
<section id="summary" class="level1">
<h1>Summary</h1>
<p>This blog post shows how to extract information about the composition and configuration of spatial patterns, visualize it on a map, and look at representative examples. For more details about the information theory-based metrics, see the <a href="https://jakubnowosad.com/posts/2019-06-25-ent-bp1/">“Information theory provides a consistent framework for the analysis of spatial patterns”</a> blog post. To learn more about the <strong>motif</strong> package, see the other blog posts in the <a href="https://jakubnowosad.com/posts.html#category=motif">“motif” category</a>.</p>



</section>

<div id="quarto-appendix" class="default"><section class="quarto-appendix-contents" id="quarto-bibliography"><h2 class="anchored quarto-appendix-heading">References</h2><div id="refs" class="references csl-bib-body hanging-indent">
<div id="ref-Nowosad2019" class="csl-entry">
Nowosad, Jakub, and Tomasz F. Stepinski. 2019. <span>“Information Theory as a Consistent Framework for Quantification and Classification of Landscape Patterns.”</span> <em>Landscape Ecology</em> 34 (9): 2091–101. <a href="https://doi.org/10.1007/s10980-019-00830-x">https://doi.org/10.1007/s10980-019-00830-x</a>.
</div>
</div></section><section class="quarto-appendix-contents" id="quarto-reuse"><h2 class="anchored quarto-appendix-heading">Reuse</h2><div class="quarto-appendix-contents"><div><a rel="license" href="https://creativecommons.org/licenses/by/4.0/">CC BY 4.0</a></div></div></section><section class="quarto-appendix-contents" id="quarto-citation"><h2 class="anchored quarto-appendix-heading">Citation</h2><div><div class="quarto-appendix-secondary-label">BibTeX citation:</div><div class="code-copy-outer-scaffold"><pre class="sourceCode quarto-appendix-bibtex code-with-copy"><code class="sourceCode bibtex">@online{nowosad2023,
  author = {Nowosad, Jakub},
  title = {Extracting Information about Spatial Patterns from Spatial
    Signatures},
  date = {2023-11-18},
  url = {https://jakubnowosad.com/posts/2023-11-18-motif-bp7/},
  doi = {10.59350/8a9dq-f6b14},
  langid = {en}
}
</code></pre></div><div class="quarto-appendix-secondary-label">For attribution, please cite this work as:</div><div id="ref-nowosad2023" class="csl-entry quarto-appendix-citeas">
Nowosad, Jakub. 2023. <span>“Extracting Information about Spatial
Patterns from Spatial Signatures.”</span> November 18. <a href="https://doi.org/10.59350/8a9dq-f6b14">https://doi.org/10.59350/8a9dq-f6b14</a>.
</div></div></section></div> ]]></description>
  <category>spatial</category>
  <category>geocompr</category>
  <category>rstats</category>
  <category>landscape-ecology</category>
  <category>spatial-patterns</category>
  <category>sil</category>
  <category>motif</category>
  <guid>https://jakubnowosad.com/posts/2023-11-18-motif-bp7/</guid>
  <pubDate>Sat, 18 Nov 2023 00:00:00 GMT</pubDate>
</item>
<item>
  <title>Spatial regionalization using universal superpixels algorithm</title>
  <dc:creator>Jakub Nowosad</dc:creator>
  <link>https://jakubnowosad.com/posts/2023-05-15-supercells-bp2/</link>
  <description><![CDATA[ 





<p>This is a second blog post in a series about the <strong>supercells</strong> package. You can read the first one at <a href="https://jakubnowosad.com/posts/2023-04-30-supercells-bp1/">“supercells: universal superpixels algorithm for applications to geospatial data”</a>.</p>
<p>The main idea of supercells is to create groupings of adjacent cells that share common characteristics. This process often results in an over-segmentation – a situation when supercells are internally homogeneous, but not very different from their neighbors. Thus, we need to find the best way to merge similar adjacent supercells into larger entities (regions). It can be done with various approaches, both supervised and unsupervised. Here, we will focus on two examples of unsupervised approaches. The first one, k-means, is a general clustering method, while the second one, SKATER, is a spatial clustering method. The main goals of this blog post are to show how the merging of supercells can be performed and how to evaluate the obtained results.</p>
<section id="example" class="level2">
<h2 class="anchored" data-anchor-id="example">Example</h2>
<p>Let’s start by attaching the packages and reading the input data. We will use the same packages as in the first blog post with one addition – <strong>dplyr</strong>.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(supercells)</span>
<span id="cb1-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(terra)</span>
<span id="cb1-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(sf)</span>
<span id="cb1-4"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(regional)</span>
<span id="cb1-5"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(tmap)</span>
<span id="cb1-6"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(dplyr)</span></code></pre></div></div>
</div>
<p>Here, we will also use the same dataset cropped to an area of 50 by 100 cells.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb2-1">flc <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rast</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"/vsicurl/https://github.com/Nowosad/supercells-examples/blob/main/raw-data/all_ned.tif?raw=true"</span>)</span>
<span id="cb2-2">flc1 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">project</span>(flc, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"EPSG:3035"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">method =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"near"</span>)</span>
<span id="cb2-3">flc1 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> flc[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">101</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">150</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>, drop <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>]</span></code></pre></div></div>
</div>
<p>Our next preparation step will be to create a set of supercells representing areas with homogeneous arrangements of fractional land cover values (Figure&nbsp;1).</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb3-1">flc_sc <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">supercells</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> flc1,</span>
<span id="cb3-2">                    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">step =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">12</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">compactness =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.1</span>,</span>
<span id="cb3-3">                    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">dist_fun =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"jensen-shannon"</span>)</span></code></pre></div></div>
</div>
<div class="cell" data-fig.asp="0.4">
<details class="code-fold">
<summary>Code</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb4-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tm_shape</span>(flc1[[<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>)]]) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb4-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tm_raster</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">style =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"cont"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">palette =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"cividis"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">title =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Fraction:"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb4-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tm_shape</span>(flc_sc) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb4-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tm_borders</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">col =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"red"</span>)</span></code></pre></div></div>
</details>
<div class="cell-output-display">
<div id="fig-1" class="quarto-float quarto-figure quarto-figure-center anchored">
<figure class="quarto-float quarto-float-fig figure">
<div aria-describedby="fig-1-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
<img src="https://jakubnowosad.com/posts/2023-05-15-supercells-bp2/index_files/figure-html/fig-1-1.png" class="img-fluid figure-img" width="672">
</div>
<figcaption class="quarto-float-caption-bottom quarto-float-caption quarto-float-fig" id="fig-1-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
Figure&nbsp;1: Supercells on the top of fractions of the forest and cropland classes
</figcaption>
</figure>
</div>
</div>
</div>
<p>As a final preparation step, we will also calculate supercells areas and keep them in the <code>area_km2</code> column.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb5-1">flc_sc<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>area_km2 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.numeric</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">st_area</span>(flc_sc)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1000000</span></span></code></pre></div></div>
</div>
<section id="k-means" class="level3">
<h3 class="anchored" data-anchor-id="k-means">K-means</h3>
<p>K-means clustering partitions observations into a selected number (<em>k</em>) of clusters. In our case, each observation is a supercell, and thus, each k-means cluster will represent a set of supercells. Notably, the k-means algorithm is unaware of spatial relationships in our data – it does not know that some supercells are adjacent to others. Therefore, clusters returned by the k-means algorithm may consist of many supercells located in different places in our area of interest. <!-- ref to some k-means resources --></p>
<p>To use the k-means algorithm, we need to extract only variables (columns) with fractional land cover values:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb6" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb6-1">vars <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Forest"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Shrubland"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Grassland"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Bare.Sparse.vegatation"</span>, </span>
<span id="cb6-2">         <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Cropland"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Built.up"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Seasonal.inland.water"</span>,</span>
<span id="cb6-3">         <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Permanent.inland.water"</span>)</span>
<span id="cb6-4">flc_sc_df <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">st_drop_geometry</span>(flc_sc)[vars]</span></code></pre></div></div>
</div>
<p>Then, we can apply the clustering by making one major decision – what is the expected number of clusters (<em>k</em>/the <code>centers</code> argument). For this example, I created four clusters:<sup>1</sup></p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb7" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb7-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">set.seed</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2022-11-10</span>)</span>
<span id="cb7-2">km_results <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">kmeans</span>(flc_sc_df, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">centers =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>)</span></code></pre></div></div>
</div>
<p>Next, we need to add a new column to our data, which represents our clusters.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb8" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb8-1">flc_sc_kmeans <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> flc_sc <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb8-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">kmeans =</span> km_results<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>cluster)</span></code></pre></div></div>
</div>
<p>Importantly, this operation does not change our geometries, and so our polygons are still the same as the input supercells.<sup>2</sup> However, some clusters lie next to each other and can be merged. This can be done with a combination of <code>group_by()</code> and <code>summarise()</code>:<sup>3</sup></p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb9" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb9-1">flc_sc_kmeans <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> flc_sc_kmeans <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb9-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">group_by</span>(kmeans) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb9-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">summarise</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb9-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">st_make_valid</span>()</span></code></pre></div></div>
</div>
<p>Now, the <code>flc_sc_kmeans</code> object has only four rows – one for each cluster, but as we mentioned before, they may consist of polygons located in different places in our area of interest. Thus, another step is needed here – to move each separate polygon into each own feature (row):</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb10" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb10-1">flc_sc_kmeans2 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">st_cast</span>(flc_sc_kmeans, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"POLYGON"</span>)</span>
<span id="cb10-2">flc_sc_kmeans2<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>kmeans <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">seq_along</span>(flc_sc_kmeans2<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>kmeans)</span></code></pre></div></div>
</div>
<p>We also replaced our <code>kmeans</code> column with a unique identifier for each row. Now, we have eleven polygons representing groupings of adjacent cells that share common characteristics. We may also calculate what the average is<sup>4</sup> fraction of each land cover in each polygon. This can be done by extracting values from our original raster and then summarizing them:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb11" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb11-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">names</span>(flc1) <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> vars</span>
<span id="cb11-2">flc_sc_kmeans2_vals <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">extract</span>(flc1, flc_sc_kmeans2, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">weights =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb11-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">group_by</span>(ID) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb11-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">summarise</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">across</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">all_of</span>(vars), \(x) <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">weighted.mean</span>(x, weight)))</span>
<span id="cb11-5">flc_sc_kmeans2 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">cbind</span>(flc_sc_kmeans2, flc_sc_kmeans2_vals)</span></code></pre></div></div>
</div>
<p>Figure&nbsp;2 summarized the results. Firstly, we created four clusters using the k-means algorithm (the left panels). You can notice that the first cluster relates to regions with large fractions of forests, while the third cluster has regions with large fractions of croplands. The second and fourth clusters have dominant shares of forests and croplands, respectively. Then each cluster is split into a few regions:</p>
<div class="cell" data-fig.asp="0.4">
<details class="code-fold">
<summary>Code</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb12" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb12-1">tm1 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tm_shape</span>(flc1[[<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>)]]) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb12-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tm_raster</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">legend.show =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb12-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tm_facets</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ncol =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb12-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tm_shape</span>(flc_sc_kmeans) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb12-5">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tm_polygons</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">col =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"kmeans"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">style =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"cat"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">palette =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Set1"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">title =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"k:"</span>)</span>
<span id="cb12-6">tm2 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tm_shape</span>(flc1[[<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>)]]) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb12-7">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tm_raster</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">legend.show =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb12-8">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tm_facets</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ncol =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb12-9">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tm_shape</span>(flc_sc_kmeans2) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb12-10">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tm_polygons</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">col =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"kmeans"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">style =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"cat"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">palette =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Set1"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">title =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ID:"</span>)</span>
<span id="cb12-11">tm3 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tm_shape</span>(flc1[[<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>)]]) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb12-12">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tm_raster</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">style =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"cont"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">palette =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"cividis"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">title =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Fraction:"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb12-13">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tm_facets</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ncol =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb12-14">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tm_shape</span>(flc_sc_kmeans2) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb12-15">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tm_borders</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">col =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"red"</span>)</span>
<span id="cb12-16"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tmap_arrange</span>(tm1, tm2, tm3)</span></code></pre></div></div>
</details>
<div class="cell-output-display">
<div id="fig-2" class="quarto-float quarto-figure quarto-figure-center anchored">
<figure class="quarto-float quarto-float-fig figure">
<div aria-describedby="fig-2-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
<img src="https://jakubnowosad.com/posts/2023-05-15-supercells-bp2/index_files/figure-html/fig-2-1.png" class="img-fluid figure-img" width="672">
</div>
<figcaption class="quarto-float-caption-bottom quarto-float-caption quarto-float-fig" id="fig-2-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
Figure&nbsp;2: Regions created with the k-means algorithm on the top of fractions of the forest and cropland classes
</figcaption>
</figure>
</div>
</div>
</div>
</section>
<section id="skater" class="level3">
<h3 class="anchored" data-anchor-id="skater">SKATER</h3>
<p>In this case, an alternative group of methods exists. Instead of using clustering methods, such as k-means, we can use some regionalization methods (they are sometimes referred to as spatial clustering methods).<sup>5</sup> SKATER (Spatial ’K’luster Analysis by Tree Edge Removal) is a procedure based on a graph representation of the input data. It prunes the graph to its minimum spanning tree (MST) and then iteratively partitions the graph by identifying edges (connections between neighbors) whose removal increases the objective function (between-group dissimilarity) the most. This iterative process stops when a specified number of regions is obtained.</p>
<p>Here, we will use the SKATER procedure as implemented in <code>skater()</code> from the <strong>rgeoda</strong> package, but we need to start by preparing a few input objects.</p>
<p>The first one is a data frame with the variables of interest for each supercell:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb13" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb13-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(rgeoda)</span></code></pre></div></div>
<div class="cell-output cell-output-stderr">
<pre><code>Loading required package: digest</code></pre>
</div>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb15" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb15-1">vars <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Forest"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Shrubland"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Grassland"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Bare.Sparse.vegatation"</span>, </span>
<span id="cb15-2">         <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Cropland"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Built.up"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Seasonal.inland.water"</span>,</span>
<span id="cb15-3">         <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Permanent.inland.water"</span>)</span>
<span id="cb15-4">flc_sc_df <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">st_drop_geometry</span>(flc_sc)[vars]</span></code></pre></div></div>
</div>
<p>The second one is an object of class <code>Weight</code> that contains information about neighbors in our data:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb16" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb16-1">queen_w <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">queen_weights</span>(flc_sc)</span></code></pre></div></div>
</div>
<p>The third (optional) object is a vector of distances between values of our supercells. In this case, we will calculate the Jensen-Shannon distance (the same that we used to create our supercells):</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb17" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb17-1">weight_dist <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> philentropy<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">distance</span>(flc_sc_df, </span>
<span id="cb17-2">                                    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">method =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"jensen-shannon"</span>, </span>
<span id="cb17-3">                                    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">as.dist.obj =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>)</span>
<span id="cb17-4">weight_dist <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.vector</span>(weight_dist)</span></code></pre></div></div>
</div>
<p>Now, we can use the <code>skater()</code> function by providing the expected number of regions (eleven in this case to match the k-means result) and the previously created objects:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb18" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb18-1">skater_results <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">skater</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">11</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">w =</span> queen_w,</span>
<span id="cb18-2">                        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">df =</span> flc_sc_df, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">rdist =</span> weight_dist)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>aaa0x56039dda6d50after gda_skater</code></pre>
</div>
</div>
<p>Next, we need to add a new column to our data that represents our regions:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb20" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb20-1">flc_sc_skater <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> flc_sc <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb20-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">skater =</span> skater_results<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>Clusters)</span></code></pre></div></div>
</div>
<p>Similarly to the k-means example, we will:</p>
<ul>
<li><ol type="1">
<li>merge supercells belonging to the same regions:</li>
</ol></li>
</ul>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb21" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb21-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#1</span></span>
<span id="cb21-2">flc_sc_skater <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> flc_sc_skater <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb21-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">group_by</span>(skater) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb21-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">summarise</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb21-5">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">st_make_valid</span>()</span></code></pre></div></div>
</div>
<ul>
<li><ol start="2" type="1">
<li>add columns with average fractions of each land cover for each region:</li>
</ol></li>
</ul>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb22" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb22-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#2</span></span>
<span id="cb22-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">names</span>(flc1) <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> vars</span>
<span id="cb22-3">flc_sc_skater_vals <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">extract</span>(flc1, flc_sc_skater, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">weights =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb22-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">group_by</span>(ID) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb22-5">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">summarise</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">across</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">all_of</span>(vars), \(x) <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">weighted.mean</span>(x, weight)))</span>
<span id="cb22-6">flc_sc_skater <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">cbind</span>(flc_sc_skater, flc_sc_skater_vals)</span></code></pre></div></div>
</div>
<p>Figure&nbsp;3 shows the SKATER results.</p>
<div class="cell" data-fig.asp="0.4">
<details class="code-fold">
<summary>Code</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb23" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb23-1">tm1s <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tm_shape</span>(flc1[[<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>)]]) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb23-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tm_raster</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">legend.show =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb23-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tm_facets</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ncol =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb23-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tm_shape</span>(flc_sc_skater) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb23-5">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tm_polygons</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">col =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"skater"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">style =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"cat"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">palette =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Set1"</span>)</span>
<span id="cb23-6">tm2s <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tm_shape</span>(flc1[[<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>)]]) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb23-7">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tm_raster</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">style =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"cont"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">palette =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"cividis"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">title =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Fraction:"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb23-8">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tm_facets</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ncol =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb23-9">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tm_shape</span>(flc_sc_skater) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb23-10">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tm_borders</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">col =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"red"</span>)</span>
<span id="cb23-11"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tmap_arrange</span>(tm1s, tm2s)</span></code></pre></div></div>
</details>
<div class="cell-output-display">
<div id="fig-3" class="quarto-float quarto-figure quarto-figure-center anchored">
<figure class="quarto-float quarto-float-fig figure">
<div aria-describedby="fig-3-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
<img src="https://jakubnowosad.com/posts/2023-05-15-supercells-bp2/index_files/figure-html/fig-3-1.png" class="img-fluid figure-img" width="672">
</div>
<figcaption class="quarto-float-caption-bottom quarto-float-caption quarto-float-fig" id="fig-3-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
Figure&nbsp;3: Regions created with the SKATER algorithm on the top of fractions of the forest and cropland classes
</figcaption>
</figure>
</div>
</div>
</div>
</section>
<section id="quality" class="level3">
<h3 class="anchored" data-anchor-id="quality">Quality</h3>
<p>The quality of the regionalization results can be evaluated in two main ways: external and internal. The external evaluation compares the obtained regions with, for example, other existing regionalizations. This can be done in R using the <strong>sabre</strong> package <span class="citation" data-cites="nowosad_spatial_2018">(Nowosad and Stepinski 2018)</span>.</p>
<p>Here, we will focus on internal evaluation. Internal evaluation checks how well the regions encapsulate similar values and differ from their neighbors. This evaluation can be performed with the two functions of the <strong>regional</strong> package:</p>
<ul>
<li><code>reg_inhomogeneity()</code> that calculates the internal inhomogeneity of each region (the lower the value, the better)</li>
<li><code>reg_isolation()</code> that calculates the isolation of each region from its neighbors (the higher the value, the better)</li>
</ul>
<p>Let’s start by calculating the inhomogeneity of each region for our two approaches:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb24" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb24-1">vars <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Forest"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Shrubland"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Grassland"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Bare.Sparse.vegatation"</span>, </span>
<span id="cb24-2">         <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Cropland"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Built.up"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Seasonal.inland.water"</span>, </span>
<span id="cb24-3">         <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Permanent.inland.water"</span>)</span>
<span id="cb24-4">flc_sc_kmeans2<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>inh <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">reg_inhomogeneity</span>(flc_sc_kmeans2[vars], flc1, </span>
<span id="cb24-5">                                      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">dist_fun =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"jensen-shannon"</span>, </span>
<span id="cb24-6">                                      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sample_size =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">200</span>)</span>
<span id="cb24-7">flc_sc_skater<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>inh <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">reg_inhomogeneity</span>(flc_sc_skater[vars], flc1, </span>
<span id="cb24-8">                                      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">dist_fun =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"jensen-shannon"</span>, </span>
<span id="cb24-9">                                      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sample_size =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">200</span>)</span></code></pre></div></div>
</div>
<p>The visual comparison shows that, in general, k-means-based regions have slightly smaller inhomogeneity values.</p>
<div class="cell">
<details class="code-fold">
<summary>Code</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb25" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb25-1">my_breaks <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">seq</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, </span>
<span id="cb25-2">                <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">max</span>(flc_sc_kmeans2<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>inh, flc_sc_skater<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>inh) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.05</span>,</span>
<span id="cb25-3">                <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">by =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.05</span>)</span>
<span id="cb25-4">tm_inh2 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tm_shape</span>(flc_sc_kmeans2) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb25-5">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tm_polygons</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">col =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"inh"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">breaks =</span> my_breaks, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">style =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"cont"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">palette =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"-viridis"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb25-6">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tm_layout</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">title =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"k-means2"</span>)</span>
<span id="cb25-7">tm_inh3 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tm_shape</span>(flc_sc_skater) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb25-8">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tm_polygons</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">col =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"inh"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">breaks =</span> my_breaks, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">style =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"cont"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">palette =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"-viridis"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb25-9">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tm_layout</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">title =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"SKATER"</span>)</span>
<span id="cb25-10"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tmap_arrange</span>(tm_inh2, tm_inh3, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">nrow =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span></code></pre></div></div>
</details>
<div class="cell-output-display">
<div id="fig-4" class="quarto-float quarto-figure quarto-figure-center anchored">
<figure class="quarto-float quarto-float-fig figure">
<div aria-describedby="fig-4-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
<img src="https://jakubnowosad.com/posts/2023-05-15-supercells-bp2/index_files/figure-html/fig-4-1.png" class="img-fluid figure-img" width="672">
</div>
<figcaption class="quarto-float-caption-bottom quarto-float-caption quarto-float-fig" id="fig-4-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
Figure&nbsp;4: Inhomogeneity of regions created using the k-means and SKATER approaches
</figcaption>
</figure>
</div>
</div>
</div>
<p>This can also be confirmed by calculating area-weighted inhomogeneity, which is a bit smaller for k-means-based regionalization.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb26" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb26-1">area_weighted_inhomogeneity <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(x){</span>
<span id="cb26-2">  x<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>area_km2 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.numeric</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">st_area</span>(x)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1000000</span></span>
<span id="cb26-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">round</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">weighted.mean</span>(x<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>inh, x<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>area_km2), <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>)</span>
<span id="cb26-4">}</span>
<span id="cb26-5"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">area_weighted_inhomogeneity</span>(flc_sc_kmeans2)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 0.08</code></pre>
</div>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb28" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb28-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">area_weighted_inhomogeneity</span>(flc_sc_skater)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 0.089</code></pre>
</div>
</div>
<p>We can also compare how different our regions are from their neighbors with <code>reg_isolation()</code>:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb30" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb30-1">flc_sc_kmeans2<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>iso <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">reg_isolation</span>(flc_sc_kmeans2[vars], flc1, </span>
<span id="cb30-2">                                      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">dist_fun =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"jensen-shannon"</span>, </span>
<span id="cb30-3">                                      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sample_size =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">200</span>)</span>
<span id="cb30-4">flc_sc_skater<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>iso <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">reg_isolation</span>(flc_sc_skater[vars], flc1, </span>
<span id="cb30-5">                                      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">dist_fun =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"jensen-shannon"</span>, </span>
<span id="cb30-6">                                      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sample_size =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">200</span>)</span></code></pre></div></div>
</div>
<p>In this case, the maps suggest that SKATER has slightly larger isolation values.</p>
<div class="cell">
<details class="code-fold">
<summary>Code</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb31" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb31-1">my_breaks2 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">seq</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, </span>
<span id="cb31-2">                <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">max</span>(flc_sc_kmeans2<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>iso, flc_sc_skater<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>iso) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.05</span>,</span>
<span id="cb31-3">                <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">by =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.05</span>)</span>
<span id="cb31-4">tm_iso2 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tm_shape</span>(flc_sc_kmeans2) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb31-5">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tm_polygons</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">col =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"iso"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">breaks =</span> my_breaks2, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">style =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"cont"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">palette =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"-magma"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb31-6">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tm_layout</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">title =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"k-means2"</span>)</span>
<span id="cb31-7">tm_iso3 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tm_shape</span>(flc_sc_skater) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb31-8">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tm_polygons</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">col =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"iso"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">breaks =</span> my_breaks2, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">style =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"cont"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">palette =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"-magma"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb31-9">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tm_layout</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">title =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"SKATER"</span>)</span>
<span id="cb31-10"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tmap_arrange</span>(tm_iso2, tm_iso3, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">nrow =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span></code></pre></div></div>
</details>
<div class="cell-output-display">
<div id="fig-5" class="quarto-float quarto-figure quarto-figure-center anchored">
<figure class="quarto-float quarto-float-fig figure">
<div aria-describedby="fig-5-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
<img src="https://jakubnowosad.com/posts/2023-05-15-supercells-bp2/index_files/figure-html/fig-5-1.png" class="img-fluid figure-img" width="672">
</div>
<figcaption class="quarto-float-caption-bottom quarto-float-caption quarto-float-fig" id="fig-5-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
Figure&nbsp;5: Isolation of regions created using the k-means and SKATER approaches
</figcaption>
</figure>
</div>
</div>
</div>
<p>This is confirmed by calculating the average isolation for both approaches, which is larger (better) for SKATER:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb32" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb32-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">round</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mean</span>(flc_sc_kmeans2<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>iso), <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 0.219</code></pre>
</div>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb34" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb34-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">round</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mean</span>(flc_sc_skater<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>iso), <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 0.222</code></pre>
</div>
</div>
</section>
</section>
<section id="summary" class="level2">
<h2 class="anchored" data-anchor-id="summary">Summary</h2>
<p>This blog post showed two examples of how to process supercells to create spatially homogeneous regions: one approach used the k-means algorithm, while the other applied the SKATER procedure. It is worth mentioning, however, that many more clustering techniques exist that could be used for this purpose; it includes non-spatial algorithms (e.g., hierarchical clustering) and spatial algorithms (e.g., REDCAP).</p>
<p>The results obtained in this blog post suggested that the k-means approach resulted in (slightly) more homogeneous regions, while regions from the SKATER approach were (also slightly) more isolated from their neighbors. However, you need to remember that the example area was very small and consisted of just a handful of supercells. Our study for a larger area <span class="citation" data-cites="nowosad_method_2022a">(Nowosad et al. 2022)</span> suggests that SKATER give better results in term of both inhomogeneity and isolation. SKATER (and other spatial clustering methods) should be, in general, preferred over k-means (and other non-spatial clustering methods). They not only result in better regionalizations, but also allow to specify of the output number of regions directly. That being said, the k-means algorithm could also be useful in some cases, especially for large data with hundreds of thousands of supercells (polygons).</p>
<p>If you want to learn more about <strong>supercells</strong> and regionalizations based on them, we encourage you to try a few entry points. One is the <span class="citation" data-cites="nowosad_extended_2022">Nowosad and Stepinski (2022)</span> article that explains the main ideas and applies them to three examples of non-imagery data. <!-- FOSS4G VIDEO!!! --> You can also see slides from a talk entitled “A method for universal superpixels-based regionalization” that I gave during the FOSS4G 2022 conference at <a href="https://jakubnowosad.com/foss4g-2022/" class="uri">https://jakubnowosad.com/foss4g-2022/</a>. Finally, the package has extensive documentation, including several vignettes, that can be found at <a href="https://jakubnowosad.com/supercells/" class="uri">https://jakubnowosad.com/supercells/</a>.</p>
<!-- # Acknowledgement -->



</section>


<div id="quarto-appendix" class="default"><section class="quarto-appendix-contents" id="quarto-bibliography"><h2 class="anchored quarto-appendix-heading">References</h2><div id="refs" class="references csl-bib-body hanging-indent">
<div id="ref-aydin_quantitative_2021" class="csl-entry">
Aydin, Orhun, Mark. V. Janikas, Renato Martins Assunção, and Ting-Hwan Lee. 2021. <span>“A Quantitative Comparison of Regionalization Methods.”</span> <em>International Journal of Geographical Information Science</em> 35 (11): 2287–315. <a href="https://doi.org/10/gnk4b4">https://doi.org/10/gnk4b4</a>.
</div>
<div id="ref-nowosad_extended_2022" class="csl-entry">
Nowosad, Jakub, and Tomasz F. Stepinski. 2022. <span>“Extended <span>SLIC</span> Superpixels Algorithm for Applications to Non-Imagery Geospatial Rasters.”</span> <em>International Journal of Applied Earth Observation and Geoinformation</em> 112 (August): 102935. <a href="https://doi.org/10.1016/j.jag.2022.102935">https://doi.org/10.1016/j.jag.2022.102935</a>.
</div>
<div id="ref-nowosad_spatial_2018" class="csl-entry">
Nowosad, J., and T. F. Stepinski. 2018. <span>“Spatial Association Between Regionalizations Using the Information-Theoretical <span><em>V</em></span> -Measure.”</span> <em>International Journal of Geographical Information Science</em> 32 (12): 2386–401. <a href="https://doi.org/10/gf283f">https://doi.org/10/gf283f</a>.
</div>
<div id="ref-nowosad_method_2022a" class="csl-entry">
Nowosad, J., T. F. Stepinski, and M. Iwicki. 2022. <span>“A <span class="nocase">method for universal supercells-based regionalization</span> (<span class="nocase">preliminary results</span>).”</span> <em>The International Archives of the Photogrammetry, Remote Sensing and Spatial Information Sciences</em> XLVIII-4/W1-2022 (August): 337–44. <a href="https://doi.org/10.5194/isprs-archives-XLVIII-4-W1-2022-337-2022">https://doi.org/10.5194/isprs-archives-XLVIII-4-W1-2022-337-2022</a>.
</div>
</div></section><section id="footnotes" class="footnotes footnotes-end-of-document"><h2 class="anchored quarto-appendix-heading">Footnotes</h2>

<ol>
<li id="fn1"><p>There are several approaches for deciding the number of clusters. Some are based on prior knowledge and some use various methods to suggest an optimal <em>k</em> number (e.g., Elbow method).↩︎</p></li>
<li id="fn2"><p>Try <code>plot(flc_sc_kmeans["kmeans"])</code>↩︎</p></li>
<li id="fn3"><p>Additionally, we used <code>st_make_valid()</code> to make sure that our result has valid geometries.↩︎</p></li>
<li id="fn4"><p>Area-weighted mean, to be precise.↩︎</p></li>
<li id="fn5"><p>Read <span class="citation" data-cites="aydin_quantitative_2021">Aydin et al. (2021)</span> to see a comparison of regionalizations methods.↩︎</p></li>
</ol>
</section><section class="quarto-appendix-contents" id="quarto-reuse"><h2 class="anchored quarto-appendix-heading">Reuse</h2><div class="quarto-appendix-contents"><div><a rel="license" href="https://creativecommons.org/licenses/by/4.0/">CC BY 4.0</a></div></div></section><section class="quarto-appendix-contents" id="quarto-citation"><h2 class="anchored quarto-appendix-heading">Citation</h2><div><div class="quarto-appendix-secondary-label">BibTeX citation:</div><div class="code-copy-outer-scaffold"><pre class="sourceCode quarto-appendix-bibtex code-with-copy"><code class="sourceCode bibtex">@online{nowosad2023,
  author = {Nowosad, Jakub},
  title = {Spatial Regionalization Using Universal Superpixels
    Algorithm},
  date = {2023-05-15},
  url = {https://jakubnowosad.com/posts/2023-05-15-supercells-bp2/},
  doi = {10.59350/h306r-mp61},
  langid = {en}
}
</code></pre></div><div class="quarto-appendix-secondary-label">For attribution, please cite this work as:</div><div id="ref-nowosad2023" class="csl-entry quarto-appendix-citeas">
Nowosad, Jakub. 2023. <span>“Spatial Regionalization Using Universal
Superpixels Algorithm.”</span> May 15. <a href="https://doi.org/10.59350/h306r-mp61">https://doi.org/10.59350/h306r-mp61</a>.
</div></div></section></div> ]]></description>
  <category>spatial</category>
  <category>geocompr</category>
  <category>rstats</category>
  <category>segmentation</category>
  <category>regionalization</category>
  <guid>https://jakubnowosad.com/posts/2023-05-15-supercells-bp2/</guid>
  <pubDate>Mon, 15 May 2023 00:00:00 GMT</pubDate>
</item>
<item>
  <title>supercells: universal superpixels algorithm for applications to geospatial data</title>
  <dc:creator>Jakub Nowosad</dc:creator>
  <link>https://jakubnowosad.com/posts/2023-04-30-supercells-bp1/</link>
  <description><![CDATA[ 





<!-- intro -->
<p>Segmentation is a process of partitioning space into smaller segments. For example, imagine looking at your family photo and trying to distinct individual people. Similarly, we can look at a satellite image (in RGB colors) with the goal of delineating where are the buildings, fields, roads, etc. In geography, segmentation can also be associated with regionalization. Here, our goal is not to detect objects (e.g., people or buildings) but rather areas with similar properties.</p>
<p>Segmentation/regionalization methods should minimize internal inhomogeneity and maximize external isolation. First, each segment (you can think of them as polygons) should contain similar values.<sup>1</sup> For example, let’s consider two segments representing two different roofs. One roof is entirely covered by red tiles, while the roof of second one looks like a chessboard with red and brown tiles. Both of the segments are homogeneous, however, the latter is more complex than the former. Second segmentation property is the maximization of external isolation. This means that any given segment is different from its neighbors as much as possible.</p>
<p>Segmentation is an optimization problem – trying and testing all of possible segments borders and their properties may take a very long time, even for relatively small data. For that reason, some heuristics need to be used. One way to improve the output and reduce the time/processing cost of segmentation is to perform a preprocessing stage with superpixels.</p>
<section id="superpixels" class="level2">
<h2 class="anchored" data-anchor-id="superpixels">Superpixels</h2>
<p>The main idea of superpixels is to create groupings of adjacent cells that share common characteristics. This process often results in an over-segmentation – this is a situation when our segments (superpixels) are internally homogeneous, but not always very different from their neighbors.</p>
<p>Superpixels are used for two main reasons:</p>
<ol type="1">
<li>Pixels are not natural entities. They are rather a consequence of the discrete representation of data. For example, depending on the data resolution, our roofs from the previous example can consist of 20 pixels or be just a fraction of one pixel.</li>
<li>Superpixels, as groupings of adjacent cells, reduce the dimensionality of the data making further segmentation tasks easier. For example, we may end up with 5,000 superpixels, instead of 150,000 original pixels.</li>
</ol>
</section>
<section id="slic-algorithm" class="level2">
<h2 class="anchored" data-anchor-id="slic-algorithm">SLIC algorithm</h2>
<p>Many superpixels algorithms have been developed; see <span class="citation" data-cites="stutz_superpixels_2018">Stutz et al. (2018)</span>. The SLIC algorithm <span class="citation" data-cites="achanta_slic_2012">(Achanta et al. 2012)</span> is one of the most often used superpixel algorithms due to its simplicity, accuracy, and low computational cost. It starts with cluster centers spaced by the interval of <img src="https://latex.codecogs.com/png.latex?S">. Each cell is assigned to the nearest cluster center, and the distance <img src="https://latex.codecogs.com/png.latex?D"> is calculated between the cluster centers and cells in the <img src="https://latex.codecogs.com/png.latex?2S%C3%972S"> region. Afterward, new cluster centers (centroids) are updated for the new superpixels, and their color values are the average of all the cells belonging to the given superpixel. The SLIC algorithm works iteratively, repeating the above process until it reaches the expected number of iterations.</p>
<p><img src="https://jakubnowosad.com/posts/2023-04-30-supercells-bp1/figs/ga.gif" class="img-fluid"></p>
<p>The distance <img src="https://latex.codecogs.com/png.latex?D"> is calculated as:</p>
<p><img src="https://latex.codecogs.com/png.latex?%0AD%20=%20%5Csqrt%7B%5Cleft(%5Cfrac%7Bd_c%7D%7Bm%7D%5Cright)%5E2%20+%20%5Cleft(%5Cfrac%7Bd_s%7D%7BS%7D%5Cright)%5E2%7D%0A"></p>
<p>where <img src="https://latex.codecogs.com/png.latex?d_c"> is the color (spectral) distance, <img src="https://latex.codecogs.com/png.latex?m"> is the compactness parameter, <img src="https://latex.codecogs.com/png.latex?d_s"> is the spatial (Euclidean) distance, and <img src="https://latex.codecogs.com/png.latex?S"> is the interval between the initial cluster centers.</p>
<p>Typical workflow for the original SLIC algorithm is to convert RGB image into the LAB color space <!-- why?? --> and then use it to create superpixels. In that case, the distance <img src="https://latex.codecogs.com/png.latex?D"> depends on (a) the Euclidean spatial distance between a cell and a superpixel centroid and (b) the Euclidean color distance between a cell’s LAB values and a superpixel centroid average LAB values. As originally implemented by its authors, the SLIC algorithm has the RGB image hard-wired as input data. Thus, its geospatial applications remain restricted to images, RGB, multispectral, or hyperspectral.</p>
</section>
<section id="slic-extension" class="level2">
<h2 class="anchored" data-anchor-id="slic-extension">SLIC extension</h2>
<p>In <span class="citation" data-cites="nowosad_extended_2022">Nowosad and Stepinski (2022)</span>, we propose an extension of SLIC that can be applied to non-imagery geospatial rasters. This includes rasters that carry:</p>
<ul>
<li>Pattern information (co-occurrence matrices)</li>
<li>Compositional information (histograms)</li>
<li>Time-series information (ordered sequences)</li>
<li>Other forms of information for which the use of Euclidean distance may not be justified<sup>2</sup></li>
</ul>
<p>The extended SLIC allows using any distance measure to calculate the semantic distance – <img src="https://latex.codecogs.com/png.latex?dc"> can be replaced with any distance/dissimilarity measure. We implemented the above idea as an R package {supercells}. Note that we decided to use the super<strong>cells</strong> name (instead of super<strong>pixels</strong>) to highlight that the method can be applied to various spatial raster data. The package installation instructions can be found at https://jakubnowosad.com/supercells/.</p>
</section>
<section id="example" class="level2">
<h2 class="anchored" data-anchor-id="example">Example</h2>
<p>This blog post presents a short example of using spatial raster data with compositional information (histograms). For the study site located in the eastern Netherlands, we downloaded fractions of a pixel’s area covered by different land cover classes (source: <a href="https://land.copernicus.eu/global/products/lc">Copernicus Global Land Service: 2019 Land Cover 100m-resolution data</a>). Our goal is to create superpixels with similar fractions of land cover classes (Figure&nbsp;1).</p>
<p>Let’s start by attaching the packages and reading the input data:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(supercells)</span>
<span id="cb1-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(terra)</span>
<span id="cb1-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(sf)</span>
<span id="cb1-4"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(regional)</span>
<span id="cb1-5"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(tmap)</span>
<span id="cb1-6">flc <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rast</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"/vsicurl/https://github.com/Nowosad/supercells-examples/blob/main/raw-data/all_ned.tif?raw=true"</span>)</span></code></pre></div></div>
</div>
<p>The input data, <code>flc</code>, is a raster of 507 by 1105 cells and eight layers (fractions of different land cover classes). We will resample our raster into a projected CRS and, for a simplicity case and to see our results easier, we will crop it to a 50 by 100 cells area:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb2-1">flc1 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">project</span>(flc, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"EPSG:3035"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">method =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"near"</span>)</span>
<span id="cb2-2">flc1 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> flc[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">101</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">150</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>, drop <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>]</span></code></pre></div></div>
</div>
<p>The <code>flc1</code> object represents an area mostly covered by croplands, with some forests in its south-eastern parts, and smaller fractions of grasslands and built-up classes:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb3-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tm_shape</span>(flc1) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb3-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tm_raster</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">style =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"cont"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">palette =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"cividis"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">title =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Fraction:"</span>) </span></code></pre></div></div>
<div class="cell-output-display">
<div id="fig-1" class="quarto-float quarto-figure quarto-figure-center anchored">
<figure class="quarto-float quarto-float-fig figure">
<div aria-describedby="fig-1-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
<img src="https://jakubnowosad.com/posts/2023-04-30-supercells-bp1/index_files/figure-html/fig-1-1.png" class="img-fluid figure-img" width="672">
</div>
<figcaption class="quarto-float-caption-bottom quarto-float-caption quarto-float-fig" id="fig-1-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
Figure&nbsp;1: Example data representing fractions of land cover classes
</figcaption>
</figure>
</div>
</div>
</div>
<p>Now, we are able to create supercells using the <strong>supercells</strong> package and its <code>supercells()</code> function.<sup>3</sup> This function is very flexible and its results can be much customized.<sup>4</sup> Here, we will just use its basic arguments:</p>
<ul>
<li><code>x</code>: our input raster with one or more layers; <code>flc1</code> in our case</li>
<li><code>step</code>: our <img src="https://latex.codecogs.com/png.latex?S"> interval between initial cluster centers; here we use the value of <code>12</code> (cells). Decreasing this value with give us more supercells, and increasing it results in fewer supercells</li>
<li><code>compactness</code>: <img src="https://latex.codecogs.com/png.latex?m">, the compactness parameter; here we use the value of <code>0.1</code> – the lower the value, the more impact the value distance has on the result</li>
<li><code>dist_fun</code>: distance function used; here we use the Jensen-Shannon distance (<code>"jensen-shannon"</code>), which is suitable for measuring the dissimilarity between histograms</li>
</ul>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb4-1">flc_sc <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">supercells</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> flc1,</span>
<span id="cb4-2">                    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">step =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">12</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">compactness =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.1</span>,</span>
<span id="cb4-3">                    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">dist_fun =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"jensen-shannon"</span>)</span></code></pre></div></div>
</div>
<p>The <code>flc_sc</code> result is an <code>sf</code> (spatial vector) object with 28 polygons. We can visualize them on top of the two most prominent land cover classes for this area, forest and cropland (Figure&nbsp;2):</p>
<div class="cell" data-fig.asp="0.4">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb5-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tm_shape</span>(flc1[[<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>)]]) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb5-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tm_raster</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">style =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"cont"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">palette =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"cividis"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">title =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Fraction:"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb5-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tm_shape</span>(flc_sc) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb5-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tm_borders</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">col =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"red"</span>)</span></code></pre></div></div>
<div class="cell-output-display">
<div id="fig-2" class="quarto-float quarto-figure quarto-figure-center anchored">
<figure class="quarto-float quarto-float-fig figure">
<div aria-describedby="fig-2-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
<img src="https://jakubnowosad.com/posts/2023-04-30-supercells-bp1/index_files/figure-html/fig-2-1.png" class="img-fluid figure-img" width="672">
</div>
<figcaption class="quarto-float-caption-bottom quarto-float-caption quarto-float-fig" id="fig-2-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
Figure&nbsp;2: Created supercells on the top of fractions of the forest and cropland classes
</figcaption>
</figure>
</div>
</div>
</div>
<p>This visual inspection allows us to see that supercells serve their purpose: they delineate areas with homogeneous arrangements of fractional land cover values. Areas with dominating fractions of forests are encapsulated in different polygons compared to there with dominating fractions of croplands, or some mixes of land cover classes. At the same time, some supercells are more homogeneous than others. <!-- For example, look at the supercell in the bottom left corner of our area: it primarily consists of cropland areas, but also has a road in the middle.  --> This is due to: (a) the set <img src="https://latex.codecogs.com/png.latex?S"> interval value (a lower value would result in a large number of more homogeneous supercells), and (b) the fact that supercells are not designed especially for roads (or other linear features) detection.</p>
<p>The quality of our result can also be determined numerically: we can calculate “inhomogeneity” of our supercells. The inhomogeneity metric represents an average distance between cells belonging to the same supercell. This value is small when all cells have similar values (land cover classes’ fractions, in our case), and large when cells’ values are very different.</p>
<p>Inhomogeneity can be calculated using the <strong>regional</strong> package’s function <code>reg_inhomogeneity()</code>. We just need to provide our “regions” (supercells), raster with values, and a distance function. Comparing values of many cells may take a lot of time; thus, usually, it is more efficient to use some subset of them for this comparison. We can specify the subset size with <code>sample_size</code>.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb6" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb6-1">vars <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Forest"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Shrubland"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Grassland"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Bare.Sparse.vegatation"</span>, </span>
<span id="cb6-2">         <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Cropland"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Built.up"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Seasonal.inland.water"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Permanent.inland.water"</span>)</span>
<span id="cb6-3">flc_sc<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>inh <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">reg_inhomogeneity</span>(flc_sc[vars], flc1, </span>
<span id="cb6-4">                                <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">dist_fun =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"jensen-shannon"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sample_size =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>)</span></code></pre></div></div>
</div>
<p>The resulting inhomogeneity values can also be visualized, signaling the most and the least consistent supercells (Figure&nbsp;3):</p>
<div class="cell" data-fig.asp="0.6">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb7" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb7-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tm_shape</span>(flc_sc) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb7-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tm_polygons</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"inh"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">title =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Inhomogeneity:"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">style =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"cont"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb7-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tm_layout</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">legend.outside =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>)</span></code></pre></div></div>
<div class="cell-output-display">
<div id="fig-3" class="quarto-float quarto-figure quarto-figure-center anchored">
<figure class="quarto-float quarto-float-fig figure">
<div aria-describedby="fig-3-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
<img src="https://jakubnowosad.com/posts/2023-04-30-supercells-bp1/index_files/figure-html/fig-3-1.png" class="img-fluid figure-img" width="672">
</div>
<figcaption class="quarto-float-caption-bottom quarto-float-caption quarto-float-fig" id="fig-3-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
Figure&nbsp;3: Inhomogeneity values of the created supercells
</figcaption>
</figure>
</div>
</div>
</div>
<p>Additionally, we can calculate an area-weighted inhomogeneity as a general metric of all the supercells:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb8" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb8-1">flc_sc<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>area_km2 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.numeric</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">st_area</span>(flc_sc)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1000000</span></span>
<span id="cb8-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">weighted.mean</span>(flc_sc<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>inh, flc_sc<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>area_km2)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 0.08516878</code></pre>
</div>
</div>
<p>Finally, you may notice that several adjacent supercells are very similar, and thus should be merged. Several approaches to merging supercells into larger segments/regions exist. I will discuss them in the next blog post.</p>
</section>
<section id="summary" class="level2">
<h2 class="anchored" data-anchor-id="summary">Summary</h2>
<p>We propose the SLIC algorithm extension to work with non-imagery data structures without data reduction and conversion to the false-color image. It allows for using a data distance measure most appropriate to a particular data structure and a custom function for averaging values of clusters centers. If you want to learn more about <strong>supercells</strong>, we encourage you to try a few entry points. One is the <span class="citation" data-cites="nowosad_extended_2022">Nowosad and Stepinski (2022)</span> article that explains the whole idea in more detail and compares our extension and original SLIC algorithms on three examples of non-imagery data. Code related to these examples is available at <a href="https://github.com/Nowosad/supercells-examples" class="uri">https://github.com/Nowosad/supercells-examples</a>. <!-- FOSS4G VIDEO!!! --> You can also see slides from a talk entitled “A method for universal superpixels-based regionalization” that I gave during the FOSS4G 2022 conference at <a href="https://jakubnowosad.com/foss4g-2022/" class="uri">https://jakubnowosad.com/foss4g-2022/</a>. Finally, the package has extensive documentation, including several vignettes, that can be found at <a href="https://jakubnowosad.com/supercells/" class="uri">https://jakubnowosad.com/supercells/</a>.</p>
<!-- # Acknowledgement -->



</section>


<div id="quarto-appendix" class="default"><section class="quarto-appendix-contents" id="quarto-bibliography"><h2 class="anchored quarto-appendix-heading">References</h2><div id="refs" class="references csl-bib-body hanging-indent">
<div id="ref-achanta_slic_2012" class="csl-entry">
Achanta, R., A. Shaji, K. Smith, A. Lucchi, P. Fua, and Sabine Süsstrunk. 2012. <span>“<span>SLIC Superpixels Compared</span> to <span class="nocase">State-of-the-Art Superpixel Methods</span>.”</span> <em>IEEE Transactions on Pattern Analysis and Machine Intelligence</em> 34 (11): 2274–82. <a href="https://doi.org/10/f39g5f">https://doi.org/10/f39g5f</a>.
</div>
<div id="ref-nowosad_extended_2022" class="csl-entry">
Nowosad, Jakub, and Tomasz F. Stepinski. 2022. <span>“Extended <span>SLIC</span> Superpixels Algorithm for Applications to Non-Imagery Geospatial Rasters.”</span> <em>International Journal of Applied Earth Observation and Geoinformation</em> 112 (August): 102935. <a href="https://doi.org/10.1016/j.jag.2022.102935">https://doi.org/10.1016/j.jag.2022.102935</a>.
</div>
<div id="ref-stutz_superpixels_2018" class="csl-entry">
Stutz, David, Alexander Hermans, and Bastian Leibe. 2018. <span>“Superpixels: <span>An Evaluation</span> of the <span class="nocase">State-of-the-Art</span>.”</span> <em>Computer Vision and Image Understanding</em> 166 (January): 1–27. <a href="https://doi.org/10/gcvnsc">https://doi.org/10/gcvnsc</a>.
</div>
</div></section><section id="footnotes" class="footnotes footnotes-end-of-document"><h2 class="anchored quarto-appendix-heading">Footnotes</h2>

<ol>
<li id="fn1"><p>On a side note: homogeneity does not always imply simplicity.↩︎</p></li>
<li id="fn2"><p>Let me know (email/twitter) if you have any examples of such data!↩︎</p></li>
<li id="fn3"><p>Supercells!↩︎</p></li>
<li id="fn4"><p>Read the <a href="https://jakubnowosad.com/supercells/articles/main-function.html">“The supercells() function”</a> vignette for more details.↩︎</p></li>
</ol>
</section><section class="quarto-appendix-contents" id="quarto-reuse"><h2 class="anchored quarto-appendix-heading">Reuse</h2><div class="quarto-appendix-contents"><div><a rel="license" href="https://creativecommons.org/licenses/by/4.0/">CC BY 4.0</a></div></div></section><section class="quarto-appendix-contents" id="quarto-citation"><h2 class="anchored quarto-appendix-heading">Citation</h2><div><div class="quarto-appendix-secondary-label">BibTeX citation:</div><div class="code-copy-outer-scaffold"><pre class="sourceCode quarto-appendix-bibtex code-with-copy"><code class="sourceCode bibtex">@online{nowosad2023,
  author = {Nowosad, Jakub},
  title = {Supercells: Universal Superpixels Algorithm for Applications
    to Geospatial Data},
  date = {2023-04-30},
  url = {https://jakubnowosad.com/posts/2023-04-30-supercells-bp1/},
  doi = {10.59350/18qc6-6r674},
  langid = {en}
}
</code></pre></div><div class="quarto-appendix-secondary-label">For attribution, please cite this work as:</div><div id="ref-nowosad2023" class="csl-entry quarto-appendix-citeas">
Nowosad, Jakub. 2023. <span>“Supercells: Universal Superpixels Algorithm
for Applications to Geospatial Data.”</span> April 30. <a href="https://doi.org/10.59350/18qc6-6r674">https://doi.org/10.59350/18qc6-6r674</a>.
</div></div></section></div> ]]></description>
  <category>spatial</category>
  <category>geocompr</category>
  <category>rstats</category>
  <category>segmentation</category>
  <category>regionalization</category>
  <guid>https://jakubnowosad.com/posts/2023-04-30-supercells-bp1/</guid>
  <pubDate>Sun, 30 Apr 2023 00:00:00 GMT</pubDate>
</item>
<item>
  <title>How to visualize landscape metrics for local landscapes?</title>
  <dc:creator>Jakub Nowosad</dc:creator>
  <link>https://jakubnowosad.com/posts/2022-02-17-lsm-bp3/</link>
  <description><![CDATA[ 





<p>In the past, I wrote <a href="../../posts/2019-03-07-lsm-bp1/">a post on how to calculate landscape-level metrics for local landscapes</a>. There, I showed how to divide the categorical input map into a number of smaller areas and calculate selected landscape metrics for each area. The result consists of a regular grid - a number of square polygons, where each polygon contained just one value of a calculated landscape-level metric (e.g., marginal entropy - <code>ent</code>). This output is easy to visualize on a map - each polygon can be colored accordingly to its value.</p>
<p>There are, although, two more levels of landscape metrics - the class- and patch-levels. Calculation of class-level metrics returns as many values as unique classes in a local landscape, while the patch-level calculations result in as many values as there are patches<sup>1</sup> in each polygon. This makes simple visualizations of class- and patch-level metrics not as straightforward as landscape-level metrics. The main goal of this blog post is to present different approaches for visualization of class- and patch-level metrics.</p>
<p>To reproduce the following results on your own computer, install and attach the packages:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(landscapemetrics)     <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># landscape metrics calculation</span></span>
<span id="cb1-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(raster)               <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># spatial raster data reading and handling</span></span>
<span id="cb1-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(sf)                   <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># spatial vector data reading and handling</span></span>
<span id="cb1-4"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(dplyr)                <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># data manipulation</span></span>
<span id="cb1-5"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(tidyr)                <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># data manipulation</span></span>
<span id="cb1-6"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(tmap)                 <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># spatial viz</span></span>
<span id="cb1-7"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(geofacet)             <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># geofacet</span></span>
<span id="cb1-8"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(ggplot2)              <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># geofacet</span></span></code></pre></div></div>
</div>
<section id="reading-the-data" class="level1">
<h1>Reading the data</h1>
<p>The first step is to read the input data. Here, we are going to use example data that is already included in the <strong>landscapemetrics</strong> package <span class="citation" data-cites="R-landscapemetrics">(Hesselbarth et al. 2019)</span>.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb2-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">data</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"augusta_nlcd"</span>)</span>
<span id="cb2-2">my_raster <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> augusta_nlcd</span></code></pre></div></div>
</div>
<p>It is also possible to read any spatial raster file with the <code>raster()</code> function, for example <code>my_raster = raster("path_to_my_file.tif")</code><sup>2</sup>. However, the input file should fulfill two requirements: (1) contain only integer values that represent categories, and (2) be in a projected coordinate reference system. You can check if your file meets the requirements using the <code>check_landscape()</code> function, and learn more about coordinate reference systems in <a href="https://geocompr.robinlovelace.net/reproj-geo-data.html">the Geocomputation with R book</a> <span class="citation" data-cites="lovelace_geocomputation_2019">(Lovelace et al. 2019)</span>.</p>
<p>Our example data looks like that:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb3-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">plot</span>(my_raster)</span></code></pre></div></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://jakubnowosad.com/posts/2022-02-17-lsm-bp3/index_files/figure-html/index-3-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
</section>
<section id="creating-a-grid" class="level1">
<h1>Creating a grid</h1>
<p>The next step is to create borders of local landscapes using the <code>st_make_grid()</code> function. This function accepts an <code>sf</code> object as the first argument, therefore we need to create a new object based on the bounding box of the input raster. Next, we also need to provide a second argument, either <code>cellsize</code> or <code>n</code>:</p>
<ul>
<li><code>cellsize</code> - vector of length 1 or 2 - the side length of each grid cell in map units (usually meters)</li>
<li><code>n</code> - vector of length 1 or 2 - the number of grid cells in a row/column</li>
</ul>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb4-1">my_grid_geom <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">st_make_grid</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">st_as_sfc</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">st_bbox</span>(my_raster)), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">cellsize =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1500</span>)</span>
<span id="cb4-2">my_grid_template <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">st_sf</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">geom =</span> my_grid_geom)</span></code></pre></div></div>
</div>
<p>We should also add a unique identification number (<code>id</code>) to each grid cell (local landscape).</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb5-1">my_grid_template<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>plot_id <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">seq_len</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">nrow</span>(my_grid_template))</span></code></pre></div></div>
</div>
<p>Next, we can overlay the newly created grid on top of our input raster:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb6" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb6-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">plot</span>(my_raster)</span>
<span id="cb6-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">plot</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">st_geometry</span>(my_grid_template), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">add =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>)</span></code></pre></div></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://jakubnowosad.com/posts/2022-02-17-lsm-bp3/index_files/figure-html/index-6-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://jakubnowosad.com/posts/2022-02-17-lsm-bp3/index_files/figure-html/index-6-2.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<p>Note that some cells cover smaller areas with data than the others.</p>
</section>
<section id="calculating-a-class-level-metric" class="level1">
<h1>Calculating a class-level metric</h1>
<p>The calculation of landscape metrics for each cell can be done with the <code>sample_lsm()</code> function. It requires an input raster as the first argument, and a grid as the second one<sup>3</sup>. The function calculates the selected landscape metric independenly for each cell. Next, we can specify which landscape metrics we want to calculate. For this example, we use aggregation index (<em>ai</em>) to be calculated on a class level^[The complete list of the implemented metrics can be obtained with the <code>list_lsm()</code> function. <a href="https://github.com/r-spatialecology/landscapemetrics/issues/new">Let us know</a> if you are missing some metrics.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb7" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb7-1">my_metric1 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sample_lsm</span>(my_raster, my_grid_template,</span>
<span id="cb7-2">                       <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">level =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"class"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">metric =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ai"</span>)</span>
<span id="cb7-3">my_metric1</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code># A tibble: 1,313 × 8
   layer level class    id metric value plot_id percentage_inside
   &lt;int&gt; &lt;chr&gt; &lt;int&gt; &lt;int&gt; &lt;chr&gt;  &lt;dbl&gt;   &lt;int&gt;             &lt;dbl&gt;
 1     1 class    11    NA ai      83.3       1               100
 2     1 class    21    NA ai      34.5       1               100
 3     1 class    22    NA ai      23.0       1               100
 4     1 class    31    NA ai      86.8       1               100
 5     1 class    41    NA ai      63.9       1               100
 6     1 class    42    NA ai      73.2       1               100
 7     1 class    43    NA ai      39.5       1               100
 8     1 class    52    NA ai      43.1       1               100
 9     1 class    71    NA ai      66.9       1               100
10     1 class    81    NA ai      85.8       1               100
# … with 1,303 more rows</code></pre>
</div>
</div>
<p>Each row in the <code>my_metric</code> object corresponds to one calculated value of <em>ai</em>, while the <code>plot_id</code> column specifies to which grid cell the results are related <sup>4</sup>. Because there are several classes in each cell, there are also several <em>ai</em> values for each cell present. Next, we can connect spatial grid (<code>my_grid_template</code>) with the calculation results (<code>my_metric1</code>) using the <code>left_join()</code> function:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb9" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb9-1">my_grid1 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">left_join</span>(my_grid_template, my_metric1, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">by =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"plot_id"</span>)</span></code></pre></div></div>
</div>
</section>
<section id="vizualizing-the-class-level-results" class="level1">
<h1>Vizualizing the class-level results</h1>
<p>For the class-level results, each local landscape has as many values as unique classes belong to it. It prevents us from creating a single comprehensive map, but, on the other hand, allows for some other visualizations.</p>
<section id="subsets" class="level2">
<h2 class="anchored" data-anchor-id="subsets">Subsets</h2>
<p>The most basic approach is to create a map just for a selected class, which can be done with the <code>subset()</code> function:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb10" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb10-1">my_grid1_class1 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">subset</span>(my_grid1, class <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">11</span>)</span>
<span id="cb10-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">plot</span>(my_grid1_class1[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"value"</span>])</span></code></pre></div></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://jakubnowosad.com/posts/2022-02-17-lsm-bp3/index_files/figure-html/unnamed-chunk-1-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<p>The above plot shows the distribution of <em>ai</em> values for class 11.</p>
</section>
<section id="map-facets" class="level2">
<h2 class="anchored" data-anchor-id="map-facets">Map facets</h2>
<p>It is also possible to quickly visualize all of the subsets at the same time with the <strong>tmap</strong> package <span class="citation" data-cites="R-tmap">(Tennekes 2018)</span>.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb11" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb11-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tm_shape</span>(my_grid1) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb11-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tm_polygons</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"value"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">style =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"cont"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">title =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ai"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb11-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tm_facets</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">by =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"class"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">free.coords =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>)</span></code></pre></div></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://jakubnowosad.com/posts/2022-02-17-lsm-bp3/index_files/figure-html/unnamed-chunk-2-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<p>The result here contains a separate panel for each unique class in our dataset, while colors represent different AI values.</p>
</section>
<section id="geofacet" class="level2">
<h2 class="anchored" data-anchor-id="geofacet">geofacet</h2>
<p>As our local landscapes are made of a regular grid, we can also test some less traditional visualizations. One possibility is to use the <strong>geofacet</strong> package <span class="citation" data-cites="R-geofacet">(Hafen 2020)</span> - it allows to create many regular plots (such as histograms, scatterplots, boxplots, etc.), but arrange them spatially. Visit the package website at https://github.com/hafen/geofacet to find more examples.</p>
<p>The first step here is to create a plotting grid with <code>grid_auto()</code>:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb12" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb12-1">grd <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">grid_auto</span>(my_grid_template, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">names =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"plot_id"</span>)</span></code></pre></div></div>
</div>
<p>Next, we can create a plot with the <strong>ggplot2</strong> syntax <span class="citation" data-cites="R-ggplot2">(Wickham 2016)</span> adding <code>facet_geo(~plot_id, grid = grd)</code> to it:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb13" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb13-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(my_grid1, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.factor</span>(class), value, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.factor</span>(class))) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb13-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_col</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb13-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">facet_geo</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span>plot_id, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">grid =</span> grd) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb13-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NULL</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ai"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Class"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb13-5">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">axis.text.x =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_blank</span>(),</span>
<span id="cb13-6">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">axis.ticks.x =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_blank</span>(),</span>
<span id="cb13-7">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">strip.background =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_blank</span>(),</span>
<span id="cb13-8">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">strip.text.x =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_blank</span>())</span></code></pre></div></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://jakubnowosad.com/posts/2022-02-17-lsm-bp3/index_files/figure-html/index-10-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<p>Here, we can see a visualization that consists of many separate bar plots. Each bar plot represents <em>ai</em> values for each local landscape.</p>
</section>
</section>
<section id="calculating-a-patch-level-metric" class="level1">
<h1>Calculating a patch-level metric</h1>
<p>The calculation of patch-level metrics can also be done with the <code>sample_lsm()</code> function. In this example, we use euclidean nearest-neighbor distance (enn) - a metric that can only be calculated on a patch level.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb14" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb14-1">my_metric2 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sample_lsm</span>(my_raster, my_grid_template,</span>
<span id="cb14-2">                       <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">level =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"patch"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">metric =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"enn"</span>)</span>
<span id="cb14-3">my_metric2</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code># A tibble: 10,479 × 8
   layer level class    id metric value plot_id percentage_inside
   &lt;int&gt; &lt;chr&gt; &lt;int&gt; &lt;int&gt; &lt;chr&gt;  &lt;dbl&gt;   &lt;int&gt;             &lt;dbl&gt;
 1     1 patch    11     1 enn    201.        1               100
 2     1 patch    11     2 enn    201.        1               100
 3     1 patch    11     3 enn    495.        1               100
 4     1 patch    21     4 enn    433.        1               100
 5     1 patch    21     5 enn     67.1       1               100
 6     1 patch    21     6 enn     67.1       1               100
 7     1 patch    21     7 enn     67.1       1               100
 8     1 patch    21     8 enn     67.1       1               100
 9     1 patch    21     9 enn     60         1               100
10     1 patch    21    10 enn     60         1               100
# … with 10,469 more rows</code></pre>
</div>
</div>
<p>The result contains a large number of rows, where each row is related to a unique combination of a patch, a class, and a grid cell. We can connect spatial grid (<code>my_grid_template</code>) with the calculation results (<code>my_metric2</code>) again using the <code>left_join()</code> function:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb16" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb16-1">my_grid2 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">left_join</span>(my_grid_template, my_metric2, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">by =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"plot_id"</span>)</span></code></pre></div></div>
</div>
</section>
<section id="vizualizing-the-patch-level-results" class="level1">
<h1>Vizualizing the patch-level results</h1>
<p>Visualization of the patch-level results could be the most challenging to create. Here, each local landscape can have as little as one value (a single large patch) and as many values as cells. Patches also can be related to just a few or to many different classes.</p>
<section id="one-patch-map" class="level2">
<h2 class="anchored" data-anchor-id="one-patch-map">One patch map</h2>
<p>The most straightforward approach here is to use the <code>spatialize_lsm()</code> function, which calculates a selected patch-level metric for a given raster, and returns a new raster with the metric values.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb17" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb17-1">my_metric_r_all <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">spatialize_lsm</span>(my_raster, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">level =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"patch"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">metric =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"enn"</span>)</span></code></pre></div></div>
</div>
<p>The output of <code>spatialize_lsm()</code> is a nested list. Each list element is used to represent different input layers (e.g., land cover data for different years), while each sub-list element relates to the calculated metric (you are able to calculate many metrics at the same time).</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb18" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb18-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">plot</span>(my_metric_r_all<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>layer_1<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>lsm_p_enn)</span></code></pre></div></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://jakubnowosad.com/posts/2022-02-17-lsm-bp3/index_files/figure-html/unnamed-chunk-4-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<p>The <code>spatialize_lsm()</code> function is fine when we need to present patch-level values for a whole raster. However, if we have many local landscapes (or sub-rasters) then we need to repeat the <code>spatialize_lsm()</code> calculations for each area.</p>
</section>
<section id="patch-maps" class="level2">
<h2 class="anchored" data-anchor-id="patch-maps">Patch maps</h2>
<p>This can be done with the help of the following code.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb19" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb19-1">patch_raster <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(my_raster, my_grid){</span>
<span id="cb19-2">  result <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">vector</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mode =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"list"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">length =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">nrow</span>(my_grid))</span>
<span id="cb19-3">  <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> (i <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">seq_len</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">nrow</span>(my_grid))){</span>
<span id="cb19-4">    my_small_raster <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">crop</span>(my_raster, my_grid[i, ])</span>
<span id="cb19-5">    result[[i]] <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">spatialize_lsm</span>(my_small_raster,</span>
<span id="cb19-6">                                 <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">level =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"patch"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">metric =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"enn"</span>)</span>
<span id="cb19-7">  }</span>
<span id="cb19-8">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">return</span>(result)</span>
<span id="cb19-9">}</span>
<span id="cb19-10"></span>
<span id="cb19-11">my_metric_r <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">patch_raster</span>(my_raster, my_grid_template)</span>
<span id="cb19-12">my_metric_r <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">unlist</span>(my_metric_r)</span>
<span id="cb19-13"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">names</span>(my_metric_r) <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NULL</span></span>
<span id="cb19-14">my_metric_r <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">do.call</span>(merge, my_metric_r)</span></code></pre></div></div>
</div>
<p>In it, we go through each grid cell, crop a raster to its extent, and calculate our metric of interest. The result is a large list consisting of many separate small rasters, that we can combine to get a full-size raster in return with <code>do.call()</code> and <code>merge()</code>.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb20" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb20-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">plot</span>(my_metric_r)</span>
<span id="cb20-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">plot</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">st_geometry</span>(my_grid_template), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">add =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>)</span></code></pre></div></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://jakubnowosad.com/posts/2022-02-17-lsm-bp3/index_files/figure-html/unnamed-chunk-6-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<p>The resulting visualization looks much different from the previous one. In it, each local landscape is treated independently, therefore, a patch belonging to many grid cells is split into many patches.</p>
</section>
</section>
<section id="summary" class="level1">
<h1>Summary</h1>
<p>The <strong>landscapemetrics</strong> package, together with many other open-source R packages, allows for spatial visualizations of class- and patch-level metrics. The class-level metrics can be presented independently for each class, or as a <em>geofacet</em> plot, while patch-level metrics might be visualized in combination with the <code>spatialize_lsm()</code> function. It is also worth to mention that patch-level metrics can also use the <strong>geofacet</strong> package; however, this could work best when the number of local landscapes is relatively small. Otherwise, the resulting plot could be hard to read. To learn more about landscape metrics and the <strong>landscapemetrics</strong> package, visit https://r-spatialecology.github.io/landscapemetrics/ and http://dx.doi.org/10.1111/ecog.04617.</p>
</section>
<section id="acknowledgement" class="level1">
<h1>Acknowledgement</h1>
<p>Many thanks to <a href="https://mhesselbarth.rbind.io/">Maximilian H.K. Hesselbarth</a> for reading and improving a draft of this blog post.</p>



</section>


<div id="quarto-appendix" class="default"><section class="quarto-appendix-contents" id="quarto-bibliography"><h2 class="anchored quarto-appendix-heading">References</h2><div id="refs" class="references csl-bib-body hanging-indent">
<div id="ref-R-geofacet" class="csl-entry">
Hafen, Ryan. 2020. <em>Geofacet: ’Ggplot2’ Faceting Utilities for Geographical Data</em>. <a href="https://CRAN.R-project.org/package=geofacet">https://CRAN.R-project.org/package=geofacet</a>.
</div>
<div id="ref-R-landscapemetrics" class="csl-entry">
Hesselbarth, Maximilian H. K., Marco Sciaini, Kimberly A. With, Kerstin Wiegand, and Jakub Nowosad. 2019. <span>“Landscapemetrics: An Open-Source r Tool to Calculate Landscape Metrics.”</span> <em>Ecography</em> 42: 1648–57.
</div>
<div id="ref-lovelace_geocomputation_2019" class="csl-entry">
Lovelace, Robin, Jakub Nowosad, and Jannes Muenchow. 2019. <em>Geocomputation with <span>R</span></em>. <span>CRC Press</span>.
</div>
<div id="ref-R-tmap" class="csl-entry">
Tennekes, Martijn. 2018. <span>“<span class="nocase">tmap</span>: Thematic Maps in <span>R</span>.”</span> <em>Journal of Statistical Software</em> 84 (6): 1–39. <a href="https://doi.org/10.18637/jss.v084.i06">https://doi.org/10.18637/jss.v084.i06</a>.
</div>
<div id="ref-R-ggplot2" class="csl-entry">
Wickham, Hadley. 2016. <em>Ggplot2: Elegant Graphics for Data Analysis</em>. Springer-Verlag New York. <a href="https://ggplot2.tidyverse.org">https://ggplot2.tidyverse.org</a>.
</div>
</div></section><section id="footnotes" class="footnotes footnotes-end-of-document"><h2 class="anchored quarto-appendix-heading">Footnotes</h2>

<ol>
<li id="fn1"><p>A patch is a group of adjacent cells of the same category.↩︎</p></li>
<li id="fn2"><p>Currently <strong>landscapemetrics</strong> accepts also objects from the <strong>terra</strong> and <strong>stars</strong> packages.↩︎</p></li>
<li id="fn3"><p>This function also allows for many more possibilities, including specifying a 2-column matrix with coordinates, SpatialPoints, SpatialLines, SpatialPolygons, sf points or sf polygons as the second argument. You can learn all of the valid options using <code>?sample_lsm</code>.↩︎</p></li>
<li id="fn4"><p>To learn more about the structure of the output read <a href="../../posts/2019-03-07-lsm-bp1/">the Efficient landscape metrics calculations for buffers around sampling points blog post</a>.↩︎</p></li>
</ol>
</section><section class="quarto-appendix-contents" id="quarto-reuse"><h2 class="anchored quarto-appendix-heading">Reuse</h2><div class="quarto-appendix-contents"><div><a rel="license" href="https://creativecommons.org/licenses/by/4.0/">CC BY 4.0</a></div></div></section><section class="quarto-appendix-contents" id="quarto-citation"><h2 class="anchored quarto-appendix-heading">Citation</h2><div><div class="quarto-appendix-secondary-label">BibTeX citation:</div><div class="code-copy-outer-scaffold"><pre class="sourceCode quarto-appendix-bibtex code-with-copy"><code class="sourceCode bibtex">@online{nowosad2022,
  author = {Nowosad, Jakub},
  title = {How to Visualize Landscape Metrics for Local Landscapes?},
  date = {2022-02-17},
  url = {https://jakubnowosad.com/posts/2022-02-17-lsm-bp3/},
  doi = {10.59350/h7t90-fq075},
  langid = {en}
}
</code></pre></div><div class="quarto-appendix-secondary-label">For attribution, please cite this work as:</div><div id="ref-nowosad2022" class="csl-entry quarto-appendix-citeas">
Nowosad, Jakub. 2022. <span>“How to Visualize Landscape Metrics for
Local Landscapes?”</span> February 17. <a href="https://doi.org/10.59350/h7t90-fq075">https://doi.org/10.59350/h7t90-fq075</a>.
</div></div></section></div> ]]></description>
  <category>spatial</category>
  <category>geocompr</category>
  <category>rstats</category>
  <category>landscape-ecology</category>
  <guid>https://jakubnowosad.com/posts/2022-02-17-lsm-bp3/</guid>
  <pubDate>Thu, 17 Feb 2022 00:00:00 GMT</pubDate>
</item>
<item>
  <title>Considerations for the pattern-based spatial analysis</title>
  <dc:creator>Jakub Nowosad</dc:creator>
  <link>https://jakubnowosad.com/posts/2021-03-10-motif-bp6/</link>
  <description><![CDATA[ 





<p>TLTR:</p>
<div class="cell" data-layout-align="right">
<div class="cell-output-display">
<div class="quarto-figure quarto-figure-right">
<figure class="figure">
<p><img src="https://jakubnowosad.com/posts/2021-03-10-motif-bp6/figs/logo.png" class="img-fluid quarto-figure quarto-figure-right figure-img" width="150"></p>
</figure>
</div>
</div>
</div>
<!-- <img src="figs/logo.png" align="right" width="150" /> -->
<p>This is a last blog post in a series about <strong>motif</strong> - an R package aimed for pattern-based spatial analysis. It sums up previous posts, but also underlines potential considerations when working with spatial patterns. Finally, it lists underexplored topics and future ideas related to pattern-based spatial analysis.</p>
<section id="pattern-based-spatial-analysis" class="level1">
<h1>Pattern-based spatial analysis</h1>
<p><a href="../../posts/2021-02-03-motif-bp1">The first blog post in this series</a> introduces a basic concept of categorical data spatial patterns, and why commonly used landscape metrics are not best suited for finding areas with similar spatial patterns. A better approach is to derive a spatial signature - a multi-number description that compactly stores information about the composition and configuration of a spatial pattern.</p>
<p><a href="../../posts/2021-02-10-motif-bp2">In the second blog post</a> presents some basic spatial signatures, including <em>coma</em> (co-occurrence matrix) for single-variable categorical rasters, <em>wecoma</em> (weighted co-occurrence matrix) for single-variable categorical rasters that have another continuous raster representing the intensity of categories, and <em>incoma</em> (integrated co-occurrence matrix) for categorical rasters with two or more variables. All of the mentioned spatial signatures can be converted into a 1D vector - a probability function, and similarity between probability functions can be calculated using one of many distance measures (e.g., Jenson-Shannon distance). Now, having spatial signatures for two areas, we can find out how similar (or dissimilar) they are. This allows us to find the most similar rasters, describe changes between rasters, or group (cluster) rasters based on the spatial patterns.</p>
<p><a href="../../posts/2021-02-17-motif-bp3">The third blog post</a> shows how we can search for areas with similar spatial patterns to a query region based on an example of finding areas of similar topography to the area of Suwalski Landscape Park. In the search process, spatial signatures are derived for the query region and many sub-areas of the search space, and distances between them are calculated. Next, sub-areas with the smallest distances from the query region are assumed to be the most similar to it.</p>
<div class="cell">
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://jakubnowosad.com/posts/2021-03-10-motif-bp6/figs/change.png" class="img-fluid figure-img" style="width:100.0%"></p>
</figure>
</div>
</div>
</div>
<p><a href="../../posts/2021-02-24-motif-bp4">The fourth blog post</a> focused on finding areas with the largest change of land cover patterns in the Amazon between 1992 and 2018. The land cover data from the Amazon in 1992 and 2018 were subdivided into areas of 90 by 90 kilometers, and a spatial signature was calculated for each subarea in each year. Then, a distance between spatial signatures for each subarea was derived, with large distance values indicating a large change of spatial patterns.</p>
<p><a href="../../posts/2021-03-03-motif-bp5">The fifth blog post</a> showcases clustering of similar spatial patterns of joint spatial patterns of land cover and landforms in Africa. In this process, Africa was divided into many sub-areas and spatial signatures were derived for each sub-area. Distances between signatures for each sub-area were calculated and stored in a distance matrix, which was used as a basis for the creation of clusters of similar spatial patterns. The quality of clusters was assessed visually using a pattern mosaic and with dedicated quality metrics.</p>
<div class="cell" data-preview="true">
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://jakubnowosad.com/posts/2021-03-10-motif-bp6/figs/clust.png" class="img-fluid figure-img" style="width:100.0%"></p>
</figure>
</div>
</div>
</div>
</section>
<section id="potential-applications" class="level1">
<h1>Potential applications</h1>
<p>The role of the presented examples is to highlight the universality and extensibility of the pattern-based methods. They could be used in a wide range of local, regional, and global studies of global environmental changes, land management, sustainable development, environmental protection, forest cover change, urban growth monitoring, or agriculture expansion studies. Some example research ideas include:</p>
<ul>
<li>studying global environmental changes by analysis of changes in patterns of different environmental features, such as land cover,</li>
<li>delineating of ecoregions - regionalization of land into homogeneous units of similar ecological and physiographic features (land cover, landform, soils, climate),</li>
<li>clustering of forest patterns, which results could be used for conservation, planning, and management</li>
<li>identifying spatial patterns of cropland usage</li>
<li>inventorying of landscape patterns and analysis of landscape spatial configuration</li>
</ul>
<!-- check google scholar examples -->
<p>Additionally, the pattern-based spatial analysis methods and tools could be useful in various other disciplines that use categorical images, for example, medical science, astronomy, or social studies.</p>
</section>
<section id="study-considerations" class="level1">
<h1>Study considerations</h1>
<p>However, no matter if we analyze patterns in an environmental raster, demographic map, or categorized microscope image, we need to consider several questions.</p>
<p>How should we preprocess the input data? For example, do we need all 18 categories in our data, or is it better to simplify the number of categories to improve analysis and streamline interpretation of the results? When we are interested in forest fragmentation, do we really need several other land cover classes, or can we merge them into one or two categories? Additionally, preprocessing can be applied to derive new categories from the data. An example of this was shown in <a href="../../posts/2021-02-17-motif-bp3/">the third blog post</a>, where elevation data was first converted into geomorphons before applying any other steps. Reprojecting of the input data may also be important in some cases. In the pattern-based spatial analysis, each cell is treated equally, which means that we usually want to apply data in some equal-area projection.</p>
<p>What is the scale of the process we want to study? Are we interested in investigating patterns in 10 by 10 cell windows or maybe 100 by 100 cell windows? If we do not have any prior information or expectation about spatial scale, then there are two general approaches that could help. Firstly, we could apply the same analysis steps a few times using different sizes of a local window, and decide on a proper spatial scale afterward. Secondly, we could use the smallest meaningful windows we can think of<sup>1</sup>, for example, 10 by 10 cells, and then apply the clustering process. After merging similar areas into larger regions, we can decide the spatial scale of homogeneous spatial patterns.</p>
<p>Which signature should we apply? The <em>coma</em> representation was developed for single-variable categorical rasters, <em>wecoma</em> for single-variable categorical rasters that have another continuous raster representing the intensity of categories, and <em>incoma</em> for categorical rasters with two or more variables. Which of the above representation suits your problem the best? Or maybe you need to create some new signature focused on the specifics of your case?</p>
<p>Which distance measure should we use? A few dozen of distance/dissimilarity measures exist<sup>2</sup>. Our previous experiences showed that the Jensen-Shannon distance is suitable to describe relations between spatial patterns of land cover data. However, there is no free lunch in selecting a distance measure, and I would usually recommend trying out a few measures before deciding on one of them.</p>
</section>
<section id="general-considerations-and-future-work" class="level1">
<h1>General considerations and future work</h1>
<p>There are also general considerations that would gain from establishing a consistent methodology. For example, how to decide which scale is valid? What type of signatures are still missing and should be developed? How to integrate categorical and continuous spatial patterns in an analysis? What are the advantages and disadvantages of using different distance measures? What are the missing workflows that can be added to the pattern-based spatial analysis?</p>
<p>I encourage everyone to submit <a href="https://github.com/Nowosad/motif/issues">their issues or enhancement requests to the <strong>motif</strong> package</a>, which will help me to prioritize my work. Furthermore, if you have any questions or ideas related to the pattern-based spatial analysis, please email me at nowosad.jakub@gmail.com.</p>


</section>


<div id="quarto-appendix" class="default"><section id="footnotes" class="footnotes footnotes-end-of-document"><h2 class="anchored quarto-appendix-heading">Footnotes</h2>

<ol>
<li id="fn1"><p>This depends on the number of categories, their spatial arrangements, etc.↩︎</p></li>
<li id="fn2"><p>Read https://users.uom.gr/~kouiruki/sung.pdf for a comprehensive review of distance measures.↩︎</p></li>
</ol>
</section><section class="quarto-appendix-contents" id="quarto-reuse"><h2 class="anchored quarto-appendix-heading">Reuse</h2><div class="quarto-appendix-contents"><div><a rel="license" href="https://creativecommons.org/licenses/by/4.0/">CC BY 4.0</a></div></div></section><section class="quarto-appendix-contents" id="quarto-citation"><h2 class="anchored quarto-appendix-heading">Citation</h2><div><div class="quarto-appendix-secondary-label">BibTeX citation:</div><div class="code-copy-outer-scaffold"><pre class="sourceCode quarto-appendix-bibtex code-with-copy"><code class="sourceCode bibtex">@online{nowosad2021,
  author = {Nowosad, Jakub},
  title = {Considerations for the Pattern-Based Spatial Analysis},
  date = {2021-03-10},
  url = {https://jakubnowosad.com/posts/2021-03-10-motif-bp6/},
  doi = {10.59350/7as1a-z2j78},
  langid = {en}
}
</code></pre></div><div class="quarto-appendix-secondary-label">For attribution, please cite this work as:</div><div id="ref-nowosad2021" class="csl-entry quarto-appendix-citeas">
Nowosad, Jakub. 2021. <span>“Considerations for the Pattern-Based
Spatial Analysis.”</span> March 10. <a href="https://doi.org/10.59350/7as1a-z2j78">https://doi.org/10.59350/7as1a-z2j78</a>.
</div></div></section></div> ]]></description>
  <category>spatial</category>
  <category>geocompr</category>
  <category>rstats</category>
  <category>landscape-ecology</category>
  <category>spatial-patterns</category>
  <category>sil</category>
  <category>motif</category>
  <guid>https://jakubnowosad.com/posts/2021-03-10-motif-bp6/</guid>
  <pubDate>Wed, 10 Mar 2021 00:00:00 GMT</pubDate>
</item>
<item>
  <title>Clustering similar spatial patterns</title>
  <dc:creator>Jakub Nowosad</dc:creator>
  <link>https://jakubnowosad.com/posts/2021-03-03-motif-bp5/</link>
  <description><![CDATA[ 





<p>TLTR:</p>
<div class="cell" data-layout-align="right">
<div class="cell-output-display">
<div class="quarto-figure quarto-figure-right">
<figure class="figure">
<p><img src="https://jakubnowosad.com/posts/2021-03-03-motif-bp5/figs/logo.png" class="img-fluid quarto-figure quarto-figure-right figure-img" width="150"></p>
</figure>
</div>
</div>
</div>
<!-- <img src="figs/logo.png" align="right" width="150" /> -->
<p>Clustering similar spatial patterns requires one or more raster datasets for the same area. Input data is divided into many sub-areas, and spatial signatures are derived for each sub-area. Next, distances between signatures for each sub-area are calculated and stored in a distance matrix. The distance matrix can be used to create clusters of similar spatial patterns. Quality of clusters can be assessed visually using a pattern mosaic or with dedicated quality metrics.</p>
<section id="spatial-data" class="level1">
<h1>Spatial data</h1>
<p>To reproduce the calculations in the following post, you need to download all of relevant datasets using the code below:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(osfr)</span>
<span id="cb1-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">dir.create</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"data"</span>)</span>
<span id="cb1-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">osf_retrieve_node</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"xykzv"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb1-4">        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">osf_ls_files</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">n_max =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">Inf</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb1-5">        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">osf_download</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">path =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"data"</span>,</span>
<span id="cb1-6">                     <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">conflicts =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"overwrite"</span>)</span></code></pre></div></div>
</div>
<p>You should also attach the following packages:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb2-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(sf)</span>
<span id="cb2-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(stars)</span>
<span id="cb2-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(motif)</span>
<span id="cb2-4"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(tmap)</span>
<span id="cb2-5"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(dplyr)</span>
<span id="cb2-6"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(readr)</span></code></pre></div></div>
</div>
</section>
<section id="land-cover-and-landforms-in-africa" class="level1">
<h1>Land cover and landforms in Africa</h1>
<p>The <code>data/land_cover.tif</code> contains land cover data and <code>data/landform.tif</code> is landform data for Africa. Both are single categorical rasters of the same extent and the same resolution (300 meters) that can be read into R using the <code>read_stars()</code> function.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb3-1">lc <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">read_stars</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"data/land_cover.tif"</span>)</span>
<span id="cb3-2">lf <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">read_stars</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"data/landform.tif"</span>)</span></code></pre></div></div>
</div>
<p>Additionally, the <code>data/lc_palette.csv</code> file contains information about colors and labels of each land cover category, and <code>data/lf_palette.csv</code> stores information about colors and labels of each landform class.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb4-1">lc_palette_df <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">read_csv</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"data/lc_palette.csv"</span>)</span>
<span id="cb4-2">lf_palette_df <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">read_csv</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"data/lf_palette.csv"</span>)</span>
<span id="cb4-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">names</span>(lc_palette_df<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>color) <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> lc_palette_df<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>value</span>
<span id="cb4-4"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">names</span>(lf_palette_df<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>color) <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> lf_palette_df<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>value</span></code></pre></div></div>
</div>
<p>Both datasets can be visualized with <strong>tmap</strong>.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb5-1">tm_lc <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tm_shape</span>(lc) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb5-2">        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tm_raster</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">style =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"cat"</span>,</span>
<span id="cb5-3">                  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">palette =</span> lc_palette_df<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>color,</span>
<span id="cb5-4">                  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">labels =</span> lc_palette_df<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>label,</span>
<span id="cb5-5">                  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">title =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Land cover:"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb5-6">        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tm_layout</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">legend.position =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"LEFT"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"BOTTOM"</span>))</span>
<span id="cb5-7">tm_lc</span></code></pre></div></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://jakubnowosad.com/posts/2021-03-03-motif-bp5/index_files/figure-html/unnamed-chunk-6-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb6" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb6-1">tm_lf <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tm_shape</span>(lf) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb6-2">        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tm_raster</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">style =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"cat"</span>,</span>
<span id="cb6-3">                  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">palette =</span> lf_palette_df<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>color,</span>
<span id="cb6-4">                  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">labels =</span> lf_palette_df<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>label,</span>
<span id="cb6-5">                  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">title =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Landform:"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb6-6">        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tm_layout</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">legend.outside =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>)</span>
<span id="cb6-7">tm_lf</span></code></pre></div></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://jakubnowosad.com/posts/2021-03-03-motif-bp5/index_files/figure-html/unnamed-chunk-7-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<p>We can combine these two datasets together with the <code>c()</code> function.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb7" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb7-1">eco_data <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(lc, lf)</span></code></pre></div></div>
</div>
<p>The problem now is how to find clusters of similar spatial patterns of both land cover categories and landform classes.</p>
</section>
<section id="clustering-spatial-patterns" class="level1">
<h1>Clustering spatial patterns</h1>
<!-- a new paper ref -->
<p>The basic step in clustering spatial patterns is to calculate a proper signature for each spatial window using the <code>lsp_signature()</code> function. Here, we use the <em>in</em>tegrated <em>co</em>-occurrence <em>ve</em>ctor (<code>type = "cove"</code>) representation. <!-- explain --> In this example, we use a window of 300 cells by 300 cells (<code>window = 300</code>). This means that our search scale will be 90 km (300 cells x data resolution) - resulting in dividing the whole area into about 7,500 regular rectangles of 90 by 90 kilometers.</p>
<p>This operation could take a few minutes.</p>
<div class="cell" data-hash="index_cache/html/eco_signature_291ffdbca6e950fc81ffc17b6a2ab752">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb8" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb8-1">eco_signature <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lsp_signature</span>(eco_data,</span>
<span id="cb8-2">                              <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">type =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"incove"</span>,</span>
<span id="cb8-3">                              <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">window =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">300</span>)</span></code></pre></div></div>
</div>
<p>The output, <code>eco_signature</code> contains numerical representation for each 90 by 90 km area. Notice that it has 3,838 rows (not 7,500) - this is due to removing areas with a large number of missing values before calculations<sup>1</sup>. <!-- After this step, we could .. --></p>
</section>
<section id="distance-matrix" class="level1">
<h1>Distance matrix</h1>
<p>Next, we can calculate the distance (dissimilarity) between patterns of each area. This can be done with the <code>lsp_to_dist()</code> function, where we must provide the output of <code>lsp_signature()</code> and a distance measure used (<code>dist_fun = "jensen-shannon"</code>). This operation also could take a few minutes.</p>
<div class="cell" data-hash="index_cache/html/cludist_61fed9fbda99da3afcc632a0c876a128">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb9" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb9-1">eco_dist <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lsp_to_dist</span>(eco_signature, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">dist_fun =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"jensen-shannon"</span>)</span></code></pre></div></div>
</div>
<p>The output, <code>eco_dist</code>, is of a <code>dist</code> class, where small values show that two areas have a similar joint spatial pattern of land cover categories and landform classes.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb10" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb10-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">class</span>(eco_dist)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] "dist"</code></pre>
</div>
</div>
</section>
<section id="hierarchical-clustering" class="level1">
<h1>Hierarchical clustering</h1>
<p>Objects of class <code>dist</code> can be used by many existing R functions for clustering. It includes different approaches of hierarchical clustering (<code>hclust()</code>, <code>cluster::agnes()</code>, <code>cluster::diana()</code>) or fuzzy clustering (<code>cluster::fanny()</code>). In the below example, we use hierarchical clustering using <code>hclust()</code>, which expects a distance matrix as the first argument and a linkage method as the second one. Here, we use the Ward’s minimum variance method (<code>method = "ward.D2"</code>) that minimizes the total within-cluster variance.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb12" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb12-1">eco_hclust <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">hclust</span>(eco_dist, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">method =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ward.D2"</span>)</span>
<span id="cb12-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">plot</span>(eco_hclust)</span></code></pre></div></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://jakubnowosad.com/posts/2021-03-03-motif-bp5/index_files/figure-html/unnamed-chunk-10-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<p>Graphical representation of the hierarchical clustering is called a dendrogram, and based on the obtained dendrogram, we can divide our local landscapes into a specified number of groups using <code>cutree()</code>. In this example, we use eight classes (<code>k = 8</code>) to create a fairly small number of clusters to showcase the presented methodology.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb13" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb13-1">clusters <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">cutree</span>(eco_hclust, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">k =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">8</span>)</span></code></pre></div></div>
</div>
<p>However, a decision about the number of clusters in real-life cases should be based on the goal of the research.</p>
</section>
<section id="clustering-results" class="level1">
<h1>Clustering results</h1>
<p>The <code>lsp_add_clusters</code> function adds: a column <code>clust</code> with a cluster number to each area, and converts the result to an <code>sf</code> object.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb14" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb14-1">eco_grid_sf <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lsp_add_clusters</span>(eco_signature,</span>
<span id="cb14-2">                               clusters)</span></code></pre></div></div>
</div>
<p>The clustering results can be further visualized using <strong>tmap</strong>.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb15" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb15-1">tm_clu <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tm_shape</span>(eco_grid_sf) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb15-2">        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tm_polygons</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"clust"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">style =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"cat"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">palette =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Set2"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">title =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Cluster:"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb15-3">        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tm_layout</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">legend.position =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"LEFT"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"BOTTOM"</span>))</span>
<span id="cb15-4">tm_clu</span></code></pre></div></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://jakubnowosad.com/posts/2021-03-03-motif-bp5/index_files/figure-html/unnamed-chunk-13-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<p>Most clusters form continuous regions, so we could merge areas of the same clusters into larger polygons.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb16" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb16-1">eco_grid_sf2 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> eco_grid_sf <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb16-2">        dplyr<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">group_by</span>(clust) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb16-3">        dplyr<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">summarize</span>()</span></code></pre></div></div>
</div>
<p>The output polygons can then be superimposed on maps of land cover categories and landform classes.</p>
<div class="cell" data-preview="true">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb17" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb17-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tm_shape</span>(eco_data) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb17-2">                <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tm_raster</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">style =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"cat"</span>,</span>
<span id="cb17-3">                          <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">palette =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(lc_palette_df<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>color, lf_palette_df<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>color)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb17-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tm_facets</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ncol =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb17-5">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tm_shape</span>(eco_grid_sf2) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb17-6">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tm_borders</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">col =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"black"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb17-7">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tm_layout</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">legend.show =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>, </span>
<span id="cb17-8">            <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">title.position =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"LEFT"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"TOP"</span>))</span></code></pre></div></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://jakubnowosad.com/posts/2021-03-03-motif-bp5/index_files/figure-html/unnamed-chunk-15-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<p>We can see that many borders (black lines) contain areas with both land cover or landform patterns distinct from their neighbors. Some clusters are also only distinct for one variable (e.g., look at Sahara on the land cover map).</p>
</section>
<section id="clustering-quality" class="level1">
<h1>Clustering quality</h1>
<p>We can also calculate the quality of the clusters with the <code>lsp_add_quality()</code> function. It requires an output of <code>lsp_add_clusters()</code> and an output of <code>lsp_to_dist()</code>, and adds three new variables: <code>inhomogeneity</code>, <code>distinction</code>, and <code>quality</code>.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb18" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb18-1">eco_grid_sfq <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lsp_add_quality</span>(eco_grid_sf, eco_dist, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">type =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"cluster"</span>)</span></code></pre></div></div>
</div>
<p>Inhomogeneity (<code>inhomogeneity</code>) measures a degree of mutual distance between all objects in a cluster. This value is between 0 and 1, where the small value indicates that all objects in the cluster represent consistent patterns, so the cluster is pattern-homogeneous. Distinction (<code>distinction</code>) is an average distance between the focus cluster and all the other clusters. This value is between 0 and 1, where the large value indicates that the cluster stands out from the rest of the clusters. Overall quality (<code>quality</code>) is calculated as <code>1 - (inhomogeneity / distinction)</code>. This value is also between 0 and 1, where increased values indicate better quality of clustering.</p>
<p>We can create a summary of each clusters’ quality using the code below.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb19" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb19-1">eco_grid_sfq2 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> eco_grid_sfq <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb19-2">        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">group_by</span>(clust) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb19-3">        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">summarise</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">inhomogeneity =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mean</span>(inhomogeneity),</span>
<span id="cb19-4">                  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">distinction =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mean</span>(distinction),</span>
<span id="cb19-5">                  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">quality =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mean</span>(quality))</span></code></pre></div></div>
</div>
<div class="cell">
<div class="cell-output-display">
<table class="caption-top table table-sm table-striped small">
<thead>
<tr class="header">
<th style="text-align: right;">clust</th>
<th style="text-align: right;">inhomogeneity</th>
<th style="text-align: right;">distinction</th>
<th style="text-align: right;">quality</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td style="text-align: right;">1</td>
<td style="text-align: right;">0.5064706</td>
<td style="text-align: right;">0.7724361</td>
<td style="text-align: right;">0.3443204</td>
</tr>
<tr class="even">
<td style="text-align: right;">2</td>
<td style="text-align: right;">0.4038704</td>
<td style="text-align: right;">0.7023297</td>
<td style="text-align: right;">0.4249561</td>
</tr>
<tr class="odd">
<td style="text-align: right;">3</td>
<td style="text-align: right;">0.3377875</td>
<td style="text-align: right;">0.7065250</td>
<td style="text-align: right;">0.5219029</td>
</tr>
<tr class="even">
<td style="text-align: right;">4</td>
<td style="text-align: right;">0.1161293</td>
<td style="text-align: right;">0.7921515</td>
<td style="text-align: right;">0.8534002</td>
</tr>
<tr class="odd">
<td style="text-align: right;">5</td>
<td style="text-align: right;">0.3043422</td>
<td style="text-align: right;">0.7366735</td>
<td style="text-align: right;">0.5868696</td>
</tr>
<tr class="even">
<td style="text-align: right;">6</td>
<td style="text-align: right;">0.2774136</td>
<td style="text-align: right;">0.6849140</td>
<td style="text-align: right;">0.5949657</td>
</tr>
<tr class="odd">
<td style="text-align: right;">7</td>
<td style="text-align: right;">0.2926504</td>
<td style="text-align: right;">0.7149212</td>
<td style="text-align: right;">0.5906537</td>
</tr>
<tr class="even">
<td style="text-align: right;">8</td>
<td style="text-align: right;">0.3486704</td>
<td style="text-align: right;">0.7579511</td>
<td style="text-align: right;">0.5399830</td>
</tr>
</tbody>
</table>
</div>
</div>
<p>The created clusters show a different degree of quality metrics. The fourth cluster has the lowest inhomogeneity and the largest distinction, and therefore the best quality. The first cluster has the most inhomogeneous patterns, and while its distinction from other clusters is relatively large, its overall quality is the worst.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb20" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb20-1">tm_inh <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tm_shape</span>(eco_grid_sfq2) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb20-2">        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tm_polygons</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"inhomogeneity"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">style =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"cont"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">palette =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"magma"</span>)</span>
<span id="cb20-3"></span>
<span id="cb20-4">tm_iso <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tm_shape</span>(eco_grid_sfq2) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb20-5">        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tm_polygons</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"distinction"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">style =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"cont"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">palette =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"-inferno"</span>)</span>
<span id="cb20-6"></span>
<span id="cb20-7">tm_qua <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tm_shape</span>(eco_grid_sfq2) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb20-8">        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tm_polygons</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"quality"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">style =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"cont"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">palette =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Greens"</span>)</span>
<span id="cb20-9"></span>
<span id="cb20-10">tm_cluster3 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tmap_arrange</span>(tm_clu, tm_qua, tm_inh, tm_iso, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ncol =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>)</span>
<span id="cb20-11">tm_cluster3</span></code></pre></div></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://jakubnowosad.com/posts/2021-03-03-motif-bp5/index_files/figure-html/unnamed-chunk-19-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
</section>
<section id="understanding-clusters" class="level1">
<h1>Understanding clusters</h1>
<p>Inhomogeneity can also be assessed visually with a pattern mosaic. Pattern mosaic is an artificial rearrangement of a subset of randomly selected areas belonging to a given cluster.</p>
<p>Using the code below, we randomly selected 100 areas for each cluster. It could take a few minutes.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb21" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb21-1">eco_grid_sample <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> eco_grid_sf <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb21-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</span>(na_prop <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb21-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">group_by</span>(clust) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb21-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">slice_sample</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">n =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>)</span></code></pre></div></div>
</div>
<p>Next, we can extract a raster for each selected area with the <code>lsp_add_examples()</code> function.</p>
<div class="cell" data-hash="index_cache/html/eco_grid_examples_64dbec10f8a514b496e348f740e6136b">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb22" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb22-1">eco_grid_examples <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lsp_add_examples</span>(eco_grid_sample, eco_data)</span></code></pre></div></div>
</div>
<p>Finally, we can use the <code>lsp_mosaic()</code> function, which creates raster mosaics by rearranging spatial data for sample areas. Note that this function is still experimental and can change in the future.</p>
<div class="cell" data-hash="index_cache/html/eco_mosaic_5c88da35d6fabdc2f9cbe1489dac85da">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb23" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb23-1">eco_mosaic <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lsp_mosaic</span>(eco_grid_examples)</span></code></pre></div></div>
</div>
<p>The output is a <code>stars</code> object with the third dimension (<code>clust</code>) representing clusters, from which we can use <code>slice()</code> to extract a raster mosaic for a selected cluster. For example, the raster mosaic for fourth cluster looks like this:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb24" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb24-1">eco_mosaic_c4 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">slice</span>(eco_mosaic, clust, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>)</span>
<span id="cb24-2"></span>
<span id="cb24-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tm_shape</span>(eco_mosaic_c4) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb24-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tm_raster</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">style =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"cat"</span>,</span>
<span id="cb24-5">            <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">palette =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(lc_palette_df<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>color, lf_palette_df<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>color)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb24-6">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tm_facets</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ncol =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb24-7">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tm_layout</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">legend.show =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>)</span></code></pre></div></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://jakubnowosad.com/posts/2021-03-03-motif-bp5/index_files/figure-html/unnamed-chunk-21-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<p>We can see that the land cover patterns for this cluster are very simple and homogeneous. The landform patterns are slightly more complex and less homogeneous.</p>
<p>And the raster mosaic for first cluster is:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb25" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb25-1">eco_mosaic_c1 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">slice</span>(eco_mosaic, clust, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span>
<span id="cb25-2"></span>
<span id="cb25-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tm_shape</span>(eco_mosaic_c1) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb25-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tm_raster</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">style =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"cat"</span>,</span>
<span id="cb25-5">            <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">palette =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(lc_palette_df<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>color, lf_palette_df<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>color)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb25-6">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tm_facets</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ncol =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb25-7">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tm_layout</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">legend.show =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>)</span></code></pre></div></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://jakubnowosad.com/posts/2021-03-03-motif-bp5/index_files/figure-html/unnamed-chunk-22-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<p>Patterns of both variables in this cluster are more complex and heterogeneous. This result could suggest that additional clusters could be necessary to distinguish some spatial patterns.</p>
</section>
<section id="summary" class="level1">
<h1>Summary</h1>
<p>The pattern-based clustering allows for grouping areas with similar spatial patterns. The above example shows the search based on two-variable raster data (land cover and landform), but by using <a href="../../posts/2021-02-10-motif-bp2/">a different spatial signature</a>, it can be performed on a single variable raster as well. R code for the pattern-based clustering can be found <a href="https://github.com/Nowosad/motif-examples/blob/master/R/clustering.R">here</a>, with other examples described in the <a href="https://nowosad.github.io/motif/articles/articles/v5_cluster.html">Spatial patterns’ clustering</a> vignette.</p>
<!--improve selection-->


</section>


<div id="quarto-appendix" class="default"><section id="footnotes" class="footnotes footnotes-end-of-document"><h2 class="anchored quarto-appendix-heading">Footnotes</h2>

<ol>
<li id="fn1"><p>See the <code>threshold</code> argument for more details.↩︎</p></li>
</ol>
</section><section class="quarto-appendix-contents" id="quarto-reuse"><h2 class="anchored quarto-appendix-heading">Reuse</h2><div class="quarto-appendix-contents"><div><a rel="license" href="https://creativecommons.org/licenses/by/4.0/">CC BY 4.0</a></div></div></section><section class="quarto-appendix-contents" id="quarto-citation"><h2 class="anchored quarto-appendix-heading">Citation</h2><div><div class="quarto-appendix-secondary-label">BibTeX citation:</div><div class="code-copy-outer-scaffold"><pre class="sourceCode quarto-appendix-bibtex code-with-copy"><code class="sourceCode bibtex">@online{nowosad2021,
  author = {Nowosad, Jakub},
  title = {Clustering Similar Spatial Patterns},
  date = {2021-03-03},
  url = {https://jakubnowosad.com/posts/2021-03-03-motif-bp5/},
  doi = {10.59350/yks30-hwn06},
  langid = {en}
}
</code></pre></div><div class="quarto-appendix-secondary-label">For attribution, please cite this work as:</div><div id="ref-nowosad2021" class="csl-entry quarto-appendix-citeas">
Nowosad, Jakub. 2021. <span>“Clustering Similar Spatial
Patterns.”</span> March 3. <a href="https://doi.org/10.59350/yks30-hwn06">https://doi.org/10.59350/yks30-hwn06</a>.
</div></div></section></div> ]]></description>
  <category>spatial</category>
  <category>geocompr</category>
  <category>rstats</category>
  <category>landscape-ecology</category>
  <category>spatial-patterns</category>
  <category>sil</category>
  <category>motif</category>
  <guid>https://jakubnowosad.com/posts/2021-03-03-motif-bp5/</guid>
  <pubDate>Wed, 03 Mar 2021 00:00:00 GMT</pubDate>
</item>
<item>
  <title>Quantifying changes of spatial patterns</title>
  <dc:creator>Jakub Nowosad</dc:creator>
  <link>https://jakubnowosad.com/posts/2021-02-24-motif-bp4/</link>
  <description><![CDATA[ 





<p>TLTR:</p>
<div class="cell" data-layout-align="right">
<div class="cell-output-display">
<div class="quarto-figure quarto-figure-right">
<figure class="figure">
<p><img src="https://jakubnowosad.com/posts/2021-02-24-motif-bp4/figs/logo.png" class="img-fluid quarto-figure quarto-figure-right figure-img" width="150"></p>
</figure>
</div>
</div>
</div>
<!-- <img src="figs/logo.png" align="right" width="150" /> -->
<p>Quantifying changes of spatial patterns requires two datasets for the same variable in the same area. Both datasets are divided into many sub-areas, and spatial signatures are derived for each sub-area for each dataset. Next, distances for each pair of areas are calculated. Sub-areas with the largest distances represent the largest change.</p>
<p>To reproduce the calculations in the following post, you need to download all of relevant datasets using the code below:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(osfr)</span>
<span id="cb1-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">dir.create</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"data"</span>)</span>
<span id="cb1-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">osf_retrieve_node</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"xykzv"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb1-4">        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">osf_ls_files</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">n_max =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">Inf</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb1-5">        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">osf_download</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">path =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"data"</span>,</span>
<span id="cb1-6">                     <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">conflicts =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"overwrite"</span>)</span></code></pre></div></div>
</div>
<p>You should also attach the following packages:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb2-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(stars)</span>
<span id="cb2-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(motif)</span>
<span id="cb2-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(tmap)</span>
<span id="cb2-4"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(readr)</span></code></pre></div></div>
</div>
<section id="spatial-patterns-changes" class="level1">
<h1>Spatial patterns changes</h1>
<p>A standard approach for detecting changes between two rasters is to calculate a change for each cell independently. This allows quantifying how cells changed their values for A to B, and how many from B to A. However, this approach does not tell us if the spatial pattern actually had changed or stayed the same. For example, consider a regular checkerboard and a checkerboard with all colors reversed. While every cell changed its value, we still have the classes, and their spatial arrangement is the same.</p>
<p>Here, we are interested in changes of spatial patterns, therefore, instead of looking at pixel-by-pixel change, we focus on pattern-by-pattern change <sup>1</sup>.</p>
</section>
<section id="land-cover-in-the-amazon" class="level1">
<h1>Land cover in the Amazon</h1>
<p>The <code>data/lc_am_1992.tif</code> contains land cover data for the year 1992 and <code>data/lc_am_2018.tif</code> for the year 2018. Both are single categorical rasters of the same extent, the Amazon, and the same resolution - 300 meters that can be read into R using the <code>read_stars()</code> function.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb3-1">lc92 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">read_stars</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"data/lc_am_1992.tif"</span>)</span>
<span id="cb3-2">lc18 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">read_stars</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"data/lc_am_2018.tif"</span>)</span></code></pre></div></div>
</div>
<p>Additionally, the <code>data/lc_palette.csv</code> file contains information about the colors and labels of each land cover category.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb4-1">lc_palette_df <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">read_csv</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"data/lc_palette.csv"</span>)</span>
<span id="cb4-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">names</span>(lc_palette_df<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>color) <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> lc_palette_df<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>value</span></code></pre></div></div>
</div>
<p>Both land cover dataset can be visualized with <strong>tmap</strong>. The <code>lc_palette_df</code> is used to set a color palette and legend’s labels.</p>
<div class="cell" data-preview="true">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb5-1">tm_compare1 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tm_shape</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(lc92, lc18)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb5-2">        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tm_raster</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">style =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"cat"</span>,</span>
<span id="cb5-3">                  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">palette =</span> lc_palette_df<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>color,</span>
<span id="cb5-4">                  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">labels =</span> lc_palette_df<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>label,</span>
<span id="cb5-5">                  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">title =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Land cover:"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb5-6">        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tm_layout</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">legend.outside =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>,</span>
<span id="cb5-7">                  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">panel.labels =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1992</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2018</span>))</span>
<span id="cb5-8">tm_compare1</span></code></pre></div></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://jakubnowosad.com/posts/2021-02-24-motif-bp4/index_files/figure-html/unnamed-chunk-6-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<p>The above map clearly shows that there has been a large land cover change in many areas of Amazon between 1992 and 2018. The problem now is to find out what areas changed the most.</p>
</section>
<section id="comparing-spatial-patterns" class="level1">
<h1>Comparing spatial patterns</h1>
<p>This could be solved with <code>lsp_compare()</code>. The <code>lsp_compare()</code> function expects two <code>stars</code> objects with the same extent and resolution. We also need to specify the spatial scale of comparison (<code>window</code>), signature (<code>type</code>), and distance method (<code>dist_fun</code>)<sup>2</sup>.</p>
<p>In this example, we use a window of 300 cells by 300 cells (<code>window = 300</code>). This means that our search scale will be 90 km (300 cells x data resolution) - resulting in dividing the whole area into about 1,500 regular rectangles of 90 by 90 kilometers. We also use the <code>"cove"</code> signature and the <code>"jensen-shannon"</code> distance here.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb6" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb6-1">lc_am_compare <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lsp_compare</span>(lc92, lc18, </span>
<span id="cb6-2">                            <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">window =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">300</span>, </span>
<span id="cb6-3">                            <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">type =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"cove"</span>,</span>
<span id="cb6-4">                            <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">dist_fun =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"jensen-shannon"</span>)</span></code></pre></div></div>
</div>
</section>
<section id="comparing-results" class="level1">
<h1>Comparing results</h1>
<p>By default, the output is a <code>stars</code> object with four attributes: (1) <code>id</code> - an id of each window, (2) <code>na_prop_x</code> - share between 0 and 1 of NA cells for each window in the first <code>stars</code> object, (3) <code>na_prop_y</code> - share between 0 and 1 of NA cells for each window in the second <code>stars</code> object, (4) <code>dist</code> - derived distance between the pattern in the first object and the second object for each window.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb7" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb7-1">lc_am_compare</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>stars object with 2 dimensions and 4 attributes
attribute(s):
           Min.      1st Qu.       Median         Mean      3rd Qu.
id            1 3.637500e+02 7.265000e+02 726.50000000 1.089250e+03
na_prop_x     0 0.000000e+00 0.000000e+00   0.02093060 0.000000e+00
na_prop_y     0 0.000000e+00 0.000000e+00   0.02112938 0.000000e+00
dist          0 1.504208e-03 3.550033e-03   0.01713996 1.100637e-02
                   Max. NA's
id         1452.0000000    0
na_prop_x     0.4933778  620
na_prop_y     0.4933778  620
dist          0.2469178  620
dimension(s):
  from to   offset  delta                       refsys point values x/y
x    1 44 -8834600  90000 Interrupted_Goode_Homolosine    NA   NULL [x]
y    1 33   964250 -90000 Interrupted_Goode_Homolosine    NA   NULL [y]</code></pre>
</div>
</div>
<p>We can visualize the result the same as a regular <code>stars</code> object, for example using the <strong>tmap</strong> package:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb9" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb9-1">tm_compare2 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tm_shape</span>(lc_am_compare) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb9-2">        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tm_raster</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"dist"</span>, </span>
<span id="cb9-3">                  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">palette =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"viridis"</span>, </span>
<span id="cb9-4">                  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">style =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"cont"</span>,</span>
<span id="cb9-5">                  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">title =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Distance (JSD):"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb9-6">        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tm_layout</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">legend.outside =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>)</span>
<span id="cb9-7">tm_compare2</span></code></pre></div></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://jakubnowosad.com/posts/2021-02-24-motif-bp4/index_files/figure-html/unnamed-chunk-9-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<p>The yellow color represents areas of the largest change. They are mostly located in the south and south-east part of the Amazon.</p>
<p>A comparison result can also be easily converted into an <code>sf</code> object with <code>st_as_sf()</code> for subsetting and analyzing the outcomes.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb10" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb10-1">lc_am_compare_sf <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">st_as_sf</span>(lc_am_compare)</span></code></pre></div></div>
</div>
</section>
<section id="areas-of-the-largest-change-in-the-pattern" class="level1">
<h1>Areas of the largest change in the pattern</h1>
<p>In <a href="../../posts/2021-02-17-motif-bp3/">the previous blog post</a>, we were interested in finding the most similar areas to the query region - smallest distance. Here, we are looking for the areas with the largest change, which is expressed by the largest <code>dist</code> values.</p>
<p>We can use <code>slice_max()</code> to subset the obtained result to a selected number of areas with the largest change between 1992 and 2018. The code below selects nine areas with the largest distance between the spatial pattern in 1992 and 2018.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb11" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb11-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(dplyr)</span>
<span id="cb11-2">lc_am_compare_sel <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">slice_max</span>(lc_am_compare_sf, dist, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">n =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">9</span>)</span></code></pre></div></div>
</div>
<p>If we want to look closer at the result, then we can extract each of the above regions with the <code>lsp_add_examples()</code> function. It adds a <code>region</code> column with a <code>stars</code> object to each row.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb12" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb12-1">lc_am_compare_ex <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lsp_add_examples</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> lc_am_compare_sel, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(lc92, lc18))</span></code></pre></div></div>
</div>
<p>It allows us to visualize area with the largest change:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb13" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb13-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tm_shape</span>(lc_am_compare_ex<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>region[[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>]]) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb13-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tm_raster</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">style =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"cat"</span>,</span>
<span id="cb13-3">                          <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">palette =</span> lc_palette_df<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>color,</span>
<span id="cb13-4">                          <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">labels =</span> lc_palette_df<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>label,</span>
<span id="cb13-5">                          <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">title =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Land cover:"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb13-6">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tm_layout</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">legend.show =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>,</span>
<span id="cb13-7">            <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">panel.labels =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1992</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2018</span>))</span></code></pre></div></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://jakubnowosad.com/posts/2021-02-24-motif-bp4/index_files/figure-html/unnamed-chunk-13-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<p>Here, we can see an area mostly covered by forest in 1992, which large parts were transformed into agriculture before 2018.</p>
<p>This approach can also be extended to plot all nine areas. We just need to create a visualization function (<code>create_map2()</code>) and use it iteratively on each region in <code>lc_am_compare_ex</code>. The output of this process, <code>map_list</code>, is a list of <code>tmap</code>s that can be plotted with <code>tmap_arrange()</code>:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb14" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb14-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(purrr)</span>
<span id="cb14-2">create_map2 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(x){</span>
<span id="cb14-3">        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tm_shape</span>(x) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb14-4">                <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tm_raster</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">style =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"cat"</span>,</span>
<span id="cb14-5">                          <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">palette =</span> lc_palette_df<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>color,</span>
<span id="cb14-6">                          <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">labels =</span> lc_palette_df<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>label,</span>
<span id="cb14-7">                          <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">title =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Land cover:"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb14-8">                <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tm_facets</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ncol =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb14-9">                <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tm_layout</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">legend.show =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>,</span>
<span id="cb14-10">                          <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">panel.labels =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1992</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2018</span>))</span>
<span id="cb14-11">}</span>
<span id="cb14-12">map_list <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">map</span>(lc_am_compare_ex<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>region, create_map2)</span>
<span id="cb14-13"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tmap_arrange</span>(map_list)</span></code></pre></div></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://jakubnowosad.com/posts/2021-02-24-motif-bp4/index_files/figure-html/unnamed-chunk-14-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<p>It shows that majority of changes in the Amazon are related to the forest being removed for agricultural purposes.</p>
</section>
<section id="summary" class="level1">
<h1>Summary</h1>
<p>The pattern-based comparison allows for finding areas with the largest change in spatial patterns. The above example shows the search based on a single variable raster data (land cover), but by using <a href="../../posts/2021-02-10-motif-bp2/">a different spatial signature</a>, it can be performed on rasters with two or more variables (think of multi-variable change). R code for the pattern-based comparison can be found <a href="https://github.com/Nowosad/motif-examples/blob/master/R/comparison.R">here</a>, with other examples described in the <a href="https://nowosad.github.io/motif/articles/articles/v4_compare.html">Spatial patterns’ comparision</a> vignette.</p>


</section>


<div id="quarto-appendix" class="default"><section id="footnotes" class="footnotes footnotes-end-of-document"><h2 class="anchored quarto-appendix-heading">Footnotes</h2>

<ol>
<li id="fn1"><p>For a more detailed explanation of spatial patterns’ changes, visit my <a href="../../posts/2018-07-24-geopat-2-compare/">older blog post</a>.↩︎</p></li>
<li id="fn2"><p>If you want more explanation about these arguments, please read the previous posts in this series.↩︎</p></li>
</ol>
</section><section class="quarto-appendix-contents" id="quarto-reuse"><h2 class="anchored quarto-appendix-heading">Reuse</h2><div class="quarto-appendix-contents"><div><a rel="license" href="https://creativecommons.org/licenses/by/4.0/">CC BY 4.0</a></div></div></section><section class="quarto-appendix-contents" id="quarto-citation"><h2 class="anchored quarto-appendix-heading">Citation</h2><div><div class="quarto-appendix-secondary-label">BibTeX citation:</div><div class="code-copy-outer-scaffold"><pre class="sourceCode quarto-appendix-bibtex code-with-copy"><code class="sourceCode bibtex">@online{nowosad2021,
  author = {Nowosad, Jakub},
  title = {Quantifying Changes of Spatial Patterns},
  date = {2021-02-24},
  url = {https://jakubnowosad.com/posts/2021-02-24-motif-bp4/},
  doi = {10.59350/7pvkx-0ry53},
  langid = {en}
}
</code></pre></div><div class="quarto-appendix-secondary-label">For attribution, please cite this work as:</div><div id="ref-nowosad2021" class="csl-entry quarto-appendix-citeas">
Nowosad, Jakub. 2021. <span>“Quantifying Changes of Spatial
Patterns.”</span> February 24. <a href="https://doi.org/10.59350/7pvkx-0ry53">https://doi.org/10.59350/7pvkx-0ry53</a>.
</div></div></section></div> ]]></description>
  <category>spatial</category>
  <category>geocompr</category>
  <category>rstats</category>
  <category>landscape-ecology</category>
  <category>spatial-patterns</category>
  <category>sil</category>
  <category>motif</category>
  <guid>https://jakubnowosad.com/posts/2021-02-24-motif-bp4/</guid>
  <pubDate>Wed, 24 Feb 2021 00:00:00 GMT</pubDate>
</item>
<item>
  <title>Finding similar spatial patterns</title>
  <dc:creator>Jakub Nowosad</dc:creator>
  <link>https://jakubnowosad.com/posts/2021-02-17-motif-bp3/</link>
  <description><![CDATA[ 





<p>TLTR:</p>
<div class="cell" data-layout-align="right">
<div class="cell-output-display">
<div class="quarto-figure quarto-figure-right">
<figure class="figure">
<p><img src="https://jakubnowosad.com/posts/2021-02-17-motif-bp3/figs/logo.png" class="img-fluid quarto-figure quarto-figure-right figure-img" width="150"></p>
</figure>
</div>
</div>
</div>
<!-- <img src="figs/logo.png" align="right" width="150" /> -->
<p>Finding similar spatial patterns requires data for a query region and a search space. Spatial signatures are derived for the query region and many sub-areas of the search space, and distances between them are calculated. Sub-areas with the smallest distances from the query region are the most similar to it.</p>
<p>To reproduce the calculations in the following post, you need to download all of relevant datasets using the code below:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(osfr)</span>
<span id="cb1-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">dir.create</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"data"</span>)</span>
<span id="cb1-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">osf_retrieve_node</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"xykzv"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb1-4">        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">osf_ls_files</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">n_max =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">Inf</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb1-5">        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">osf_download</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">path =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"data"</span>,</span>
<span id="cb1-6">                     <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">conflicts =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"overwrite"</span>)</span></code></pre></div></div>
</div>
<p>You should also attach the following packages:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb2-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(sf)</span>
<span id="cb2-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(stars)</span>
<span id="cb2-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(tmap)</span></code></pre></div></div>
</div>
<section id="suwalski-landscape-park" class="level1">
<h1>Suwalski Landscape Park</h1>
<p>Spatial pattern search allows for quantifying similarity between the query region and the search space and finally finding regions that are the most similar to the query one. Here, we were interested in finding areas of similar topography to the area of Suwalski Landscape Park. Suwalski Landscape Park is a protected area in north-eastern Poland with a post-glacial landscape consisting of young morainic hills.</p>
<div class="cell" data-preview="true">
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://jakubnowosad.com/posts/2021-02-17-motif-bp3/figs/slp.JPG" class="img-fluid figure-img" style="width:100.0%"></p>
</figure>
</div>
</div>
</div>
<p>One possible approach to classify the topography of a given region is to use geomorphons. Geomorphons categorize cells in this area into one of ten forms: flat, summit, ridge, shoulder, spur, slope, hollow, footslope, valley, and depression <sup>1</sup>.</p>
<p>The <code>"data/geomorphons_pol.tif"</code> file contains a raster with geomorphons calculated for Poland’s area, while <code>"data/suw_lp.gpkg"</code> is a vector polygon with the Suwalski Landscape Park borders. Let’s start by reading these two files into R.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb3-1">gm <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">read_stars</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"data/geomorphons_pol.tif"</span>)</span>
<span id="cb3-2">suw_lp <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">read_sf</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"data/suw_lp.gpkg"</span>)</span></code></pre></div></div>
</div>
<p>Now, we can visualize geomorphons and the location of Suwalski Landscape Park with the <strong>tmap</strong>.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb4-1">tm_gm <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tm_shape</span>(gm) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb4-2">        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tm_raster</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">title =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Geomorphons:"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb4-3">        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tm_shape</span>(suw_lp) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb4-4">        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tm_symbols</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">col =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"black"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">shape =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">6</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb4-5">        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tm_layout</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">legend.outside =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">frame =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>)</span>
<span id="cb4-6">tm_gm</span></code></pre></div></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://jakubnowosad.com/posts/2021-02-17-motif-bp3/index_files/figure-html/unnamed-chunk-6-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
</section>
<section id="search-area" class="level1">
<h1>Search area</h1>
<p>The geomorphon data for Poland is our search space. Now, we also need a second raster object with a query region. The query region is an area to which we want to find other similar areas.</p>
<p>There are two main ways to create a query region:</p>
<ol type="1">
<li>By cropping spatial data of a large area to the extent or borders of a query region.</li>
<li>By reading an external file. In the second case, the values in the external file should match the values in the search space.</li>
</ol>
<p>Here, we are using the former approach by reading the Suwalski Landscape Park borders and then using it to crop the whole-country raster.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb5-1">suw_lp <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">read_sf</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"data/suw_lp.gpkg"</span>)</span>
<span id="cb5-2">gm_suw <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">st_crop</span>(gm, suw_lp)</span></code></pre></div></div>
</div>
<p>The query area has irregular spatial patterns represented by slopes and a limited number of flat areas.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb6" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb6-1">tm_gm_suw <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tm_shape</span>(gm_suw) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb6-2">        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tm_raster</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb6-3">        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tm_shape</span>(suw_lp) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb6-4">        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tm_borders</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">col =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"black"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb6-5">        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tm_layout</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">legend.show =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">frame =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>)</span>
<span id="cb6-6">tm_gm_suw</span></code></pre></div></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://jakubnowosad.com/posts/2021-02-17-motif-bp3/index_files/figure-html/unnamed-chunk-8-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
</section>
<section id="search-process" class="level1">
<h1>Search process</h1>
<p>The searching process consists of:</p>
<ol type="1">
<li>Selecting a query region and a search space. In our case, the query region is <code>gm_sum</code>, while the search space is the <code>gm</code> object.</li>
<li>Dividing the search space using regular (non-overlapping) squares or using polygons.</li>
<li>Creating numerical representation (called a signature) for the query region and the search space.</li>
<li>Comparing the signature of the query region with signatures for each part of the search space using a distance measure.</li>
</ol>
</section>
<section id="search-scale" class="level1">
<h1>Search scale</h1>
<p>The first important consideration is the search scale - what is the size of areas we want to find? This is not an easy question and largely depends on the research problem. <!-- ... --> The current version of <strong>motif</strong> accepts either regular (non-overlapping) squares or polygons.</p>
</section>
<section id="search-signature" class="level1">
<h1>Search signature</h1>
<p>The second consideration is the search signature. We are able to describe the above area in words, however, how to translate the spatial pattern properties to a computer and, at the same time, made them more objective? Again, it is a complex question, and largely depends on the type of input data.</p>
<p>In our case, we have a single categorical raster, and for this type of data, we found out that the <em>cove</em> signature works well. <em>Cove</em> stands for <em>c</em>o-<em>o</em>ccurrence <em>ve</em>ctor - it is a 1D vector where each value represents what is the share of one category is adjacent to some other cells<sup>2</sup>. More information about <em>cove</em> can be found in <a href="../../posts/2021-02-10-motif-bp2/">the previous blog post</a>.</p>
<p>We can calculate <em>cove</em> for our query region using <code>lsp_signature()</code> with the <code>type</code> argument set to <code>"cove"</code>.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb7" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb7-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(motif)</span>
<span id="cb7-2">gm_sum_sig <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lsp_signature</span>(gm_suw, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">type =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"cove"</span>)</span>
<span id="cb7-3">gm_sum_sig</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code># A tibble: 1 × 3
     id na_prop signature      
* &lt;int&gt;   &lt;dbl&gt; &lt;list&gt;         
1     1   0.381 &lt;dbl [1 × 100]&gt;</code></pre>
</div>
</div>
<p>The output object contains a <code>signature</code> column, which stores the <em>cove</em> signature for our region. We can see this signature with <code>gm_sum_sig$signature[[1]]</code>.</p>
</section>
<section id="search-distance" class="level1">
<h1>Search distance</h1>
<p>The third consideration is a distance measure. Many distance measures have been developed for different types of data, and each of them has different properties. The <strong>motif</strong> package allows using any distance measure implemented in the <strong>philentropy</strong> package, which includes more than 40 different measures<sup>3</sup>.</p>
</section>
<section id="searching" class="level1">
<h1>Searching</h1>
<p>The <code>lsp_search()</code> function performs spatial pattern-based search. It expects two <code>stars</code> objects: a query region (<code>gm_suw</code>) and a search space (<code>gm</code>). Next, we need to specify the search scale (<code>window</code>), signature (<code>type</code>), and distance method (<code>dist_fun</code>).</p>
<p>In this example, we use a window of 100 cells by 100 cells (<code>window = 100</code>). This means that our search scale will be 2500 meters (100 cells x data resolution) - resulting in dividing the search space into about 70,000 regular rectangles of 2500 by 2500 meters. We also use the <code>"cove"</code> signature and the <code>"jensen-shannon"</code> distance here.</p>
<div class="cell" data-hash="index_cache/html/gm_search_3f048d912f55747bf8ccbdf62519e4e4">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb9" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb9-1">gm_search <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lsp_search</span>(gm_suw, gm,</span>
<span id="cb9-2">                       <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">window =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>,</span>
<span id="cb9-3">                       <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">type =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"cove"</span>,</span>
<span id="cb9-4">                       <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">dist_fun =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"jensen-shannon"</span>)</span></code></pre></div></div>
</div>
<p>The above calculation could take several minutes on a modern computer.</p>
</section>
<section id="search-results" class="level1">
<h1>Search results</h1>
<p>By default, the output of the search is a <code>stars</code> object with three attributes:</p>
<ol type="1">
<li><code>id</code> - an id of each window,</li>
<li><code>na_prop</code> - share between 0 and 1 of NA cells for each window in the search space,</li>
<li><code>dist</code> - derived distance between the query region and each window in the search space.</li>
</ol>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb10" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb10-1">gm_search</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>stars object with 2 dimensions and 3 attributes
attribute(s):
                Min.      1st Qu.       Median         Mean      3rd Qu.
id       1.000000000 1.764075e+04 3.528050e+04 3.528050e+04 5.292025e+04
na_prop  0.000000000 0.000000e+00 0.000000e+00 3.111193e-03 0.000000e+00
dist     0.001737701 4.483980e-02 1.051218e-01 1.461936e-01 2.080158e-01
                 Max.  NA's
id       7.056000e+04     0
na_prop  4.997000e-01 20485
dist     6.451746e-01 20485
dimension(s):
  from  to  offset delta                       refsys point values x/y
x    1 288 4595300  2500 +proj=laea +lat_0=52 +lon...    NA   NULL [x]
y    1 245 3556600 -2500 +proj=laea +lat_0=52 +lon...    NA   NULL [y]</code></pre>
</div>
</div>
<p>We can visualize the result in the same fashion as a regular <code>stars</code> object (see the final map at the end of the post):</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb12" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb12-1">tm_search2 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tm_shape</span>(gm_search) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb12-2">          <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tm_raster</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"dist"</span>,</span>
<span id="cb12-3">                  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">style =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"log10"</span>, </span>
<span id="cb12-4">                  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">palette =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"BrBG"</span>,</span>
<span id="cb12-5">                  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">title =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Distance (JSD):"</span>,</span>
<span id="cb12-6">                  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">legend.is.portrait =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>)</span></code></pre></div></div>
</div>
<p>A search result can also be easily converted into an <code>sf</code> object with <code>st_as_sf()</code>. This allows for straightforward analysis and subsetting of the search results.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb13" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb13-1">gm_search_sf <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">st_as_sf</span>(gm_search)</span></code></pre></div></div>
</div>
</section>
<section id="the-most-similar-areas" class="level1">
<h1>The most similar areas</h1>
<p>Spatial pattern-based search is similar to a search using internet search engines - we do not care about the most dissimilar areas. We just want to locate the ones most similar to the query region. Therefore, we should select only areas with the smallest distance values - this means that they are the most similar to the query region.</p>
<p>We can achieve it, for example, using the <code>slide_min()</code> function. The code below selects nine areas with the smallest distance from the query region.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb14" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb14-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(dplyr)</span>
<span id="cb14-2">gm_search_sel <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">slice_min</span>(gm_search_sf, dist, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">n =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">9</span>)</span></code></pre></div></div>
</div>
<p>If we want to look closer at the result, then we can extract each of the above regions with the <code>lsp_add_examples()</code> function. It adds a <code>region</code> column with a <code>stars</code> object to each row.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb15" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb15-1">gm_search_ex <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lsp_add_examples</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> gm_search_sel, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> gm)</span></code></pre></div></div>
</div>
<p>It allows us to visualize any of the most similar areas.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb16" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb16-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tm_shape</span>(gm_search_ex<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>region[[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>]]) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb16-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tm_raster</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb16-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tm_layout</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">legend.show =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>)</span></code></pre></div></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://jakubnowosad.com/posts/2021-02-17-motif-bp3/index_files/figure-html/unnamed-chunk-16-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<p>This approach can also be extended to plot all nine of the most similar areas. We just need to create a visualization function (<code>create_map()</code>) and use it iteratively on each region in <code>gm_search_ex</code>. The output of this process, <code>map_list</code>, is a list of <code>tmap</code>s that can be plotted with <code>tmap_arrange()</code>:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb17" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb17-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(purrr)</span>
<span id="cb17-2">create_map <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(x, y){</span>
<span id="cb17-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tm_shape</span>(x) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb17-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tm_raster</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb17-5">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tm_layout</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">legend.show =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>,</span>
<span id="cb17-6">            <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">title =</span> y)</span>
<span id="cb17-7">}</span>
<span id="cb17-8">map_list <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">map2</span>(gm_search_ex<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>region, gm_search_ex<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>id, create_map)</span>
<span id="cb17-9"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tmap_arrange</span>(map_list)</span></code></pre></div></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://jakubnowosad.com/posts/2021-02-17-motif-bp3/index_files/figure-html/unnamed-chunk-17-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<p>Nine examples of the areas with the most similar patterns of geomorphons comparing to the Suwalski Landscape Park are presented above. They are also similar to each other, suggesting a high-quality result.</p>
<p>The final map consists of two parts: (a) a distance raster and (b) symbols representing the nine most similar areas.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb18" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb18-1">tm_search2 <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb18-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tm_shape</span>(gm_search_sel) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb18-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tm_symbols</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">shape =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">col =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"black"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb18-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tm_text</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"id"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">auto.placement =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>)</span></code></pre></div></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://jakubnowosad.com/posts/2021-02-17-motif-bp3/index_files/figure-html/unnamed-chunk-18-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<p>The brown color on the above map represents areas with the most similar patterns of geomorphons to the Suwalski Landscape Park. The majority of similar areas are located in northern Poland and forms a belt with homogeneous topography.</p>
</section>
<section id="summary" class="level1">
<h1>Summary</h1>
<p>The pattern-based search allows for finding areas with similar spatial patterns. The above example shows the search based on a single variable raster data (geomorphons), but by using <a href="../../posts/2021-02-10-motif-bp2/">a different spatial signature</a>, it can be performed on rasters with two or more variables. Additionally, search space can be not only divided into regular areas, but also in irregular ones - see <a href="https://nowosad.github.io/motif/articles/articles/v3_search.html">an example</a>. R code for the pattern-based search can be found <a href="https://github.com/Nowosad/motif-examples/blob/master/R/search.R">here</a>.</p>


</section>


<div id="quarto-appendix" class="default"><section id="footnotes" class="footnotes footnotes-end-of-document"><h2 class="anchored quarto-appendix-heading">Footnotes</h2>

<ol>
<li id="fn1"><p>Learn more about geomorphons by reading the dedicated <a href="https://www.sciencedirect.com/science/article/abs/pii/S0169555X12005028">paper</a> or its <a href="http://sil.home.amu.edu.pl/data/uploads/pdf/journal_papers/geomorphology2013jasiewiczstepinski.pdf">preprint</a>. You can also calculate geomorphons for your own data using a GRASS GIS module <a href="https://grass.osgeo.org/grass78/manuals/r.geomorphon.html">r.geomorphon</a>.↩︎</p></li>
<li id="fn2"><p>In other words, it is a vector containing a normalized form of the co-occurrence matrix.↩︎</p></li>
<li id="fn3"><p>You can check all of them using <code>philentropy::getDistMethods()</code>.↩︎</p></li>
</ol>
</section><section class="quarto-appendix-contents" id="quarto-reuse"><h2 class="anchored quarto-appendix-heading">Reuse</h2><div class="quarto-appendix-contents"><div><a rel="license" href="https://creativecommons.org/licenses/by/4.0/">CC BY 4.0</a></div></div></section><section class="quarto-appendix-contents" id="quarto-citation"><h2 class="anchored quarto-appendix-heading">Citation</h2><div><div class="quarto-appendix-secondary-label">BibTeX citation:</div><div class="code-copy-outer-scaffold"><pre class="sourceCode quarto-appendix-bibtex code-with-copy"><code class="sourceCode bibtex">@online{nowosad2021,
  author = {Nowosad, Jakub},
  title = {Finding Similar Spatial Patterns},
  date = {2021-02-17},
  url = {https://jakubnowosad.com/posts/2021-02-17-motif-bp3/},
  doi = {10.59350/z8pzx-9s133},
  langid = {en}
}
</code></pre></div><div class="quarto-appendix-secondary-label">For attribution, please cite this work as:</div><div id="ref-nowosad2021" class="csl-entry quarto-appendix-citeas">
Nowosad, Jakub. 2021. <span>“Finding Similar Spatial Patterns.”</span>
February 17. <a href="https://doi.org/10.59350/z8pzx-9s133">https://doi.org/10.59350/z8pzx-9s133</a>.
</div></div></section></div> ]]></description>
  <category>spatial</category>
  <category>geocompr</category>
  <category>rstats</category>
  <category>landscape-ecology</category>
  <category>spatial-patterns</category>
  <category>sil</category>
  <category>motif</category>
  <guid>https://jakubnowosad.com/posts/2021-02-17-motif-bp3/</guid>
  <pubDate>Wed, 17 Feb 2021 00:00:00 GMT</pubDate>
</item>
<item>
  <title>Describing categorical rasters with spatial signatures</title>
  <dc:creator>Jakub Nowosad</dc:creator>
  <link>https://jakubnowosad.com/posts/2021-02-10-motif-bp2/</link>
  <description><![CDATA[ 





<p>TLTR:</p>
<div class="cell" data-layout-align="right">
<div class="cell-output-display">
<div class="quarto-figure quarto-figure-right">
<figure class="figure">
<p><img src="https://jakubnowosad.com/posts/2021-02-10-motif-bp2/figs/logo.png" class="img-fluid quarto-figure quarto-figure-right figure-img" width="150"></p>
</figure>
</div>
</div>
</div>
<!-- <img src="figs/logo.png" align="right" width="150" /> -->
<p>Spatial signatures are multi-value representations of the patterns that compress information about spatial composition and configuration. Spatial signatures can be directly compared using various distance measures.</p>
<section id="describing-categorical-rasters" class="level1">
<h1>Describing categorical rasters</h1>
<p>A categorical raster shown below represents land cover data for some area. This area is mainly covered by forest, with some small patches of agriculture, grasslands, and water.</p>
<div class="cell">
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://jakubnowosad.com/posts/2021-02-10-motif-bp2/index_files/figure-html/unnamed-chunk-4-1.png" class="img-fluid figure-img" width="576"></p>
</figure>
</div>
</div>
</div>
<p>If we want to describe this area, we could start by measuring areas of different land cover categories. Then, we could know that forest cover about 0.986% and agriculture cover about 0.013%. We could also use landscape metrics to put a number on some property of this raster. Then, we would know that the entropy is 0.116, and relative mutual information is 0.331<sup>1</sup>.</p>
<p>This approach can be applied to many categorical rasters, as you can see below.</p>
<div class="cell">
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://jakubnowosad.com/posts/2021-02-10-motif-bp2/index_files/figure-html/unnamed-chunk-5-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<div class="cell">
<div class="cell-output-display">
<table class="caption-top table table-sm table-striped small">
<colgroup>
<col style="width: 13%">
<col style="width: 5%">
<col style="width: 5%">
<col style="width: 5%">
<col style="width: 5%">
<col style="width: 5%">
<col style="width: 5%">
<col style="width: 5%">
<col style="width: 5%">
<col style="width: 5%">
<col style="width: 5%">
<col style="width: 5%">
<col style="width: 5%">
<col style="width: 5%">
<col style="width: 5%">
<col style="width: 5%">
<col style="width: 5%">
</colgroup>
<tbody>
<tr class="odd">
<td style="text-align: left;">id</td>
<td style="text-align: left;">1</td>
<td style="text-align: left;">2</td>
<td style="text-align: left;">3</td>
<td style="text-align: left;">4</td>
<td style="text-align: left;">5</td>
<td style="text-align: left;">6</td>
<td style="text-align: left;">7</td>
<td style="text-align: left;">8</td>
<td style="text-align: left;">9</td>
<td style="text-align: left;">10</td>
<td style="text-align: left;">11</td>
<td style="text-align: left;">12</td>
<td style="text-align: left;">13</td>
<td style="text-align: left;">14</td>
<td style="text-align: left;">15</td>
<td style="text-align: left;">16</td>
</tr>
<tr class="even">
<td style="text-align: left;">ent</td>
<td style="text-align: left;">0.12</td>
<td style="text-align: left;">0.45</td>
<td style="text-align: left;">0.53</td>
<td style="text-align: left;">0.63</td>
<td style="text-align: left;">0.75</td>
<td style="text-align: left;">1.16</td>
<td style="text-align: left;">1.16</td>
<td style="text-align: left;">1.28</td>
<td style="text-align: left;">1.53</td>
<td style="text-align: left;">1.2</td>
<td style="text-align: left;">1.65</td>
<td style="text-align: left;">1.6</td>
<td style="text-align: left;">1.74</td>
<td style="text-align: left;">1.72</td>
<td style="text-align: left;">1.6</td>
<td style="text-align: left;">2.02</td>
</tr>
<tr class="odd">
<td style="text-align: left;">relmutinf</td>
<td style="text-align: left;">0.33</td>
<td style="text-align: left;">0.39</td>
<td style="text-align: left;">0.34</td>
<td style="text-align: left;">0.52</td>
<td style="text-align: left;">0.44</td>
<td style="text-align: left;">0.51</td>
<td style="text-align: left;">0.39</td>
<td style="text-align: left;">0.33</td>
<td style="text-align: left;">0.42</td>
<td style="text-align: left;">0.36</td>
<td style="text-align: left;">0.5</td>
<td style="text-align: left;">0.58</td>
<td style="text-align: left;">0.43</td>
<td style="text-align: left;">0.34</td>
<td style="text-align: left;">0.2</td>
<td style="text-align: left;">0.38</td>
</tr>
<tr class="even">
<td style="text-align: left;">forest</td>
<td style="text-align: left;">0.99</td>
<td style="text-align: left;">0.93</td>
<td style="text-align: left;">0.9</td>
<td style="text-align: left;">0.84</td>
<td style="text-align: left;">0.83</td>
<td style="text-align: left;">0.76</td>
<td style="text-align: left;">0.69</td>
<td style="text-align: left;">0.68</td>
<td style="text-align: left;">0.59</td>
<td style="text-align: left;">0.57</td>
<td style="text-align: left;">0.53</td>
<td style="text-align: left;">0.5</td>
<td style="text-align: left;">0.44</td>
<td style="text-align: left;">0.4</td>
<td style="text-align: left;">0.39</td>
<td style="text-align: left;">0.36</td>
</tr>
<tr class="odd">
<td style="text-align: left;">agriculture</td>
<td style="text-align: left;">0.01</td>
<td style="text-align: left;">0.05</td>
<td style="text-align: left;">0.08</td>
<td style="text-align: left;">0.16</td>
<td style="text-align: left;">0.16</td>
<td style="text-align: left;">0.12</td>
<td style="text-align: left;">0.25</td>
<td style="text-align: left;">0.23</td>
<td style="text-align: left;">0.24</td>
<td style="text-align: left;">0.39</td>
<td style="text-align: left;">0.3</td>
<td style="text-align: left;">0.36</td>
<td style="text-align: left;">0.36</td>
<td style="text-align: left;">0.39</td>
<td style="text-align: left;">0.36</td>
<td style="text-align: left;">0.3</td>
</tr>
</tbody>
</table>
</div>
</div>
<p>Now, each rasters’ spatial properties are expressed by a vector of numbers representing its categories and selected landscape metrics.</p>
</section>
<section id="basic-spatial-signature" class="level1">
<h1>Basic spatial signature</h1>
<p>As I mentioned in <a href="../../posts/2021-02-03-motif-bp1/">my previous blog posts</a>, we could represent categorical rasters with a large number of landscape metrics. However, many landscape metrics are highly correlated, and some of them depend on the resolution of the input data and the size of the study area.</p>
<p>An alternative approach is to derive a multi-value representation of the raster that compress information about its spatial composition and configuration. One of such representations is a co-occurrence matrix (<em>coma</em>).</p>
<p>The <em>coma</em> representation is calculated by moving through each cell, looking at its value, and counting how many neighbors of each class our central cell has. For example, the co-occurrence matrix below shows that the forest category cells are 38,778 times adjacent to other cells of this category, 218 times to the cells of the agriculture category, and four times to the cells of the grassland category, and so on.</p>
<div class="cell">
<div class="cell-output-display">
<table class="caption-top table table-sm table-striped small">
<thead>
<tr class="header">
<th style="text-align: left;"></th>
<th style="text-align: right;">agriculture</th>
<th style="text-align: right;">forest</th>
<th style="text-align: right;">grassland</th>
<th style="text-align: right;">water</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td style="text-align: left;">agriculture</td>
<td style="text-align: right;">272</td>
<td style="text-align: right;">218</td>
<td style="text-align: right;">4</td>
<td style="text-align: right;">0</td>
</tr>
<tr class="even">
<td style="text-align: left;">forest</td>
<td style="text-align: right;">218</td>
<td style="text-align: right;">38778</td>
<td style="text-align: right;">32</td>
<td style="text-align: right;">12</td>
</tr>
<tr class="odd">
<td style="text-align: left;">grassland</td>
<td style="text-align: right;">4</td>
<td style="text-align: right;">32</td>
<td style="text-align: right;">16</td>
<td style="text-align: right;">0</td>
</tr>
<tr class="even">
<td style="text-align: left;">water</td>
<td style="text-align: right;">0</td>
<td style="text-align: right;">12</td>
<td style="text-align: right;">0</td>
<td style="text-align: right;">2</td>
</tr>
</tbody>
</table>
</div>
</div>
<p>Importantly, this signature contains information about the categories and their shares (composition), and also the spatial relation between categories (configuration).</p>
<p>The co-occurrence matrix (<em>coma</em>) representation is two-dimensional, with values of categories in row and columns. It can be converted into a one-dimensional representation called a co-occurrence vector (<em>cove</em>).</p>
<div class="cell">
<div class="cell-output-display">
<table class="caption-top table table-sm table-striped small">
<tbody>
<tr class="odd">
<td style="text-align: right;">272</td>
<td style="text-align: right;">218</td>
<td style="text-align: right;">4</td>
<td style="text-align: right;">0</td>
<td style="text-align: right;">218</td>
<td style="text-align: right;">38778</td>
<td style="text-align: right;">32</td>
<td style="text-align: right;">12</td>
<td style="text-align: right;">4</td>
<td style="text-align: right;">32</td>
<td style="text-align: right;">16</td>
<td style="text-align: right;">0</td>
<td style="text-align: right;">0</td>
<td style="text-align: right;">12</td>
<td style="text-align: right;">0</td>
<td style="text-align: right;">2</td>
</tr>
</tbody>
</table>
</div>
</div>
<p>As you can see, some elements of this vector represent the same relations. For example, the first value of <code>4</code> shows the relation between grassland and agriculture, and the second value of <code>4</code> represents the relation between agriculture and grassland. We can simplify the above vector by counting all relations only once<sup>2</sup>:</p>
<div class="cell">
<div class="cell-output-display">
<table class="caption-top table table-sm table-striped small">
<tbody>
<tr class="odd">
<td style="text-align: right;">136</td>
<td style="text-align: right;">218</td>
<td style="text-align: right;">19389</td>
<td style="text-align: right;">4</td>
<td style="text-align: right;">32</td>
<td style="text-align: right;">8</td>
<td style="text-align: right;">0</td>
<td style="text-align: right;">12</td>
<td style="text-align: right;">0</td>
<td style="text-align: right;">1</td>
</tr>
</tbody>
</table>
</div>
</div>
<p>This vector can be further transformed to have its values to sum up to one. The output vector is called the normalized co-occurrence vector.</p>
<div class="cell">
<div class="cell-output-display">
<table class="caption-top table table-sm table-striped small">
<tbody>
<tr class="odd">
<td style="text-align: right;">0.0069</td>
<td style="text-align: right;">0.011</td>
<td style="text-align: right;">0.9792</td>
<td style="text-align: right;">0.0002</td>
<td style="text-align: right;">0.0016</td>
<td style="text-align: right;">0.0004</td>
<td style="text-align: right;">0</td>
<td style="text-align: right;">0.0006</td>
<td style="text-align: right;">0</td>
<td style="text-align: right;">0.0001</td>
</tr>
</tbody>
</table>
</div>
</div>
<p>The role of normalization is to create a probability function, and thus be able to compare categorical rasters of different sizes using mathematical distance measures.</p>
</section>
<section id="measuring-similarity-between-patterns" class="level1">
<h1>Measuring similarity between patterns</h1>
<p>Let’s consider two rasters below. We want to know how similar they are to each other.</p>
<div class="cell">
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://jakubnowosad.com/posts/2021-02-10-motif-bp2/index_files/figure-html/unnamed-chunk-12-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<p>To answer this question, we need to perform three steps:</p>
<ol type="1">
<li>calculate a normalized co-occurrence vector for the first raster,</li>
<li>calculate a normalized co-occurrence vector for the second raster,</li>
<li>calculate a numerical distance between these two signatures. <!-- important - the same length and order! --></li>
</ol>
<p>Normalized co-occurrence vector for the first raster is:</p>
<div class="cell">
<div class="cell-output-display">
<table class="caption-top table table-sm table-striped small">
<tbody>
<tr class="odd">
<td style="text-align: right;">0.0069</td>
<td style="text-align: right;">0.011</td>
<td style="text-align: right;">0.9792</td>
<td style="text-align: right;">0.0002</td>
<td style="text-align: right;">0.0016</td>
<td style="text-align: right;">0.0004</td>
<td style="text-align: right;">0</td>
<td style="text-align: right;">0.0006</td>
<td style="text-align: right;">0</td>
<td style="text-align: right;">0.0001</td>
</tr>
</tbody>
</table>
</div>
</div>
<p>Normalized co-occurrence vector for the second raster is:</p>
<div class="cell">
<div class="cell-output-display">
<table class="caption-top table table-sm table-striped small">
<tbody>
<tr class="odd">
<td style="text-align: right;">0.1282</td>
<td style="text-align: right;">0.0609</td>
<td style="text-align: right;">0.8105</td>
<td style="text-align: right;">0.0002</td>
<td style="text-align: right;">0.0002</td>
<td style="text-align: right;">0.0001</td>
<td style="text-align: right;">0</td>
<td style="text-align: right;">0</td>
<td style="text-align: right;">0</td>
<td style="text-align: right;">0</td>
</tr>
</tbody>
</table>
</div>
</div>
<p>A large number of possible distance measures between probability functions exists<sup>3</sup>. In this example, we use the Jenson-Shannon distance.</p>
<p><code>$$ JSD(A, B) = H(\frac{A + B}{2}) - \frac{1}{2}[H(A) + H(B)] $$</code></p>
<p>It takes two probability functions (spatial signatures in our case) A and B, and calculates entropy values (H). The Jenson-Shannon distance is a value between 0 and 1, where 0 means that two probability functions are identical, and 1 means that they have nothing in common.</p>
<p>Jensen-Shannon distance between our two rasters is 0.068, suggesting that their spatial composition and configuration are fairly similar, but not identical. Now, let’s consider two rasters that are visually very different. One is covered mostly by forest, while the second one is mostly a mosaic of forests, agricultural areas, and grasslands.</p>
<div class="cell">
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://jakubnowosad.com/posts/2021-02-10-motif-bp2/index_files/figure-html/unnamed-chunk-16-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<p>Normalized co-occurrence vector for the first raster is:</p>
<div class="cell">
<div class="cell-output-display">
<table class="caption-top table table-sm table-striped small">
<colgroup>
<col style="width: 9%">
<col style="width: 7%">
<col style="width: 9%">
<col style="width: 9%">
<col style="width: 9%">
<col style="width: 9%">
<col style="width: 3%">
<col style="width: 3%">
<col style="width: 3%">
<col style="width: 3%">
<col style="width: 3%">
<col style="width: 9%">
<col style="width: 3%">
<col style="width: 3%">
<col style="width: 9%">
</colgroup>
<tbody>
<tr class="odd">
<td style="text-align: right;">0.0069</td>
<td style="text-align: right;">0.011</td>
<td style="text-align: right;">0.9792</td>
<td style="text-align: right;">0.0002</td>
<td style="text-align: right;">0.0016</td>
<td style="text-align: right;">0.0004</td>
<td style="text-align: right;">0</td>
<td style="text-align: right;">0</td>
<td style="text-align: right;">0</td>
<td style="text-align: right;">0</td>
<td style="text-align: right;">0</td>
<td style="text-align: right;">0.0006</td>
<td style="text-align: right;">0</td>
<td style="text-align: right;">0</td>
<td style="text-align: right;">0.0001</td>
</tr>
</tbody>
</table>
</div>
</div>
<p>Normalized co-occurrence vector for the second raster is:</p>
<div class="cell">
<div class="cell-output-display">
<table class="caption-top table table-sm table-striped small">
<colgroup>
<col style="width: 7%">
<col style="width: 7%">
<col style="width: 7%">
<col style="width: 7%">
<col style="width: 7%">
<col style="width: 7%">
<col style="width: 7%">
<col style="width: 7%">
<col style="width: 7%">
<col style="width: 7%">
<col style="width: 7%">
<col style="width: 7%">
<col style="width: 3%">
<col style="width: 3%">
<col style="width: 7%">
</colgroup>
<tbody>
<tr class="odd">
<td style="text-align: right;">0.2033</td>
<td style="text-align: right;">0.1335</td>
<td style="text-align: right;">0.2944</td>
<td style="text-align: right;">0.1747</td>
<td style="text-align: right;">0.0562</td>
<td style="text-align: right;">0.1307</td>
<td style="text-align: right;">0.0035</td>
<td style="text-align: right;">0.0002</td>
<td style="text-align: right;">0.0004</td>
<td style="text-align: right;">0.0015</td>
<td style="text-align: right;">0.0007</td>
<td style="text-align: right;">0.0005</td>
<td style="text-align: right;">0</td>
<td style="text-align: right;">0</td>
<td style="text-align: right;">0.0005</td>
</tr>
</tbody>
</table>
</div>
</div>
<p>The <strong>Jensen-Shannon distance</strong> between this pair of rasters is 0.444, indicating that two rasters are fairly different<sup>4</sup>.</p>
<p>Calculating spatial signatures for many areas allows us to find the most similar rasters, describe changes between rasters, or group (cluster) rasters with similar spatial patterns.</p>
</section>
<section id="other-spatial-signatures" class="level1">
<h1>Other spatial signatures</h1>
<p>The co-occurrence matrix (<em>coma</em>) is suitable to represent pattern of a single categorical variable. There are, however, other spatial signatures aimed to describe spatial patterns of multi-variable cases.</p>
<section id="a-weighted-co-occurrence-matrix-wecoma-representation" class="level2">
<h2 class="anchored" data-anchor-id="a-weighted-co-occurrence-matrix-wecoma-representation">A weighted co-occurrence matrix (wecoma) representation</h2>
<p>Let’s consider a situation, in which we have two rasters: one with categories, and one with weights. So, now we have not only a category of each cell, but also its intensity.</p>
<div class="cell">
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://jakubnowosad.com/posts/2021-02-10-motif-bp2/index_files/figure-html/unnamed-chunk-21-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<p>Regular co-occurrence matrix (<em>coma</em>) based just on a categorical raster looks the following:</p>
<div class="cell">
<div class="cell-output-display">
<table class="caption-top table table-sm table-striped small">
<thead>
<tr class="header">
<th style="text-align: left;"></th>
<th style="text-align: right;">1</th>
<th style="text-align: right;">2</th>
<th style="text-align: right;">3</th>
<th style="text-align: right;">4</th>
<th style="text-align: right;">5</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td style="text-align: left;">1</td>
<td style="text-align: right;">0</td>
<td style="text-align: right;">4</td>
<td style="text-align: right;">5</td>
<td style="text-align: right;">0</td>
<td style="text-align: right;">1</td>
</tr>
<tr class="even">
<td style="text-align: left;">2</td>
<td style="text-align: right;">4</td>
<td style="text-align: right;">1652</td>
<td style="text-align: right;">493</td>
<td style="text-align: right;">86</td>
<td style="text-align: right;">316</td>
</tr>
<tr class="odd">
<td style="text-align: left;">3</td>
<td style="text-align: right;">5</td>
<td style="text-align: right;">493</td>
<td style="text-align: right;">1148</td>
<td style="text-align: right;">38</td>
<td style="text-align: right;">509</td>
</tr>
<tr class="even">
<td style="text-align: left;">4</td>
<td style="text-align: right;">0</td>
<td style="text-align: right;">86</td>
<td style="text-align: right;">38</td>
<td style="text-align: right;">6</td>
<td style="text-align: right;">14</td>
</tr>
<tr class="odd">
<td style="text-align: left;">5</td>
<td style="text-align: right;">1</td>
<td style="text-align: right;">316</td>
<td style="text-align: right;">509</td>
<td style="text-align: right;">14</td>
<td style="text-align: right;">818</td>
</tr>
</tbody>
</table>
</div>
</div>
<p>It represents a spatial pattern of the categories, however, it completely omits the secondary information about the weight of each raster cell. To utilize the secondary information, a weighted co-occurrence matrix (<em>wecoma</em>) was developed. It is a modification of the co-occurrence matrix, in which each adjacency contributes to the output based on the values from the weight raster. The contributed value is calculated as the average of the weights in the two adjacent cells.</p>
<div class="cell">
<div class="cell-output-display">
<table class="caption-top table table-sm table-striped small">
<thead>
<tr class="header">
<th style="text-align: left;"></th>
<th style="text-align: right;">1</th>
<th style="text-align: right;">2</th>
<th style="text-align: right;">3</th>
<th style="text-align: right;">4</th>
<th style="text-align: right;">5</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td style="text-align: left;">1</td>
<td style="text-align: right;">0.00</td>
<td style="text-align: right;">7.08</td>
<td style="text-align: right;">15.42</td>
<td style="text-align: right;">0.00</td>
<td style="text-align: right;">2.18</td>
</tr>
<tr class="even">
<td style="text-align: left;">2</td>
<td style="text-align: right;">7.08</td>
<td style="text-align: right;">3513.53</td>
<td style="text-align: right;">1723.24</td>
<td style="text-align: right;">92.45</td>
<td style="text-align: right;">923.97</td>
</tr>
<tr class="odd">
<td style="text-align: left;">3</td>
<td style="text-align: right;">15.42</td>
<td style="text-align: right;">1723.24</td>
<td style="text-align: right;">4524.03</td>
<td style="text-align: right;">113.75</td>
<td style="text-align: right;">2029.07</td>
</tr>
<tr class="even">
<td style="text-align: left;">4</td>
<td style="text-align: right;">0.00</td>
<td style="text-align: right;">92.45</td>
<td style="text-align: right;">113.75</td>
<td style="text-align: right;">3.72</td>
<td style="text-align: right;">36.37</td>
</tr>
<tr class="odd">
<td style="text-align: left;">5</td>
<td style="text-align: right;">2.18</td>
<td style="text-align: right;">923.97</td>
<td style="text-align: right;">2029.07</td>
<td style="text-align: right;">36.37</td>
<td style="text-align: right;">1574.14</td>
</tr>
</tbody>
</table>
</div>
</div>
<p>As you can see above, the weighted co-occurrence matrix differs from regular <em>coma</em>.</p>
<p>Similarly to the previous case, we can also convert <em>wecoma</em> into a one-dimensional normalized representation now called a weighted co-occurrence vector (<em>wecove</em>):</p>
<div class="cell">
<div class="cell-output-display">
<table class="caption-top table table-sm table-striped small">
<colgroup>
<col style="width: 3%">
<col style="width: 7%">
<col style="width: 7%">
<col style="width: 7%">
<col style="width: 7%">
<col style="width: 6%">
<col style="width: 3%">
<col style="width: 7%">
<col style="width: 7%">
<col style="width: 7%">
<col style="width: 7%">
<col style="width: 7%">
<col style="width: 7%">
<col style="width: 7%">
<col style="width: 7%">
</colgroup>
<tbody>
<tr class="odd">
<td style="text-align: right;">0</td>
<td style="text-align: right;">0.0007</td>
<td style="text-align: right;">0.1802</td>
<td style="text-align: right;">0.0016</td>
<td style="text-align: right;">0.1767</td>
<td style="text-align: right;">0.232</td>
<td style="text-align: right;">0</td>
<td style="text-align: right;">0.0095</td>
<td style="text-align: right;">0.0117</td>
<td style="text-align: right;">0.0002</td>
<td style="text-align: right;">0.0002</td>
<td style="text-align: right;">0.0948</td>
<td style="text-align: right;">0.2081</td>
<td style="text-align: right;">0.0037</td>
<td style="text-align: right;">0.0807</td>
</tr>
</tbody>
</table>
</div>
</div>
<p>You can also see the weighted co-occurrence matrix (wecoma) concept, there described as an exposure matrix, in action in <a href="https://cran.r-project.org/package=raceland">the vignettes of the raceland package</a>.</p>
</section>
</section>
<section id="an-integrated-co-occurrence-matrix-incoma-representation" class="level1">
<h1>An integrated co-occurrence matrix (incoma) representation</h1>
<p>Another situation would be when we have two or more categorical raster variables. For example, let’s consider one raster with land cover categories and one with landform classes.</p>
<div class="cell" data-preview="true">
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://jakubnowosad.com/posts/2021-02-10-motif-bp2/index_files/figure-html/unnamed-chunk-26-1.png" class="img-fluid figure-img" width="864"></p>
</figure>
</div>
</div>
</div>
<p>The question here is how to create a signature that incorporates spatial patterns of both land cover and landform data? The apparent solution would be to create a new raster with the joint-distribution of class labels. For example, if agriculture is represented as <code>1</code> in the first raster and flat plains are represented as <code>1</code> in the second raster, then a value of <code>101</code> would represent agriculture on a flat plain in a new raster. Next, we could just calculate a regular co-occurrence matrix. However, this approach is not recommended - by creating joint labels in this example data, we would end up with 84 categories, and therefore with a co-occurrence matrix of 84 by 84. Large signatures not only occupy more storage but also are harder to meaningfuly compare.</p>
<p>An alternative approach is to use an integrated co-occurrence matrix (<em>incoma</em>). It consists of co-occurrence matrices (<em>coma</em>) and co-located co-occurrence matrices (<em>cocoma</em>). In the co-occurrence matrix, we only use one raster and count adjacent categories of each cell. The co-located co-occurrence matrix, on the other hand, uses two rasters and counts neighbors in the second raster for each cell in the first raster.</p>
<p>The <em>incoma</em> representation for two rasters consists of four sectors (see an example below):</p>
<ol type="1">
<li>A co-occurrence matrix for the first raster.</li>
<li>A co-located co-occurrence matrix between the first raster and the second raster. It is between the first and third column and the third and fourth row.</li>
<li>A co-located co-occurrence matrix between the second and the first raster.</li>
<li>A co-occurrence matrix for the second raster.</li>
</ol>
<div class="cell">
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://jakubnowosad.com/posts/2021-02-10-motif-bp2/figs/coma_text.png" class="img-fluid figure-img" style="width:100.0%"></p>
</figure>
</div>
</div>
</div>
<p>Similar to the previous signatures, it is possible to convert <em>incoma</em> to its 1D normalized representation called an integrated co-occurrence vector (<em>incove</em>).</p>
</section>
<section id="summary" class="level1">
<h1>Summary</h1>
<p>Spatial signatures allow to store compressed information about spatial patterns of many types of data. It includes a co-occurrence matrix (<em>coma</em>) for regular categorical rasters, a weighted co-occurrence matrix (<em>wecoma</em>) for categorical rasters with related intensity rasters, and an integrated co-occurrence matrix (<em>incoma</em>) for two or more categorical rasters. A spatial signature can be represented by 1D vectors and compared using a large number of distance measures.</p>
<p>To learn more how different spatial signatures can be calculated read the <a href="https://nowosad.github.io/motif/articles/articles/v2_signatures.html">Types of spatial patterns’ signatures</a>, <a href="https://nowosad.github.io/comat/articles/coma.html">A co-occurrence matrix (coma) representation</a>, <a href="https://nowosad.github.io/comat/articles/wecoma.html">A weighted co-occurrence matrix (wecoma) representation</a>, and <a href="https://nowosad.github.io/comat/articles/incoma.html">An integrated co-occurrence matrix (incoma) representation</a> vignettes.</p>


</section>


<div id="quarto-appendix" class="default"><section id="footnotes" class="footnotes footnotes-end-of-document"><h2 class="anchored quarto-appendix-heading">Footnotes</h2>

<ol>
<li id="fn1"><p>See the <a href="../../posts/2019-06-25-ent-bp1/">Information theory provides a consistent framework for the analysis of spatial patterns</a> blog post.↩︎</p></li>
<li id="fn2"><p>It also means dividing the diagonal by two.↩︎</p></li>
<li id="fn3"><p>Read https://users.uom.gr/~kouiruki/sung.pdf for a comprehensive review of distance measures.↩︎</p></li>
<li id="fn4"><p>Larger values of the <strong>Jensen-Shannon distance</strong> could occur when two rasters have different categories↩︎</p></li>
</ol>
</section><section class="quarto-appendix-contents" id="quarto-reuse"><h2 class="anchored quarto-appendix-heading">Reuse</h2><div class="quarto-appendix-contents"><div><a rel="license" href="https://creativecommons.org/licenses/by/4.0/">CC BY 4.0</a></div></div></section><section class="quarto-appendix-contents" id="quarto-citation"><h2 class="anchored quarto-appendix-heading">Citation</h2><div><div class="quarto-appendix-secondary-label">BibTeX citation:</div><div class="code-copy-outer-scaffold"><pre class="sourceCode quarto-appendix-bibtex code-with-copy"><code class="sourceCode bibtex">@online{nowosad2021,
  author = {Nowosad, Jakub},
  title = {Describing Categorical Rasters with Spatial Signatures},
  date = {2021-02-10},
  url = {https://jakubnowosad.com/posts/2021-02-10-motif-bp2/},
  doi = {10.59350/ye17c-jw24},
  langid = {en}
}
</code></pre></div><div class="quarto-appendix-secondary-label">For attribution, please cite this work as:</div><div id="ref-nowosad2021" class="csl-entry quarto-appendix-citeas">
Nowosad, Jakub. 2021. <span>“Describing Categorical Rasters with Spatial
Signatures.”</span> February 10. <a href="https://doi.org/10.59350/ye17c-jw24">https://doi.org/10.59350/ye17c-jw24</a>.
</div></div></section></div> ]]></description>
  <category>spatial</category>
  <category>geocompr</category>
  <category>rstats</category>
  <category>landscape-ecology</category>
  <category>spatial-patterns</category>
  <category>sil</category>
  <category>motif</category>
  <guid>https://jakubnowosad.com/posts/2021-02-10-motif-bp2/</guid>
  <pubDate>Wed, 10 Feb 2021 00:00:00 GMT</pubDate>
</item>
<item>
  <title>Pattern-based spatial analysis: an approach for discovering, describing and studying geographical patterns</title>
  <dc:creator>Jakub Nowosad</dc:creator>
  <link>https://jakubnowosad.com/posts/2021-02-04-pattern-based-spatial-analysis/</link>
  <description><![CDATA[ 





<p>I gave the overview of what is the pattern-based spatial analysis and how it can be applied for the <a href="https://geoinfo.science/">RGS-IBG GIScience Webinar Series</a>. You can find the workshop abstract, slides, and recording below.</p>
<section id="abstract" class="level2">
<h2 class="anchored" data-anchor-id="abstract">Abstract</h2>
<p>Discovering and describing spatial patterns is an important element of many geographical studies with spatial patterns being related to ecological and sociological processes. While spatial patterns are often clearly visible on maps, it is not easy to unequivocally decide if two areas are much alike or delineate regions with similar patterns. In this talk, Jakub Nowosad will present a set of consistent ideas on how spatial patterns can be described and analyzed, with a focus on categorical raster data. The core idea is to divide raster data consisting of cells having simple content (a single value) into a large number of smaller areas, and then characterize each area using a statistical description of a pattern - a spatial signature. Spatial signatures are multi-values representations of spatial composition and configuration, and therefore can be compared using a large number of existing distance or dissimilarity measures. This enables spatial analysis such as search, change detection, clustering, and segmentation. During this talk, a number of real-life examples of finding similar spatial patterns, detecting changes over time, and grouping areas with homogeneous patterns for regional, continental, and global scales will be shown.</p>
</section>
<section id="slides" class="level2">
<h2 class="anchored" data-anchor-id="slides">Slides</h2>
<p>You can find the slides for the talk at https://nowosad.github.io/giscience-webinar-2021.</p>
</section>
<section id="recording" class="level2">
<h2 class="anchored" data-anchor-id="recording">Recording</h2>
<iframe width="560" height="315" src="https://www.youtube.com/embed/CDgsgcvsg_Y" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen="">
</iframe>


</section>

<div id="quarto-appendix" class="default"><section class="quarto-appendix-contents" id="quarto-reuse"><h2 class="anchored quarto-appendix-heading">Reuse</h2><div class="quarto-appendix-contents"><div><a rel="license" href="https://creativecommons.org/licenses/by/4.0/">CC BY 4.0</a></div></div></section><section class="quarto-appendix-contents" id="quarto-citation"><h2 class="anchored quarto-appendix-heading">Citation</h2><div><div class="quarto-appendix-secondary-label">BibTeX citation:</div><div class="code-copy-outer-scaffold"><pre class="sourceCode quarto-appendix-bibtex code-with-copy"><code class="sourceCode bibtex">@online{nowosad2021,
  author = {Nowosad, Jakub},
  title = {Pattern-Based Spatial Analysis: An Approach for Discovering,
    Describing and Studying Geographical Patterns},
  date = {2021-02-04},
  url = {https://jakubnowosad.com/posts/2021-02-04-pattern-based-spatial-analysis/},
  doi = {10.59350/agr99-0a486},
  langid = {en}
}
</code></pre></div><div class="quarto-appendix-secondary-label">For attribution, please cite this work as:</div><div id="ref-nowosad2021" class="csl-entry quarto-appendix-citeas">
Nowosad, Jakub. 2021. <span>“Pattern-Based Spatial Analysis: An Approach
for Discovering, Describing and Studying Geographical Patterns.”</span>
February 4. <a href="https://doi.org/10.59350/agr99-0a486">https://doi.org/10.59350/agr99-0a486</a>.
</div></div></section></div> ]]></description>
  <category>spatial</category>
  <category>landscape-ecology</category>
  <category>spatial-patterns</category>
  <category>sil</category>
  <category>motif</category>
  <guid>https://jakubnowosad.com/posts/2021-02-04-pattern-based-spatial-analysis/</guid>
  <pubDate>Thu, 04 Feb 2021 00:00:00 GMT</pubDate>
</item>
<item>
  <title>Pattern-based spatial analysis in R: an introduction</title>
  <dc:creator>Jakub Nowosad</dc:creator>
  <link>https://jakubnowosad.com/posts/2021-02-03-motif-bp1/</link>
  <description><![CDATA[ 





<p>TLTR:</p>
<div class="cell" data-layout-align="right">
<div class="cell-output-display">
<div class="quarto-figure quarto-figure-right">
<figure class="figure">
<p><img src="https://jakubnowosad.com/posts/2021-02-03-motif-bp1/figs/logo.png" class="img-fluid quarto-figure quarto-figure-right figure-img" width="150"></p>
</figure>
</div>
</div>
</div>
<!-- <img src="figs/logo.png" align="right" width="150" /> -->
<p><strong>motif</strong> 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.</p>
<section id="spatial-patterns" class="level1">
<h1>Spatial patterns</h1>
<p>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.</p>
<p>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.</p>
</section>
<section id="landscape-metrics" class="level1">
<h1>Landscape metrics</h1>
<p>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).</p>
<p>The <strong>landscapemetrics</strong> package allows calculating various landscape metrics in R. It contains a simple categorical raster named <code>landscape</code> with three classes, which we can use to calculate some metrics. To learn more about these ideas and the <strong>landscapemetrics</strong> package visit https://r-spatialecology.github.io/landscapemetrics.</p>
<div class="cell" data-preview="true">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(landscapemetrics)</span>
<span id="cb1-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(raster)</span>
<span id="cb1-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">plot</span>(landscape)</span></code></pre></div></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://jakubnowosad.com/posts/2021-02-03-motif-bp1/index_files/figure-html/unnamed-chunk-2-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<p>For example, the <code>lsm_l_shdi()</code> 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.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb2-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lsm_l_shdi</span>(landscape)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code># A tibble: 1 × 6
  layer level     class    id metric value
  &lt;int&gt; &lt;chr&gt;     &lt;int&gt; &lt;int&gt; &lt;chr&gt;  &lt;dbl&gt;
1     1 landscape    NA    NA shdi    1.01</code></pre>
</div>
</div>
<p>The <code>lsm_l_ai()</code> 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.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb4-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lsm_l_ai</span>(landscape)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code># A tibble: 1 × 6
  layer level     class    id metric value
  &lt;int&gt; &lt;chr&gt;     &lt;int&gt; &lt;int&gt; &lt;chr&gt;  &lt;dbl&gt;
1     1 landscape    NA    NA ai      81.1</code></pre>
</div>
</div>
</section>
<section id="spatial-signatures" class="level1">
<h1>Spatial signatures</h1>
<p>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?</p>
<p>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.</p>
<p>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.</p>
</section>
<section id="motif" class="level1">
<h1>motif</h1>
<p>Search, change detection, and clustering of spatial patterns have been possible in GRASS GIS using <a href="http://sil.home.amu.edu.pl/index.php?id=software">the GeoPAT module</a> or command-line tool <a href="https://github.com/Nowosad/geopat2">GeoPAT 2</a>. All of the above actions can also be now performed natively in R with the <strong>motif</strong> package. In a series of blog posts, I plan to show and explain several use cases. They include:</p>
<p><em>A. Finding areas similar to the area of interest</em></p>
<div class="cell">
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://jakubnowosad.com/posts/2021-02-03-motif-bp1/figs/search2.png" class="img-fluid figure-img" style="width:100.0%"></p>
</figure>
</div>
</div>
</div>
<p><em>B. Comparing changes between two times</em></p>
<div class="cell">
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://jakubnowosad.com/posts/2021-02-03-motif-bp1/figs/compare2.png" class="img-fluid figure-img" style="width:100.0%"></p>
</figure>
</div>
</div>
</div>
<p><em>C. Clustering areas with similar patterns of more than one layer of data</em></p>
<div class="cell">
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://jakubnowosad.com/posts/2021-02-03-motif-bp1/figs/cluster2.png" class="img-fluid figure-img" style="width:100.0%"></p>
</figure>
</div>
</div>
</div>
<p>If you do not want to wait for the next blog post, you can install the the <strong>motif</strong> package with:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb6" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb6-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">install.packages</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"motif"</span>)</span></code></pre></div></div>
<p>You can read more about it in the <a href="https://link.springer.com/article/10.1007/s10980-020-01135-0">Landscape Ecology article</a> or <a href="https://ecoevorxiv.org/kj7fu">its preprint</a>:</p>
<blockquote class="blockquote">
<p>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</p>
</blockquote>
<p>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.</p>


</section>

<div id="quarto-appendix" class="default"><section class="quarto-appendix-contents" id="quarto-reuse"><h2 class="anchored quarto-appendix-heading">Reuse</h2><div class="quarto-appendix-contents"><div><a rel="license" href="https://creativecommons.org/licenses/by/4.0/">CC BY 4.0</a></div></div></section><section class="quarto-appendix-contents" id="quarto-citation"><h2 class="anchored quarto-appendix-heading">Citation</h2><div><div class="quarto-appendix-secondary-label">BibTeX citation:</div><div class="code-copy-outer-scaffold"><pre class="sourceCode quarto-appendix-bibtex code-with-copy"><code class="sourceCode bibtex">@online{nowosad2021,
  author = {Nowosad, Jakub},
  title = {Pattern-Based Spatial Analysis in {R:} An Introduction},
  date = {2021-02-03},
  url = {https://jakubnowosad.com/posts/2021-02-03-motif-bp1/},
  doi = {10.59350/3xae9-80f62},
  langid = {en}
}
</code></pre></div><div class="quarto-appendix-secondary-label">For attribution, please cite this work as:</div><div id="ref-nowosad2021" class="csl-entry quarto-appendix-citeas">
Nowosad, Jakub. 2021. <span>“Pattern-Based Spatial Analysis in R: An
Introduction.”</span> February 3. <a href="https://doi.org/10.59350/3xae9-80f62">https://doi.org/10.59350/3xae9-80f62</a>.
</div></div></section></div> ]]></description>
  <category>spatial</category>
  <category>geocompr</category>
  <category>rstats</category>
  <category>landscape-ecology</category>
  <category>spatial-patterns</category>
  <category>sil</category>
  <category>motif</category>
  <guid>https://jakubnowosad.com/posts/2021-02-03-motif-bp1/</guid>
  <pubDate>Wed, 03 Feb 2021 00:00:00 GMT</pubDate>
</item>
<item>
  <title>How to choose a bivariate color palette?</title>
  <dc:creator>Jakub Nowosad</dc:creator>
  <link>https://jakubnowosad.com/posts/2020-08-25-cbc-bp2/</link>
  <description><![CDATA[ 





<!-- intro -->
<!-- how to interpret? -->
<p>Bivariate color palettes are products of combining two separate color palettes. They are usually represented by a square with rows (one color palette) and columns (second color palette). You can more about how they are made in the blog post <a href="https://www.joshuastevens.net/cartography/make-a-bivariate-choropleth-map/">“Bivariate Choropleth Maps: A How-to Guide”</a> by Joshua Stevens.</p>
<p>The main role of bivariate color palettes is to present the values of two variables simultaneously. For example, the map below uses a bivariate palette to represent both GDP per capita and life expectancy for countries in Africa.</p>
<p><img src="https://jakubnowosad.com/posts/2020-08-25-cbc-bp2/figs/bivarmap.png" class="img-fluid"></p>
<p>The code to create this map is <a href="https://github.com/mtennekes/tmap/issues/183#issuecomment-670554921">in the <strong>tmap</strong> issue tracker</a>. Some other bivariate maps’ examples can be found in the <a href="https://cran.r-project.org/web/packages/biscale/vignettes/biscale.html">“Bivarite Mapping with ggplot2”</a> vignette and the <a href="https://timogrossenbacher.ch/2019/04/bivariate-maps-with-ggplot2-and-sf/">“Bivariate maps with ggplot2 and sf”</a> blog post.</p>
<p>The above map has one issue, though. As pointed out by <a href="https://twitter.com/fred_r_ramos/status/1293579782467264512">Frederico R Ramos</a>, it is not suitable for people with color vision deficiencies. They are not able to distinguish between some colors, and therefore, cannot understand the map correctly. Therefore, the main question is how to choose a proper bivariate color palette?</p>
<section id="bivariate-palettes" class="level2">
<h2 class="anchored" data-anchor-id="bivariate-palettes">Bivariate palettes</h2>
<p>The <a href="https://cran.r-project.org/package=pals"><strong>pals</strong></a> R package has a dozen or so bivariate color palettes.</p>
<div class="cell" data-layout-align="center">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(pals)</span>
<span id="cb1-2">bivcol <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(pal){</span>
<span id="cb1-3">        tit <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">substitute</span>(pal)</span>
<span id="cb1-4">        pal <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">pal</span>()</span>
<span id="cb1-5">        ncol <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">length</span>(pal)</span>
<span id="cb1-6">        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">image</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">matrix</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">seq_along</span>(pal), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">nrow =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sqrt</span>(ncol)),</span>
<span id="cb1-7">              <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">axes =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>, </span>
<span id="cb1-8">              <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">col =</span> pal, </span>
<span id="cb1-9">              <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">asp =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span>
<span id="cb1-10">        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mtext</span>(tit)</span>
<span id="cb1-11">}</span></code></pre></div></div>
</div>
<p>Twelve of these palettes are presented below.</p>
<div class="cell" data-layout-align="center">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb2-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">par</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mfrow =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mar =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>))</span>
<span id="cb2-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">bivcol</span>(arc.bluepink)</span>
<span id="cb2-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">bivcol</span>(brewer.divdiv)</span>
<span id="cb2-4"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">bivcol</span>(brewer.divseq)</span>
<span id="cb2-5"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">bivcol</span>(brewer.qualseq)</span>
<span id="cb2-6"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">bivcol</span>(brewer.seqseq1)</span>
<span id="cb2-7"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">bivcol</span>(brewer.seqseq2)</span>
<span id="cb2-8"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">bivcol</span>(census.blueyellow)</span>
<span id="cb2-9"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">bivcol</span>(stevens.bluered)</span>
<span id="cb2-10"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">bivcol</span>(stevens.greenblue)</span>
<span id="cb2-11"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">bivcol</span>(stevens.pinkblue)</span>
<span id="cb2-12"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">bivcol</span>(stevens.pinkgreen)</span>
<span id="cb2-13"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">bivcol</span>(stevens.purplegold)</span></code></pre></div></div>
<div class="cell-output-display">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://jakubnowosad.com/posts/2020-08-25-cbc-bp2/index_files/figure-html/unnamed-chunk-2-1.png" class="img-fluid quarto-figure quarto-figure-center figure-img" width="768"></p>
</figure>
</div>
</div>
</div>
</section>
<section id="palettes-properties" class="level2">
<h2 class="anchored" data-anchor-id="palettes-properties">Palettes’ properties</h2>
<p>Now, we can use the <strong>colorblindcheck</strong> package to decide if the selected color palette is colorblind-friendly or not.</p>
<div class="cell" data-layout-align="center">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb3-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># remotes::install_github("nowosad/colorblindcheck")</span></span>
<span id="cb3-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(colorblindcheck)</span></code></pre></div></div>
</div>
<p>The main function in this package is <code>palette_check()</code>, which creates summary statistics comparing the original input palette and simulations of three main color vision deficiencies. Let’s use it on two color palettes: <code>arc.bluepink()</code> and <code>brewer.seqseq2()</code>.</p>
<div class="cell" data-layout-align="center">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb4-1">colorblindcheck<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">palette_check</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">arc.bluepink</span>(),</span>
<span id="cb4-2">                               <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">plot =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">bivariate =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>)</span></code></pre></div></div>
<div class="cell-output-display">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://jakubnowosad.com/posts/2020-08-25-cbc-bp2/index_files/figure-html/unnamed-chunk-4-1.png" class="img-fluid quarto-figure quarto-figure-center figure-img" width="672"></p>
</figure>
</div>
</div>
<div class="cell-output cell-output-stdout">
<pre><code>          name  n tolerance ncp ndcp  min_dist mean_dist max_dist
1       normal 16  7.135562 120  120 7.1355623  27.72463 53.76783
2 deuteranopia 16  7.135562 120  100 0.3450842  19.79323 52.46731
3   protanopia 16  7.135562 120   96 0.0000000  20.08030 50.20137
4   tritanopia 16  7.135562 120  120 7.9914570  31.48801 71.57927</code></pre>
</div>
</div>
<p>The visual inspection of <code>arc.bluepink()</code> suggests that this palette is not suitable for people with color vision deficiencies, namely deuteranopia and protanopia. In deuteranopia and protanopia simulations, it is almost impossible to distinguish some colors. This problem is also confirmed by the summary statistics, where the minimal distance between colors of the original palette is about 7, while it is only about 0.345 for deuteranopia and 0 (no difference at all) for protanopia.</p>
<div class="cell" data-layout-align="center">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb6" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb6-1">colorblindcheck<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">palette_check</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">brewer.seqseq2</span>(), </span>
<span id="cb6-2">                               <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">plot =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">bivariate =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>)</span></code></pre></div></div>
<div class="cell-output-display">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://jakubnowosad.com/posts/2020-08-25-cbc-bp2/index_files/figure-html/unnamed-chunk-5-1.png" class="img-fluid quarto-figure quarto-figure-center figure-img" width="672"></p>
</figure>
</div>
</div>
<div class="cell-output cell-output-stdout">
<pre><code>          name n tolerance ncp ndcp min_dist mean_dist max_dist
1       normal 9  13.21133  36   36 13.21133  39.99288 94.59810
2 deuteranopia 9  13.21133  36   34 10.99234  40.33172 94.22020
3   protanopia 9  13.21133  36   34 10.53062  38.99158 94.59810
4   tritanopia 9  13.21133  36   36 13.66888  39.60803 94.48661</code></pre>
</div>
</div>
<p>On the other hand, the inspection of <code>brewer.seqseq2()</code> indicate that it is possible to differentiate between all of the colors in this palette based on the original colors and simulations of color vision deficiencies. You can see more examples of <strong>colorblindcheck</strong> in action at https://nowosad.github.io/colorblindcheck.</p>
</section>
<section id="colorblind-friendly-palettes" class="level2">
<h2 class="anchored" data-anchor-id="colorblind-friendly-palettes">Colorblind-friendly palettes</h2>
<p>Using the above function, <a href="https://nowosad.github.io/colorblindcheck/articles/articles/check_bivariate_pals.html">I tested all of the bivariate color palettes from <strong>pals</strong></a>. I visualized all of the palettes and decided to keep only the ones for which the minimal distance between colors was above 6.</p>
<p>It allowed to distinguish four palettes - <code>brewer.divseq</code>, <code>brewer.seqseq2</code>, <code>stevens.greenblue</code>, and <code>stevens.purplegold</code>. You can see the comparison between them and simulations of color vision deficiencies below.</p>
<div class="cell" data-layout-align="center">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb8" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb8-1">colorblindcheck<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">palette_check</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">brewer.divseq</span>(), </span>
<span id="cb8-2">                               <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">plot =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">bivariate =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>)</span></code></pre></div></div>
<div class="cell-output-display">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://jakubnowosad.com/posts/2020-08-25-cbc-bp2/index_files/figure-html/unnamed-chunk-6-1.png" class="img-fluid quarto-figure quarto-figure-center figure-img" width="672"></p>
</figure>
</div>
</div>
<div class="cell-output cell-output-stdout">
<pre><code>          name n tolerance ncp ndcp min_dist mean_dist max_dist
1       normal 9  9.237516  36   36 9.237516  38.32933 87.90123
2 deuteranopia 9  9.237516  36   36 9.267188  39.85751 90.88415
3   protanopia 9  9.237516  36   36 9.237516  40.79861 86.08385
4   tritanopia 9  9.237516  36   35 6.777558  32.82160 83.10774</code></pre>
</div>
</div>
<div class="cell" data-layout-align="center">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb10" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb10-1">colorblindcheck<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">palette_check</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">brewer.seqseq2</span>(),</span>
<span id="cb10-2">                               <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">plot =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">bivariate =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>)</span></code></pre></div></div>
<div class="cell-output-display">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://jakubnowosad.com/posts/2020-08-25-cbc-bp2/index_files/figure-html/unnamed-chunk-7-1.png" class="img-fluid quarto-figure quarto-figure-center figure-img" width="672"></p>
</figure>
</div>
</div>
<div class="cell-output cell-output-stdout">
<pre><code>          name n tolerance ncp ndcp min_dist mean_dist max_dist
1       normal 9  13.21133  36   36 13.21133  39.99288 94.59810
2 deuteranopia 9  13.21133  36   34 10.99234  40.33172 94.22020
3   protanopia 9  13.21133  36   34 10.53062  38.99158 94.59810
4   tritanopia 9  13.21133  36   36 13.66888  39.60803 94.48661</code></pre>
</div>
</div>
<div class="cell" data-layout-align="center">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb12" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb12-1">colorblindcheck<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">palette_check</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">stevens.greenblue</span>(),</span>
<span id="cb12-2">                               <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">plot =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">bivariate =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>)</span></code></pre></div></div>
<div class="cell-output-display">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://jakubnowosad.com/posts/2020-08-25-cbc-bp2/index_files/figure-html/unnamed-chunk-8-1.png" class="img-fluid quarto-figure quarto-figure-center figure-img" width="672"></p>
</figure>
</div>
</div>
<div class="cell-output cell-output-stdout">
<pre><code>          name n tolerance ncp ndcp min_dist mean_dist max_dist
1       normal 9   9.29651  36   36 9.296510  26.34666 50.19184
2 deuteranopia 9   9.29651  36   33 7.238684  24.60856 51.19105
3   protanopia 9   9.29651  36   35 7.693015  24.51814 47.10098
4   tritanopia 9   9.29651  36   29 6.154169  20.06474 50.20386</code></pre>
</div>
</div>
<div class="cell" data-layout-align="center">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb14" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb14-1">colorblindcheck<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">palette_check</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">stevens.purplegold</span>(),</span>
<span id="cb14-2">                               <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">plot =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">bivariate =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>)</span></code></pre></div></div>
<div class="cell-output-display">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://jakubnowosad.com/posts/2020-08-25-cbc-bp2/index_files/figure-html/unnamed-chunk-9-1.png" class="img-fluid quarto-figure quarto-figure-center figure-img" width="672"></p>
</figure>
</div>
</div>
<div class="cell-output cell-output-stdout">
<pre><code>          name n tolerance ncp ndcp min_dist mean_dist max_dist
1       normal 9  11.97625  36   36 11.97625  30.13646 53.56032
2 deuteranopia 9  11.97625  36   35 10.57857  27.58839 46.59557
3   protanopia 9  11.97625  36   34 11.48625  29.32017 50.36899
4   tritanopia 9  11.97625  36   28  6.31650  20.96426 49.27898</code></pre>
</div>
</div>
</section>
<section id="summary" class="level2">
<h2 class="anchored" data-anchor-id="summary">Summary</h2>
<p>Four palettes from the <strong>pals</strong> package, <code>brewer.divseq</code>, <code>brewer.seqseq2</code>, <code>stevens.greenblue</code>, and <code>stevens.purplegold</code> seem to be the most adequate to use for bivariate visualizations.</p>
<div class="cell" data-layout-align="center">
<div class="cell-output-display">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://jakubnowosad.com/posts/2020-08-25-cbc-bp2/index_files/figure-html/unnamed-chunk-10-1.png" class="img-fluid quarto-figure quarto-figure-center figure-img" width="768"></p>
</figure>
</div>
</div>
</div>
<p>All of them are suitable for people with color deficiencies. It is important to note that <code>brewer.divseq</code> is made of a sequential (from bottom to top) and a diverging (from left to right) palette. Therefore its use should be limited only to some subset of applications, when we want to present one variable going from high to low (or vice versa) and one variable that has values around a central neutral point. <code>brewer.seqseq2</code>, <code>stevens.greenblue</code>, and <code>stevens.purplegold</code>, on the other hand, consists of a mix of two sequential palettes and, thus, should be used to present two variables with values going from high to low (or vice versa).</p>


</section>

<div id="quarto-appendix" class="default"><section class="quarto-appendix-contents" id="quarto-reuse"><h2 class="anchored quarto-appendix-heading">Reuse</h2><div class="quarto-appendix-contents"><div><a rel="license" href="https://creativecommons.org/licenses/by/4.0/">CC BY 4.0</a></div></div></section><section class="quarto-appendix-contents" id="quarto-citation"><h2 class="anchored quarto-appendix-heading">Citation</h2><div><div class="quarto-appendix-secondary-label">BibTeX citation:</div><div class="code-copy-outer-scaffold"><pre class="sourceCode quarto-appendix-bibtex code-with-copy"><code class="sourceCode bibtex">@online{nowosad2020,
  author = {Nowosad, Jakub},
  title = {How to Choose a Bivariate Color Palette?},
  date = {2020-08-25},
  url = {https://jakubnowosad.com/posts/2020-08-25-cbc-bp2/},
  doi = {10.59350/60m5m-r0622},
  langid = {en}
}
</code></pre></div><div class="quarto-appendix-secondary-label">For attribution, please cite this work as:</div><div id="ref-nowosad2020" class="csl-entry quarto-appendix-citeas">
Nowosad, Jakub. 2020. <span>“How to Choose a Bivariate Color
Palette?”</span> August 25. <a href="https://doi.org/10.59350/60m5m-r0622">https://doi.org/10.59350/60m5m-r0622</a>.
</div></div></section></div> ]]></description>
  <category>rstats</category>
  <category>vis</category>
  <category>colors</category>
  <category>bivariate</category>
  <guid>https://jakubnowosad.com/posts/2020-08-25-cbc-bp2/</guid>
  <pubDate>Tue, 25 Aug 2020 00:00:00 GMT</pubDate>
</item>
<item>
  <title>How to measure spatial diversity and segregation?</title>
  <dc:creator>Jakub Nowosad</dc:creator>
  <link>https://jakubnowosad.com/posts/2020-08-03-raceland-bp1/</link>
  <description><![CDATA[ 





<p>The <strong>raceland</strong> package implements a computational framework for a pattern-based, zoneless analysis and visualization of (ethno)racial topography.</p>
<p>The main concept in this package is a racial landscape (RL). It consists of many large and small patches (racial enclaves) formed by adjacent raster grid cells having the same race categories. The distribution of racial enclaves creates a specific spatial pattern, which can be quantified by two metrics (entropy and mutual information) derived from the Information Theory concept (IT). Entropy is the measure of racial diversity and mutual information measures racial segregation.</p>
<p>Methods in the <strong>raceland</strong> package are based on the raster data, and unlike the previous methods, do not depend on the division for specific zones (census tract, census block, etc.). Calculation of racial diversity (entropy) and racial segregation (mutual information) can be performed for the whole area of interests (i.e., metropolitan area) or any portion of the whole area without introducing any arbitrary divisions.</p>
<p>To learn more about this topic, read our <a href="https://www.sciencedirect.com/science/article/abs/pii/S0143622819310367">Applied Geography article</a> or <a href="https://osf.io/preprints/socarxiv/mejz5">its preprint</a>:</p>
<blockquote class="blockquote">
<p>Dmowska, A., Stepinski T., Nowosad J. Racial landscapes – a pattern-based, zoneless method for analysis and visualization of racial topography. Applied Geography. 122:1-9, DOI:10.1016/j.apgeog.2020.102239</p>
</blockquote>
<section id="example-calculations" class="level1">
<h1>Example calculations</h1>
<p>To reproduce the results on your own computer, install and attach the following packages:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(raceland)</span>
<span id="cb1-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(raster)</span>
<span id="cb1-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(sf)</span>
<span id="cb1-4"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(tmap)</span>
<span id="cb1-5"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(dplyr)</span></code></pre></div></div>
</div>
<p>You also need to download and extract the <code>data.zip</code> file containing the example data.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb2-1">temp_data_file <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tempfile</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fileext =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">".zip"</span>)</span>
<span id="cb2-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">download.file</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"https://github.com/Nowosad/raceland-bp1/raw/master/data.zip"</span>,</span>
<span id="cb2-3">              <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">destfile =</span> temp_data_file,</span>
<span id="cb2-4">              <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mode =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"wb"</span>)</span>
<span id="cb2-5"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">unzip</span>(temp_data_file)</span></code></pre></div></div>
</div>
<section id="input-data" class="level2">
<h2 class="anchored" data-anchor-id="input-data">Input data</h2>
<p>The presented approach requires a set of rasters, where each raster represents one of five race-groups: Asians, Blacks, Hispanic, others, and Whites. In this example, we use data limited to the city of Cincinnati, Ohio.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb3-1">list_raster <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">dir</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"data"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">pattern =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">".tif$"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">full.names =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>)</span>
<span id="cb3-2">race_raster <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">stack</span>(list_raster)</span></code></pre></div></div>
</div>
<p>We also use vector data containing the city borders to ease the understanding of the results.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb4-1">cincinnati <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">read_sf</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"data/cincinnati.gpkg"</span>)</span></code></pre></div></div>
</div>
<p>We can visualize the data using the <strong>tmap</strong> package:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb5-1">tm_race <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tm_shape</span>(race_raster) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb5-2">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tm_raster</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">style =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"fisher"</span>,</span>
<span id="cb5-3">              <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">n =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>,</span>
<span id="cb5-4">              <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">palette =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"viridis"</span>,</span>
<span id="cb5-5">              <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">title =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Number of people"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb5-6">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tm_facets</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">nrow =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb5-7">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tm_shape</span>(cincinnati) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb5-8">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tm_borders</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">lwd =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">col =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"black"</span>)</span>
<span id="cb5-9">tm_race</span></code></pre></div></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://jakubnowosad.com/posts/2020-08-03-raceland-bp1/index_files/figure-html/unnamed-chunk-5-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<p>The above maps show the distribution of people from different race-groups in Cincinnati. Each, 30 by 30 meters, cell represents a number of people living in this area. Data was obtained from https://www.socscape.edu.pl/ and preprocessed using the instructions at https://cran.r-project.org/web/packages/raceland/vignettes/raceland-intro3.html.</p>
</section>
</section>
<section id="basic-example" class="level1">
<h1>Basic example</h1>
<p>Our goal is to measure racial diversity and racial segregation for different places in the city. We can use the <code>quantify_raceland()</code> function for this purpose.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb6" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb6-1">results_metrics <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">quantify_raceland</span>(race_raster,</span>
<span id="cb6-2">                                    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">n =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">30</span>,</span>
<span id="cb6-3">                                    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">window_size =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>, </span>
<span id="cb6-4">                                    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fun =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"mean"</span>,</span>
<span id="cb6-5">                                    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">20</span>,</span>
<span id="cb6-6">                                    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">threshold =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.75</span>) </span>
<span id="cb6-7"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">head</span>(results_metrics)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>Simple feature collection with 6 features and 4 fields
Geometry type: POLYGON
Dimension:     XY
Bounding box:  xmin: 978285 ymin: 1858035 xmax: 984885 ymax: 1859235
CRS:           PROJCRS["unknown",
    BASEGEOGCRS["unknown",
        DATUM["Unknown_based_on_GRS80_ellipsoid",
            ELLIPSOID["GRS 1980",6378137,298.257222101004,
                LENGTHUNIT["metre",1],
                ID["EPSG",7019]]],
        PRIMEM["Greenwich",0,
            ANGLEUNIT["degree",0.0174532925199433,
                ID["EPSG",9122]]]],
    CONVERSION["Albers Equal Area",
        METHOD["Albers Equal Area",
            ID["EPSG",9822]],
        PARAMETER["Latitude of false origin",23,
            ANGLEUNIT["degree",0.0174532925199433],
            ID["EPSG",8821]],
        PARAMETER["Longitude of false origin",-96,
            ANGLEUNIT["degree",0.0174532925199433],
            ID["EPSG",8822]],
        PARAMETER["Latitude of 1st standard parallel",29.5,
            ANGLEUNIT["degree",0.0174532925199433],
            ID["EPSG",8823]],
        PARAMETER["Latitude of 2nd standard parallel",45.5,
            ANGLEUNIT["degree",0.0174532925199433],
            ID["EPSG",8824]],
        PARAMETER["Easting at false origin",0,
            LENGTHUNIT["metre",1],
            ID["EPSG",8826]],
        PARAMETER["Northing at false origin",0,
            LENGTHUNIT["metre",1],
            ID["EPSG",8827]]],
    CS[Cartesian,2],
        AXIS["easting",east,
            ORDER[1],
            LENGTHUNIT["metre",1,
                ID["EPSG",9001]]],
        AXIS["northing",north,
            ORDER[2],
            LENGTHUNIT["metre",1,
                ID["EPSG",9001]]]]
   row col       ent     mutinf                       geometry
30   1  30 1.0928666 0.01224849 POLYGON ((981885 1859235, 9...
31   1  31 1.2931567 0.03783377 POLYGON ((982485 1859235, 9...
33   1  33 1.1044932 0.01699973 POLYGON ((983685 1859235, 9...
34   1  34 1.6397933 0.05802896 POLYGON ((984285 1859235, 9...
74   2  24 0.9367936 0.01367241 POLYGON ((978285 1858635, 9...
80   2  30 1.4189221 0.04388582 POLYGON ((981885 1858635, 9...</code></pre>
</div>
</div>
<p>It requires several arguments:</p>
<ul>
<li><code>x</code> - RasterStack with race-specific population densities assign to each cell</li>
<li><code>n</code> - a number of realizations</li>
<li><code>window_size</code> - expressed in the numbers of cells, is a length of the side of a square-shaped block of cells for which local densities will be calculated</li>
<li><code>fun</code> - function to calculate values from adjacent cells to contribute to exposure matrix, <code>"mean"</code> - calculate average values of local population densities from adjacent cells, <code>"geometric_mean"</code> - calculate geometric mean values of local population densities from adjacent cells, or <code>"focal"</code> assign value from the focal cell</li>
<li><code>size</code> - expressed in the numbers of cells, is a length of the side of a square-shaped block of cells. It defines the extent of a local pattern</li>
<li><code>threshold</code> - the share of NA cells to allow metrics calculation</li>
</ul>
<p>The result is a spatial vector object containing areas of the size of 20 by 20 cells from input data (600 by 600 meters in this example). Its attribute table has five columns - <code>row</code> and <code>col</code> allowing for identification of each square polygon, <code>ent</code> - entropy measuring racial diversity, <code>mutinf</code> - mutual information, which is associated with measuring racial segregation, and <code>geometry</code> containing spatial geometries.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb8" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb8-1">diversity_map <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tm_shape</span>(results_metrics) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb8-2">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tm_polygons</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">col =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ent"</span>,</span>
<span id="cb8-3">                <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">title =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Diversity"</span>,</span>
<span id="cb8-4">                <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">style =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"cont"</span>,</span>
<span id="cb8-5">                <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">palette =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"magma"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb8-6">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tm_shape</span>(cincinnati) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb8-7">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tm_borders</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">lwd =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">col =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"black"</span>)</span>
<span id="cb8-8">segregation_map <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tm_shape</span>(results_metrics) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb8-9">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tm_polygons</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">col =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"mutinf"</span>,</span>
<span id="cb8-10">                <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">title =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Segregation"</span>,</span>
<span id="cb8-11">                <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">style =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"cont"</span>, </span>
<span id="cb8-12">                <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">palette =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"cividis"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb8-13">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tm_shape</span>(cincinnati) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb8-14">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tm_borders</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">lwd =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">col =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"black"</span>)</span>
<span id="cb8-15"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tmap_arrange</span>(diversity_map, segregation_map)</span></code></pre></div></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://jakubnowosad.com/posts/2020-08-03-raceland-bp1/index_files/figure-html/unnamed-chunk-7-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<p>The above result present areas with different levels of racial diversity and segregation. Interestingly, there is a low correlation between these two properties. Some areas inside of the city do not have any value attached - this indicates either they are covered with missing values in more than 75% of their areas or nobody lives there.</p>
</section>
<section id="extended-example" class="level1">
<h1>Extended example</h1>
<p>The <code>quantify_raceland()</code> function is a wrapper around several steps implemented in <strong>raceland</strong>, namely <code>create_realizations()</code>, <code>create_densities()</code>, <code>calculate_metrics()</code>, and <code>create_grid()</code>. All of them can be used sequentially, as you can see below.</p>
<p><img src="https://jakubnowosad.com/posts/2020-08-03-raceland-bp1/figs/fig0framework.png" class="img-fluid"></p>
<p>Additionally, the <strong>raceland</strong> package has <code>zones_to_raster()</code> function that prepares input data based on spatial vector data with race counts.</p>
<section id="constructing-racial-landscapes" class="level2">
<h2 class="anchored" data-anchor-id="constructing-racial-landscapes">Constructing racial landscapes</h2>
<p>The racial landscape is a high-resolution grid in which each cell contains only inhabitants of a single race. It is constructed using the <code>create_realizations()</code> function, which expects a stack of race-specific rasters. Racial composition at each cell is translated into probabilities of drawing a person of a specific race from a cell. For example, if a cell has 100 people, where 90 are classified as Black (90% chance) and 10 as White (10% chance), then we can assign a specific race randomly based on these probabilities.</p>
<p>This approach generates a specified number (<code>n = 30</code>, in this case) of realization with slightly different patterns.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb9" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb9-1">realizations_raster <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">create_realizations</span>(race_raster, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">n =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">30</span>)</span></code></pre></div></div>
</div>
<p>The output of this function is a RasterStack, where each raster contains values from 1 to <code>k</code>, where <code>k</code> is a number of provided race-specific grids. In this case, we provided five race-specific grids (Asians, Blacks, Hispanic, others, and Whites), therefore the value of <code>1</code> in the output object represents Asians, number <code>2</code> Blacks, etc.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb10" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb10-1">my_pal <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#F16667"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#6EBE44"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#7E69AF"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#C77213"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#F8DF1D"</span>)</span>
<span id="cb10-2">tm_realizations <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tm_shape</span>(realizations_raster[[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>]]) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb10-3">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tm_raster</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">style =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"cat"</span>,</span>
<span id="cb10-4">              <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">palette =</span> my_pal,</span>
<span id="cb10-5">              <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">labels =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Asians"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Blacks"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Hispanic"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"others"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Whites"</span>),</span>
<span id="cb10-6">              <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">title =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">""</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb10-7">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tm_facets</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ncol =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb10-8">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tm_shape</span>(cincinnati) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb10-9">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tm_borders</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">lwd =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">col =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"black"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb10-10">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tm_layout</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">panel.labels =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">paste</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Realization"</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">30</span>))</span>
<span id="cb10-11">tm_realizations</span></code></pre></div></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://jakubnowosad.com/posts/2020-08-03-raceland-bp1/index_files/figure-html/unnamed-chunk-9-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<p>The above plot shows four of 30 created realizations and makes it clear that all of them are fairly similar.</p>
</section>
<section id="local-densities" class="level2">
<h2 class="anchored" data-anchor-id="local-densities">Local densities</h2>
<p>Now, for each of the created realization, we can calculate local densities of subpopulations (race-specific local densities) using the <code>create_densities()</code> function.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb11" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb11-1">dens_raster <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">create_densities</span>(realizations_raster,</span>
<span id="cb11-2">                               race_raster,</span>
<span id="cb11-3">                               <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">window_size =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>)</span></code></pre></div></div>
</div>
<p>The output is a RasterStack with local densities calculated separately for each realization.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb12" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb12-1">tm_density <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tm_shape</span>(dens_raster[[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>]]) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb12-2">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tm_raster</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">style =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"fisher"</span>,</span>
<span id="cb12-3">              <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">n =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>,</span>
<span id="cb12-4">              <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">palette =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"viridis"</span>,</span>
<span id="cb12-5">              <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">title =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Number of people"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb12-6">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tm_facets</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ncol =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb12-7">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tm_shape</span>(cincinnati) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb12-8">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tm_borders</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">lwd =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">col =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"black"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb12-9">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tm_layout</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">panel.labels =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">paste</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Realization"</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">30</span>))</span>
<span id="cb12-10">tm_density</span></code></pre></div></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://jakubnowosad.com/posts/2020-08-03-raceland-bp1/index_files/figure-html/unnamed-chunk-11-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
</section>
<section id="total-diversity-and-segregation" class="level2">
<h2 class="anchored" data-anchor-id="total-diversity-and-segregation">Total diversity and segregation</h2>
<p>We can use both, realizations and density rasters, to calculate racial diversity and segregation using <code>calculate_metrics()</code> function. It calculates four information theory-derived metrics: entropy (<code>ent</code>), joint entropy (<code>joinent</code>), conditional entropy (<code>condent</code>), and mutual information (<code>mutinf</code>). As we mentioned before, <code>ent</code> is measuring racial diversity, while <code>mutinf</code> is associated with racial segregation. These metrics can be calculated for a given spatial scale. For example, setting <code>size</code> to <code>NULL</code>, as in the example below, calculates the metrics for the whole area of each realization.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb13" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb13-1">metr_df <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">calculate_metrics</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> realizations_raster, </span>
<span id="cb13-2">                            <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">w =</span> dens_raster, </span>
<span id="cb13-3">                            <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fun =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"mean"</span>, </span>
<span id="cb13-4">                            <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NULL</span>, </span>
<span id="cb13-5">                            <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">threshold =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span>
<span id="cb13-6"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">head</span>(metr_df)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>  realization row col      ent  joinent  condent    mutinf
1           1   1   1 1.394937 2.617351 1.222414 0.1725237
2           2   1   1 1.395346 2.618786 1.223440 0.1719062
3           3   1   1 1.397898 2.621963 1.224065 0.1738325
4           4   1   1 1.397268 2.622896 1.225628 0.1716395
5           5   1   1 1.396112 2.617471 1.221359 0.1747527
6           6   1   1 1.398692 2.624289 1.225597 0.1730954</code></pre>
</div>
</div>
<p>Now, we can calculate average metrics across all realization, which should give more accurate results.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb15" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb15-1">metr_df <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb15-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">summarise</span>(</span>
<span id="cb15-3">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mean_ent =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mean</span>(ent, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">na.rm =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>),</span>
<span id="cb15-4">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mean_mutinf =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mean</span>(mutinf)</span>
<span id="cb15-5">  )</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>  mean_ent mean_mutinf
1   1.3984   0.1740535</code></pre>
</div>
</div>
<p>These values could be compared with values obtained by other US cities to evaluate, which cities have high average racial diversity (larger values of <code>mean_ent</code>) and which have high average racial segregation (larger values of <code>mean_mutinf</code>). <!-- Weighted co-occurrence matrix --> <!-- exposure matrix --></p>
</section>
<section id="local-diversity-and-segregation" class="level2">
<h2 class="anchored" data-anchor-id="local-diversity-and-segregation">Local diversity and segregation</h2>
<p>The information theory-derived metrics can be also calculated for smaller, local scales using the <code>size</code> argument. It describes the size of a local area for metrics calculations. For example, <code>size = 20</code> indicates that each local area will consist of 20 by 20 cells of the original raster.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb17" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb17-1">metr_df_20 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">calculate_metrics</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> realizations_raster,</span>
<span id="cb17-2">                               <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">w =</span> dens_raster, </span>
<span id="cb17-3">                               <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fun =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"mean"</span>, </span>
<span id="cb17-4">                               <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">20</span>, </span>
<span id="cb17-5">                               <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">threshold =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.75</span>)</span></code></pre></div></div>
</div>
<p>Now, we can summarize the results for each local area independently (<code>group_by(row, col)</code>).</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb18" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb18-1">smr <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> metr_df_20 <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb18-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">group_by</span>(row, col) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb18-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">summarize</span>(</span>
<span id="cb18-4">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ent_mean =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mean</span>(ent, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">na.rm =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>),</span>
<span id="cb18-5">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mutinf_mean =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mean</span>(mutinf, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">na.rm =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>),</span>
<span id="cb18-6">  ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb18-7">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">na.omit</span>()</span>
<span id="cb18-8"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">head</span>(smr)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code># A tibble: 6 × 4
# Groups:   row [2]
    row   col ent_mean mutinf_mean
  &lt;dbl&gt; &lt;dbl&gt;    &lt;dbl&gt;       &lt;dbl&gt;
1     1    30    1.08       0.0154
2     1    31    1.31       0.0356
3     1    33    1.12       0.0166
4     1    34    1.62       0.0552
5     2    24    0.951      0.0163
6     2    30    1.44       0.0494</code></pre>
</div>
</div>
<p>Each row in the obtained results relates to some spatial locations. We can create an empty grid with appropriate dimensions using the <code>create_grid()</code> function. Its <code>size</code> argument expects the same value as used in the <code>calculate_metrics()</code> function.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb20" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb20-1">grid_sf <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">create_grid</span>(realizations_raster, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">20</span>)</span></code></pre></div></div>
</div>
<p>The result is a spatial vector object with three columns: <code>row</code> and <code>col</code> allowing for identification of each square polygon, and <code>geometry</code> containing spatial geometries.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb21" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb21-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tm_shape</span>(grid_sf) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb21-2">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tm_polygons</span>()</span></code></pre></div></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://jakubnowosad.com/posts/2020-08-03-raceland-bp1/index_files/figure-html/unnamed-chunk-17-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<p>The first two columns,<code>row</code> and <code>col</code>, can be used to connect the grid with summary results.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb22" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb22-1">grid_attr <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> dplyr<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">left_join</span>(grid_sf, smr, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">by =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"row"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"col"</span>))</span>
<span id="cb22-2">grid_attr <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">na.omit</span>(grid_attr)</span></code></pre></div></div>
</div>
<p>Finally, we are able to create two maps. The first one represents racial diversity (larger the value, larger the diversity; the <code>ent_mean</code> variable) and the second one shows racial segregation (larger the value, larger the segregation; the <code>ent_mean</code> variable).</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb23" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb23-1">diversity_map <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tm_shape</span>(grid_attr) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb23-2">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tm_polygons</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">col =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ent_mean"</span>,</span>
<span id="cb23-3">                <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">title =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Diversity"</span>,</span>
<span id="cb23-4">                <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">style =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"cont"</span>,</span>
<span id="cb23-5">                <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">palette =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"magma"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb23-6">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tm_shape</span>(cincinnati) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb23-7">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tm_borders</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">lwd =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">col =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"black"</span>)</span>
<span id="cb23-8">segregation_map <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tm_shape</span>(grid_attr) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb23-9">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tm_polygons</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">col =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"mutinf_mean"</span>,</span>
<span id="cb23-10">                <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">title =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Segregation"</span>,</span>
<span id="cb23-11">                <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">style =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"cont"</span>, </span>
<span id="cb23-12">                <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">palette =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"cividis"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb23-13">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tm_shape</span>(cincinnati) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb23-14">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tm_borders</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">lwd =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">col =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"black"</span>)</span>
<span id="cb23-15"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tmap_arrange</span>(diversity_map, segregation_map)</span></code></pre></div></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://jakubnowosad.com/posts/2020-08-03-raceland-bp1/index_files/figure-html/unnamed-chunk-19-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
</section>
</section>
<section id="bonus-visualizing-racial-landscapes" class="level1">
<h1>Bonus: visualizing racial landscapes</h1>
<p>While the realizations created few steps before represents race spatial distribution fairly well, they do not take the spatial variability of the population densities into consideration. Additional function <code>plot_realization()</code> displays a selected realization taking into account not only race spatial distribution, but also the population density.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb24" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb24-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">plot_realization</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> realizations_raster[[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>]],</span>
<span id="cb24-2">                 <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> race_raster,</span>
<span id="cb24-3">                 <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">hex =</span> my_pal)</span></code></pre></div></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://jakubnowosad.com/posts/2020-08-03-raceland-bp1/index_files/figure-html/unnamed-chunk-20-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<p>In its result, darker areas have larger populations, and brighter represent areas less-inhabited areas.</p>
</section>
<section id="summary" class="level1">
<h1>Summary</h1>
<p>The <strong>raceland</strong> package implements a computational framework for a pattern-based, zoneless analysis and visualization of (ethno)racial topography. The most comprehensive description of the method can be found in the <a href="https://www.sciencedirect.com/science/article/abs/pii/S0143622819310367">Racial landscapes – a pattern-based, zoneless method for analysis and visualization of racial topography article published in Applied Geography</a>. Its preprint is available at https://osf.io/preprints/socarxiv/mejz5. Additionally, <strong>raceland</strong> has three extensive vignettes:</p>
<ul>
<li><a href="https://cran.r-project.org/web/packages/raceland/vignettes/raceland-intro1.html">raceland: R package for a pattern-based, zoneless method for analysis and visualization of racial topography</a> - introducing the package and its functions</li>
<li><a href="https://cran.r-project.org/web/packages/raceland/vignettes/raceland-intro2.html">raceland: Describing local racial patterns of racial landscapes at different spatial scales</a> - showing how the calculations can be performed at different spatial scales</li>
<li><a href="https://cran.r-project.org/web/packages/raceland/vignettes/raceland-intro3.html">raceland: Describing local pattern of the racial landscape using SocScape grids</a> - presenting how to use the <strong>raceland</strong> methods with SocScape race-specific grids to perform analysis for different spatial scales, using the Cook county as an example.</li>
</ul>
<p>This approach is based on the concept of ‘landscape’ used in the domain of landscape ecology. To learn more about information theory metrics used in this approach you can read the <a href="https://link.springer.com/article/10.1007/s10980-019-00830-x">Information theory as a consistent framework for quantification and classification of landscape patterns article published in Landscape Ecology</a>.</p>
<p>The <strong>raceland</strong> package requires race-specific grids. They can be obtained in two main ways. The first one is to download prepared grids from <a href="https://www.socscape.edu.pl/">the SocScape project</a>. It provides high-resolution raster grids for 1990, 2000, 2010 years for 365 metropolitan areas and each county in the conterminous US. The second way is to rasterize a spatial vector file (e.g., an ESRI Shapefile) with an attribute table containing race counts for some areas using the <code>zones_to_raster()</code> function.</p>
<p>Finally, while the presented methods have been applied to race-specific raster grids, they can be also used for many other problems where it is important to determine spatial diversity and segregation.</p>


</section>

<div id="quarto-appendix" class="default"><section class="quarto-appendix-contents" id="quarto-reuse"><h2 class="anchored quarto-appendix-heading">Reuse</h2><div class="quarto-appendix-contents"><div><a rel="license" href="https://creativecommons.org/licenses/by/4.0/">CC BY 4.0</a></div></div></section><section class="quarto-appendix-contents" id="quarto-citation"><h2 class="anchored quarto-appendix-heading">Citation</h2><div><div class="quarto-appendix-secondary-label">BibTeX citation:</div><div class="code-copy-outer-scaffold"><pre class="sourceCode quarto-appendix-bibtex code-with-copy"><code class="sourceCode bibtex">@online{nowosad2020,
  author = {Nowosad, Jakub},
  title = {How to Measure Spatial Diversity and Segregation?},
  date = {2020-08-03},
  url = {https://jakubnowosad.com/posts/2020-08-03-raceland-bp1/},
  doi = {10.59350/a9j92-xwk05},
  langid = {en}
}
</code></pre></div><div class="quarto-appendix-secondary-label">For attribution, please cite this work as:</div><div id="ref-nowosad2020" class="csl-entry quarto-appendix-citeas">
Nowosad, Jakub. 2020. <span>“How to Measure Spatial Diversity and
Segregation?”</span> August 3. <a href="https://doi.org/10.59350/a9j92-xwk05">https://doi.org/10.59350/a9j92-xwk05</a>.
</div></div></section></div> ]]></description>
  <category>spatial</category>
  <category>geocompr</category>
  <category>rstats</category>
  <category>landscape-ecology</category>
  <category>sil</category>
  <guid>https://jakubnowosad.com/posts/2020-08-03-raceland-bp1/</guid>
  <pubDate>Mon, 03 Aug 2020 00:00:00 GMT</pubDate>
</item>
<item>
  <title>Introduction to landscape ecology with R</title>
  <dc:creator>Jakub Nowosad</dc:creator>
  <link>https://jakubnowosad.com/posts/2020-05-26-intro-to-landscape-ecology/</link>
  <description><![CDATA[ 





<p><a href="https://mhesselbarth.rbind.io/">Maximilian H.K. Hesselbarth</a> and I gave the Introduction to landscape ecology with R workshop during <a href="http://www.ialena.org/">IALE-North America 2020 Annual Meeting</a>. You can find the workshop abstract, slides, and recordings below.</p>
<section id="abstract" class="level2">
<h2 class="anchored" data-anchor-id="abstract">Abstract</h2>
<p><a href="https://www.r-project.org/">R</a> is a free, open-source programming language created as an environment for statistical computing and visualization. The advantages of using R include its flexibility, ease of collaboration, and focus on reproducibility. Additionally, the concept of packages - collections of R functions, data, and compiled code created by users - allowed for the growth of its capabilities and expansion into many scientific fields. In recent years, R also has become one of the most often used tools in ecology.</p>
<p>R also has a long history of supporting spatial data analysis, including spatial data downloading, preprocessing, visualizing, and modeling. Recently, however, some new packages have appeared which have significantly changed the work with spatial data in R; in particular, <a href="https://cran.r-project.org/package=sf">the sf package</a>.</p>
<p>The workshop is divided into two parts. The first one introduces participants to the spatial data analysis system in R. The focus is on getting started, with demonstrations of key packages, spatial analysis, and making maps. The second part of the workshop focuses on how to use <a href="https://r-spatialecology.github.io/landscapemetrics/">the landscapemetrics package</a>. This package is based on the main concepts from FRAGSTATS, but it is characterized by a number of advantages. These include, among others, removing existing metric implementation errors, adding new landscape metrics, enabling landscape visualization, and allowing for calculations on large input data. A particular advantage is also an ability to integrate this package with other packages for spatial analysis, so it is possible to download spatial data, process it, calculate landscape metrics and visualize them within one tool.</p>
<p>The workshop is a mixture of theoretical and practical. Pointers to further materials ensure that participants know where to get help and how to take confident next steps after the workshop.</p>
</section>
<section id="slides" class="level2">
<h2 class="anchored" data-anchor-id="slides">Slides</h2>
<p>You can find the slides for the talk at https://nowosad.github.io/whyr_webinar004/.</p>
</section>
<section id="recordings" class="level2">
<h2 class="anchored" data-anchor-id="recordings">Recordings</h2>
<p>Part I:</p>
<iframe width="560" height="315" src="https://www.youtube.com/embed/dCTOGDnDuvM" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen="">
</iframe>
<p>Part II:</p>
<iframe width="560" height="315" src="https://www.youtube.com/embed/jtAxJ-S89qI" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen="">
</iframe>


</section>

<div id="quarto-appendix" class="default"><section class="quarto-appendix-contents" id="quarto-reuse"><h2 class="anchored quarto-appendix-heading">Reuse</h2><div class="quarto-appendix-contents"><div><a rel="license" href="https://creativecommons.org/licenses/by/4.0/">CC BY 4.0</a></div></div></section><section class="quarto-appendix-contents" id="quarto-citation"><h2 class="anchored quarto-appendix-heading">Citation</h2><div><div class="quarto-appendix-secondary-label">BibTeX citation:</div><div class="code-copy-outer-scaffold"><pre class="sourceCode quarto-appendix-bibtex code-with-copy"><code class="sourceCode bibtex">@online{nowosad2020,
  author = {Nowosad, Jakub},
  title = {Introduction to Landscape Ecology with {R}},
  date = {2020-05-26},
  url = {https://jakubnowosad.com/posts/2020-05-26-intro-to-landscape-ecology/},
  doi = {10.59350/3dqyd-3p298},
  langid = {en}
}
</code></pre></div><div class="quarto-appendix-secondary-label">For attribution, please cite this work as:</div><div id="ref-nowosad2020" class="csl-entry quarto-appendix-citeas">
Nowosad, Jakub. 2020. <span>“Introduction to Landscape Ecology with
R.”</span> May 26. <a href="https://doi.org/10.59350/3dqyd-3p298">https://doi.org/10.59350/3dqyd-3p298</a>.
</div></div></section></div> ]]></description>
  <category>spatial</category>
  <category>geocompr</category>
  <category>rstats</category>
  <category>landscape-ecology</category>
  <guid>https://jakubnowosad.com/posts/2020-05-26-intro-to-landscape-ecology/</guid>
  <pubDate>Tue, 26 May 2020 00:00:00 GMT</pubDate>
</item>
<item>
  <title>How to calculate landscape metrics for local landscapes?</title>
  <dc:creator>Jakub Nowosad</dc:creator>
  <link>https://jakubnowosad.com/posts/2020-01-23-lsm_bp2/</link>
  <description><![CDATA[ 





<p>In the last few weeks, I was asked a similar question several times - how to calculate landscape metrics for local landscapes? In other words, how to divide the categorical input map into a number of smaller areas, and next calculate selected landscape metrics for each of the areas. Those areas have many names, such as tiles, squares, or motifels. The main goal of this post is to show how to calculate landscape metrics using <a href="https://r-spatialecology.github.io/landscapemetrics/">the <strong>landscapemetrics</strong> R package</a>.</p>
<p>To reproduce the results on your own computer, install and attach the following packages:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(landscapemetrics)     <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># landscape metrics calculation</span></span>
<span id="cb1-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(raster)               <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># spatial raster data reading and handling</span></span>
<span id="cb1-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(sf)                   <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># spatial vector data reading and handling</span></span>
<span id="cb1-4"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(dplyr)                <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># data manipulation</span></span></code></pre></div></div>
</div>
<section id="reading-the-data" class="level1">
<h1>Reading the data</h1>
<p>The first step is to read the input data.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb2-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">data</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"augusta_nlcd"</span>)</span>
<span id="cb2-2">my_raster <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> augusta_nlcd</span></code></pre></div></div>
</div>
<p>In this example, we will use the <code>augusta_nlcd</code> dataset build in the <strong>landscapemetrics</strong> package. It is also possible to read any spatial raster file with the <code>raster()</code> function, for example <code>my_raster = raster("path_to_my_file.tif")</code>. The input file, however, should fulfill two requirements: (1) contain only integer values that represent categories, and (2) be in a projected coordinate reference system. You can check if your file meets the requirements using the <code>check_landscape()</code> function, and learn more about coordinate reference systems in <a href="https://geocompr.robinlovelace.net/reproj-geo-data.html">the Geocomputation with R book</a>.</p>
<p>Our example data looks like that:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb3-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">plot</span>(my_raster)</span></code></pre></div></div>
</div>
<p><img src="https://jakubnowosad.com/posts/2020-01-23-lsm_bp2/figs/lsm-bp2-r.png" class="img-fluid"></p>
</section>
<section id="creating-a-grid" class="level1">
<h1>Creating a grid</h1>
<!-- The basic  is to calculate selected metrics for each non-overlapping local landscape.  -->
<p>One of the ways to create borders of local landscapes is to use the <code>st_make_grid()</code> function. This function accepts an <code>sf</code> object as the first argument, therefore we need to create a new object based on the bounding box of the input raster. Next, we also need to provide a second argument, either <code>cellsize</code> or <code>n</code>:</p>
<ul>
<li><code>cellsize</code> - vector of length 1 or 2 - the side length of each grid cell in map units (usually meters)</li>
<li><code>n</code> - vector of length 1 or 2 - the number of grid cells in a row/column</li>
</ul>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb4-1">my_grid_geom <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">st_make_grid</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">st_as_sfc</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">st_bbox</span>(my_raster)), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">cellsize =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1500</span>)</span>
<span id="cb4-2">my_grid <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">st_sf</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">geom =</span> my_grid_geom)</span></code></pre></div></div>
</div>
<p>Next, we can overlay the newly created grid on top of our input raster:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb5-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">plot</span>(my_raster)</span>
<span id="cb5-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">plot</span>(my_grid, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">add =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>)</span></code></pre></div></div>
</div>
<p><img src="https://jakubnowosad.com/posts/2020-01-23-lsm_bp2/figs/lsm-bp2-rg.png" class="img-fluid"></p>
</section>
<section id="calculating-a-metric" class="level1">
<h1>Calculating a metric</h1>
<p>The calculation of landscape metrics for each cell can be done with the <code>sample_lsm()</code> function. It requires the input raster as the first argument, and the grid as the second one<sup>1</sup>. Next, we can specify which landscape metrics we want to calculate. For example, we selected <a href="../../posts/2019-06-25-ent-bp1/">marginal entropy</a> to be calculated on a landscape level. The complete list of the implemented metrics can be obtained with the <code>list_lsm()</code> function. <a href="https://github.com/r-spatialecology/landscapemetrics/issues/new">Let us know</a> if you are missing some metrics.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb6" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb6-1">my_metric <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sample_lsm</span>(my_raster, my_grid,</span>
<span id="cb6-2">                       <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">level =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"landscape"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">metric =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ent"</span>)</span>
<span id="cb6-3">my_metric</span></code></pre></div></div>
</div>
<pre><code>## # A tibble: 126 x 8
##    layer level     class    id metric value plot_id percentage_inside
##    &lt;int&gt; &lt;chr&gt;     &lt;int&gt; &lt;int&gt; &lt;chr&gt;  &lt;dbl&gt;   &lt;int&gt;             &lt;dbl&gt;
##  1     1 landscape    NA    NA ent     2.54       1               100
##  2     1 landscape    NA    NA ent     2.52       2               100
##  3     1 landscape    NA    NA ent     2.47       3               100
##  4     1 landscape    NA    NA ent     2.49       4               100
##  5     1 landscape    NA    NA ent     2.38       5               100
##  6     1 landscape    NA    NA ent     2.48       6               100
##  7     1 landscape    NA    NA ent     2.65       7               100
##  8     1 landscape    NA    NA ent     2.41       8               100
##  9     1 landscape    NA    NA ent     2.28       9               100
## 10     1 landscape    NA    NA ent     2.44      10               100
## # … with 116 more rows</code></pre>
<p>Each row in the <code>my_metric</code> object corresponds to each provided grid cell, with the values of marginal entropy in the <code>value</code> column <sup>2</sup>. Next, we can connect spatial grid with <code>my_metric</code> using the <code>bind_cols()</code> function:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb8" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb8-1">my_grid <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">bind_cols</span>(my_grid, my_metric)</span></code></pre></div></div>
</div>
<p>The quickest visualization of the results can be done with the <code>plot()</code> function.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb9" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb9-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">plot</span>(my_grid[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"value"</span>])</span></code></pre></div></div>
</div>
<p><img src="https://jakubnowosad.com/posts/2020-01-23-lsm_bp2/figs/lsm-bp2-value.png" class="img-fluid"></p>
<p>More complex vizualizations can be done with the <a href="https://github.com/mtennekes/tmap"><strong>tmap</strong></a> or <a href="https://ggplot2.tidyverse.org/reference/ggsf.html"><strong>ggplot2</strong></a> packages.</p>
</section>
<section id="saving-the-results" class="level1">
<h1>Saving the results</h1>
<p>The <code>write_sf()</code> function can save the results together with spatial geometry.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb10" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb10-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">write_sf</span>(my_grid, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"my_grid.gpkg"</span>)</span></code></pre></div></div>
</div>
<p>This output file can be read in any GIS software, such as QGIS, GRASS GIS, or ArcGIS.</p>
</section>
<section id="bonus-1---show-each-raster" class="level1">
<h1>Bonus 1 - show each raster</h1>
<p>We can also see local landscapes for each grid cell, when we set the <code>return_raster</code> argument in <code>sample_lsm()</code> to <code>TRUE</code>:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb11" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb11-1">my_metric_r <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sample_lsm</span>(my_raster, my_grid,</span>
<span id="cb11-2">                         <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">level =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"landscape"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">metric =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ent"</span>,</span>
<span id="cb11-3">                         <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">return_raster =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>)</span>
<span id="cb11-4">my_metric_r</span></code></pre></div></div>
</div>
<pre><code>## # A tibble: 126 x 9
##    layer level class    id metric value plot_id percentage_insi…
##    &lt;int&gt; &lt;chr&gt; &lt;int&gt; &lt;int&gt; &lt;chr&gt;  &lt;dbl&gt;   &lt;int&gt;            &lt;dbl&gt;
##  1     1 land…    NA    NA ent     2.54       1              100
##  2     1 land…    NA    NA ent     2.52       2              100
##  3     1 land…    NA    NA ent     2.47       3              100
##  4     1 land…    NA    NA ent     2.49       4              100
##  5     1 land…    NA    NA ent     2.38       5              100
##  6     1 land…    NA    NA ent     2.48       6              100
##  7     1 land…    NA    NA ent     2.65       7              100
##  8     1 land…    NA    NA ent     2.41       8              100
##  9     1 land…    NA    NA ent     2.28       9              100
## 10     1 land…    NA    NA ent     2.44      10              100
## # … with 116 more rows, and 1 more variable: raster_sample_plots &lt;list&gt;</code></pre>
<p>This adds a new column, <code>raster_sample_plots</code>, that contains each local landscapes. Next, we can vizualize whichever landscapes we want, for example, number <code>1</code> which is located in the bottom left corner of the study area:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb13" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb13-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">plot</span>(my_metric_r<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>raster_sample_plots[[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>]],</span>
<span id="cb13-2">     <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">main =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">paste0</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ent: "</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">round</span>(my_metric_r<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>value[[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>]], <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>)))</span></code></pre></div></div>
</div>
<p><img src="https://jakubnowosad.com/posts/2020-01-23-lsm_bp2/figs/lsm-bp2-sr.png" class="img-fluid"></p>
</section>
<section id="bonus-2---calculate-many-metrics" class="level1">
<h1>Bonus 2 - calculate many metrics</h1>
<p>We are not limited to calculating just one metric at the time. The <strong>landscapemetrics</strong> package makes it possible to calculate up to 65 metrics on landscape level. There are several ways to specify the metrics we want to obtain as you can learn in the <code>?calculate_lsm</code> help file. For this example, we selected two metrics - marginal entropy (abbriviation <code>"ent"</code>) and mutual information (abbriviate as <code>"mutinf"</code>):</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb14" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb14-1">my_metrics <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sample_lsm</span>(my_raster, my_grid, </span>
<span id="cb14-2">                        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">level =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"landscape"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">metric =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ent"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"mutinf"</span>))</span>
<span id="cb14-3">my_metrics</span></code></pre></div></div>
</div>
<pre><code>## # A tibble: 252 x 8
##    layer level     class    id metric value plot_id percentage_inside
##    &lt;int&gt; &lt;chr&gt;     &lt;int&gt; &lt;int&gt; &lt;chr&gt;  &lt;dbl&gt;   &lt;int&gt;             &lt;dbl&gt;
##  1     1 landscape    NA    NA ent    2.54        1               100
##  2     1 landscape    NA    NA mutinf 1.07        1               100
##  3     1 landscape    NA    NA ent    2.52        2               100
##  4     1 landscape    NA    NA mutinf 0.841       2               100
##  5     1 landscape    NA    NA ent    2.47        3               100
##  6     1 landscape    NA    NA mutinf 0.973       3               100
##  7     1 landscape    NA    NA ent    2.49        4               100
##  8     1 landscape    NA    NA mutinf 1.09        4               100
##  9     1 landscape    NA    NA ent    2.38        5               100
## 10     1 landscape    NA    NA mutinf 1.04        5               100
## # … with 242 more rows</code></pre>
<p>The output data frame has two rows for each grid cell; therefore, if we want to connect the result with a spatial object, we need to reformat it. It can be done with the <code>pivot_wider()</code> function from the <strong>tidyr</strong> package.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb16" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb16-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(tidyr)</span>
<span id="cb16-2">my_metrics <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">pivot_wider</span>(my_metrics, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">names_from =</span> metric, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">values_from =</span> value)</span>
<span id="cb16-3">my_metrics</span></code></pre></div></div>
</div>
<pre><code>## # A tibble: 126 x 8
##    layer level     class    id plot_id percentage_inside   ent mutinf
##    &lt;int&gt; &lt;chr&gt;     &lt;int&gt; &lt;int&gt;   &lt;int&gt;             &lt;dbl&gt; &lt;dbl&gt;  &lt;dbl&gt;
##  1     1 landscape    NA    NA       1               100  2.54  1.07 
##  2     1 landscape    NA    NA       2               100  2.52  0.841
##  3     1 landscape    NA    NA       3               100  2.47  0.973
##  4     1 landscape    NA    NA       4               100  2.49  1.09 
##  5     1 landscape    NA    NA       5               100  2.38  1.04 
##  6     1 landscape    NA    NA       6               100  2.48  1.15 
##  7     1 landscape    NA    NA       7               100  2.65  1.12 
##  8     1 landscape    NA    NA       8               100  2.41  0.779
##  9     1 landscape    NA    NA       9               100  2.28  0.914
## 10     1 landscape    NA    NA      10               100  2.44  0.857
## # … with 116 more rows</code></pre>
<p>This function moves metrics values from the <code>value</code> column into two new columns - <code>ent</code> and <code>mutinf</code>. Now, we are able to connect it to the spatial object with <code>bind_cols()</code>:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb18" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb18-1">my_grid2 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">bind_cols</span>(my_grid, my_metrics)</span></code></pre></div></div>
</div>
<p>Plotting of the result requires providing the name of the column of a metric:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb19" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb19-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">plot</span>(my_grid2[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ent"</span>])</span></code></pre></div></div>
</div>
<p><img src="https://jakubnowosad.com/posts/2020-01-23-lsm_bp2/figs/lsm-bp2-ent.png" class="img-fluid"></p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb20" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb20-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">plot</span>(my_grid2[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"mutinf"</span>])</span></code></pre></div></div>
</div>
<p><img src="https://jakubnowosad.com/posts/2020-01-23-lsm_bp2/figs/lsm-bp2-mutinf.png" class="img-fluid"></p>
</section>
<section id="summary" class="level1">
<h1>Summary</h1>
<p>The <code>sample_lsm()</code> function offers more than calculations for regular areas. It is also possible to provide irregular polygons as the second argument and calculate any landscape metrics for them. The <strong>landscapemetrics</strong> package also has many additional features, including calculation of metrics in moving window with <a href="https://r-spatialecology.github.io/landscapemetrics/reference/window_lsm.html"><code>window_lsm()</code></a> and in subsequential buffers around points of interest with <a href="https://r-spatialecology.github.io/landscapemetrics/reference/scale_sample.html"><code>scale_sample()</code></a>. Learn more about landscape metrics and the <strong>landscapemetrics</strong> package at https://r-spatialecology.github.io/landscapemetrics/ and http://dx.doi.org/10.1111/ecog.04617.</p>


</section>


<div id="quarto-appendix" class="default"><section id="footnotes" class="footnotes footnotes-end-of-document"><h2 class="anchored quarto-appendix-heading">Footnotes</h2>

<ol>
<li id="fn1"><p>This function also allows for many more possibilities, including specifying 2-column matrix with coordinates, SpatialPoints, SpatialLines, SpatialPolygons, sf points or sf polygons as the second argument. You can learn all of the possible options using <code>?sample_lsm</code>.↩︎</p></li>
<li id="fn2"><p>To learn more about the structure of the output read <a href="../../posts/2019-03-07-lsm-bp1/">the Efficient landscape metrics calculations for buffers around sampling points blog post</a>.↩︎</p></li>
</ol>
</section><section class="quarto-appendix-contents" id="quarto-reuse"><h2 class="anchored quarto-appendix-heading">Reuse</h2><div class="quarto-appendix-contents"><div><a rel="license" href="https://creativecommons.org/licenses/by/4.0/">CC BY 4.0</a></div></div></section><section class="quarto-appendix-contents" id="quarto-citation"><h2 class="anchored quarto-appendix-heading">Citation</h2><div><div class="quarto-appendix-secondary-label">BibTeX citation:</div><div class="code-copy-outer-scaffold"><pre class="sourceCode quarto-appendix-bibtex code-with-copy"><code class="sourceCode bibtex">@online{nowosad2020,
  author = {Nowosad, Jakub},
  title = {How to Calculate Landscape Metrics for Local Landscapes?},
  date = {2020-01-23},
  url = {https://jakubnowosad.com/posts/2020-01-23-lsm_bp2/},
  doi = {10.59350/qfcya-g0z57},
  langid = {en}
}
</code></pre></div><div class="quarto-appendix-secondary-label">For attribution, please cite this work as:</div><div id="ref-nowosad2020" class="csl-entry quarto-appendix-citeas">
Nowosad, Jakub. 2020. <span>“How to Calculate Landscape Metrics for
Local Landscapes?”</span> January 23. <a href="https://doi.org/10.59350/qfcya-g0z57">https://doi.org/10.59350/qfcya-g0z57</a>.
</div></div></section></div> ]]></description>
  <category>spatial</category>
  <category>geocompr</category>
  <category>rstats</category>
  <category>landscape-ecology</category>
  <guid>https://jakubnowosad.com/posts/2020-01-23-lsm_bp2/</guid>
  <pubDate>Thu, 23 Jan 2020 00:00:00 GMT</pubDate>
</item>
<item>
  <title>Evaluation of the new palette() for R</title>
  <dc:creator>Jakub Nowosad</dc:creator>
  <link>https://jakubnowosad.com/posts/2019-12-01-cbc-bp1/</link>
  <description><![CDATA[ 





<p>R version 4.0 is just around the corner. One of the changes in the new version is the improved default color palette using the <code>palette()</code> function.</p>
<section id="proposed-colors" class="level2">
<h2 class="anchored" data-anchor-id="proposed-colors">Proposed colors</h2>
<p>The new proposed <code>palette()</code> default is less saturated and more balanced, while at the same time, it follows the same basic pattern of colors (hues). You can read more about it at https://developer.r-project.org/Blog/public/2019/11/21/a-new-palette-for-r/.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1">col_ver3 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#000000"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#FF0000"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#00CD00"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#0000FF"</span>,</span>
<span id="cb1-2">             <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#00FFFF"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#FF00FF"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#FFFF00"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#BEBEBE"</span>)</span>
<span id="cb1-3">col_ver4 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#000000"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#DF536B"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#61D04F"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#2297E6"</span>,</span>
<span id="cb1-4">             <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#28E2E5"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#CD0BBC"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#EEC21F"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#9E9E9E"</span>)</span></code></pre></div></div>
</div>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb2-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(colorspace)</span>
<span id="cb2-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">swatchplot</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Version 3"</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> col_ver3,</span>
<span id="cb2-3">           <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Version 4"</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> col_ver4)</span></code></pre></div></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://jakubnowosad.com/posts/2019-12-01-cbc-bp1/index_files/figure-html/unnamed-chunk-2-1.png" class="img-fluid figure-img" width="576"></p>
</figure>
</div>
</div>
</div>
<p>This proposal is still being disussed and modified as mentioned by <a href="https://twitter.com/AchimZeileis">Achim Zeileis</a> on Twitter:</p>
<div class="cell">
<div class="cell-output-display">
<p>{{% tweet "1199463785364434944" %}}</p>
</div>
</div>
<p>Therefore, I decided it is a good time to test the properties of the proposed <code>palette()</code> default for color vision deficiencies - deuteranopia, protanopia, and tritanopia.</p>
</section>
<section id="comparision-between-palettes" class="level2">
<h2 class="anchored" data-anchor-id="comparision-between-palettes">Comparision between palettes</h2>
<p>I used the <strong>colorblindcheck</strong> package for this purpose.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb3-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># remotes::install_github("nowosad/colorblindcheck")</span></span>
<span id="cb3-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(colorblindcheck)</span></code></pre></div></div>
</div>
<p>This tiny R package provides tools for helping to decide if the selected color palette is colorblind-friendly. You can see examples of its use at https://nowosad.github.io/colorblindcheck.</p>
<p>The primary function in this package is <code>palette_check()</code>, which creates summary statistics comparing the original input palette and simulations of color vision deficiencies.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb4-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">palette_check</span>(col_ver3, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">plot =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>)</span></code></pre></div></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://jakubnowosad.com/posts/2019-12-01-cbc-bp1/index_files/figure-html/unnamed-chunk-5-1.png" class="img-fluid figure-img" width="576"></p>
</figure>
</div>
</div>
<div class="cell-output cell-output-stdout">
<pre><code>          name n tolerance ncp ndcp  min_dist mean_dist  max_dist
1       normal 8  26.64945  28   28 26.649454  58.57976 105.67883
2 deuteranopia 8  26.64945  28   26 12.790929  51.65788  99.81401
3   protanopia 8  26.64945  28   24  4.337187  54.06193  95.06426
4   tritanopia 8  26.64945  28   23 13.934054  51.49383  90.45153</code></pre>
</div>
</div>
<p>Visual inspection of the old <code>palette()</code> default allows seeing that it is not suitable for people with color vision deficiencies. For example, people with protanopia could have problems distinguishing the first from the second color and the forth from the sixth color. This problem is also confirmed in the summary statistics, where the minimal distance between colors of the original palette is about 26, while it is only about 4 for protanopia.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb6" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb6-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">palette_check</span>(col_ver4, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">plot =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>)</span></code></pre></div></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://jakubnowosad.com/posts/2019-12-01-cbc-bp1/index_files/figure-html/unnamed-chunk-6-1.png" class="img-fluid figure-img" width="576"></p>
</figure>
</div>
</div>
<div class="cell-output cell-output-stdout">
<pre><code>          name n tolerance ncp ndcp  min_dist mean_dist max_dist
1       normal 8  23.51878  28   28 23.518780  50.21307 95.04017
2 deuteranopia 8  23.51878  28   22 12.094062  41.11547 78.45654
3   protanopia 8  23.51878  28   24  5.402646  42.28841 81.02547
4   tritanopia 8  23.51878  28   22 11.032589  44.47677 83.19068</code></pre>
</div>
</div>
<p>The proposed <code>palette()</code> looks considerably better, as it is easier to distinguish between colors for each color vision deficiency. However, the minimal distance between colors for protanopia is just marginally better with a value of about 5.</p>
</section>
<section id="protanomaly" class="level2">
<h2 class="anchored" data-anchor-id="protanomaly">Protanomaly</h2>
<p>Let’s use the <code>palette_dist()</code> function to compare each pair of colors in the old and proposed <code>palette()</code> using the protanopia color vision deficiency.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb8" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb8-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">palette_dist</span>(col_ver3, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">cvd =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"pro"</span>)</span></code></pre></div></div>
</div>
<div class="cell">
<div class="cell-output cell-output-stdout">
<pre><code>     [,1]  [,2]  [,3]  [,4]  [,5]  [,6]  [,7]  [,8]
[1,]   NA 13.89 63.29 40.08 85.59 41.23 90.98 66.30
[2,]   NA    NA 54.81 52.02 82.32 52.17 82.02 62.61
[3,]   NA    NA    NA 78.71 43.28 75.54 18.13 28.28
[4,]   NA    NA    NA    NA 52.63  4.34 95.06 52.40
[5,]   NA    NA    NA    NA    NA 48.08 44.74 14.58
[6,]   NA    NA    NA    NA    NA    NA 91.20 48.37
[7,]   NA    NA    NA    NA    NA    NA    NA 31.09
[8,]   NA    NA    NA    NA    NA    NA    NA    NA</code></pre>
</div>
</div>
<p>The shortest distance between colors in the old <code>palette()</code> default was between the fourth and sixth color (4.33).</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb10" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb10-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">palette_dist</span>(col_ver4, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">cvd =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"pro"</span>)</span></code></pre></div></div>
</div>
<div class="cell">
<div class="cell-output cell-output-stdout">
<pre><code>     [,1]  [,2]  [,3]  [,4]  [,5]  [,6]  [,7]  [,8]
[1,]   NA 30.25 68.88 53.60 74.53 35.78 71.67 51.95
[2,]   NA    NA 41.26 27.23 34.35 26.52 43.84 21.75
[3,]   NA    NA    NA 55.72 37.58 78.62  5.40 26.47
[4,]   NA    NA    NA    NA 20.76 31.85 58.06 23.89
[5,]   NA    NA    NA    NA    NA 51.88 39.84 16.37
[6,]   NA    NA    NA    NA    NA    NA 81.03 45.51
[7,]   NA    NA    NA    NA    NA    NA    NA 29.48
[8,]   NA    NA    NA    NA    NA    NA    NA    NA</code></pre>
</div>
</div>
<p>This pair of colors is substantially more distinguishable in the new proposed <code>palette()</code> default with a distance of about 32. However, the shortest distance in this palette was 5.40 between the third and seventh color.</p>
</section>
<section id="summary" class="level2">
<h2 class="anchored" data-anchor-id="summary">Summary</h2>
<p>The new proposed <code>palette()</code> default is a step in the right direction with more balanced luminance while keeping similar hues to the old version. This constraint, however, results in having a pair of very similar colors for people with protanopia.</p>
<p>What can be done then to ensure that the color palette we use is colorblind friendly? Gladly, there are many additional color palettes available in R that can be used. It includes some of the palettes introduced in the R 3.6 function <code>hcl.colors()</code>. Read more about them at https://developer.r-project.org/Blog/public/2019/04/01/hcl-based-color-palettes-in-grdevices/ or see them by yourself using <code>example("hcl.colors"</code>. Additionally, a new <code>palette.colors()</code> function will be added to R 4.0 with several sensible predefined palettes for representing qualitative data.</p>
<p><strong>UPDATE 2019-12-04</strong></p>
<p>The proposed <code>palette()</code> <a href="https://developer.r-project.org/Blog/public/2019/11/21/a-new-palette-for-r/index.html">was updated based on the provided feedback and suggestions</a>. The current version is:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb12" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb12-1">col_ver4b <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#000000"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#DF536B"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#61D04F"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#2297E6"</span>,</span>
<span id="cb12-2">              <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#28E2E5"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#CD0BBC"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#F5C710"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#9E9E9E"</span>)    </span></code></pre></div></div>
</div>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb13" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb13-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">swatchplot</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Version 4b"</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> col_ver4b)</span></code></pre></div></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://jakubnowosad.com/posts/2019-12-01-cbc-bp1/index_files/figure-html/unnamed-chunk-12-1.png" class="img-fluid figure-img" width="576"></p>
</figure>
</div>
</div>
</div>
<p>Let’s check how it changes our palette’s summaries:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb14" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb14-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">palette_check</span>(col_ver4b, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">plot =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>)    </span></code></pre></div></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://jakubnowosad.com/posts/2019-12-01-cbc-bp1/index_files/figure-html/unnamed-chunk-13-1.png" class="img-fluid figure-img" width="576"></p>
</figure>
</div>
</div>
<div class="cell-output cell-output-stdout">
<pre><code>          name n tolerance ncp ndcp  min_dist mean_dist max_dist
1       normal 8  23.51878  28   28 23.518780  50.51179 95.04017
2 deuteranopia 8  23.51878  28   22 13.714363  41.47104 81.33288
3   protanopia 8  23.51878  28   24  6.851961  42.56735 81.90781
4   tritanopia 8  23.51878  28   22 11.032589  44.46748 83.19068</code></pre>
</div>
</div>
<p>The new version is an improvement with a greater distance between the most similar pair of colors for people with protanomaly. It was 4.33 in the version 3 default, 5.4 in the previously proposed version 4 default, and it is 6.85 now. That being said, it is relevant to mention that the evaluation of color palettes cannot be done in an entirely automated fashion: tools in <strong>colorblindcheck</strong> should be used together with visual judgments.</p>


</section>

<div id="quarto-appendix" class="default"><section class="quarto-appendix-contents" id="quarto-reuse"><h2 class="anchored quarto-appendix-heading">Reuse</h2><div class="quarto-appendix-contents"><div><a rel="license" href="https://creativecommons.org/licenses/by/4.0/">CC BY 4.0</a></div></div></section><section class="quarto-appendix-contents" id="quarto-citation"><h2 class="anchored quarto-appendix-heading">Citation</h2><div><div class="quarto-appendix-secondary-label">BibTeX citation:</div><div class="code-copy-outer-scaffold"><pre class="sourceCode quarto-appendix-bibtex code-with-copy"><code class="sourceCode bibtex">@online{nowosad2019,
  author = {Nowosad, Jakub},
  title = {Evaluation of the New Palette() for {R}},
  date = {2019-12-01},
  url = {https://jakubnowosad.com/posts/2019-12-01-cbc-bp1/},
  doi = {10.59350/kf2v5-mhd42},
  langid = {en}
}
</code></pre></div><div class="quarto-appendix-secondary-label">For attribution, please cite this work as:</div><div id="ref-nowosad2019" class="csl-entry quarto-appendix-citeas">
Nowosad, Jakub. 2019. <span>“Evaluation of the New Palette() for
R.”</span> December 1. <a href="https://doi.org/10.59350/kf2v5-mhd42">https://doi.org/10.59350/kf2v5-mhd42</a>.
</div></div></section></div> ]]></description>
  <category>rstats</category>
  <category>vis</category>
  <category>colors</category>
  <guid>https://jakubnowosad.com/posts/2019-12-01-cbc-bp1/</guid>
  <pubDate>Sun, 01 Dec 2019 00:00:00 GMT</pubDate>
</item>
<item>
  <title>Information theory provides a consistent framework for the analysis of spatial patterns</title>
  <dc:creator>Jakub Nowosad</dc:creator>
  <link>https://jakubnowosad.com/posts/2019-06-25-ent-bp1/</link>
  <description><![CDATA[ 





<p>Quantitative assessment of spatial patterns has been a keen interest of generations of spatial scientists and practitioners using spatial data. This post describes Information Theory-based metrics allowing for numerical description of spatial patterns. Each example is accompanied by an R code allowing for reproducing these results and encouraging to try these metrics on different data.</p>
<!-- a comprehensive, theory-driven approach to analysis of landscape patterns -->
<p>To learn more about this topic, read our open access article:</p>
<blockquote class="blockquote">
<p>Nowosad, J., and T. F. Stepinski. (2019). Information Theory as a consistent framework for quantification and classification of landscape patterns. Landscape Ecology, DOI: 10.1007/s10980-019-00830-x</p>
</blockquote>
<section id="example-data" class="level1">
<h1>Example data</h1>
<p>To reproduce the calculations in this blog post, you need to download the <code>landscapes.tif</code> file containing the example data.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(raster)</span>
<span id="cb1-2"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># downloading the example data</span></span>
<span id="cb1-3">temp_data_file <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tempfile</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fileext =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">".tif"</span>)</span>
<span id="cb1-4"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">download.file</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"https://github.com/Nowosad/ent_bp/raw/master/data/landscapes.tif"</span>,</span>
<span id="cb1-5">              <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">destfile =</span> temp_data_file)</span>
<span id="cb1-6"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># read the example data</span></span>
<span id="cb1-7">landscapes <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">brick</span>(temp_data_file)</span></code></pre></div></div>
</div>
<p>The data can be visualized using the <strong>tmap</strong> package.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb2-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(tmap)</span>
<span id="cb2-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tm_shape</span>(landscapes) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb2-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tm_raster</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">palette =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#FFFF64"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#006400"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#966400"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#BE9600"</span>), </span>
<span id="cb2-4">            <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">style =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"cat"</span>,</span>
<span id="cb2-5">            <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">title =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Land cover category"</span>, </span>
<span id="cb2-6">            <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">labels =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Argiculture"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Forest"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Shrubland"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Grassland"</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb2-7">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tm_facets</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">free.scales =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">nrow =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb2-8">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tm_layout</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">panel.labels =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">6</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">legend.outside.position =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"bottom"</span>)</span></code></pre></div></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://jakubnowosad.com/posts/2019-06-25-ent-bp1/index_files/figure-html/unnamed-chunk-5-1.png" class="img-fluid figure-img" width="960"></p>
</figure>
</div>
</div>
</div>
<p>Let’s consider six fairly simple, but diverse examples:</p>
<ol type="1">
<li>One dominating category and second spatially aggregated minor category</li>
<li>One dominating category and second spatially disaggregated minor category</li>
<li>One less dominating category and second spatially aggregated minor category</li>
<li>One less dominating category and second spatially disaggregated minor category</li>
<li>Four categories, where each one is spatially aggregated</li>
<li>Four categories, where each one is spatially disaggregated</li>
</ol>
<p>The example data represent different land cover categories; however, they could be any discrete values. Additionally, those cases are only a small subset of possible spatial arrangement, but the ideas presented below works for more and less complex cases.</p>
</section>
<section id="spatial-representation" class="level1">
<h1>Spatial representation</h1>
<p>One of the ways to describe the above examples is to count a number of cells for each category (this metrics is known as a composition). However, this representation does not contain any information about the spatial distribution of the values. For example, let’s consider example data 1 and 2 – they both have a very similar number of cells for each category, but the spatial arrangement of the values is very different.</p>
<p>Another way to represent the above example is to calculate a co-occurrence matrix.<sup>1</sup> It is created by counting all of the pairs of the adjacent cells in the data. As you will see below, this representation describes not only composition but also spatial configuration of the values.</p>
<p><a href="https://r-spatialecology.github.io/landscapemetrics/index.html">The <strong>landscapemetrics</strong> package</a> allows for calculating co-occurrence matrices using the <code>get_adjacencies()</code> function.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb3-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(landscapemetrics)</span>
<span id="cb3-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">get_adjacencies</span>(landscapes)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>$layer_1
    1    2
1 200   50
2  50 3180

$layer_2
    1    2
1  22  130
2 130 3198

$layer_3
     1    2
1 1142  106
2  106 2126

$layer_4
    1    2
1 546  304
2 304 2326

$layer_5
    1   2   3   4
1 712  47 144  62
2  47 684 183 108
3 144 183 370  35
4  62 108  35 556

$layer_6
    1   2   3   4
1 294 168 107  85
2 168 748  95 152
3 107  95 684 139
4  85 152 139 262</code></pre>
</div>
</div>
<p>The result for each example dataset is a matrix with a number of rows and columns equal to the number of unique categories in the dataset. For example data 1, 200 times cells of the first category are adjacent to other cells of this category, 3180 times cells of the second category are adjacent to other cells of this category, and 50 times cells of the first category are adjacent to cells of the second category. This representation shows that the second category dominates in the data (3180 pairs), but also that the cells from the first category are more often next to other cells of this category than a different one (200 vs.&nbsp;50).</p>
</section>
<section id="information-theory" class="level1">
<h1>Information theory</h1>
<p>The co-occurrence matrix representation is not only compact but also allows to calculate several metrics based on the information theory applied to bivariate random variable (x,y), where x is a category of a focus cell and y is category of a cell adjacent to it. This metrics are include <strong>marginal entropy</strong>, <strong>conditional entropy</strong>, <strong>joint entropy</strong>, <strong>mutual information</strong>, and <strong>relative mutual information</strong>.</p>
<section id="marginal-entropy-hx" class="level2">
<h2 class="anchored" data-anchor-id="marginal-entropy-hx">Marginal entropy [H(x)]</h2>
<p>The first metric, <strong>marginal entropy</strong>, represents a diversity (thematic complexity, composition) of spatial categories. It is calculated as the entropy of the marginal distribution. The <code>lsm_l_ent()</code> function allows for calculating <strong>marginal entropy</strong>. Its output gives a layer number (example data id), informs that the data is only of the landscape level, this metric abbreviation is <code>ent</code>, and finally it shows the resulting value.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb5-1">mar_ent <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lsm_l_ent</span>(landscapes)</span>
<span id="cb5-2">mar_ent</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code># A tibble: 6 × 6
  layer level     class    id metric value
  &lt;int&gt; &lt;chr&gt;     &lt;int&gt; &lt;int&gt; &lt;chr&gt;  &lt;dbl&gt;
1     1 landscape    NA    NA ent    0.373
2     2 landscape    NA    NA ent    0.259
3     3 landscape    NA    NA ent    0.942
4     4 landscape    NA    NA ent    0.802
5     5 landscape    NA    NA ent    1.98 
6     6 landscape    NA    NA ent    1.95 </code></pre>
</div>
</div>
<!-- The values can be vizualized together with spatial data using the **tmap** package. -->
<div class="cell">
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://jakubnowosad.com/posts/2019-06-25-ent-bp1/index_files/figure-html/unnamed-chunk-8-1.png" class="img-fluid figure-img" width="960"></p>
</figure>
</div>
</div>
</div>
<p>The resulting values indicate that the example datasets have different levels of thematic complexity. Example data 1 and 2 have one dominating category (low values of <strong>marginal entropy</strong>), while categories are more evenly distributed in example data 3 and 4 (medium values of <strong>marginal entropy</strong>). Example data 5 and 6 have the highest levels of thematic complexity due to the fact of having more unique, evenly distributed categories.</p>
</section>
<section id="conditional-entropy-hyx" class="level2">
<h2 class="anchored" data-anchor-id="conditional-entropy-hyx">Conditional entropy [H(y|x)]</h2>
<p>The second metric, <strong>conditional entropy</strong>, represents a configurational complexity (geometric intricacy) of a spatial pattern. If the value of <strong>conditional entropy</strong> is small, cells of one category are predominantly adjacent to only one category of cells. On the other hand, the high value of <strong>conditional entropy</strong> shows that cells of one category are adjacent to cells of many different categories. <strong>Conditional entropy</strong> can be calculated using the <code>lsm_l_condent()</code> function.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb7" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb7-1">cond_ent <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lsm_l_condent</span>(landscapes)</span>
<span id="cb7-2">cond_ent</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code># A tibble: 6 × 6
  layer level     class    id metric  value
  &lt;int&gt; &lt;chr&gt;     &lt;int&gt; &lt;int&gt; &lt;chr&gt;   &lt;dbl&gt;
1     1 landscape    NA    NA condent 0.159
2     2 landscape    NA    NA condent 0.254
3     3 landscape    NA    NA condent 0.327
4     4 landscape    NA    NA condent 0.620
5     5 landscape    NA    NA condent 1.36 
6     6 landscape    NA    NA condent 1.61 </code></pre>
</div>
</div>
<div class="cell">
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://jakubnowosad.com/posts/2019-06-25-ent-bp1/index_files/figure-html/unnamed-chunk-10-1.png" class="img-fluid figure-img" width="960"></p>
</figure>
</div>
</div>
</div>
<!-- interpretation -->
<p>The <strong>conditional entropy</strong> values are the smallest for example data 1, where most of the “green” cells are adjacent to other “green” ones, and the “yellow” cells are next to other “yellow” cells, and where one category (“green”) dominates the entire area. In example data 2, the proportion of categories is similar; however, the configuration of the cells is less organized and therefore the <strong>conditional entropy</strong> value is higher than in example data 1. For the next examples, the <strong>conditional entropy</strong> values grow with both categories proportions and their arrangement. This is due to the fact that the diversity of categories induces configurational complexity.</p>
</section>
<section id="joint-entropy-hx-y" class="level2">
<h2 class="anchored" data-anchor-id="joint-entropy-hx-y">Joint entropy [H(x, y)]</h2>
<p>The third metric, <strong>joint entropy</strong>, is an overall spatio-thematic complexity metric. It represents the uncertainty in determining a category of the focus cell and the category of the adjacent cell. In other words, it measures diversity of values in a co-occurrence matrix – the smaller the diversity, the larger the value of <strong>joint entropy</strong>. You can use the <code>lsm_l_joinent()</code> to calculate this metric.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb9" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb9-1">join_ent <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lsm_l_joinent</span>(landscapes)</span>
<span id="cb9-2">join_ent</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code># A tibble: 6 × 6
  layer level     class    id metric  value
  &lt;int&gt; &lt;chr&gt;     &lt;int&gt; &lt;int&gt; &lt;chr&gt;   &lt;dbl&gt;
1     1 landscape    NA    NA joinent 0.532
2     2 landscape    NA    NA joinent 0.513
3     3 landscape    NA    NA joinent 1.27 
4     4 landscape    NA    NA joinent 1.42 
5     5 landscape    NA    NA joinent 3.34 
6     6 landscape    NA    NA joinent 3.56 </code></pre>
</div>
</div>
<div class="cell">
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://jakubnowosad.com/posts/2019-06-25-ent-bp1/index_files/figure-html/unnamed-chunk-12-1.png" class="img-fluid figure-img" width="960"></p>
</figure>
</div>
</div>
</div>
<p>The values of <strong>joint entropy</strong> organize the example data from the most simple (example data 2) to the most complex (example data 6). However, <strong>joint entropy</strong> alone is not capable to sufficiently distinguish situations with spatially aggregated values (example data 1, 3, and 5) from situations that are spatially disaggregated (example data 2, 4, 6).</p>
</section>
<section id="mutual-information-iyx" class="level2">
<h2 class="anchored" data-anchor-id="mutual-information-iyx">Mutual information [I(y,x)]</h2>
<p>The fourth metric, <strong>mutual information</strong>, quantifies the information that one random variable (x) provides about another random variable (y). It tells how much easier is to predict a category of an adjacent cell if the category of the focus cell is known. Mutual information disambiguates landscape pattern types characterized by the same value of overall complexity. The <code>lsm_l_mutinf()</code> function calculates <strong>mutual information</strong>.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb11" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb11-1">mut_inf <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lsm_l_mutinf</span>(landscapes)</span>
<span id="cb11-2">mut_inf</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code># A tibble: 6 × 6
  layer level     class    id metric   value
  &lt;int&gt; &lt;chr&gt;     &lt;int&gt; &lt;int&gt; &lt;chr&gt;    &lt;dbl&gt;
1     1 landscape    NA    NA mutinf 0.214  
2     2 landscape    NA    NA mutinf 0.00527
3     3 landscape    NA    NA mutinf 0.614  
4     4 landscape    NA    NA mutinf 0.182  
5     5 landscape    NA    NA mutinf 0.627  
6     6 landscape    NA    NA mutinf 0.337  </code></pre>
</div>
</div>
<div class="cell">
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://jakubnowosad.com/posts/2019-06-25-ent-bp1/index_files/figure-html/unnamed-chunk-14-1.png" class="img-fluid figure-img" width="960"></p>
</figure>
</div>
</div>
</div>
<p>Larger values indicate that the cells of the same category are more aggregated, while the smaller values are an indication of disaggregation. <!-- The resulting values can be useful to differentiate between examples of similar **marginal entropy**. --></p>
</section>
<section id="relative-mutual-information-u" class="level2">
<h2 class="anchored" data-anchor-id="relative-mutual-information-u">Relative mutual information [U]</h2>
<p>Due to the spatial autocorrelation, the value of mutual information tends to grow with a diversity of the landscape (<strong>marginal entropy</strong>). To adjust this tendency, it is possible to calculate <strong>relative mutual information</strong> by dividing the <strong>mutual information</strong> by the <strong>marginal entropy</strong>. <strong>Relative mutual information</strong> always has a range between 0 and 1 and can be used to compare spatial data with different number and distribution of categories.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb13" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb13-1">rel_mut_inf <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lsm_l_mutinf</span>(landscapes)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>value <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lsm_l_ent</span>(landscapes)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>value</span>
<span id="cb13-2">rel_mut_inf</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 0.57389601 0.02036298 0.65250917 0.22678349 0.31603770 0.17301316</code></pre>
</div>
</div>
<div class="cell">
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://jakubnowosad.com/posts/2019-06-25-ent-bp1/index_files/figure-html/unnamed-chunk-16-1.png" class="img-fluid figure-img" width="960"></p>
</figure>
</div>
</div>
</div>
<p><strong>Relative mutual information</strong> for the example data orders them from the least aggregated (example data 2, 6), through those with medium aggregation (example data 4, 5), to the most aggregated (example data 1 and 2), without the influence of the number or distribution of categories.</p>
</section>
</section>
<section id="applications" class="level1">
<h1>Applications</h1>
<p>The direct applications of the above metrics are ordering and classifying of spatial patterns. Each of the presented metrics can be used alone to order the spatial data according to a different property. For example, values of <strong>marginal entropy</strong> orders by the diversity of spatial categories, while values of <strong>relative mutual information</strong> orders by spatial aggregation. Two or more of the above metrics can be a basis for spatial classification, grouping similar spatial pattern together.</p>
<p>These basic applications can be further used for a myriad of practical purposes. For example, let’s consider spatial data representing a land cover. An area’s pattern is related to many environmental characteristics and processes, such as vegetation diversity, animal distribution, or water quality. Therefore, the possibility of a quantitative assessment of spatial patterns could enable a better understanding of this relationship, and allow for improvements in management and conservation of the environment.</p>
<p>Spatial patterns do exist in many other domains, including urban science, demography, agriculture, etc. Consequently, these metrics can be used to add new knowledge to these domains.</p>
</section>
<section id="summary" class="level1">
<h1>Summary</h1>
<p>Information theory provides a framework for quantification of the spatial patterns. This includes several metrics based on the co-occurrence matrix, such as <strong>marginal entropy</strong>, <strong>conditional entropy</strong>, <strong>joint entropy</strong>, <strong>mutual information</strong>, and <strong>relative mutual information</strong>. <!-- The information theory based metrics have one important advantage over  --> <!--vs standard metrics--></p>
<p><a href="https://r-spatialecology.github.io/landscapemetrics/index.html">The <strong>landscapemetrics</strong> R package</a> implements these metrics as the <code>lsm_l_ent()</code> (<strong>marginal entropy</strong>), <code>lsm_l_condent()</code> (<strong>conditional entropy</strong>), <code>lsm_l_joinent()</code> (<strong>joint entropy</strong>), and <code>lsm_l_mutinf()</code> (<strong>mutual information</strong>) functions. The function accepts raster data (in the form of <code>Raster*</code>, <code>matrix</code>, or <code>stars</code> objects) as an input. Additional parameters include cells adjacency type (4-connected or 8-connected), type of pairs considered (ordered and unordered), and the unit in which entropy is measured (logarithm to base 2, natural logarithm, and logarithm to base 10).</p>
<p>A complete demonstration of the spatial patterns quantification within the framework of the Information Theory is in the open access <a href="https://link.springer.com/article/10.1007/s10980-019-00830-x">Landscape Ecology</a> article. The article shows that one-dimensional parametrizations of patterns using the information theory based measures correspond to orderings, and two-dimensional parametrizations correspond to classifications. These findings are based on the dataset which contains a rich variety of land cover patterns. Different pattern metrics are compared, and the correlation coefficients between them are calculated. Finally, the article shows an example of how these concepts can be used for spatial patterns classifications by grouping similar patterns into distinct regions of the parameters space.</p>


</section>


<div id="quarto-appendix" class="default"><section id="footnotes" class="footnotes footnotes-end-of-document"><h2 class="anchored quarto-appendix-heading">Footnotes</h2>

<ol>
<li id="fn1"><p>Another name for this representation is the adjacency matrix.↩︎</p></li>
</ol>
</section><section class="quarto-appendix-contents" id="quarto-reuse"><h2 class="anchored quarto-appendix-heading">Reuse</h2><div class="quarto-appendix-contents"><div><a rel="license" href="https://creativecommons.org/licenses/by/4.0/">CC BY 4.0</a></div></div></section><section class="quarto-appendix-contents" id="quarto-citation"><h2 class="anchored quarto-appendix-heading">Citation</h2><div><div class="quarto-appendix-secondary-label">BibTeX citation:</div><div class="code-copy-outer-scaffold"><pre class="sourceCode quarto-appendix-bibtex code-with-copy"><code class="sourceCode bibtex">@online{nowosad2019,
  author = {Nowosad, Jakub},
  title = {Information Theory Provides a Consistent Framework for the
    Analysis of Spatial Patterns},
  date = {2019-06-25},
  url = {https://jakubnowosad.com/posts/2019-06-25-ent-bp1/},
  doi = {10.59350/gdy3d-vpy90},
  langid = {en}
}
</code></pre></div><div class="quarto-appendix-secondary-label">For attribution, please cite this work as:</div><div id="ref-nowosad2019" class="csl-entry quarto-appendix-citeas">
Nowosad, Jakub. 2019. <span>“Information Theory Provides a Consistent
Framework for the Analysis of Spatial Patterns.”</span> June 25. <a href="https://doi.org/10.59350/gdy3d-vpy90">https://doi.org/10.59350/gdy3d-vpy90</a>.
</div></div></section></div> ]]></description>
  <category>spatial</category>
  <category>geocompr</category>
  <category>sil</category>
  <category>rstats</category>
  <category>landscape-ecology</category>
  <guid>https://jakubnowosad.com/posts/2019-06-25-ent-bp1/</guid>
  <pubDate>Tue, 25 Jun 2019 00:00:00 GMT</pubDate>
</item>
<item>
  <title>World population growth through time</title>
  <dc:creator>Jakub Nowosad</dc:creator>
  <link>https://jakubnowosad.com/posts/2019-03-13-world_pop_change2/</link>
  <description><![CDATA[ 





<p>A few months ago I have made an attempt to visualize the world population changes from 1800 to 2100:</p>
<div class="cell">
<div class="cell-output-display">
<p>{{% tweet "1049685831475187712" %}}</p>
</div>
</div>
<p>This way of visualization is good to show the ever-changing distribution of the population on a global scale. It allows seeing that, China and India dominated the world population, but also a large share of the world population had lived in Europe in 1800. At the same time, Australia, the Americas, or Africa were not densely populated. By 1900, there is a noticeable growth in relative population in North America (especially the USA). From 1950 onwards, a decrease in population in Europe and a tremendous increase in population in Africa is visible. Based on this prediction, it is also possible to see the relative population of China and India will decrease in the last decades of this century.</p>
<p>However, while we can see changes in proportion, we cannot see one important dimension - population growth on a global scale. Recently, Tomasz Stepinski, Pawel Netzel and I published a paper describing the landscapes change on a global scale between 1992 and 2015<sup>1</sup> showing how humans impacted our planet just in 24 years time. It is clear the population and its growth has an enormous impact on our planet as it results in more demand for land and natural resources. Therefore, I’ve updated my code to add this dimension…</p>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://jakubnowosad.com/posts/2019-03-13-world_pop_change2/figs/worlds_animate.gif" class="img-fluid figure-img"></p>
<figcaption>Scaled cartogram of the world population changes between 1800 and 2100.</figcaption>
</figure>
</div>
<p>Now, we can see not only the relative distribution of the population for each year but also population growth on a global scale.</p>
<p>Similarly to <a href="../../post/world-pop-change/">the previous post</a>, the rest of this blog post will focus on explaining the steps and code to create the above animation. It involves downloading the data from two sources, cleaning, merging, and preprocessing it. Prepared data can be transformed to cartogram to indicate spatial relations between countries at any given year, and scaled to indicate the global world population at any given year compared to the predicted population for 2100.</p>
<section id="starting" class="level2">
<h2 class="anchored" data-anchor-id="starting">Starting</h2>
<p>Let’s start with the packages. If you are new to R, you may want to take a read of <a href="https://geocompr.robinlovelace.net/spatial-class.html">this</a> first, which points to various resources for setting-up R for geographic data. When you have a recent R version and the appropriate packages installed (e.g.&nbsp;by executing <code>devtools::install_github("geocompr/geocompkg")</code>) the packages can be attached as follows:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(sf)             <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># spatial data classes</span></span>
<span id="cb1-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(rnaturalearth)  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># world map data</span></span>
<span id="cb1-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(readxl)         <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># reading excel files</span></span>
<span id="cb1-4"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(dplyr)          <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># data manipulation</span></span>
<span id="cb1-5"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(stringr)        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># data manipulation</span></span>
<span id="cb1-6"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(tidyr)          <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># data manipulation</span></span>
<span id="cb1-7"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(purrr)          <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># data manipulation</span></span>
<span id="cb1-8"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(cartogram)      <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># cartograms creation</span></span>
<span id="cb1-9"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(gganimate)      <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># animation creation</span></span></code></pre></div></div>
</div>
</section>
<section id="getting-data" class="level2">
<h2 class="anchored" data-anchor-id="getting-data">Getting data</h2>
<p>To create the maps of the world population for each year we will need two datasets - one containing spatial data of the world’s countries and one non-spatial with information about the annual population in the world’s countries. The first one can be easily downloaded from the <a href="http://www.naturalearthdata.com/">Natural Earth</a> website, for example using the <strong>rnaturalearth</strong> package:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb2-1">world_map <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ne_countries</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">returnclass =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"sf"</span>)</span></code></pre></div></div>
</div>
<p>The second one is available from the <a href="https://www.gapminder.org/">Gapminder</a> foundation. Gapminder provides <a href="https://www.gapminder.org/data/documentation/gd003/">a dataset with population data</a> for all countries and world regions from 1800 to 2100. We can download and read the dataset using the code below:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb3-1"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">!</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">dir.exists</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"data"</span>)) <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">dir.create</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"data"</span>)</span>
<span id="cb3-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">download.file</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"http://gapm.io/dl_pop"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">destfile =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"data/pop1800_2100.xlsx"</span>)</span>
<span id="cb3-3">world_pop <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">read_xlsx</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"data/pop1800_2100.xlsx"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sheet =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">7</span>)</span></code></pre></div></div>
</div>
</section>
<section id="cleaning" class="level2">
<h2 class="anchored" data-anchor-id="cleaning">Cleaning</h2>
<p>As always when working with multiple datasets - some data cleaning is necessary. Our <code>world_map</code> dataset has many columns irrelevant for cartograms creation and we do not need spatial data of Antarctica. We can also transform our data into a more appropriate projection<sup>2</sup>.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb4-1">world_map <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> world_map <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb4-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">str_detect</span>(type, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"country|Country"</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb4-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</span>(sovereignt <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">!=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Antarctica"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb4-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">select</span>(sovereignt) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb4-5">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">st_transform</span>(world_map, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">crs =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"+proj=moll"</span>)</span></code></pre></div></div>
</div>
<p>We need to have a common identifier to combine our spatial and non-spatial datasets, for example, names of the countries. However, there are inconsistencies between some of the names. We can fix it manually:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb5-1">world_pop <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> world_pop <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb5-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sovereignt =</span> name) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb5-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sovereignt =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">replace</span>(sovereignt, sovereignt <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Tanzania"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"United Republic of Tanzania"</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb5-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sovereignt =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">replace</span>(sovereignt, sovereignt <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"United States"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"United States of America"</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb5-5">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sovereignt =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">replace</span>(sovereignt, sovereignt <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Congo, Dem. Rep."</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Democratic Republic of the Congo"</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb5-6">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sovereignt =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">replace</span>(sovereignt, sovereignt <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Bahamas"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"The Bahamas"</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb5-7">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sovereignt =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">replace</span>(sovereignt, sovereignt <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Serbia"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Republic of Serbia"</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb5-8">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sovereignt =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">replace</span>(sovereignt, sovereignt <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Macedonia, FYR"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Macedonia"</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb5-9">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sovereignt =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">replace</span>(sovereignt, sovereignt <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Slovak Republic"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Slovakia"</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb5-10">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sovereignt =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">replace</span>(sovereignt, sovereignt <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Congo, Rep."</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Republic of Congo"</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb5-11">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sovereignt =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">replace</span>(sovereignt, sovereignt <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Kyrgyz Republic"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Kyrgyzstan"</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb5-12">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sovereignt =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">replace</span>(sovereignt, sovereignt <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Lao"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Laos"</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb5-13">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sovereignt =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">replace</span>(sovereignt, sovereignt <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Cote d'Ivoire"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Ivory Coast"</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb5-14">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sovereignt =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">replace</span>(sovereignt, sovereignt <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Timor-Leste"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"East Timor"</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb5-15">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sovereignt =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">replace</span>(sovereignt, sovereignt <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Guinea-Bissau"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Guinea Bissau"</span>))</span></code></pre></div></div>
</div>
</section>
<section id="preparing" class="level2">
<h2 class="anchored" data-anchor-id="preparing">Preparing</h2>
<p>Now we can join our two datasets, remove missing values and unimportant columns. We also should transform the data into a long format<sup>3</sup>, which is accepted by the plotting packages (such as <strong>tmap</strong> or <strong>ggplot2</strong>).</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb6" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb6-1">world_data <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">left_join</span>(world_map, world_pop, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">by =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"sovereignt"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb6-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">na.omit</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb6-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">select</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>geo, <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>name, <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>indicator) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb6-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">gather</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">key =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"year"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">value =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"population"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">`</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">1800.0</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">`</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">`</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">2100.0</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">`</span>,</span>
<span id="cb6-5">         <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">convert =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>)</span></code></pre></div></div>
</div>
<p>Additionally, we can calculate total global populations in each year:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb7" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb7-1">world_data <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> world_data <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb7-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">group_by</span>(year) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb7-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">total_pop =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sum</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.numeric</span>(population), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">na.rm =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb7-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">title =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">paste0</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Year: "</span>, year, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">Total population (billions): "</span>, </span>
<span id="cb7-5">                        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">round</span>(total_pop <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1e9</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>))) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb7-6">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ungroup</span>()</span></code></pre></div></div>
</div>
</section>
<section id="subsetting" class="level2">
<h2 class="anchored" data-anchor-id="subsetting">Subsetting</h2>
<p>Now, our data contains information about the world population for each year between 1800 and 2100. It is possible to use it to create cartograms, however, to reduce calculation time and simplification of the results we will only use data for every 25 years:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb8" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb8-1">world_data <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> world_data <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb8-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</span>(year <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%in%</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">seq</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1800</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2100</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">by =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">25</span>))</span></code></pre></div></div>
</div>
</section>
<section id="transforming" class="level2">
<h2 class="anchored" data-anchor-id="transforming">Transforming</h2>
<p>With newly created data, we are able to create our cartograms. We need to split our data into independent annual datasets, create cartograms based on the <code>population</code> variable, and combine all of the cartograms back into one object.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb9" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb9-1">world_data_carto <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> world_data <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb9-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">split</span>(.<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>year) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb9-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">map</span>(cartogram_cont, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"population"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">maxSizeError =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">threshold =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.1</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb9-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">do.call</span>(rbind, .)</span></code></pre></div></div>
</div>
</section>
<section id="scaling" class="level2">
<h2 class="anchored" data-anchor-id="scaling">Scaling</h2>
<p>Predicted global population is the largest for the year 2100 and consists of more than 11 billion human beings. We can resize a map for each year using this value as our reference point, where the rest of the maps would be proportionally smaller than the one for 2100. To scale the areas of the other maps we need to divide a total population in each year by the maximum value and compute the square root of the result. The last step of calculations is to resize the geometries (coordinates) by multiplying it with the scales values<sup>4</sup>:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb10" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb10-1">world_data_scaled <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> world_data_carto <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb10-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">scales =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sqrt</span>(total_pop <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">max</span>(total_pop))) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb10-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">group_by</span>(year) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb10-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">geometry =</span> geometry <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> scales)</span></code></pre></div></div>
</div>
<p>For more on the topic, we recommend checking out the <a href="https://geocompr.robinlovelace.net/geometric-operations.html#affine-transformations">Affine transformations</a> section of Geocomputation with R.</p>
</section>
<section id="visualizing" class="level2">
<h2 class="anchored" data-anchor-id="visualizing">Visualizing</h2>
<p>Now when we have all of the pieces needed, the animated map can be created using either <strong>tmap</strong><sup>5</sup> or <strong>gganimate</strong>. It consists of three steps:</p>
<ol type="1">
<li>Creating maps for each year using a combination of the <code>ggplot()</code>, <code>geom_sf()</code>, and <code>transition_states()</code> functions.</li>
<li>Rendering an animation using <code>animate()</code>. It allows for changing the size of a resulting image, and the speed of animation - you can control it with any two combinations of <code>nframes</code>, <code>fps</code>, and <code>duration</code>.</li>
<li>Saving an animation with <code>anim_save()</code>.</li>
</ol>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb11" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb11-1">worlds_anim <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb11-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_sf</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> world_data_scaled, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> population <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1000000</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">lwd =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.1</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb11-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">coord_sf</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">datum =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NA</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb11-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_fill_viridis_c</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">name =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Population (mln)"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb11-5">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_minimal</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb11-6">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme</span>(</span>
<span id="cb11-7">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">plot.title =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_text</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">22</span>),</span>
<span id="cb11-8">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">plot.background =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_rect</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#f5f5f4"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NA</span>), </span>
<span id="cb11-9">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">panel.background =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_rect</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#f5f5f4"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NA</span>), </span>
<span id="cb11-10">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">legend.background =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_rect</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#f5f5f4"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NA</span>),</span>
<span id="cb11-11">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">legend.position =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.1</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.2</span>),</span>
<span id="cb11-12">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">legend.title =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_text</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">18</span>)</span>
<span id="cb11-13">  ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb11-14">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">title =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"{closest_state}"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb11-15">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">transition_states</span>(title, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">transition_length =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">state_length =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">6</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb11-16">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ease_aes</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"cubic-in-out"</span>)</span>
<span id="cb11-17"></span>
<span id="cb11-18">worlds_animate <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">animate</span>(worlds_anim, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">width =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1200</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">height =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">550</span>,</span>
<span id="cb11-19">                         <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">duration =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">30</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fps =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>)</span>
<span id="cb11-20"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">anim_save</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"worlds_animate.gif"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">animation =</span> worlds_animate)</span></code></pre></div></div>
</div>


</section>


<div id="quarto-appendix" class="default"><section id="footnotes" class="footnotes footnotes-end-of-document"><h2 class="anchored quarto-appendix-heading">Footnotes</h2>

<ol>
<li id="fn1"><p>You can learn more about it at https://doi.org/10.1016/j.jag.2018.09.013 or by reading its preprint at https://eartharxiv.org/k3rmn/.↩︎</p></li>
<li id="fn2"><p>You can read more about projections in the <a href="https://geocompr.robinlovelace.net/reproj-geo-data.html">Reprojecting geographic data</a> chapter of Geocomputation with R.↩︎</p></li>
<li id="fn3"><p>This process is called <a href="http://r4ds.had.co.nz/tidy-data.html#gathering">gathering</a>.↩︎</p></li>
<li id="fn4"><p>Mutating of geometry columns is possible with <a href="https://github.com/r-spatial/sf/issues/903">the recent improvements in the <strong>sf</strong> package</a>.↩︎</p></li>
<li id="fn5"><p>Visit <a href="https://geocompr.robinlovelace.net/adv-map.html#animated-maps">the animated maps chapter of Geocomputation with R</a> or <a href="../../post/world-pop-change/">my previous blog post</a> to learn how to make animations with <strong>tmap</strong>.↩︎</p></li>
</ol>
</section><section class="quarto-appendix-contents" id="quarto-reuse"><h2 class="anchored quarto-appendix-heading">Reuse</h2><div class="quarto-appendix-contents"><div><a rel="license" href="https://creativecommons.org/licenses/by/4.0/">CC BY 4.0</a></div></div></section><section class="quarto-appendix-contents" id="quarto-citation"><h2 class="anchored quarto-appendix-heading">Citation</h2><div><div class="quarto-appendix-secondary-label">BibTeX citation:</div><div class="code-copy-outer-scaffold"><pre class="sourceCode quarto-appendix-bibtex code-with-copy"><code class="sourceCode bibtex">@online{nowosad2019,
  author = {Nowosad, Jakub},
  title = {World Population Growth Through Time},
  date = {2019-03-13},
  url = {https://jakubnowosad.com/posts/2019-03-13-world_pop_change2/},
  doi = {10.59350/r7hrj-kc392},
  langid = {en}
}
</code></pre></div><div class="quarto-appendix-secondary-label">For attribution, please cite this work as:</div><div id="ref-nowosad2019" class="csl-entry quarto-appendix-citeas">
Nowosad, Jakub. 2019. <span>“World Population Growth Through
Time.”</span> March 13. <a href="https://doi.org/10.59350/r7hrj-kc392">https://doi.org/10.59350/r7hrj-kc392</a>.
</div></div></section></div> ]]></description>
  <category>spatial</category>
  <category>geocompr</category>
  <category>rstats</category>
  <category>vis</category>
  <category>sil</category>
  <guid>https://jakubnowosad.com/posts/2019-03-13-world_pop_change2/</guid>
  <pubDate>Wed, 13 Mar 2019 00:00:00 GMT</pubDate>
</item>
<item>
  <title>Efficient landscape metrics calculations for buffers around sampling points</title>
  <dc:creator>Jakub Nowosad</dc:creator>
  <link>https://jakubnowosad.com/posts/2019-03-07-lsm-bp1/</link>
  <description><![CDATA[ 





<p>Landscape metrics are algorithms that quantify physical characteristics of landscape mosaics (aka categorical raster) in order to connect them to some ecological processes. Many different landscape metrics exist and they can provide three main levels of information: (i) landscape level, (ii) class level, and (iii) patch level. A landscape level metric gives just one value describing a certain property of a landscape mosaic, such as its diversity. A class level metric returns one value for each class (category) present in a landscape. A patch level metric separates neighboring cells belonging to the same class, and calculates a value for each patch.<sup>1</sup></p>
<p>Landscape metrics are often applied to better understand a landscape structure around some sampling points. <!-- For example, ... .  --> In this post, I show how to prepare input data and how to calculate selected landscape metrics based on different buffer sizes and shapes. All calculations were performed in R, using <a href="https://cran.r-project.org/web/packages/gdalUtils/index.html"><strong>gdalUtils</strong></a>, <a href="https://github.com/r-spatial/sf"><strong>sf</strong></a>, and <a href="https://github.com/rspatial/raster"><strong>raster</strong></a> packages for spatial data handling, <a href="https://github.com/tidyverse/purrr"><strong>purrr</strong></a> to ease some calculations, and most importantly the <a href="https://github.com/r-spatialecology/landscapemetrics"><strong>landscapemetrics</strong></a> package was used to calculate landscape metrics.</p>
<section id="data-preparation" class="level1">
<h1>Data preparation</h1>
<p>Every software for calculations of landscape metrics, such as FRAGSTATS or <strong>landscapemetrics</strong>, expects the input data in a projected coordinate reference system instead of a geographic coordinate reference system. This is due to a fact that geographic coordinate reference systems are expressed in degrees, and one degree on the equator has a different length in meters than one degree on a middle latitude. Projected coordinate reference systems, on the other hand, have a linear unit of measurement such as meters. This allows for proper calculations of distances or areas.<sup>2</sup></p>
<p>In this case, the input datasets (<code>cci_lc_australia.tif</code> and <code>sample_points.csv</code>) are in a geographic coordinate reference system called WGS84. Therefore, it is important to reproject the data before calculating any landscape metrics. Reprojection of raster objects is possible with an R package called <strong>gdalUtils</strong>:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(gdalUtils) </span>
<span id="cb1-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">dir.create</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"data"</span>)</span>
<span id="cb1-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># downloading the land cover data</span></span>
<span id="cb1-4"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">download.file</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"https://github.com/Nowosad/lsm-bp1/raw/master/data/cci_lc_australia.zip"</span>,</span>
<span id="cb1-5">              <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">destfile =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"data/cci_lc_australia.zip"</span>)</span>
<span id="cb1-6"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># unziping the file</span></span>
<span id="cb1-7"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">unzip</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"data/cci_lc_australia.zip"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"data/cci_lc_australia.tif"</span>)</span>
<span id="cb1-8"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># reprojecting the raster data to a local projected coordinate reference system</span></span>
<span id="cb1-9"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># read more at http://spatialreference.org/ref/epsg/gda94-australian-albers/</span></span>
<span id="cb1-10"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">gdalwarp</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">srcfile =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"data/cci_lc_australia.tif"</span>,</span>
<span id="cb1-11">         <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">dstfile =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"data/cci_lc_australia_aea.vrt"</span>,</span>
<span id="cb1-12">         <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">of =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"VRT"</span>,</span>
<span id="cb1-13">         <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">t_srs =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"+proj=aea +lat_1=-18 +lat_2=-36 +lat_0=0 +lon_0=132 +x_0=0 +y_0=0 +ellps=GRS80 +towgs84=0,0,0,0,0,0,0 +units=m +no_defs"</span>)</span>
<span id="cb1-14"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># compressing the output file</span></span>
<span id="cb1-15"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">gdal_translate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">src_dataset =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"data/cci_lc_australia_aea.vrt"</span>,</span>
<span id="cb1-16">               <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">dst_dataset =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"data/cci_lc_australia_aea.tif"</span>,</span>
<span id="cb1-17">               <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">co =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"COMPRESS=LZW"</span>)</span></code></pre></div></div>
</div>
<p>Reading and reprojecting vector (points) objects can be done with the <strong>sf</strong> package:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb2-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(sf)</span>
<span id="cb2-2"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># downloading the points</span></span>
<span id="cb2-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">download.file</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"https://raw.githubusercontent.com/Nowosad/lsm-bp1/master/data/sample_points.csv"</span>,</span>
<span id="cb2-4">              <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">destfile =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"data/sample_points.csv"</span>)</span>
<span id="cb2-5"></span>
<span id="cb2-6"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># reading the point data and reprojecting it</span></span>
<span id="cb2-7">points <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">read.csv</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"data/sample_points.csv"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb2-8">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">st_as_sf</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">coords =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"longitude"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"latitude"</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb2-9">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">st_set_crs</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4326</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb2-10">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">st_transform</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"+proj=aea +lat_1=-18 +lat_2=-36 +lat_0=0 +lon_0=132 +x_0=0 +y_0=0 +ellps=GRS80 +towgs84=0,0,0,0,0,0,0 +units=m +no_defs "</span>)</span>
<span id="cb2-11"></span>
<span id="cb2-12"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># saving the new object to a spatial data format GPKG</span></span>
<span id="cb2-13"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">st_write</span>(points, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"data/sample_points.gpkg"</span>)</span></code></pre></div></div>
</div>
<div class="figure">
<img src="https://jakubnowosad.com/posts/2019-03-07-lsm-bp1/figs/01-map-all.png">
<p class="caption">
Location of three sample points on a land cover map of Australia. Source of the input map: <a href="https://maps.elie.ucl.ac.be/CCI/viewer/" class="uri">https://maps.elie.ucl.ac.be/CCI/viewer/</a>
</p>
</div>
</section>
<section id="calculations-of-landscape-metrics-in-a-buffer" class="level1">
<h1>Calculations of landscape metrics in a buffer</h1>
<p>Now, we can read the new files - the new raster with the <code>raster()</code> function and the new points file with the <code>st_read()</code> function:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb3-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># attaching the packages used in the next examples</span></span>
<span id="cb3-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(raster)</span>
<span id="cb3-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(sf)</span>
<span id="cb3-4"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(purrr)</span>
<span id="cb3-5"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(landscapemetrics)</span>
<span id="cb3-6"></span>
<span id="cb3-7"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># reading the raster and vector datasets</span></span>
<span id="cb3-8">lc <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">raster</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"data/cci_lc_australia_aea.tif"</span>)</span>
<span id="cb3-9">sample_points <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">st_read</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"data/sample_points.gpkg"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">quiet =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>)</span></code></pre></div></div>
</div>
<p>The <strong>landscapemetrics</strong> package has a function designed for calculations of landscape metrics for a given buffer. It is called <code>sample_lsm()</code> and it expects, at least three input arguments - a raster, a vector (points), and a buffer size. As a default, it calculates all of the available metrics in a square buffer, where buffer size is the side-length in map units (e.g.&nbsp;meters).</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb4-1">square_all <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sample_lsm</span>(lc,</span>
<span id="cb4-2">                        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> sample_points,</span>
<span id="cb4-3">                        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3000</span>)</span>
<span id="cb4-4">square_all</span></code></pre></div></div>
</div>
<pre><code>## # A tibble: 1,282 x 8
##    layer level class    id metric  value plot_id percentage_inside
##    &lt;int&gt; &lt;chr&gt; &lt;int&gt; &lt;int&gt; &lt;chr&gt;   &lt;dbl&gt;   &lt;int&gt;             &lt;dbl&gt;
##  1     1 class    11    NA ai       92.1       1              109.
##  2     1 class   100    NA ai      100         1              109.
##  3     1 class   122    NA ai       NA         1              109.
##  4     1 class   130    NA ai       50         1              109.
##  5     1 class   150    NA ai       71.1       1              109.
##  6     1 class    11    NA area_cv  NA         1              109.
##  7     1 class   100    NA area_cv  NA         1              109.
##  8     1 class   122    NA area_cv  NA         1              109.
##  9     1 class   130    NA area_cv  33.3       1              109.
## 10     1 class   150    NA area_cv 119.        1              109.
## # … with 1,272 more rows</code></pre>
<p>The output, <code>square_all</code>, is a data frame containing values of all of the landscape metrics available in the <strong>landscapemetrics</strong> package. The structure of the output is stable and contains the following columns:</p>
<ol type="1">
<li><code>layer</code> - id of the input raster (landscape). It is possible to calculate landscape metrics on several rasters at the same time.</li>
<li><code>level</code> - a landscape metrics level (“landscape”, “class”, or “patch”).</li>
<li><code>class</code> - a number of a class for class and patch level metrics.</li>
<li><code>id</code> - a number of a patch for patch level metrics.</li>
<li><code>metric</code> - a metric name.</li>
<li><code>value</code> - an output value of the metric.</li>
<li><code>plot_id</code> - id of the input vector (point).</li>
<li><code>percentage_inside</code> - the size of the actual sampled landscape in %. It is not always 100% due to two reasons: (i) clipping raster cells using a buffer not directly intersecting a raster cell center lead to inaccuracies and (ii) sample plots can exceed the landscape boundary.</li>
</ol>
<div class="figure">
<img src="https://jakubnowosad.com/posts/2019-03-07-lsm-bp1/figs/p1.png">
<p class="caption">
Square buffers with the side-length of 3000 meters around the three sample points.
</p>
</div>
<p>The <code>sample_lsm()</code> function also allows for selecting metrics using a level name (<code>"landscape"</code>, <code>"class"</code>, or <code>"patch"</code>), a metric abbreviation, full name of a metric, or a metric type. For example, SHDI (Shannon’s diversity index) on a landscape level (<code>"lsm_l_shdi"</code>) can be calculated using the code below.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb6" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb6-1">square_shdi <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sample_lsm</span>(lc,</span>
<span id="cb6-2">                         <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> sample_points,</span>
<span id="cb6-3">                         <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3000</span>,</span>
<span id="cb6-4">                         <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">what =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"lsm_l_shdi"</span>) </span>
<span id="cb6-5">square_shdi</span></code></pre></div></div>
</div>
<pre><code>## # A tibble: 3 x 8
##   layer level     class    id metric value plot_id percentage_inside
##   &lt;int&gt; &lt;chr&gt;     &lt;int&gt; &lt;int&gt; &lt;chr&gt;  &lt;dbl&gt;   &lt;int&gt;             &lt;dbl&gt;
## 1     1 landscape    NA    NA shdi   0.870       1             109. 
## 2     1 landscape    NA    NA shdi   1.49        2              98.9
## 3     1 landscape    NA    NA shdi   0.665       3              98.9</code></pre>
<p>The result shows that the area around the second sample point is the most diverse (SHDI value of 1.49), and the area around the third sample point is the least diverse (SHDI value of 0.665).</p>
<p>As the default, <code>sample_lsm()</code> uses a square buffer, but it could be changed to a circle one with the <code>shape</code> argument set to <code>"circle"</code>. In this case, the size argument represents the radius in map units.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb8" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb8-1">circle_shdi <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sample_lsm</span>(lc,</span>
<span id="cb8-2">                         <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> sample_points,</span>
<span id="cb8-3">                         <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3000</span>,</span>
<span id="cb8-4">                         <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">what =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"lsm_l_shdi"</span>,</span>
<span id="cb8-5">                         <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">shape =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"circle"</span>) </span>
<span id="cb8-6">circle_shdi</span></code></pre></div></div>
</div>
<pre><code>## # A tibble: 3 x 8
##   layer level     class    id metric value plot_id percentage_inside
##   &lt;int&gt; &lt;chr&gt;     &lt;int&gt; &lt;int&gt; &lt;chr&gt;  &lt;dbl&gt;   &lt;int&gt;             &lt;dbl&gt;
## 1     1 landscape    NA    NA shdi   1.03        1              99.6
## 2     1 landscape    NA    NA shdi   1.65        2             100. 
## 3     1 landscape    NA    NA shdi   0.689       3              99.9</code></pre>
<div class="figure">
<img src="https://jakubnowosad.com/posts/2019-03-07-lsm-bp1/figs/p3.png">
<p class="caption">
Circle buffers with the radius of 3000 meters around the three sample points.
</p>
</div>
</section>
<section id="calculations-of-landscape-metrics-for-many-buffers" class="level1">
<h1>Calculations of landscape metrics for many buffers</h1>
<p>Finally, it is also possible to calculate landscape metrics on many different buffer sizes at the same time. You just need to select what sizes you are interested in and use the <code>sample_lsm()</code> function inside a <code>map_dfr()</code> function.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb10" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb10-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># creating buffers of 3000 and 6000 meters </span></span>
<span id="cb10-2">sizes <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3000</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">6000</span>)</span>
<span id="cb10-3"></span>
<span id="cb10-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># calculating shdi (landscape metric) for each buffer</span></span>
<span id="cb10-5">two_sizes_output <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> sizes <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb10-6">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">set_names</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb10-7">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">map_dfr</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sample_lsm</span>(lc, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> sample_points, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">what =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"lsm_l_shdi"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> .), </span>
<span id="cb10-8">          <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">.id =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"buffer"</span>)</span></code></pre></div></div>
</div>
<p>It calculates all of the selected metrics for all of the buffers.</p>
<div class="figure">
<img src="https://jakubnowosad.com/posts/2019-03-07-lsm-bp1/figs/p2.png">
<p class="caption">
Square buffers with the side-length of 3000 and 6000 meters around the three sample points.
</p>
</div>
<p>The result is a data frame, where the first column describes the buffer size, and the rest is a standard <strong>landscapemetrics</strong> output.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb11" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb11-1">two_sizes_output</span></code></pre></div></div>
</div>
<pre><code>## # A tibble: 6 x 9
##   buffer layer level     class    id metric value plot_id percentage_inside
##   &lt;chr&gt;  &lt;int&gt; &lt;chr&gt;     &lt;int&gt; &lt;int&gt; &lt;chr&gt;  &lt;dbl&gt;   &lt;int&gt;             &lt;dbl&gt;
## 1 3000       1 landscape    NA    NA shdi   0.870       1             109. 
## 2 3000       1 landscape    NA    NA shdi   1.49        2              98.9
## 3 3000       1 landscape    NA    NA shdi   0.665       3              98.9
## 4 6000       1 landscape    NA    NA shdi   1.02        1              99.1
## 5 6000       1 landscape    NA    NA shdi   1.62        2              99.1
## 6 6000       1 landscape    NA    NA shdi   0.731       3              99.1</code></pre>
</section>
<section id="summary" class="level1">
<h1>Summary</h1>
<p>This post gave a few examples of how to calculate selected landscape metrics for buffers around sampling points using the <strong>landscapemetrics</strong> package and its function <code>sample_lsm()</code>. In the same time, the <strong>landscapemetrics</strong> package has much more to offer, including <a href="https://r-spatialecology.github.io/landscapemetrics/reference/index.html">a lot of implemented landscape metrics</a>, a number of utility functions (e.g.&nbsp;<code>extract_lsm()</code> or <code>get_patches()</code>), and several C++ functions designed for the development of new metrics.</p>
<p>The <strong>landscapemetrics</strong> is also an open-source collaborative project. It enables <a href="https://r-spatialecology.github.io/landscapemetrics/CONTRIBUTING.html">community contributions</a>, including documentation improvements, bug reports, and ideas to add new metrics and functions. To learn more you can visit the package website at <a href="https://r-spatialecology.github.io/landscapemetrics/" class="uri">https://r-spatialecology.github.io/landscapemetrics/</a> or read the <a href="https://doi.org/10.1111/ecog.04617">Ecography</a> article.</p>


</section>


<div id="quarto-appendix" class="default"><section id="footnotes" class="footnotes footnotes-end-of-document"><h2 class="anchored quarto-appendix-heading">Footnotes</h2>

<ol>
<li id="fn1"><p>For a more detailed explanation see <a href="https://r-spatialecology.github.io/landscapemetrics/articles/articles/general-background.html" class="uri">https://r-spatialecology.github.io/landscapemetrics/articles/articles/general-background.html</a>.↩︎</p></li>
<li id="fn2"><p>More information about coordinate reference systems can be found at <a href="https://geocompr.robinlovelace.net/spatial-class.html#crs-intro" class="uri">https://geocompr.robinlovelace.net/spatial-class.html#crs-intro</a>.↩︎</p></li>
</ol>
</section><section class="quarto-appendix-contents" id="quarto-reuse"><h2 class="anchored quarto-appendix-heading">Reuse</h2><div class="quarto-appendix-contents"><div><a rel="license" href="https://creativecommons.org/licenses/by/4.0/">CC BY 4.0</a></div></div></section><section class="quarto-appendix-contents" id="quarto-citation"><h2 class="anchored quarto-appendix-heading">Citation</h2><div><div class="quarto-appendix-secondary-label">BibTeX citation:</div><div class="code-copy-outer-scaffold"><pre class="sourceCode quarto-appendix-bibtex code-with-copy"><code class="sourceCode bibtex">@online{nowosad2019,
  author = {Nowosad, Jakub},
  title = {Efficient Landscape Metrics Calculations for Buffers Around
    Sampling Points},
  date = {2019-03-07},
  url = {https://jakubnowosad.com/posts/2019-03-07-lsm-bp1/},
  doi = {10.59350/db1ge-ryy38},
  langid = {en}
}
</code></pre></div><div class="quarto-appendix-secondary-label">For attribution, please cite this work as:</div><div id="ref-nowosad2019" class="csl-entry quarto-appendix-citeas">
Nowosad, Jakub. 2019. <span>“Efficient Landscape Metrics Calculations
for Buffers Around Sampling Points.”</span> March 7. <a href="https://doi.org/10.59350/db1ge-ryy38">https://doi.org/10.59350/db1ge-ryy38</a>.
</div></div></section></div> ]]></description>
  <category>spatial</category>
  <category>geocompr</category>
  <category>sil</category>
  <category>rstats</category>
  <category>landscape-ecology</category>
  <guid>https://jakubnowosad.com/posts/2019-03-07-lsm-bp1/</guid>
  <pubDate>Thu, 07 Mar 2019 00:00:00 GMT</pubDate>
</item>
<item>
  <title>GeoPAT2: Entropy calculations for local landscapes</title>
  <dc:creator>Jakub Nowosad</dc:creator>
  <link>https://jakubnowosad.com/posts/2019-01-20-geopat-2-ent/</link>
  <description><![CDATA[ 





<section id="introduction" class="level1">
<h1>Introduction</h1>
<p><strong>GeoPAT 2</strong> is an open-source software written in C and dedicated to pattern-based spatial and temporal analysis. Four main types of analysis available in <strong>GeoPAT 2</strong> are (i) search, (ii) change detection, (iii) segmentation, and (iv) clustering. However, additional applications are also possible, including extracting information about spatial patterns.</p>
<!-- html_preserve-->
<!-- {{% tweet "1085174957678780417" %}} -->
<!--/html_preserve -->
<blockquote class="twitter-tweet blockquote">
<p lang="en" dir="ltr">
Global landscape diversity (based on Shannon entropy of land cover categories in a 9 by 9 km local window). It shows where only one land cover category dominates, and where there is a large number of lc categories. <a href="https://twitter.com/hashtag/sil?src=hash&amp;ref_src=twsrc%5Etfw">#sil</a> <a href="https://twitter.com/hashtag/gis?src=hash&amp;ref_src=twsrc%5Etfw">#gis</a> <a href="https://twitter.com/hashtag/landscapeecology?src=hash&amp;ref_src=twsrc%5Etfw">#landscapeecology</a> <a href="https://t.co/E7pVkIwwY7">pic.twitter.com/E7pVkIwwY7</a>
</p>
— Jakub Nowosad (<span class="citation" data-cites="jakub_nowosad">@jakub_nowosad</span>) <a href="https://twitter.com/jakub_nowosad/status/1085174957678780417?ref_src=twsrc%5Etfw">January 15, 2019</a>
</blockquote>
<script async="" src="https://platform.twitter.com/widgets.js" charset="utf-8"></script>
<p>In the above case, I was interested in extracting values of Shannon entropy of land cover categories for local landscapes on a global scale. It had prompted a comment from <a href="https://twitter.com/DerEhrmann">Steffen Ehrmann</a>:</p>
<p>
<!-- {{% tweet "1085651345552490496" %}} -->
</p>
<blockquote class="twitter-tweet blockquote">
<p lang="en" dir="ltr">
The LC data I have, but it would be awesome to see the code you use in GeoPAT2, would be a good excuse to learn it!
</p>
— Steffen Ehrmann (<span class="citation" data-cites="DerEhrmann">@DerEhrmann</span>) <a href="https://twitter.com/DerEhrmann/status/1085651345552490496?ref_src=twsrc%5Etfw">January 16, 2019</a>
</blockquote>
<script async="" src="https://platform.twitter.com/widgets.js" charset="utf-8"></script>
<p>Therefore, this blog post describes how to calculate Shannon entropy of land cover categories using <strong>GeoPAT 2</strong> and R.</p>
</section>
<section id="setup" class="level1">
<h1>Setup</h1>
<p>Installation instructions for <strong>GeoPAT 2</strong> can be found at https://github.com/Nowosad/geopat2#Installation and for R at https://cloud.r-project.org/. Additionally, a few R packages should be installed.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1">pkgs <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(</span>
<span id="cb1-2">  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"rgeopat2"</span>,          <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># helper functions for GeoPAT 2 in R</span></span>
<span id="cb1-3">  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"sf"</span>,                <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># vector data classes</span></span>
<span id="cb1-4">  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"raster"</span>,            <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># raster data classes</span></span>
<span id="cb1-5">  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"dplyr"</span>,             <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># data manipulation</span></span>
<span id="cb1-6">  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"tmap"</span>,              <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># spatial visualisation</span></span>
<span id="cb1-7">  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"landscapemetrics"</span>   <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># contains example dataset</span></span>
<span id="cb1-8">)</span>
<span id="cb1-9">to_install <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">!</span>pkgs <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%in%</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">installed.packages</span>()</span>
<span id="cb1-10"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">any</span>(to_install)) {</span>
<span id="cb1-11">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">install.packages</span>(pkgs[to_install])</span>
<span id="cb1-12">}</span></code></pre></div></div>
</div>
<p>Now you have the necessary packages, the next step is to attach the ones we will use.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb2-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(rgeopat2)</span>
<span id="cb2-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(sf)</span>
<span id="cb2-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(raster)</span>
<span id="cb2-4"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(dplyr)</span>
<span id="cb2-5"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(tmap)</span>
<span id="cb2-6"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(landscapemetrics)</span></code></pre></div></div>
</div>
</section>
<section id="input-data" class="level1">
<h1>Input data</h1>
<p>We will use the <code>augusta_nlcd</code> dataset from the <strong>landscapemetics</strong> package as an example. It is a raster representing land cover categories for an area of about 270 km<sup>2</sup> west from Augusta, Georga.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb3-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">data</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"augusta_nlcd"</span>)</span>
<span id="cb3-2">augusta_nlcd <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">deratify</span>(augusta_nlcd, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"NLCD.2011.Land.Cover.Class"</span>)</span>
<span id="cb3-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">dir.create</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"data"</span>)</span>
<span id="cb3-4"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">writeRaster</span>(augusta_nlcd, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"data/augusta_nlcd.tif"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">overwrite =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>)</span></code></pre></div></div>
</div>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb4-1">nlcd_colors <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#000000"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#00F900"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#476BA0"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#D1DDF9"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#DDC9C9"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#D89382"</span>,</span>
<span id="cb4-2">                <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#ED0000"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#AA0000"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#B2ADA3"</span>,  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#68AA63"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#1C6330"</span>, </span>
<span id="cb4-3">                <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#B5C98E"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#dcca8f"</span>, </span>
<span id="cb4-4">                <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#fde9aa"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#DBD83C"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#AA7028"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#BAD8EA"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#64b3d5"</span>)</span>
<span id="cb4-5">p1 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tm_shape</span>(augusta_nlcd) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb4-6">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tm_raster</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"NLCD.2011.Land.Cover.Class"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">palette =</span> nlcd_colors) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb4-7">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tm_layout</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">legend.outside =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>)</span>
<span id="cb4-8">p1</span></code></pre></div></div>
</div>
<p><img src="https://jakubnowosad.com/posts/2019-01-20-geopat-2-ent/figs/p1-1.png" class="img-fluid"></p>
</section>
<section id="calculate-shannon-entropy" class="level1">
<h1>Calculate Shannon entropy</h1>
<p>The <code>gpat_gridhis</code> module of <strong>GeoPAT 2</strong> reads the input raster data from a GeoTIFF file and creates an output based on selected size and signature. Firstly, we need to choose the size of the local landscapes. The input data has a resolution of 30 meters, and we decided on local landscapes of 600 by 600 meters (0.36 km<sup>2</sup>) by setting both <code>size</code> (<code>-z</code>) and <code>shift</code> (<code>-f</code>) to 20 (map resolution of 30 * 20 = 600 meters). Secondly, we need to pick a proper signature - in this case, it is <code>ent</code> (Shannon <strong>ent</strong>ropy). For this signature, we also must disable any value normalization (<code>-n 'none'</code>).</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb5-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">system</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"gpat_gridhis -i data/augusta_nlcd.tif -o data/augusta_ent.grd -z 20 -f 20 -s 'ent' -n 'none'"</span>)</span></code></pre></div></div>
</div>
<p>The output is a binary file and we can easily convert it to text with the <code>gpat_grid2txt</code> module.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb6" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb6-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">system</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"gpat_grid2txt -i data/augusta_ent.grd -o data/augusta_ent.txt"</span>)</span></code></pre></div></div>
</div>
</section>
<section id="create-a-vector-grid" class="level1">
<h1>Create a vector grid</h1>
<p>The output text file can be next processed in any software. However, we will use R here, as there is an R package for working with <strong>GeoPAT 2</strong> outputs called <strong>rgeopat2</strong>. One of its functions, <code>gpat_create_grid()</code>, creates a spatial vector object recreating the grid used in the previous <strong>GeoPAT 2</strong> calculations.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb7" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb7-1">augusta_grid <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">gpat_create_grid</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"data/augusta_ent.grd.hdr"</span>)</span></code></pre></div></div>
</div>
<p>This new object contains a number of regular square polygons (aka local landscapes), each having a size of 600 by 600 meters.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb8" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb8-1">p2 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tm_shape</span>(augusta_nlcd) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb8-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tm_raster</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"NLCD.2011.Land.Cover.Class"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">palette =</span> nlcd_colors) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb8-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tm_shape</span>(augusta_grid) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb8-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tm_borders</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb8-5">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tm_layout</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">legend.outside =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>)</span>
<span id="cb8-6">p2</span></code></pre></div></div>
</div>
<p><img src="https://jakubnowosad.com/posts/2019-01-20-geopat-2-ent/figs/p2-1.png" class="img-fluid"></p>
</section>
<section id="read-data-to-r-and-connect-it-to-the-grid" class="level1">
<h1>Read data to R and connect it to the grid</h1>
<p>The <strong>rgeopat2</strong> also has a helper function, <code>gpat_read_txt()</code>, for reading text outputs from <strong>GeoPAT 2</strong>.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb9" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb9-1">augusta_ent <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">gpat_read_txt</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"data/augusta_ent.txt"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">signature =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ent"</span>)</span></code></pre></div></div>
</div>
<p>The new object <code>augusta_ent</code> is a data frame with five columns describing each local landscape - (i) value of Shannon entropy, (ii) number of land cover categories, (iii) area with values in cells<sup>2</sup>, (iv) column number, (v) row number.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb10" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb10-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">head</span>(augusta_ent)</span></code></pre></div></div>
</div>
<pre><code>##   Shannon_entropy number_of_categories object_size col row
## 1        1.174216                    5         400   1   1
## 2        2.005147                    6         400   2   1
## 3        1.485236                    5         400   3   1
## 4        2.200218                   10         400   4   1
## 5        2.226918                    8         400   5   1
## 6        1.918720                    6         400   6   1</code></pre>
<p>Now, we can combine the spatial object with the data frame.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb12" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb12-1">augusta_grid <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">bind_cols</span>(augusta_grid, augusta_ent)</span></code></pre></div></div>
</div>
<p>The output, <code>augusta_grid</code> is a set of polygons, where each is represented by the value of Shannon entropy. It allows distinguishing the local landscapes with only one or two land cover categories (low values of Shannon entropy) and the ones with many land cover categories (high values of Shannon entropy).</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb13" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb13-1">p3 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tm_shape</span>(augusta_nlcd) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb13-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tm_raster</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">legend.show =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">palette =</span> nlcd_colors) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb13-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tm_shape</span>(augusta_grid) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb13-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tm_polygons</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Shannon_entropy"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb13-5">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tm_layout</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">legend.outside =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>)</span>
<span id="cb13-6"></span>
<span id="cb13-7">p3</span></code></pre></div></div>
</div>
<p><img src="https://jakubnowosad.com/posts/2019-01-20-geopat-2-ent/figs/p3-1.png" class="img-fluid"></p>
</section>
<section id="summary" class="level1">
<h1>Summary</h1>
<p>This post has shown how to use <strong>GeoPAT 2</strong> to extract certain metric (Shannon entropy), and how to connect the result with its spatial representation for a relatively small area in Georgia, USA.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb14" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb14-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tmap_arrange</span>(p1, p2, p3, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ncol =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span></code></pre></div></div>
</div>
<p><img src="https://jakubnowosad.com/posts/2019-01-20-geopat-2-ent/figs/p4-1.png" class="img-fluid"></p>
<p>However, <strong>GeoPAT 2</strong> was written to handle large spatial rasters, including ones on continental and global scales. Therefore, I encourage you to try it on your own study area - regardless of its size. You can also read more about additional applications of <strong>GeoPAT 2</strong> and see more examples at https://github.com/Nowosad/geopat2.</p>


</section>

<div id="quarto-appendix" class="default"><section class="quarto-appendix-contents" id="quarto-reuse"><h2 class="anchored quarto-appendix-heading">Reuse</h2><div class="quarto-appendix-contents"><div><a rel="license" href="https://creativecommons.org/licenses/by/4.0/">CC BY 4.0</a></div></div></section><section class="quarto-appendix-contents" id="quarto-citation"><h2 class="anchored quarto-appendix-heading">Citation</h2><div><div class="quarto-appendix-secondary-label">BibTeX citation:</div><div class="code-copy-outer-scaffold"><pre class="sourceCode quarto-appendix-bibtex code-with-copy"><code class="sourceCode bibtex">@online{nowosad2019,
  author = {Nowosad, Jakub},
  title = {GeoPAT2: {Entropy} Calculations for Local Landscapes},
  date = {2019-01-20},
  url = {https://jakubnowosad.com/posts/2019-01-20-geopat-2-ent/},
  doi = {10.59350/khdd9-0r754},
  langid = {en}
}
</code></pre></div><div class="quarto-appendix-secondary-label">For attribution, please cite this work as:</div><div id="ref-nowosad2019" class="csl-entry quarto-appendix-citeas">
Nowosad, Jakub. 2019. <span>“GeoPAT2: Entropy Calculations for Local
Landscapes.”</span> January 20. <a href="https://doi.org/10.59350/khdd9-0r754">https://doi.org/10.59350/khdd9-0r754</a>.
</div></div></section></div> ]]></description>
  <category>spatial</category>
  <category>geocompr</category>
  <category>geopat</category>
  <category>sil</category>
  <category>rstats</category>
  <guid>https://jakubnowosad.com/posts/2019-01-20-geopat-2-ent/</guid>
  <pubDate>Sun, 20 Jan 2019 00:00:00 GMT</pubDate>
</item>
<item>
  <title>Geocomputation with R - the afterword</title>
  <dc:creator>Jakub Nowosad</dc:creator>
  <link>https://jakubnowosad.com/posts/2018-12-12-geocomputation-with-r-the-afterword/</link>
  <description><![CDATA[ 





<p>I am extremely proud to announce that <a href="https://geocompr.robinlovelace.net/">Geocomputation with R</a> is complete. It took <a href="https://twitter.com/robinlovelace">Robin</a>, <a href="http://www.geographie.uni-jena.de/en/Muenchow.html">Jannes</a>, and me almost 2 years of collaborative planning, writing, refinement, and deployment to make the book available for anyone interested in open source, command-line approaches for handling geographic data. We’re very happy that it’s now ready to present to the world (and that physical copies of the book can be pre-ordered already from <a href="https://www.amazon.com/Geocomputation-Chapman-Hall-Robin-Lovelace/dp/1138304514/">Amazon</a>, <a href="https://wordery.com/geocomputation-with-r-robin-lovelace-9781138304512">Wordery</a> and direct from the publisher, <a href="https://www.crcpress.com/Geocomputation-with-R/Lovelace-Nowosad-Munchow/p/book/9781138304512">CRC Press</a>).</p>
<p>In addition to celebrating this achievement (🎉) the purpose of this blog post is to share important information about extra materials and how to get involved. I also want to provide some context: how and why did we end-up writing an open source book (the front cover of which is shown below)?</p>
<p><img src="https://jakubnowosad.com/posts/2018-12-12-geocomputation-with-r-the-afterword/figs/book_cover.jpg" class="img-fluid"></p>
<section id="the-books-beginnings" class="level1">
<h1>The book’s beginnings</h1>
<p>During my PhD, I spent a lot of time learning R and applying it to my research. That was also a period when I had an opportunity to teach several courses, and some of them, including Geostatistics, where heavily based on R and its spatial components.</p>
<p>I had met Robin Lovelace in June 2016. He was in Poznan, Poland to give a short course on spatial data analysis in R for undergraduate and graduate students at the Adam Mickiewicz University, while at that time I was just a few weeks before my PhD thesis defense. We spent some time exchanging our own experiences about spatial R and our approaches to teaching it. During this talks, we found that we shared an idea of writing a book. A book explaining how to use R for spatial data analysis, where the main audience is ourselves just a few years before.</p>
<p>Less than a year later, we have signed a contract and formally started writing the Geocomputation with R book (aka #geocompr). You can read about it more in <a href="../../posts/2017-08-10-geocompr/">my first blog post about the book</a>.</p>
</section>
<section id="geocompr-team" class="level1">
<h1>Geocompr team</h1>
<p>A few months into making the book, Robin and I were contacted by a person we never met before, a postdoc at Friedrich-Schiller-University Jena and an author of <a href="https://cran.r-project.org/web/packages/RQGIS/index.html">the RQGIS package</a>. The ‘first contact’ seems to have been in the <a href="https://github.com/jannes-m/RQGIS/issues/66#issuecomment-304695465">RQGIS</a> issue tracker, where Robin and I commented saying that we’d like to mention the package in the book, hoping for feedback. We ended up with much more than that!</p>
<p>Jannes Muenchow, the mysterious figure mentioned above, firstly gave us a concise idea about a chapter showing how to bridge R with traditional geographic information system software and next he wrote a high-quality piece of text and code. You can read about it more in <a href="../../posts/2018-01-20-geocomputation-with-r-the-intermission/">a blog post about from about a year ago</a>.</p>
</section>
<section id="international-collaboration" class="level1">
<h1>International collaboration</h1>
<p>An interesting feature of working on Geocomputation with R was that it was coordinated remotely across 3 countries and between 3 people who had never previously worked together. We worked as a very close team though, using <a href="https://github.com/Robinlovelace/geocompr">GitHub</a> to track our progress, g<a href="https://gitter.im/">itter</a> for almost daily communication, and <a href="https://www.skype.com/en/">Skype</a> to have a group chat every several weeks. The whole team finally met during <a href="http://geostat-course.org/2018">the GEOSTAT 2018 summer school</a>.</p>
<p><img src="https://jakubnowosad.com/posts/2018-12-12-geocomputation-with-r-the-afterword/figs/authors-in-czech-republic.jpg" class="img-fluid"></p>
</section>
<section id="the-book-structure" class="level1">
<h1>The book structure</h1>
<p>Our ideas about the geocompr structure evolved over time. Finally, we ended up with 15 chapters divided into three parts.</p>
<p>The first part is <a href="https://geocompr.robinlovelace.net/spatial-class.html">Foundations</a>, which focuses on information needed to get a firm grasp on reading and writing spatial data, its representations, and how to operate it. It is aimed at intermediate-to-advanced R users interested in spatial data analysis and R beginners who have prior experience with geographic data.</p>
<p>The second part is <a href="https://geocompr.robinlovelace.net/adv-map.html">Extensions</a>. These chapters are aimed at people who feel relatively confident about spatial R and wants to extend their skills to new areas. It shows how to make static and interactive maps, connect R to GIS software, write spatial functions, and apply robust machine learning methods on spatial data.</p>
<p>The third part is A<a href="https://geocompr.robinlovelace.net/transport.html">Applications</a> In these chapters, we show how spatial R skills can be used to solve practical problems: how to generate strong evidence for investment in sustainable transport in cities? How to locate stores most effectively using open access data? And how to spatially predict the spatial distribution of plants while accounting for spatial autocorrelation? Each chapter is based on a concrete case study using real data from England, Germany, and Peru, to demonstrate how the methods are applicable to a wide range of problems across the world.</p>
</section>
<section id="additional-materials" class="level1">
<h1>Additional materials</h1>
<p>As a bonus, there is much extra material, providing additional depth in particular topic areas and solutions to the exercises at the end of each chapter. These can all be found at <a href="https://geocompr.github.io/">geocompr.github.io/</a>, which contains links to presentations, <a href="https://geocompr.github.io/geocompkg/articles/">articles</a> and more content related to Geocomputation with R. Of particular interest may be <a href="https://geocompr.github.io/workshops/">the geocompr workshop at eRum 2018</a> and an extended article on <a href="https://geocompr.github.io/geocompkg/articles/tidyverse-pitfalls.html">geographic data and the tidyverse: pitfalls to avoid</a>.</p>
<p>Key features of the book are exercises at the end of each chapter. Solutions to these can be found as <a href="https://geocompr.github.io/geocompkg/articles/">vignettes</a> in our metapackage <strong>geocompkg</strong>. It is also a place for extended examples for several methods and functions mentioned in Geocomputation with R, and other miscellaneous materials related to Geocomputation with R.</p>
</section>
<section id="community" class="level1">
<h1>Community</h1>
<p>The book has been greatly improved by countless interactions with members of the wider R/geo communities. 32 people (including ourselves) have so far contributed directly via pull requests (see the <a href="https://github.com/Robinlovelace/geocompr/graphs/contributors">contributors page on GitHub</a>). Even the cover was provided by the community: we launched an open book cover image competition, which was won by <a href="https://twitter.com/msciain">Marco Sciaini</a><sup>1</sup>.</p>
<blockquote class="twitter-tweet blockquote">
<p lang="en" dir="ltr">
Our book needs a cover! We start a front cover image competition for the Geocomputation with <a href="https://twitter.com/hashtag/rstats?src=hash&amp;ref_src=twsrc%5Etfw">#rstats</a> book. Prize: $150 CRC Press book voucher. Ideas/code/graphics are welcome. We accept them at nowosad.jakub@gmail.com until the end of Friday <a href="https://twitter.com/hashtag/geocompr?src=hash&amp;ref_src=twsrc%5Etfw">#geocompr</a> <a href="https://twitter.com/hashtag/rspatial?src=hash&amp;ref_src=twsrc%5Etfw">#rspatial</a> <a href="https://t.co/ejwqMinWND">pic.twitter.com/ejwqMinWND</a>
</p>
— Jakub Nowosad (<span class="citation" data-cites="jakub_nowosad">@jakub_nowosad</span>) <a href="https://twitter.com/jakub_nowosad/status/1031565482397196289?ref_src=twsrc%5Etfw">August 20, 2018</a>
</blockquote>
<script async="" src="https://platform.twitter.com/widgets.js" charset="utf-8"></script>
<p>A vital part of any rigorous work is a review. Our publisher CRC Press organized 4 rounds of technical review, for each of the book’s 3 parts, and then a final round for the last chapters. These are documented in the following GitHub issues:</p>
<ol type="1">
<li>The first review process, for (old) chapters 1 to 5 (<a href="https://github.com/Robinlovelace/geocompr/issues/222">#222</a>)</li>
<li>The second round of reviews, for (old) chapters 5, 7 and 8 (<a href="https://github.com/Robinlovelace/geocompr/issues/221">#221</a>)</li>
<li>The third round of reviews, for (old) chapters 9, 11 and 13 (<a href="https://github.com/Robinlovelace/geocompr/issues/244">#244</a>, with 95 specific issues addressed!)</li>
<li>The fourth and final round of reviews for the remaining chapters (<a href="https://github.com/Robinlovelace/geocompr/issues/302">#302</a>)</li>
</ol>
<p>As can be seen from the reviewer comments, and our responses to them, the technical review process had a major impact on the book. For anyone else writing a book: we recommend you implement a review process, even if it is an internal review process, as illustrated by our <a href="https://github.com/robinlovelace/geocompr/issues?utf8=%E2%9C%93&amp;q=is%3Aissue+review">internal review</a> of the geomarketing chapter. We think putting reviews out there in the open has advantages from learning and accountability perspectives, similar to those achieved by rOpenSci’s peer review process (see <a href="https://github.com/ropensci/software-review/issues">github.com/ropensci/software-review</a>).</p>
<p>Our work was also heavily influenced by important figures in the community, including Roger Bivand (who wrote the book’s <a href="https://geocompr.robinlovelace.net/foreword.html">foreword</a>), Edzer Pebesma, Robert Hijmans, just to mentioned a few. We heartily thank more people, and the community as a whole, in the <a href="https://geocompr.robinlovelace.net/preface.html#acknowledgements">acknowledgments</a>. Equally, we try to contribute to the community. This is documented in a list of <a href="https://github.com/Robinlovelace/geocompr/blob/master/our-impact.csv">contributions including pointing issue reports and pull requests that resulted from the book</a>.</p>
</section>
<section id="where-to-find-the-book" class="level1">
<h1>Where to find the book</h1>
<p>Geocomputation with R is hosted by netlify at <a href="https://geocompr.robinlovelace.net/">geocompr.robinlovelace.net/</a>, which is free for anyone around the world to use non-commercially, under the terms of the <a href="https://creativecommons.org/licenses/by-nc-nd/4.0/">Creative Commons Attribution-NonCommercial-NoDerivatives 4.0 International License</a>. Nothing beats a physical book though, and a hardback copy of the book will be published in March 2019. Pre-order it in hardback from <a href="https://www.crcpress.com/Geocomputation-with-R/Lovelace-Nowosad-Munchow/p/book/9781138304512">CRC Press</a>, <a href="https://www.amazon.com/Geocomputation-Chapman-Hall-Robin-Lovelace/dp/1138304514/%22%3EAmazon.com">Amazon.com</a>, <a href="https://www.amazon.co.uk/Geocomputation-Chapman-Hall-Robin-Lovelace/dp/1138304514/%22%3EAmazon.co.uk">Amazon.co.uk</a>.</p>
<p>Overall, writing this book has been an amazing experience. All three authors, and I think many others, have learned a huge amount, and advocate other would-be authors to see what is possible using new technologies such as <strong>bookdown</strong> (and the <a href="https://bookdown.org/">accompanying website which hosts Geocomputation with R alongside other open source books</a>) and GitHub/GitLab. Removing barriers to information, especially teaching materials on how to use open source software, is an important task for progress. We hope that Geocomputation with R contributes to this objective and encourages others to get involved.</p>


</section>


<div id="quarto-appendix" class="default"><section id="footnotes" class="footnotes footnotes-end-of-document"><h2 class="anchored quarto-appendix-heading">Footnotes</h2>

<ol>
<li id="fn1"><p>The creation of book cover image is fully reproducible. The code can be found at https://github.com/Robinlovelace/geocompr/blob/master/code/frontcover.R.↩︎</p></li>
</ol>
</section><section class="quarto-appendix-contents" id="quarto-reuse"><h2 class="anchored quarto-appendix-heading">Reuse</h2><div class="quarto-appendix-contents"><div><a rel="license" href="https://creativecommons.org/licenses/by/4.0/">CC BY 4.0</a></div></div></section><section class="quarto-appendix-contents" id="quarto-citation"><h2 class="anchored quarto-appendix-heading">Citation</h2><div><div class="quarto-appendix-secondary-label">BibTeX citation:</div><div class="code-copy-outer-scaffold"><pre class="sourceCode quarto-appendix-bibtex code-with-copy"><code class="sourceCode bibtex">@online{nowosad2018,
  author = {Nowosad, Jakub},
  title = {Geocomputation with {R} - the Afterword},
  date = {2018-12-12},
  url = {https://jakubnowosad.com/posts/2018-12-12-geocomputation-with-r-the-afterword/},
  doi = {10.59350/ykdfg-qd143},
  langid = {en}
}
</code></pre></div><div class="quarto-appendix-secondary-label">For attribution, please cite this work as:</div><div id="ref-nowosad2018" class="csl-entry quarto-appendix-citeas">
Nowosad, Jakub. 2018. <span>“Geocomputation with R - the
Afterword.”</span> December 12. <a href="https://doi.org/10.59350/ykdfg-qd143">https://doi.org/10.59350/ykdfg-qd143</a>.
</div></div></section></div> ]]></description>
  <category>rstats</category>
  <category>spatial</category>
  <category>book</category>
  <category>geocompr</category>
  <guid>https://jakubnowosad.com/posts/2018-12-12-geocomputation-with-r-the-afterword/</guid>
  <pubDate>Wed, 12 Dec 2018 00:00:00 GMT</pubDate>
</item>
<item>
  <title>How much our maps are distorted?</title>
  <dc:creator>Jakub Nowosad</dc:creator>
  <link>https://jakubnowosad.com/posts/2018-10-23-map-distortion/</link>
  <description><![CDATA[ 





<p>Last week <a href="https://twitter.com/neilrkaye">Neil Kaye</a> tweeted:</p>
<blockquote class="twitter-tweet blockquote">
<p lang="en" dir="ltr">
Animating the Mercator projection to the true size of each country in relation to all the others.<br><br>Focusing on a single country helps to see effect best.<a href="https://twitter.com/hashtag/dataviz?src=hash&amp;ref_src=twsrc%5Etfw">#dataviz</a> <a href="https://twitter.com/hashtag/maps?src=hash&amp;ref_src=twsrc%5Etfw">#maps</a> <a href="https://twitter.com/hashtag/GIS?src=hash&amp;ref_src=twsrc%5Etfw">#GIS</a> <a href="https://twitter.com/hashtag/projectionmapping?src=hash&amp;ref_src=twsrc%5Etfw">#projectionmapping</a> <a href="https://twitter.com/hashtag/mapping?src=hash&amp;ref_src=twsrc%5Etfw">#mapping</a> <a href="https://t.co/clpCiluS1z">pic.twitter.com/clpCiluS1z</a>
</p>
— Neil Kaye (<span class="citation" data-cites="neilrkaye">@neilrkaye</span>) <a href="https://twitter.com/neilrkaye/status/1050740679008296967?ref_src=twsrc%5Etfw">October 12, 2018</a>
</blockquote>
<script async="" src="https://platform.twitter.com/widgets.js" charset="utf-8"></script>
<p>This, of course, provoked me to ask: is it reproduclible? And more specifically, can it be reproduced in the open source statistical programming language, R?</p>
<p>This post, building on experiences working on the open source and reproducible book project <a href="http://robinlovelace.net/geocompr/">Geocomputation with R</a>, answers both questions with a definitive YES. What follows is a description of how this can be done and, importantly, reproducible code that can be modified to check the distortion of any given projection.</p>
<section id="prepare" class="level2">
<h2 class="anchored" data-anchor-id="prepare">Prepare</h2>
<p>Let’s start with installing the necessary packages. We can install the packages hosted on CRAN as follows (note: these and other geographic packages can be installed via the <strong>geocompkg</strong> ‘metapackage’ with <code>devtools::install_github("geocompr/geocompkg")</code>):</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1">pkgs <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(</span>
<span id="cb1-2">  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"devtools"</span>,       <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># for install_github</span></span>
<span id="cb1-3">  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"sf"</span>,             <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># spatial data classes</span></span>
<span id="cb1-4">  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"rnaturalearth"</span>,  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># world map data</span></span>
<span id="cb1-5">  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"dplyr"</span>,          <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># data manipulation</span></span>
<span id="cb1-6">  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"purrr"</span>,          <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># data manipulation</span></span>
<span id="cb1-7">  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ggplot2"</span>         <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># visualisation</span></span>
<span id="cb1-8">)</span>
<span id="cb1-9">to_install <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">!</span>pkgs <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%in%</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">installed.packages</span>()</span>
<span id="cb1-10"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">any</span>(to_install)) {</span>
<span id="cb1-11">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">install.packages</span>(pkgs[to_install])</span>
<span id="cb1-12">}</span></code></pre></div></div>
</div>
<p>In the case of <strong>gganimate</strong> and <strong>transformr</strong> the packages must be installed with <code>install_github()</code> or equivalent because they are not (yet) on CRAN. This can be done as follows:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb2-1">devtools<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">install_github</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"thomasp85/gganimate"</span>)</span>
<span id="cb2-2">devtools<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">install_github</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"thomasp85/transformr"</span>)</span></code></pre></div></div>
</div>
<p>Now you have the necessary packages, the next step is to attach (aka load) the ones we’ll use:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb3-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(sf)             </span>
<span id="cb3-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(rnaturalearth)  </span>
<span id="cb3-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(dplyr)          </span>
<span id="cb3-4"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(purrr)          </span>
<span id="cb3-5"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(ggplot2)        </span>
<span id="cb3-6"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(gganimate)      </span></code></pre></div></div>
</div>
</section>
<section id="data" class="level2">
<h2 class="anchored" data-anchor-id="data">Data</h2>
<p>Firstly, we need to get the data. The simplest way is to download the map of World’s countries from the <a href="https://www.naturalearthdata.com/">Natural Earth</a> project, for example using the <strong>rnaturalearth</strong> package:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb4-1">world_orig <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ne_countries</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">returnclass =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"sf"</span>)</span></code></pre></div></div>
</div>
<p>We are only interested in the geometries of each country. The <code>select()</code> function is used from the <strong>dplyr</strong> package to remove all of the columns except those that are needed (the geometry column is kept because the geometry column is ‘sticky’):</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb5-1">world_sel <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> world_orig <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb5-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">select</span>()</span></code></pre></div></div>
</div>
<p>An additional issue is that some countries consist of multiple islands, some of which can be very far from the capital. For example, the territory of the French Republic includes overseas territories such as French Guiana in South America. <code>sf</code> objects deal with this with the <code>MULTIPOLYGON</code> geometry type: each feature can have multiple polygons, as described in <a href="https://geocompr.robinlovelace.net/spatial-class.html#sfg">Chapter 2</a> of Geocomputation with R. To check the distortions on the map, each area needs to be treated independently. To do this we will ‘cast’ the <code>world</code> object into single polygons using the <code>st_cast()</code> function:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb6" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb6-1">world_poly <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> world_sel <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb6-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">st_cast</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"POLYGON"</span>)</span></code></pre></div></div>
</div>
<p>The last step of data preparation is to transform the object into a new coordinate reference system (CRS), allowing us to visualise area distortions. In my case the projection of interest is Web Mercator (<a href="https://en.wikipedia.org/wiki/Spatial_reference_system#Identifiers">EPSG code</a> of 3857):</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb7" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb7-1">world <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> world_poly <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb7-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">st_transform</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">crs =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3857</span>)</span></code></pre></div></div>
</div>
<p>However, you can modify it! You could try a different one, for example, <code>crs = 4326</code>.</p>
</section>
<section id="ground-truth" class="level2">
<h2 class="anchored" data-anchor-id="ground-truth">“Ground truth”</h2>
<p>To see how much area we distort using a given projection, we need to know how large it actually is. We can calculate it using one of the equal-area projections, such as the Mollweide projection (<code>+proj=moll</code>), using the <code>st_transform()</code> and <code>st_area()</code> functions:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb8" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb8-1">world_areas <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> world <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb8-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">st_transform</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">crs =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"+proj=moll"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb8-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">st_area</span>()</span></code></pre></div></div>
</div>
</section>
<section id="calculate-the-distortions" class="level2">
<h2 class="anchored" data-anchor-id="calculate-the-distortions">Calculate the distortions</h2>
<p>Next, we need to know how large each area in our projection is. A tiny trick can be helpful here. We can set our projection to <code>NA</code> and this will cause the <code>st_area()</code> function to calculate areas based on map units, instead of the “real” ones:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb9" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb9-1">map_areas <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> world <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb9-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">st_set_crs</span>(<span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NA</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb9-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">st_area</span>()</span></code></pre></div></div>
</div>
<p>Now we have all the pieces and can calculate the distortions using a simple equation - <code>1 / (sqrt(MAP_AREAS, TRUE_AREAS))</code>. To keep all the values to be equal to or lower than 1, we can also scale them using the maximum value:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb10" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb10-1">world_scaled <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> world <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb10-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">scale =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> (<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sqrt</span>(map_areas <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> world_areas))) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb10-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">scale =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.numeric</span>(scale <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">max</span>(scale)))</span></code></pre></div></div>
</div>
<p>The result is a good approximation of the relations between true and distorted areas.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb11" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb11-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">summary</span>(world_scaled<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>scale)</span></code></pre></div></div>
</div>
<p>We see that some areas stay the same (values of 1), but some are heavily distorted. Some are even enlarged in our projection more than six times compared with their actual sizes!</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb12" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb12-1"><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">min</span>(world_scaled<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>scale)</span></code></pre></div></div>
</div>
</section>
<section id="map-transformation" class="level2">
<h2 class="anchored" data-anchor-id="map-transformation">Map transformation</h2>
<p>We have now prepared the data and should be ready to create an animated map. But how can we animate changes in size? We can use affine transformations! To help here we will define a new function that scales geometries relative to a centroid and a scaling factor:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb13" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb13-1">scaler <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(x, y, z) {</span>
<span id="cb13-2">  (x <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> z) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> y <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> z</span>
<span id="cb13-3">}</span></code></pre></div></div>
</div>
<p>The previous code chunk creates a new function called <code>scaler</code>, that accepts three arguments:</p>
<ul>
<li><code>x</code>, a map geometry</li>
<li><code>y</code>, a scaling factor</li>
<li><code>z</code>, a geometry centroid</li>
</ul>
<p>For more on the topic, we recommend checking-out the <a href="https://geocompr.robinlovelace.net/geometric-operations.html#affine-transformations">Affine transformations</a> section of Geocomputation with R. To generate the centroids used by <code>scaler</code> we will create them with the <strong>sf</strong> function <code>st_geometry()</code>:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb14" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb14-1">world_geom <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">st_geometry</span>(world) </span>
<span id="cb14-2">world_center <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">st_centroid</span>(world_geom)</span></code></pre></div></div>
</div>
<p>Now, we can apply our new function. This can be done with <code>pmap</code>, which accepts a list of our arguments and a function as inputs. Next, we convert the results back to a spatial object:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb15" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb15-1">world_transf <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">pmap</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(world_geom, world_scaled<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>scale, world_center), scaler) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb15-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">st_sfc</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">crs =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">st_crs</span>(world)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb15-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">st_sf</span>()</span></code></pre></div></div>
</div>
<p>Note: it is good practice to check intermediary results by plotting them:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb16" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb16-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">plot</span>(world)</span>
<span id="cb16-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">plot</span>(world_transf)</span></code></pre></div></div>
</div>
<p><img src="https://jakubnowosad.com/posts/2018-10-23-map-distortion/figs/world_plot.png" class="img-fluid"></p>
</section>
<section id="combine-two-maps" class="level2">
<h2 class="anchored" data-anchor-id="combine-two-maps">Combine two maps</h2>
<p>To create an animation, our two objects should have unique ids, and be combined into a single object:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb17" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb17-1">world<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>state <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span></span>
<span id="cb17-2">world_transf<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>state <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span></span>
<span id="cb17-3">worlds <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rbind</span>(world, world_transf)</span></code></pre></div></div>
</div>
</section>
<section id="animate" class="level2">
<h2 class="anchored" data-anchor-id="animate">Animate</h2>
<p>Finally, we can create an animation, for example using the <strong>gganimate</strong> package. The animation is built using a few functions:</p>
<ul>
<li><code>ggplot()</code> to initialize the animation</li>
<li><code>geom_sf()</code> to plot our maps (and change a color of countries)</li>
<li><code>transition_states()</code> to set that our transition will be based on the <code>state</code> variable</li>
<li><code>ease_aes()</code> to control the animation</li>
</ul>
<p>We can render it with the <code>animate()</code> function and save using <code>anim_save()</code>.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb18" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb18-1">worlds_anim <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb18-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_sf</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> worlds, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"grey50"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb18-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">transition_states</span>(state, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">transition_length =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">state_length =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb18-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ease_aes</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"cubic-in-out"</span>)</span>
<span id="cb18-5"></span>
<span id="cb18-6">worlds_animate <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">animate</span>(worlds_anim, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">nframes =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">20</span>)</span>
<span id="cb18-7"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">anim_save</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"worlds_animate.gif"</span>, worlds_animate)</span></code></pre></div></div>
</div>
<p>Now, we can enjoy our result:</p>
<p><img src="https://jakubnowosad.com/posts/2018-10-23-map-distortion/figs/worlds_animate.gif" class="img-fluid"></p>
</section>
<section id="final-words" class="level2">
<h2 class="anchored" data-anchor-id="final-words">Final words</h2>
<p>This post has shown not only how to make fun animated maps that demonstrate an important geographic concept (map distortion). It has also demonstrated the ability of the R programming language to do it in a way that is reproducible, allowing others to modify and build-on the work (exercise for the reader: use the code to demonstrate distortions caused by other CRSs).</p>
<p>Reproducibility is vital for scientific research and was a major motivation behind the <em>Geocomputation with R</em> book that has been mentioned throughout this post. If you’re interested in some of the other reasons for doing geographic research with R, check-out <a href="https://geocompr.robinlovelace.net/intro.html">Chapter 1</a>. Now is a great time to check-out and get involved-in the book project because, for the first time, we can say that the content is 100% complete!</p>
<p>The animated maps presented here and in my <a href="https://nowosad.github.io/post/life-expectancy-animated/">previous post</a> are therefore just some examples of the power of geographic data processing with R. For more examples, please head over to the book’s <a href="http://geocompr.robinlovelace.net/">website</a>, check out the extended articles at <a href="https://geocompr.github.io/">geocompr.github.io/</a> and see the project’s source code at <a href="https://github.com/robinlovelace/geocompr">github.com/robinlovelace/geocompr</a>.</p>


</section>

<div id="quarto-appendix" class="default"><section class="quarto-appendix-contents" id="quarto-reuse"><h2 class="anchored quarto-appendix-heading">Reuse</h2><div class="quarto-appendix-contents"><div><a rel="license" href="https://creativecommons.org/licenses/by/4.0/">CC BY 4.0</a></div></div></section><section class="quarto-appendix-contents" id="quarto-citation"><h2 class="anchored quarto-appendix-heading">Citation</h2><div><div class="quarto-appendix-secondary-label">BibTeX citation:</div><div class="code-copy-outer-scaffold"><pre class="sourceCode quarto-appendix-bibtex code-with-copy"><code class="sourceCode bibtex">@online{nowosad2018,
  author = {Nowosad, Jakub},
  title = {How Much Our Maps Are Distorted?},
  date = {2018-10-16},
  url = {https://jakubnowosad.com/posts/2018-10-23-map-distortion/},
  doi = {10.59350/5ctbt-bd511},
  langid = {en}
}
</code></pre></div><div class="quarto-appendix-secondary-label">For attribution, please cite this work as:</div><div id="ref-nowosad2018" class="csl-entry quarto-appendix-citeas">
Nowosad, Jakub. 2018. <span>“How Much Our Maps Are Distorted?”</span>
October 16. <a href="https://doi.org/10.59350/5ctbt-bd511">https://doi.org/10.59350/5ctbt-bd511</a>.
</div></div></section></div> ]]></description>
  <category>spatial</category>
  <category>geocompr</category>
  <category>rstats</category>
  <category>vis</category>
  <guid>https://jakubnowosad.com/posts/2018-10-23-map-distortion/</guid>
  <pubDate>Tue, 16 Oct 2018 00:00:00 GMT</pubDate>
</item>
<item>
  <title>The world (population) is changing</title>
  <dc:creator>Jakub Nowosad</dc:creator>
  <link>https://jakubnowosad.com/posts/2018-10-09-world_pop_change/</link>
  <description><![CDATA[ 





<p>Last month, <a href="https://twitter.com/MaxCRoser">Max Roser</a> presented a <a href="https://ourworldindata.org/wp-content/uploads/2018/09/Population-cartogram_World-1.png">cartogram</a> of the Earth’s population in 2018. He also provided some perspectives on its spatial distribution in an article on the <a href="https://ourworldindata.org/world-population-cartogram">worldinourdata.org</a>, which I recommend. Links to the article were shared in many places, including in the blog post <a href="https://kottke.org/18/09/a-map-of-the-world-where-the-sizes-of-countries-are-determined-by-population">A Map of the World Where the Sizes of Countries Are Determined by Population</a>. The author, <a href="https://twitter.com/jkottke">Jason Kottke</a>, concluded with a wish:</p>
<blockquote class="blockquote">
<p>“I would love to see an animated version of this cartogram from like 1950 to 2100”.</p>
</blockquote>
<p>Why not from 1800 to 2100? And why not do it using open data and open source software, so that anyone can reproduce the results? That is the aim of this post. In it, we will see how to generate the animated cartogram illustrated below.</p>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://jakubnowosad.com/posts/2018-10-09-world_pop_change/figs/world_pop_1800_2100.gif" class="img-fluid figure-img"></p>
<figcaption>Animated cartogram of the world population changes between 1800 and 2100.</figcaption>
</figure>
</div>
<p>The above animation was created using open data (from <a href="https://www.gapminder.org">gapminder.org</a>) and open source software (R, which can be downloaded from <a href="https://cloud.r-project.org/">r-project.org</a>). This means not only that you can reproduce the results. You can also change (for example to create yearly changes instead of changes every 25 years) and potentially improve them.</p>
<p>Described in the book <a href="https://geocompr.robinlovelace.net/">Geocomputation with R</a>, which helped inspire this post, this is how open source software works: someone writes code that does something useful and the community contributes to make it even better.</p>
<p>The rest of this post is more focused on the code. I will explain how the steps involved in making the above animation, and, in the final section, link to more comprehensive teaching material that will teach not only how to make a multitude of other types of maps in R, but also how to ensure the results are reproducible.</p>
<p>The first stage is to prepare the data.</p>
<section id="prepare" class="level2">
<h2 class="anchored" data-anchor-id="prepare">Prepare</h2>
<p>Let’s start with the packages. If you are new to R, you may want to take a read of <a href="https://geocompr.robinlovelace.net/spatial-class.html">this</a> first, which points to various resources for setting-up R for geographic data. When you have a recent R version and the appropriate packages installed (e.g.&nbsp;by executing <code>devtools::install_github("geocompr/geocompkg")</code>) the packages can be attached as follows:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(sf)             <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># spatial data classes</span></span>
<span id="cb1-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(rnaturalearth)  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># world map data</span></span>
<span id="cb1-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(readxl)         <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># reading excel files</span></span>
<span id="cb1-4"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(dplyr)          <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># data manipulation</span></span>
<span id="cb1-5"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(tidyr)          <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># data manipulation</span></span>
<span id="cb1-6"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(purrr)          <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># data manipulation</span></span>
<span id="cb1-7"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(cartogram)      <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># cartograms creation</span></span>
<span id="cb1-8"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(tmap)           <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># maps creation</span></span></code></pre></div></div>
</div>
</section>
<section id="data" class="level2">
<h2 class="anchored" data-anchor-id="data">Data</h2>
<p>To create cartograms of the world population for each year we will need two datasets - one containing spatial data of the world’s countries and one non-spatial with information about the annual population in the world’s countries. The first one can be easily downloaded from the <a href="http://www.naturalearthdata.com/">Natural Earth</a> website, for example using the <strong>rnaturalearth</strong> package:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb2-1">world_map <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ne_countries</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">returnclass =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"sf"</span>)</span></code></pre></div></div>
</div>
<p>The second one is available from the <a href="https://www.gapminder.org/">Gapminder</a> foundation. Gapminder provides <a href="https://www.gapminder.org/data/documentation/gd003/">a dataset with population data</a> for all countries and world regions from 1800 to 2100. We can download and read the dataset using the code below:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb3-1"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">!</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">dir.exists</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"data"</span>)) <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">dir.create</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"data"</span>)</span>
<span id="cb3-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">download.file</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"http://gapm.io/dl_pop"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">destfile =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"data/pop1800_2100.xlsx"</span>)</span>
<span id="cb3-3">world_pop <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">read_xlsx</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"data/pop1800_2100.xlsx"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sheet =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">7</span>)</span></code></pre></div></div>
</div>
</section>
<section id="cleaning" class="level2">
<h2 class="anchored" data-anchor-id="cleaning">Cleaning</h2>
<p>As always when working with multiple datasets - some data cleaning will be necessary. Our <code>world_map</code> dataset has many columns unnecessary for cartograms creation and we do not need spatial data of Antarctica. Let’s get rid of them. we can also transform our data into a more appropriate projection<sup>1</sup>.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb4-1">world_map <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> world_map <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb4-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">select</span>(sovereignt) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb4-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</span>(sovereignt <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">!=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Antarctica"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb4-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">st_transform</span>(world_map, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">crs =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"+proj=robin"</span>)</span></code></pre></div></div>
</div>
<p>We need to have a common identifier to combine our spatial and non-spatial datasets, for example, names of the countries. However, there are inconsistencies between some of the names. We need to fix it manually:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb5-1">world_pop <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> world_pop <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb5-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sovereignt =</span> name) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb5-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sovereignt =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">replace</span>(sovereignt, sovereignt <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Tanzania"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"United Republic of Tanzania"</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb5-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sovereignt =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">replace</span>(sovereignt, sovereignt <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"United States"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"United States of America"</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb5-5">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sovereignt =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">replace</span>(sovereignt, sovereignt <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Congo, Dem. Rep."</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Democratic Republic of the Congo"</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb5-6">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sovereignt =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">replace</span>(sovereignt, sovereignt <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Bahamas"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"The Bahamas"</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb5-7">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sovereignt =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">replace</span>(sovereignt, sovereignt <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Serbia"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Republic of Serbia"</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb5-8">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sovereignt =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">replace</span>(sovereignt, sovereignt <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Macedonia, FYR"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Macedonia"</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb5-9">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sovereignt =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">replace</span>(sovereignt, sovereignt <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Slovak Republic"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Slovakia"</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb5-10">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sovereignt =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">replace</span>(sovereignt, sovereignt <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Czech Republic"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Czechia"</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb5-11">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sovereignt =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">replace</span>(sovereignt, sovereignt <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Congo, Rep."</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Republic of the Congo"</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb5-12">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sovereignt =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">replace</span>(sovereignt, sovereignt <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Kyrgyz Republic"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Kyrgyzstan"</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb5-13">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sovereignt =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">replace</span>(sovereignt, sovereignt <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Lao"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Laos"</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb5-14">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sovereignt =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">replace</span>(sovereignt, sovereignt <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Swaziland"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"eSwatini"</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb5-15">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sovereignt =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">replace</span>(sovereignt, sovereignt <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Cote d'Ivoire"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Ivory Coast"</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb5-16">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sovereignt =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">replace</span>(sovereignt, sovereignt <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Timor-Leste"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"East Timor"</span>))</span></code></pre></div></div>
</div>
</section>
<section id="preparing" class="level2">
<h2 class="anchored" data-anchor-id="preparing">Preparing</h2>
<p>Now we can join our two datasets, remove missing values and unimportant columns. We also should transform the data into a long format<sup>2</sup> accepted by the <strong>tmap</strong> package.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb6" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb6-1">world_data <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">left_join</span>(world_map, world_pop, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">by =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"sovereignt"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb6-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">na.omit</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb6-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">select</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>geo, <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>name, <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>indicator) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb6-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">gather</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">key =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"year"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">value =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"population"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">`</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">1800.0</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">`</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">`</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">2100.0</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">`</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb6-5">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">year =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.integer</span>(year)) </span></code></pre></div></div>
</div>
<p>Additionally, we can calculate total global populations in each year:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb7" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb7-1">world_data <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> world_data <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb7-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">group_by</span>(year) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb7-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">total_pop =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sum</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.numeric</span>(population), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">na.rm =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb7-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">title =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">paste0</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Year: "</span>, year, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">Total population (mln): "</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">round</span>(total_pop<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1e6</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>)))</span></code></pre></div></div>
</div>
</section>
<section id="subset" class="level2">
<h2 class="anchored" data-anchor-id="subset">Subset</h2>
<p>Now, our data contains information about the world population for each year between 1800 and 2100. It is possible to use it to create cartograms, however, to reduce calculation time and simplification of the results we will only use data for every 25 years:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb8" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb8-1">world_data <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> world_data <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb8-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</span>(year <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%in%</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">seq</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1800</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2100</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">by =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">25</span>))</span></code></pre></div></div>
</div>
</section>
<section id="transforming" class="level2">
<h2 class="anchored" data-anchor-id="transforming">Transforming</h2>
<p>Finally, we are able to create our cartograms. We need to split our data into independent annual datasets, create cartograms based on the <code>population</code> variable, and combine all of the cartograms back into one object.</p>
<p>Before we create cartograms of the world, let’s create a single cartogram:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb9" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb9-1">world_data_2100 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> world_data <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb9-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</span>(year <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2100</span>)</span>
<span id="cb9-3">world_carto1 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">cartogram_cont</span>(world_data_2100, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"population"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">maxSizeError =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.5</span>)</span>
<span id="cb9-4"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">plot</span>(world_carto1[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"population"</span>])</span></code></pre></div></div>
</div>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://jakubnowosad.com/posts/2018-10-09-world_pop_change/figs/world_carto1.png" class="img-fluid figure-img"></p>
<figcaption>A single cartogram of year 2100.</figcaption>
</figure>
</div>
<p>The resulting plot was with the awesome <strong>sf</strong> package, showing what global populations could look like based on recent trends. To make this animated, we need to a) do some more processing (to calculate the cartogram shapes for every year in our sequence) and b) switch to using the awesome <strong>tmap</strong> package for animated map creation. Note: this next step may take a few minutes.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb10" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb10-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># warning: this may make your computer's fan spin!</span></span>
<span id="cb10-2">world_data_carto <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> world_data <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb10-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">split</span>(.<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>year) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb10-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">map</span>(cartogram_cont, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"population"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">maxSizeError =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.5</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb10-5">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">do.call</span>(rbind, .) </span></code></pre></div></div>
</div>
</section>
<section id="visualizing" class="level2">
<h2 class="anchored" data-anchor-id="visualizing">Visualizing</h2>
<p>Animated maps can be created in a single command using the <strong>tmap</strong> package. Below we pass it the data produced in the previous command:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb11" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb11-1">carto_anim <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tm_shape</span>(world_data_carto) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb11-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tm_polygons</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"population"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">title =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Population: "</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb11-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tm_facets</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">along =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"title"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">free.coords =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">drop.units =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>)</span></code></pre></div></div>
</div>
<p>The last step is to save the output object as a .gif file:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb12" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb12-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tmap_animation</span>(carto_anim, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">filename =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"world_pop_1800_2100.gif"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">delay =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">75</span>,</span>
<span id="cb12-2">               <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">width =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1326</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">height =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">942</span>)</span></code></pre></div></div>
</div>
<p>If you enjoyed this tutorial you’re in luck: there’s lots more to learn! In addition to chapters on a range of fundamental geographic subjects, there’s an entire chapter on visualization in the online version of <strong>Geocomputation with R</strong>, which can be found at https://geocompr.robinlovelace.net/ (the physical book will be on sale soon).</p>
</section>
<section id="epilogue" class="level2">
<h2 class="anchored" data-anchor-id="epilogue">Epilogue</h2>
<p>There are several things that should be kept in mind about this animation. Firstly, the animation (as well the Gapminder data) shows the world as if all countries always had the borders they have today. This is, of course, untrue, but this simplification allows for the straightforward creation of the animation as well as for an easier understanding of the results. Secondly, the most time-consuming part of the animation creation, constructing continuous area cartograms, can be accelerated, with the use of simplified geometries of the world countries (for example using the <strong>rmapshaper</strong> package) or with parallel processing (for example using the <strong>future</strong> or <strong>furrr</strong> packages). Thirdly, different time steps can be used - an animation could be created for each year, every five years, etc. And finally, cartograms can be animated using a different R package, for example, <strong>gganimate</strong>, or even combine into an interactive app using <strong>shiny</strong>.</p>


</section>


<div id="quarto-appendix" class="default"><section id="footnotes" class="footnotes footnotes-end-of-document"><h2 class="anchored quarto-appendix-heading">Footnotes</h2>

<ol>
<li id="fn1"><p>You can read more about projections in the <a href="https://geocompr.robinlovelace.net/reproj-geo-data.html">Reprojecting geographic data</a> chapter of Geocomputation with R.↩︎</p></li>
<li id="fn2"><p>This process is called <a href="http://r4ds.had.co.nz/tidy-data.html#gathering">gathering</a>.↩︎</p></li>
</ol>
</section><section class="quarto-appendix-contents" id="quarto-reuse"><h2 class="anchored quarto-appendix-heading">Reuse</h2><div class="quarto-appendix-contents"><div><a rel="license" href="https://creativecommons.org/licenses/by/4.0/">CC BY 4.0</a></div></div></section><section class="quarto-appendix-contents" id="quarto-citation"><h2 class="anchored quarto-appendix-heading">Citation</h2><div><div class="quarto-appendix-secondary-label">BibTeX citation:</div><div class="code-copy-outer-scaffold"><pre class="sourceCode quarto-appendix-bibtex code-with-copy"><code class="sourceCode bibtex">@online{nowosad2018,
  author = {Nowosad, Jakub},
  title = {The World (Population) Is Changing},
  date = {2018-10-09},
  url = {https://jakubnowosad.com/posts/2018-10-09-world_pop_change/},
  doi = {10.59350/e1peg-97x55},
  langid = {en}
}
</code></pre></div><div class="quarto-appendix-secondary-label">For attribution, please cite this work as:</div><div id="ref-nowosad2018" class="csl-entry quarto-appendix-citeas">
Nowosad, Jakub. 2018. <span>“The World (Population) Is Changing.”</span>
October 9. <a href="https://doi.org/10.59350/e1peg-97x55">https://doi.org/10.59350/e1peg-97x55</a>.
</div></div></section></div> ]]></description>
  <category>spatial</category>
  <category>geocompr</category>
  <category>rstats</category>
  <category>vis</category>
  <guid>https://jakubnowosad.com/posts/2018-10-09-world_pop_change/</guid>
  <pubDate>Tue, 09 Oct 2018 00:00:00 GMT</pubDate>
</item>
<item>
  <title>sabre: or how to compare two maps?</title>
  <dc:creator>Jakub Nowosad</dc:creator>
  <link>https://jakubnowosad.com/posts/2018-09-10-sabre-bp/</link>
  <description><![CDATA[ 





<p>Creating or determination of regions is a useful way to describe the world. Regionalization does not only allow for a quicker understanding of spatial patterns but also can influence how regions are managed. Regions are created in various disciplines. We can delineate regions based on a single property (e.g.&nbsp;landform regions or climate regions) or several factors (e.g.&nbsp;ecoregions). There are also political regions divided by borders that are established through political or social agreements. Regions are powerful also because they can be easily visualized.</p>
<p>On the other hand, it is difficult to compare different regionalizations. For example, you have two regionalizations based on the same property but from two different times and you want to know how similar they are to each other and where the largest change had occurred. Another example is when you have two maps of the same property created by two different entities and you want to know where they are in agreement and where they are very different. Comparing different regionalizations also apply when we have two maps of different properties and we want to know is there a spatial relationship between the first regionalization and the second one.</p>
<div class="figure">
<img src="https://jakubnowosad.com/posts/2018-09-10-sabre-bp/figs/03_sabre.png">
<p class="caption">
European countries map (left). Biogeographical regions of Europe (right)
</p>
</div>
<p>The figure above is the simplest way of comparison - a visual approach by plotting two maps side by side. Now you could take a look at the map on the left, decide on some area of interest and next try to locate the same area on the map on the right side. Alternatively, it is possible to use lines to indicate the first regionalization, and color to express the second regionalization:</p>
<div class="figure">
<img src="https://jakubnowosad.com/posts/2018-09-10-sabre-bp/figs/04_sabre.png">
<p class="caption">
Borders of European countries superimposed on the biogeographical regions of Europe
</p>
</div>
<p>This way it is easy to see that the Netherlands lies in only one biogeographic region “Atlantic”, while some countries are in two or more biogeographic regions. There are also several ways to compare regionalization maps interactively, for example using <a href="https://www.rdocumentation.org/packages/tmap/versions/2.1-1/topics/tmap_arrange"><code>tmap::tmap_arrange()</code> in the view mode</a> or the <a href="https://www.rdocumentation.org/packages/mapview/versions/2.5.0/topics/slideView"><code>mapview::slideView()</code></a> function.</p>
<p>There is still one issue though - how to compare two regionalizations quantitatively? In other words - how to calculate a similarity between two regionalizations or categorical maps? For this purpose, we developed a new R package called <strong>sabre</strong>, which stands for <strong>S</strong>patial <strong>A</strong>ssociation <strong>B</strong>etween <strong>RE</strong>egionalizations. You can download it from CRAN:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">install.packages</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"sabre"</span>)</span></code></pre></div></div>
</div>
<p>In this blogpost, we will use only two R packages:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb2-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># attach packages</span></span>
<span id="cb2-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(sf)</span>
<span id="cb2-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(sabre)</span></code></pre></div></div>
</div>
<p>Additionally, <strong>ggplot2</strong> was used to create the maps and <strong>gganimate</strong> to create the animations.</p>
<section id="data" class="level2">
<h2 class="anchored" data-anchor-id="data">Data</h2>
<p>Let’s try this package by using the two datasets mentioned above - the first one consists of counties borders and the second one represents biogeographical regions of Europe. They could be downloaded through R with:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb3-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># data download</span></span>
<span id="cb3-2">url <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"https://github.com/Nowosad/bp-data/raw/main/2018-09-10-sabre-bp/regions.zip"</span></span>
<span id="cb3-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">download.file</span>(url, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">destfile =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"regions.zip"</span>)</span>
<span id="cb3-4"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">unzip</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"regions.zip"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">exdir =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"data"</span>)</span></code></pre></div></div>
</div>
<p>Importantly, <strong>sabre</strong> requires that two input datasets have the same coordinate reference system (CRS). If your datasets have different CRS’s, you can transform one of the datasets to have the same CRS as the second one. To learn more read the section about <a href="https://geocompr.robinlovelace.net/reproj-geo-data.html#reproj-vec-geom">reprojecting vector geometries</a> from the <a href="https://geocompr.robinlovelace.net">Geocomputation with R</a> book.</p>
<p>Next, we just need to read the data:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb4-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># read data</span></span>
<span id="cb4-2">europe_borders <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">st_read</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"data/europe_borders.gpkg"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">quiet =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>)</span>
<span id="cb4-3">biogeo_regions <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">st_read</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"data/biogeo_regions.gpkg"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">quiet =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>)</span></code></pre></div></div>
</div>
<p>Note: the <strong>sabre</strong> package expects data with valid geometries only. You can fix invalid geometries with <code>sf::st_make_valid()</code>.</p>
</section>
<section id="v-measure" class="level2">
<h2 class="anchored" data-anchor-id="v-measure">V-measure</h2>
<p>The main function in <strong>sabre</strong> is <code>vmeasure_calc()</code>. It calculates a degree of spatial association between two regionalizations using an information-theoretical measure called the V-measure. We adapted this measure from computer science, where it is used to compare (non-spatial) clusterings (<a href="http://www.aclweb.org/anthology/D07-1043">Rosenberg and Hirschberg, 2007</a>).</p>
<p>V-measure is built upon two intermediate metrics - homogeneity and completeness. Homogeneity is a measure of how well regions from the first map fit inside of regions from the second map. Completeness measures how well regions from the second map fit inside of regions from the first map. The final value of v-measure is calculated as the weighted harmonic mean of homogeneity and completeness. All of these metrics have values between 0 and 1, where larger values indicate better spatial agreement.</p>
</section>
<section id="comparision-between-two-maps" class="level2">
<h2 class="anchored" data-anchor-id="comparision-between-two-maps">Comparision between two maps</h2>
<p>The <code>vmeasure_calc()</code> function requires four arguments:</p>
<ul>
<li><code>x</code> - a name of spatial object (map 1)</li>
<li><code>y</code> - a name of spatial object (map 2)</li>
<li><code>x_name</code> - a name of the column with regions/clusters names in map 1</li>
<li><code>y_name</code> - a name of the column with regions/clusters names in map 2</li>
</ul>
<p>Let’s apply v-measure to compare our two maps:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb5-1">sabre_output <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">vmeasure_calc</span>(europe_borders, biogeo_regions, name, code)</span></code></pre></div></div>
</div>
<p>The output gives five elements - values of v-measure, homogeneity, and completeness and two maps:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb6" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb6-1">sabre_output</span></code></pre></div></div>
</div>
<pre><code>## The SABRE results:
## 
##  V-measure: 0.47 
##  Homogeneity: 0.38 
##  Completeness: 0.61 
## 
##  The spatial objects could be retrived with:
##  $map1 - the first map
##  $map2 - the second map</code></pre>
<p>V-measure, homogeneity, and completeness are global measures of association between the two regionalizations. Additional two maps indicate local associations by presenting regions’ inhomogeneities (<strong>rih</strong>). The first map shows how inhomogenous are countries with respect to biogeographical regions. For example, inhomogeneity of the Netherlands is zero as the country lies in only one biogeographical region and inhomogeneity of France is 0.63 as the country is divided by four different biogeographical regions. This measure also takes the area of biogeographical regions that cover each country into consideration. Both Poland and Belarus have two biogeographical regions, however in Poland, one region dominates the country area (rih = 0.08) and Belarus is almost equally divided by two regions (rih = 0.38).</p>
<div class="figure">
<img src="https://jakubnowosad.com/posts/2018-09-10-sabre-bp/figs/map21_anim.gif">
<p class="caption">
A map of inhomogeneity of countries in terms of biogeographical regions
</p>
</div>
<p>The second map represents how inhomogenous biogeographical regions are with respect to countries:</p>
<div class="figure">
<img src="https://jakubnowosad.com/posts/2018-09-10-sabre-bp/figs/map22_anim.gif">
<p class="caption">
A map of inhomogeneity of biogeographical regions in terms of countries
</p>
</div>
</section>
<section id="additional-applications" class="level2">
<h2 class="anchored" data-anchor-id="additional-applications">Additional applications</h2>
<p>This blog post focuses on providing the basic understanding of the spatial association between regionalizations. However, this method has a broader spectrum of possible applications, which we divide into three basic groups: comparative, associative, and derivative. In this blog post, we show an example of the comparative context. The associative context is used when you need to asses a degree of correspondence between a map of regions and maps of possible influencing factors. The role of derivative context is to help to select a number of spatial clusters. To learn more about all of these contexts take a look at our paper <a href="https://doi.org/10.1080/13658816.2018.1511794">“Spatial association between regionalizations using the information-theoretical V-measure”</a>. A freely available preprint is at https://eartharxiv.org/rcjh7/.</p>
</section>
<section id="quick-summary" class="level2">
<h2 class="anchored" data-anchor-id="quick-summary">Quick summary</h2>
<p><strong>S</strong>patial <strong>A</strong>ssociation <strong>B</strong>etween <strong>RE</strong>egionalizations, <strong>sabre</strong>, is a spatial method adapted from computer science. It allows for three main types of analysis - comparative (comparison of regions), associative (relation between a regionalization and factor maps), and derivative (obtaining an optimal number of clusters). We implemented this method as an open-source software - an R package called <strong>sabre</strong>. This package allows for comparisons of maps in a form of vector objects. Let us know if you also want a support for raster map using <a href="https://github.com/Nowosad/sabre/issues/3">the GitHub issue</a>. To learn more you can visit <a href="https://nowosad.github.io/sabre/index.html">the package website</a>, read the <a href="http://www.aclweb.org/anthology/D07-1043">original V-measure</a> paper or our article <a href="https://eartharxiv.org/rcjh7/">“Spatial association between regionalizations using the information-theoretical V-measure”</a>.</p>


</section>

<div id="quarto-appendix" class="default"><section class="quarto-appendix-contents" id="quarto-reuse"><h2 class="anchored quarto-appendix-heading">Reuse</h2><div class="quarto-appendix-contents"><div><a rel="license" href="https://creativecommons.org/licenses/by/4.0/">CC BY 4.0</a></div></div></section><section class="quarto-appendix-contents" id="quarto-citation"><h2 class="anchored quarto-appendix-heading">Citation</h2><div><div class="quarto-appendix-secondary-label">BibTeX citation:</div><div class="code-copy-outer-scaffold"><pre class="sourceCode quarto-appendix-bibtex code-with-copy"><code class="sourceCode bibtex">@online{nowosad2018,
  author = {Nowosad, Jakub},
  title = {Sabre: Or How to Compare Two Maps?},
  date = {2018-09-10},
  url = {https://jakubnowosad.com/posts/2018-09-10-sabre-bp/},
  doi = {10.59350/ezt06-rth72},
  langid = {en}
}
</code></pre></div><div class="quarto-appendix-secondary-label">For attribution, please cite this work as:</div><div id="ref-nowosad2018" class="csl-entry quarto-appendix-citeas">
Nowosad, Jakub. 2018. <span>“Sabre: Or How to Compare Two Maps?”</span>
September 10. <a href="https://doi.org/10.59350/ezt06-rth72">https://doi.org/10.59350/ezt06-rth72</a>.
</div></div></section></div> ]]></description>
  <category>spatial</category>
  <category>geocompr</category>
  <category>sil</category>
  <category>rstats</category>
  <guid>https://jakubnowosad.com/posts/2018-09-10-sabre-bp/</guid>
  <pubDate>Mon, 10 Sep 2018 00:00:00 GMT</pubDate>
</item>
<item>
  <title>Moving beyond pattern-based analysis: Additional applications of GeoPAT 2</title>
  <dc:creator>Jakub Nowosad</dc:creator>
  <link>https://jakubnowosad.com/posts/2018-08-28-geopat-2-extend/</link>
  <description><![CDATA[ 





<p>This is the sixth and the last blog post in the series introducing <a href="http://sil.home.amu.edu.pl/index.php?id=geopat2">GeoPAT 2 - a software for pattern-based spatial and temporal analysis</a>. <a href="../../posts/2018-08-13-geopat-2-segmentation/index.html">In the previous one</a> we presented the pattern-based spatial segmentation - a method for creating regions of homogeneous patterns. Here, we will mention other pattern-based methods and show some examples of how you can use pieces of GeoPAT 2 in your own workflow.</p>
<section id="introduction" class="level2">
<h2 class="anchored" data-anchor-id="introduction">Introduction</h2>
<p>GeoPAT 2 gives its users a lot of freedom, having a large number of possible workflows:</p>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://jakubnowosad.com/posts/2018-08-28-geopat-2-extend/figs/extend01.png" class="img-fluid figure-img"></p>
<figcaption>Outline of the GeoPAT 2 architecture</figcaption>
</figure>
</div>
<p>Some of them can consist of only one step, while others require several steps. However, that is not the end of GeoPAT 2 capabilities - it is also possible to extract spatial signature or calculate distance matrix and use these outputs in further analysis outside of GeoPAT 2. For example, you can cluster areas of similar patterns or use machine learning methods to find numerical relationships between patterns descriptors and other variables. Finally, there are several experimental features of GeoPAT 2, including a set of methods for analysis of spatiotemporal data.</p>
<p>We are going to use land cover data of Australia from the year 2015 and <a href="http://maps.tnc.org/gis_data.html">TNC’s terrestrial ecoregions</a> of the same area as examples in this blog post. To follow the subsequent calculations, you need to download these datasets, <a href="https://osf.io/5s37z/download">cci_lc2015.tif</a> and <a href="https://osf.io/ra2ck/download">tnc_terr_ecoregions.tif</a>, and install <a href="http://sil.uc.edu/cms/index.php?id=geopat2">GeoPAT 2</a>. Some examples also use <strong>rgeopat2</strong> - <a href="https://github.com/Nowosad/rgeopat2">an R package with additional functions for GeoPAT 2</a>:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">install.packages</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"rgeopat2"</span>)</span></code></pre></div></div>
</div>
</section>
<section id="extracting-information-about-spatial-patterns" class="level2">
<h2 class="anchored" data-anchor-id="extracting-information-about-spatial-patterns">Extracting information about spatial patterns</h2>
<p>Most of the GeoPAT 2 modules depends on spatial information about patterns in the form of <a href="../../posts/2018-07-06-pattern-based-spatial-analysis-core-ideas/index.html#signature">signatures</a>. We can then <a href="../../posts/2018-07-16-geopat-2-search/index.html">find the most similar signatures (search)</a>, <a href="../../posts/2018-07-24-geopat-2-compare/index.html">compare local signatures (comparison)</a>, or <a href="../../posts/2018-08-13-geopat-2-segmentation/index.html">merge areas with similar signatures (segmentation)</a>. This information can be also used outside of GeoPAT 2 - it is possible to fit a spatial pattern information to your own workflow.</p>
<p>There are three main ways to extract information about spatial patterns. The first one extracts information from a regular grid of motifels. In the example below, we create a grid of motifels with information about the composition of land cover categories (<code>-s prod</code>). Next, using <code>gpat_grid2txt</code> we convert the output grid file into a text file that can be read by standard software, such as R, Python, Excel.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="cb2-1"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">gpat_gridhis</span> <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">-i</span> cci_lc2015.tif <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">-o</span> patterns2015.grd <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">-s</span> prod <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">-z</span> 200 <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">-f</span> 200</span>
<span id="cb2-2"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">gpat_grid2txt</span> <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">-i</span> patterns2015.grd <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">-o</span> patterns2015.txt</span></code></pre></div></div>
</div>
<p>The second one extracts a spatial pattern information based on point coordinates within the given buffer. Below, we extract a land cover composition (Cartesian product) of a square area of 30 by 30 pixels (9km by 9km) around Australia’s capital - Canberra:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="cb3-1"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">gpat_pointshis</span> <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">-i</span> cci_lc2015.tif <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">-o</span> patterns_pts.txt <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">-s</span> prod <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">-z</span> 30 <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">-x</span> 1370000 <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">-y</span> <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">-4050000</span></span></code></pre></div></div>
</div>
<p>The third one involves extracting information about spatial patterns using irregular polygons with <code>gpat_polygon</code>. Let’s say, we are interested in extracting land cover composition in each of TNC’s terrestrial ecoregions in Australia. <em>(Due to large sizes of some ecoregions, the below calculation can require about 16GB RAM available.)</em></p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="cb4-1"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">gpat_polygon</span> <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">-i</span> cci_lc2015.tif <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">-e</span> tnc_terr_ecoregions.tif <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">-o</span> patterns_poly.txt <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">-s</span> prod</span></code></pre></div></div>
</div>
<p>To ease some of the non-standard GeoPAT 2 operations we created an R package called <strong>rgeopat2</strong>.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb5-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(rgeopat2)</span></code></pre></div></div>
</div>
<p>Its function <code>gpat_read_txt()</code> reads the output text from GeoPAT 2 and formats it in a convinent way:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb6" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb6-1">patterns_poly <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">gpat_read_txt</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"patterns_poly.txt"</span>)</span></code></pre></div></div>
</div>
<table class="caption-top table">
<caption>Example of two lines from the gpat_read_txt() output</caption>
<colgroup>
<col style="width: 2%">
<col style="width: 4%">
<col style="width: 4%">
<col style="width: 2%">
<col style="width: 4%">
<col style="width: 4%">
<col style="width: 4%">
<col style="width: 4%">
<col style="width: 4%">
<col style="width: 4%">
<col style="width: 3%">
<col style="width: 4%">
<col style="width: 3%">
<col style="width: 4%">
<col style="width: 4%">
<col style="width: 4%">
<col style="width: 4%">
<col style="width: 3%">
<col style="width: 4%">
<col style="width: 4%">
<col style="width: 4%">
<col style="width: 3%">
<col style="width: 4%">
<col style="width: 5%">
</colgroup>
<thead>
<tr class="header">
<th style="text-align: left;"></th>
<th style="text-align: right;">X1</th>
<th style="text-align: right;">X2</th>
<th style="text-align: right;">X3</th>
<th style="text-align: right;">X4</th>
<th style="text-align: right;">X5</th>
<th style="text-align: right;">X6</th>
<th style="text-align: right;">X7</th>
<th style="text-align: right;">X8</th>
<th style="text-align: right;">X9</th>
<th style="text-align: right;">X10</th>
<th style="text-align: right;">X11</th>
<th style="text-align: right;">X12</th>
<th style="text-align: right;">X13</th>
<th style="text-align: right;">X14</th>
<th style="text-align: right;">X15</th>
<th style="text-align: right;">X16</th>
<th style="text-align: right;">X17</th>
<th style="text-align: right;">X18</th>
<th style="text-align: right;">X19</th>
<th style="text-align: right;">X20</th>
<th style="text-align: right;">X21</th>
<th style="text-align: right;">X22</th>
<th style="text-align: right;">cat</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td style="text-align: left;">1</td>
<td style="text-align: right;">0.00</td>
<td style="text-align: right;">0.00</td>
<td style="text-align: right;">0</td>
<td style="text-align: right;">0.00</td>
<td style="text-align: right;">0.00</td>
<td style="text-align: right;">0.00</td>
<td style="text-align: right;">0.00</td>
<td style="text-align: right;">0.00</td>
<td style="text-align: right;">0.00</td>
<td style="text-align: right;">0</td>
<td style="text-align: right;">0.00</td>
<td style="text-align: right;">0</td>
<td style="text-align: right;">0.00</td>
<td style="text-align: right;">0.00</td>
<td style="text-align: right;">0.00</td>
<td style="text-align: right;">0.00</td>
<td style="text-align: right;">0</td>
<td style="text-align: right;">0.00</td>
<td style="text-align: right;">0.00</td>
<td style="text-align: right;">0.00</td>
<td style="text-align: right;">0</td>
<td style="text-align: right;">1.00</td>
<td style="text-align: right;">0</td>
</tr>
<tr class="even">
<td style="text-align: left;">34</td>
<td style="text-align: right;">0.00</td>
<td style="text-align: right;">0.00</td>
<td style="text-align: right;">0</td>
<td style="text-align: right;">0.00</td>
<td style="text-align: right;">0.00</td>
<td style="text-align: right;">0.00</td>
<td style="text-align: right;">0.00</td>
<td style="text-align: right;">0.00</td>
<td style="text-align: right;">0.00</td>
<td style="text-align: right;">0</td>
<td style="text-align: right;">0.00</td>
<td style="text-align: right;">0</td>
<td style="text-align: right;">0.00</td>
<td style="text-align: right;">0.00</td>
<td style="text-align: right;">0.54</td>
<td style="text-align: right;">0.45</td>
<td style="text-align: right;">0</td>
<td style="text-align: right;">0.00</td>
<td style="text-align: right;">0.00</td>
<td style="text-align: right;">0.00</td>
<td style="text-align: right;">0</td>
<td style="text-align: right;">0.00</td>
<td style="text-align: right;">10072</td>
</tr>
</tbody>
</table>
<p>The last column in this table, <code>cat</code>, represents the id of the terrestrial ecoregion, while the rest of the columns are the land cover categories. For example, <code>cat</code> 0 is an ocean and 100% of its area is covered by water (class X22), while <code>cat</code> 10072, Gibson Desert, is located in the middle of the continent and is covered by grassland (X15) and sparse vegetation (X16).</p>
</section>
<section id="visualize-spatial-grids" class="level2">
<h2 class="anchored" data-anchor-id="visualize-spatial-grids">Visualize spatial grids</h2>
<p>Creating a grid of motifels is a basic task in many pattern-based analyses, which also includes selecting a size of the motifels.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb7" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="cb7-1"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">gpat_gridhis</span> <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">-i</span> cci_lc2015.tif <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">-o</span> patterns2015.grd <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">-s</span> prod <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">-z</span> 200 <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">-f</span> 200</span></code></pre></div></div>
</div>
<p>To ease selection of a motifels’ size, we can use another function from the <strong>rgeopat2</strong> package <code>gpat_create_grid()</code>. It takes a header output of the <code>gpat_gridhis</code> and produces a vectorized grid.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb8" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb8-1">my_grid <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">gpat_create_grid</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"patterns2015.grd.hdr"</span>)</span></code></pre></div></div>
</div>
<p>Now, you can impose it on top of your map to make a better decision about a motifel size.</p>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://jakubnowosad.com/posts/2018-08-28-geopat-2-extend/figs/extend02.png" class="img-fluid figure-img"></p>
<figcaption>A grid of motifels of 60 by 60 km imposed on the top of a land cover raster map</figcaption>
</figure>
</div>
<p>This function also can be used to analyze and present the results. One of the examples here is to extract information about spatial patterns from a grid using <code>gpat_grid2txt</code>, read it to R with <code>gpat_read_txt()</code> and finally merge it with spatial data.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb9" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb9-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(sf)</span>
<span id="cb9-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(dplyr)</span>
<span id="cb9-3">patterns2015 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">gpat_read_txt</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"patterns2015.txt"</span>)</span>
<span id="cb9-4">my_patterns <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">bind_cols</span>(my_grid, patterns2015)</span></code></pre></div></div>
</div>
<p>The output of this operation is a spatial object (grid of motifels) with attributes about land cover composition.</p>
</section>
<section id="spatial-pattern-based-clustering" class="level2">
<h2 class="anchored" data-anchor-id="spatial-pattern-based-clustering">Spatial pattern-based clustering</h2>
<p>Extracting spatial signatures is not the end of the GeoPAT 2 capabilities. You can also calculate similarity matrices between motifels, points, or polygons using <code>gpat_distmtx</code>. It accepts outputs from <code>gpat_grid2txt</code>, <code>gpat_pointshis</code>, or <code>gpat_polygon</code> and returns a CSV file containing a similarity matrix - also known as a distance matrix. Below, we focus on the output from <code>gpat_grid2txt</code>. Read <a href="https://zenodo.org/record/1291123">the GeoPAT 2 manual</a> to see examples of clustering based on the output of <code>gpat_pointshis</code> and <code>gpat_polygon</code>.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb10" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="cb10-1"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">gpat_distmtx</span> <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">-i</span> patterns2015.txt <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">-o</span> similarity2015.csv</span></code></pre></div></div>
</div>
<p>The output, <code>similarity2015.csv</code>, can be read into your preferable software to apply any clustering method that depends on a distance matrix. For example, the <code>gpat_read_distmtx()</code> function can be used to read the data in R:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb11" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb11-1">dist_2015 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">gpat_read_distmtx</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"similarity2015.csv"</span>)</span></code></pre></div></div>
</div>
<p>This new object, <code>dist_2015</code>, is of the <code>dist</code> class and therefore can be used by many clustering functions, including hierarchical clustering (<code>hclust()</code>), or k-medoids clustering methods (<code>cluster::pam()</code>).</p>
<p>Let’s try a simple example:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb12" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb12-1">hclust_result <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">hclust</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">d =</span> dist_2015, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">method =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ward.D2"</span>)</span>
<span id="cb12-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">plot</span>(hclust_result, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">labels =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>)</span>
<span id="cb12-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rect.hclust</span>(hclust_result, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">k =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">12</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">border =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"blue"</span>)</span>
<span id="cb12-4">hclust_cut <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">cutree</span>(hclust_result, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">12</span>)</span>
<span id="cb12-5">my_grid<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>class <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> hclust_cut</span>
<span id="cb12-6"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">plot</span>(my_grid[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"class"</span>])</span></code></pre></div></div>
</div>
<p>We cluster motifels based on the dissimilarity matrix output of GeoPAT 2 using hierarchical clustering (function <code>hclust()</code>). Based on the dendrogram visualization (<code>plot()</code>), we decide that the data should be divided into 12 clusters (<code>cutree()</code>):</p>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://jakubnowosad.com/posts/2018-08-28-geopat-2-extend/figs/extend03a.png" class="img-fluid figure-img"></p>
<figcaption>Dendrogram representing hierarchical clustering</figcaption>
</figure>
</div>
<p>Finally, we add identifiers of the new clusters to our spatial object containing a grid of motifels and visualize it:</p>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://jakubnowosad.com/posts/2018-08-28-geopat-2-extend/figs/extend03b.png" class="img-fluid figure-img"></p>
<figcaption>A map of Australia divided into twelve clusters based on the similarity of land cover composition</figcaption>
</figure>
</div>
<p>The output is a map with motifels grouped into the areas of the similar land cover composition. Some of the clusters represent one dominant land cover category, e.g.&nbsp;cluster 1 is water, cluster 3 is shrubland, and cluster 7 is sparse vegetation. On the other hand, some of the clusters can consist of many land cover categories. For example, cluster 6 is a mosaic of grassland and sparse vegetation and cluster 8 is a mosaic of sparse vegetation and shrubland.</p>
</section>
<section id="spatiotemporal-pattern-based-analysis" class="level2">
<h2 class="anchored" data-anchor-id="spatiotemporal-pattern-based-analysis">Spatiotemporal pattern-based analysis</h2>
<p>GeoPAT 2 also offers several additional features, including spatiotemporal pattern-based analysis. It is highly experimental, but already can be executed and tested. Let’s take a look at a couple examples from <a href="https://zenodo.org/record/1291123">the GeoPAT 2 manual</a>.</p>
<p>The input data is a set of twelve rasters of monthly sums of precipitation for the area of the British Isles from <a href="http://www.worldclim.org/">the WorldClim database</a>. Each raster has 76560 pixels (240 rows and 319 columns). In other words, each pixel has 12 values representing temporal precipitation pattern.</p>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://jakubnowosad.com/posts/2018-08-28-geopat-2-extend/figs/extend04a.png" class="img-fluid figure-img"></p>
<figcaption>Monthly sums of precipitation for the area of the British Isles</figcaption>
</figure>
</div>
<p>Many GeoPAT 2 methods for spatial data also work with spatiotemporal datasets. For example, it is possible to regionalize the British Isles based on its annual precipitation patterns. Each region has similar temporal precipitation pattern.</p>
<center>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://jakubnowosad.com/posts/2018-08-28-geopat-2-extend/figs/extend04b.png" class="img-fluid figure-img"></p>
<figcaption>Regions of the similar annual precipitation patterns for the British Isles</figcaption>
</figure>
</div>
</center>
<p>Another GeoPAT 2 application is clustering of spatiotemporal data. For example, we can extract temporal precipitation patterns of 97 largest cities in the British Isles and cluster them:</p>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://jakubnowosad.com/posts/2018-08-28-geopat-2-extend/figs/extend04c.png" class="img-fluid figure-img"></p>
<figcaption>Example of a time series clustering</figcaption>
</figure>
</div>
</section>
<section id="more-information" class="level2">
<h2 class="anchored" data-anchor-id="more-information">More information</h2>
<p>Previous blog posts in the series introducing <a href="http://sil.home.amu.edu.pl/index.php?id=geopat2">GeoPAT 2 - a software for pattern-based spatial and temporal analysis</a> are:</p>
<ol type="1">
<li><a href="../../posts/2018-06-18-geopat-2-software-for-pattern-based-spatial-and-temporal-analysis/index.html">GeoPAT 2: Software for Pattern-Based Spatial and Temporal Analysis</a></li>
<li><a href="../../posts/2018-07-06-pattern-based-spatial-analysis-core-ideas/index.html">Pattern-based Spatial Analysis - core ideas</a></li>
<li><a href="../../posts/2018-07-16-geopat-2-search/index.html">Finding similar local landscapes</a></li>
<li><a href="../../posts/2018-07-24-geopat-2-compare/index.html">Quantifying temporal change of landscape pattern</a></li>
<li><a href="../../posts/2018-08-13-geopat-2-segmentation/index.html">Pattern-based regionalization</a></li>
</ol>
<p>If you want to learn more about the pattern-based spatial analysis read <a href="https://zenodo.org/record/1291123">the official GeoPAT 2 manual</a>. It contains installation instructions, description of the GeoPAT 2 architecture, several workflow paths with examples, and explanations of numerical signatures, dissimilarity measures, and topologies available in GeoPAT 2.</p>
<p>We also encourage users to submit issues and enhancement requests so we may continue to improve our software. Furthermore, if you have any question related to GeoPAT 2 please email me at nowosad.jakub@gmail.com.</p>


</section>

<div id="quarto-appendix" class="default"><section class="quarto-appendix-contents" id="quarto-reuse"><h2 class="anchored quarto-appendix-heading">Reuse</h2><div class="quarto-appendix-contents"><div><a rel="license" href="https://creativecommons.org/licenses/by/4.0/">CC BY 4.0</a></div></div></section><section class="quarto-appendix-contents" id="quarto-citation"><h2 class="anchored quarto-appendix-heading">Citation</h2><div><div class="quarto-appendix-secondary-label">BibTeX citation:</div><div class="code-copy-outer-scaffold"><pre class="sourceCode quarto-appendix-bibtex code-with-copy"><code class="sourceCode bibtex">@online{nowosad2018,
  author = {Nowosad, Jakub},
  title = {Moving Beyond Pattern-Based Analysis: {Additional}
    Applications of {GeoPAT} 2},
  date = {2018-08-28},
  url = {https://jakubnowosad.com/posts/2018-08-28-geopat-2-extend/},
  doi = {10.59350/f7q9t-hdc55},
  langid = {en}
}
</code></pre></div><div class="quarto-appendix-secondary-label">For attribution, please cite this work as:</div><div id="ref-nowosad2018" class="csl-entry quarto-appendix-citeas">
Nowosad, Jakub. 2018. <span>“Moving Beyond Pattern-Based Analysis:
Additional Applications of GeoPAT 2.”</span> August 28. <a href="https://doi.org/10.59350/f7q9t-hdc55">https://doi.org/10.59350/f7q9t-hdc55</a>.
</div></div></section></div> ]]></description>
  <category>spatial</category>
  <category>pattern</category>
  <category>spatiotemporal</category>
  <category>geopat</category>
  <category>sil</category>
  <guid>https://jakubnowosad.com/posts/2018-08-28-geopat-2-extend/</guid>
  <pubDate>Tue, 28 Aug 2018 00:00:00 GMT</pubDate>
</item>
<item>
  <title>Pattern-based regionalization</title>
  <dc:creator>Jakub Nowosad</dc:creator>
  <link>https://jakubnowosad.com/posts/2018-08-13-geopat-2-segmentation/</link>
  <description><![CDATA[ 





<hr>
<p>Blog posts in the series introducing <a href="http://sil.home.amu.edu.pl/index.php?id=geopat2">GeoPAT 2 - a software for pattern-based spatial and temporal analysis</a>:</p>
<ol type="1">
<li><a href="../../posts/2018-06-18-geopat-2-software-for-pattern-based-spatial-and-temporal-analysis/index.html">GeoPAT 2: Software for Pattern-Based Spatial and Temporal Analysis</a></li>
<li><a href="../../posts/2018-07-06-pattern-based-spatial-analysis-core-ideas/index.html">Pattern-based Spatial Analysis - core ideas</a></li>
<li><a href="../../posts/2018-07-16-geopat-2-search/index.html">Finding similar local landscapes</a></li>
<li><a href="../../posts/2018-07-24-geopat-2-compare/index.html">Quantifying temporal change of landscape pattern</a></li>
<li><a href="../../posts/2018-08-13-geopat-2-segmentation/index.html">Pattern-based regionalization</a></li>
<li><a href="../../posts/2018-08-28-geopat-2-extend/index.html">Moving beyond pattern-based analysis: Additional applications of GeoPAT 2</a>
<hr></li>
</ol>
<p>This is a fifth blog post in the series introducing <a href="http://sil.home.amu.edu.pl/index.php?id=geopat2">GeoPAT 2 - a software for pattern-based spatial and temporal analysis</a>. In <a href="../../posts/2018-07-24-geopat-2-compare/index.html">the previous one</a> we compared two raster images to quantify the temporal change of pattern in a fixed spatial location. Here, we focus on the most complex of the pattern-based analysis - segmentation. In other words, we will join areas of similar patterns in a way that they create homogeneous regions.</p>
<section id="introduction" class="level2">
<h2 class="anchored" data-anchor-id="introduction">Introduction</h2>
<p>Spatial patterns are an underexplored venue of the Earth and ecological sciences. They could be both an effect of some processes and at the same time affect other ones. For example, a land cover pattern (spatial arrangement of land cover categories) could exist because of an impact of a terrain topology, soils, climate, or human action. Next, this pattern can influence abiotic (e.g.&nbsp;some land cover patterns can be better to prevent erosion and flood than others) and biotic (e.g.&nbsp;some land cover patterns creates habitats for certain animals) components.</p>
<p>Although patterns are sometimes easy to see on a map, their delineation is difficult. Probably the most crucial obstacle is the undefinability of patterns’ borders. There is not a ground truth that enables you to validate which areas have a homogeneous pattern and where the line between two patterns should be drawn. Moreover, several practical problems exist - manual delineation of patterns is inconsistent, very often bias and hardly reproducible as different people have different definitions of patterns in their minds. On top of that there is an issue of time - how long would it take to create a detailed pattern-based regionalization on a global scale?</p>
<p>GeoPAT 2 provides a fast, consistent, but also a highly customizable way to regionalize space based on the underlining spatial patterns. It also has methods to analyze the quality of the output regionalization. The pattern-based segmentation process consists of three steps:</p>
<ol type="1">
<li>Creating a grid of motifels.</li>
<li>Regionalizing of homogeneous patterns.</li>
<li>Obtaining quality of the segmentation.</li>
</ol>
<p>As an example, we are going to use land cover data of Australia from the year 2015. To follow the subsequent calculations, you need to download the dataset, <a href="https://osf.io/5s37z/download">cci_lc2015.tif</a>, and install <a href="http://sil.home.amu.edu.pl/index.php?id=geopat2">GeoPAT 2</a>.</p>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://jakubnowosad.com/posts/2018-08-13-geopat-2-segmentation/figs/segmentation00.png" class="img-fluid figure-img"></p>
<figcaption>Land cover map of Australia in 2015</figcaption>
</figure>
</div>
</section>
<section id="creating-grids-of-motifels" class="level2">
<h2 class="anchored" data-anchor-id="creating-grids-of-motifels">Creating grids of motifels</h2>
<p>In the first step, we need to create a grid of motifels.</p>
<p><code>{bash, eval = FALSE} gpat_gridhis -i cci_lc2015.tif -o patterns2015.grd -z 25 -f 25 -s cooc</code></p>
<p>The <code>patterns2015.grd</code> grid contains a large number of <a href="../../posts/2018-07-06-pattern-based-spatial-analysis-core-ideas/index.html">motifels</a> that are represented by the spatial coocurrence of categories. Motifels are rectangles of 7,500 by 7,500 meters (<code>-z 25</code>: 25 pixels x map resolution of 300 meters) covering the whole area of Australia. However, you need to keep in mind that they are going to be converted into “brick” blocks of 15,000 by 15,000 meters in the segmentation step. More about the “brick” topology in a moment…</p>
</section>
<section id="pattern-based-segmentation" class="level2">
<h2 class="anchored" data-anchor-id="pattern-based-segmentation">Pattern-based segmentation</h2>
<p>The second step is to create regions of homogeneous patterns using the output of the first step. Segmentation result depends on several parameters:</p>
<ul>
<li><code>-i</code> - an input grid of motifels (output from the first step)</li>
<li><code>-o</code> - a name of the output GeoTIFF file - the result in a raster format</li>
<li><code>-v</code> - a name of the output GeoPackage file - the result in a vector format</li>
<li><code>-m</code> - selected <a href="read [the previous blog post](/posts/2018-07-06-pattern-based-spatial-analysis-core-ideas/index.html) to learn more about **similarity metrics**"><strong>similarity metric</strong></a></li>
<li><code>--lthreshold</code> - minimum distance threshold to build areas (see the examples below). It controls segments’ sizes</li>
<li><code>--uthreshold</code> - maximum distance threshold to build areas (see the examples below). It prevents the growth of inhomogeneous segments</li>
<li><code>--swap</code> - improve segmentation by swapping unmatched areas. 0 forces repetition until all unmatched segments will be swapped, while 1 skip this process</li>
<li><code>--minarea</code> - suppress creation of segments smaller than the set value</li>
<li><code>--maxhist</code> - decide how many neighbors are used to calculate similarity. When 0 is set it uses all of the neighbors, which can be time-consuming</li>
<li><code>-q</code> - set the root topology instead of the brick topology (see the examples below)</li>
<li><code>-t</code> - segmentation can be a computationally demanding process. This option allows using multiple CPU threads</li>
</ul>
<p>Additional parameters are either experimental (<code>--weights</code>) or are used mostly for diagnostics purposes (<code>-g</code>, <code>-r</code>). Before digging deep into the parameters, let’s try and leave the default values of most of them:</p>
<p><code>{bash, eval = FALSE} # example A gpat_segment -i patterns2015.grd -o segments2015.tif -v segments2015.gpkg -m jsd -t 3</code></p>
<p>Using only three CPU threads, this process should take about four minutes on a modern laptop for the whole area of Australia. The result of the pattern-based segmentation consists of a large number of regions of varied size. Some of them are relatively small (one motifel) - they are different from the surrounding, while others span over large areas. To make it easier to see how segmentation works, let’s focus on the area in north-eastern Australia defined by the red rectangle:</p>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://jakubnowosad.com/posts/2018-08-13-geopat-2-segmentation/figs/segmentation01.png" class="img-fluid figure-img"></p>
<figcaption>The output of the pattern-based segmentation is represented by the black lines. The red rectangle indicates area used in the next few examples</figcaption>
</figure>
</div>
<p>One of the most important concepts here is the way how motifels are created. Segmentation differs in this element from the rest of the spatial pattern-based analysis, as it uses so-called “brick topology”. Before the segmentation process can even start, grid created in the first step is transformed into a set “bricks” consisting of four motifels that are laid in alternate layers (see the figure below). Therefore, when you set the size to 25 in the first step, the segmented motifels would have a size of 50 by 50 cells (four motifels of 25 by 25). The “brick” topology allows creating segments using 6-connectivity, which gives better results than a simple 4-connectivity.</p>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://jakubnowosad.com/posts/2018-08-13-geopat-2-segmentation/figs/segmentation02a.png" class="img-fluid figure-img"></p>
<figcaption>Segmentation output on the top of a land cover map for an area in north-eastern Australia</figcaption>
</figure>
</div>
<p>Segmentation process consists of several steps that are explained in the <a href="https://eartharxiv.org/smva7/">“Multi-scale segmentation algorithm for pattern-based partitioning of large categorical rasters”</a> article. However, the most basic part to understand is how the segment growing works. We start from one motifel and calculate its similarity with all of the adjacent motifels. Next, we merge it with the most similar one and this process is repeated until we get to the threshold value:</p>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://jakubnowosad.com/posts/2018-08-13-geopat-2-segmentation/figs/segmentation03.png" class="img-fluid figure-img"></p>
<figcaption>Illustration of the segment growing process</figcaption>
</figure>
</div>
</section>
<section id="segmentation-quality" class="level2">
<h2 class="anchored" data-anchor-id="segmentation-quality">Segmentation quality</h2>
<p>As I have mentioned in the introduction, ground truth does not exist in the pattern-based regionalization. However, we can measure several properties of the output segments and they could give us an insight into how good our results are.</p>
<p><code>{bash, eval = FALSE} gpat_segquality -i patterns2015.grd -s segments2015.tif -g inhomogeneity2015.tif -o isolation2015.tif</code></p>
<p>All of them have values between 0 and 1. The most important first-order property is inhomogeneity, which tells how internally inconsistent each segment is. Our main goal of segmentation is to obtain regions of homogeneous patterns, therefore we want this value to be as low as possible.</p>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://jakubnowosad.com/posts/2018-08-13-geopat-2-segmentation/figs/segmentation04a.png" class="img-fluid figure-img"></p>
<figcaption>Inhomogeneity of the example segmentation</figcaption>
</figure>
</div>
<p>Secondary property of segmentation is isolation - we want regions that are distinct from their surroundings and therefore we aim for high values of isolation. However, you need to keep in mind that inhomogeneity and isolation are not equally important. You may want to obtain regions of homogeneous patterns that are not drastically isolated from surroundings, however isolated yet inhomogenous regions are usually meaningless.</p>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://jakubnowosad.com/posts/2018-08-13-geopat-2-segmentation/figs/segmentation04b.png" class="img-fluid figure-img"></p>
<figcaption>Isolation of the example segmentation</figcaption>
</figure>
</div>
<p>Finally, segmentation quality can be calculated as:</p>
<p><img src="https://latex.codecogs.com/png.latex?1%20-%20(inhomogeneity/isolation)"></p>
<p>It gives an overall metric of the output, where the larger the value the better.</p>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://jakubnowosad.com/posts/2018-08-13-geopat-2-segmentation/figs/segmentation04c.png" class="img-fluid figure-img"></p>
<figcaption>Quality of the example segmentation</figcaption>
</figure>
</div>
</section>
<section id="segmentation-control" class="level2">
<h2 class="anchored" data-anchor-id="segmentation-control">Segmentation control</h2>
<p>Pattern-based segmentation is a complex operation and visibly depends on several parameters mentioned above. Let’s try to manipulate some of them and see the results:</p>
<p><code>{bash, eval = FALSE} # example B gpat_segment -i patterns2015.grd -o segments2015_B.tif -v segments2015_B.gpkg -m jsd -t 3 --lthreshold=0.3 --uthreshold=0.5 # example C gpat_segment -i patterns2015.grd -o segments2015_C.tif -v segments2015_C.gpkg -m jsd -t 3 --minarea=6 # example D gpat_segment -i patterns2015.grd -o segments2015_D.tif -v segments2015_D.gpkg -m jac -t 3 # example E gpat_gridhis -i cci_lc2015.tif -o patterns2015_100.grd -s cooc -z 100 -f 100 gpat_segment -i patterns2015_100.grd -o segments2015_E.tif -v segments2015_E.gpkg -m jsd -t 3 -q</code></p>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://jakubnowosad.com/posts/2018-08-13-geopat-2-segmentation/figs/segmentation05.png" class="img-fluid figure-img"></p>
<figcaption>Five examples of segmentation with a different set of parameters</figcaption>
</figure>
</div>
<p>Example A uses the default values of the segmentation. In the second example (B), we increased values of two parameters, <code>--lthreshold</code> and <code>--uthreshold</code>. The first controls how homogenous the output regions should be in terms of patterns, with larger values indicating more inhomogenous regions (which also means larger regions/smaller number of segments). The second one prevents the growth of too large inhomogeneous segments. Example C has the <code>--minarea</code> parameter set to 6, which suppress creation of segments with fewer motifels than the set number. In example D, we used Jaccard distance, a different similarity measure. It gave fairly similar results to the Janson-Shannon divergence but different in details. And finally, example E used a root topology (also know as 4-connectivity). In the growing stage, it can only use neighbors from the left, right, top, and bottom. As a result, the output segmentation seems more clunky and the segments are aligned along horizontal and vertical lines.</p>
</section>
<section id="whats-next" class="level2">
<h2 class="anchored" data-anchor-id="whats-next">What’s next</h2>
<p>In the next blog post, I will show a few additional applications of GeoPAT 2 and how to adjust it to your own workflow. In the meantime, you can find other simple examples of segmentation as well as more detail explanation of the brick topology in <a href="https://zenodo.org/record/1291123">the GeoPAT 2 user’s manual</a>. If you are interested in a deeper explanation of the pattern-based segmentation, I encourage you to take a look at the article <a href="https://eartharxiv.org/smva7/">“Multi-scale segmentation algorithm for pattern-based partitioning of large categorical rasters”</a>. It explains basic concepts (e.g.&nbsp;rook and brick topology), steps in the segmentation algorithm, and provides several examples. Finally, this method has been also applied for a machine ecoregionalization of terrestrial Earth’s landmass, where each ecophysiographic region was characterized by homogeneous physiography. You can read more about it at https://eartharxiv.org/fsver.</p>


</section>

<div id="quarto-appendix" class="default"><section class="quarto-appendix-contents" id="quarto-reuse"><h2 class="anchored quarto-appendix-heading">Reuse</h2><div class="quarto-appendix-contents"><div><a rel="license" href="https://creativecommons.org/licenses/by/4.0/">CC BY 4.0</a></div></div></section><section class="quarto-appendix-contents" id="quarto-citation"><h2 class="anchored quarto-appendix-heading">Citation</h2><div><div class="quarto-appendix-secondary-label">BibTeX citation:</div><div class="code-copy-outer-scaffold"><pre class="sourceCode quarto-appendix-bibtex code-with-copy"><code class="sourceCode bibtex">@online{nowosad2018,
  author = {Nowosad, Jakub},
  title = {Pattern-Based Regionalization},
  date = {2018-08-13},
  url = {https://jakubnowosad.com/posts/2018-08-13-geopat-2-segmentation/},
  doi = {10.59350/fmntx-66n69},
  langid = {en}
}
</code></pre></div><div class="quarto-appendix-secondary-label">For attribution, please cite this work as:</div><div id="ref-nowosad2018" class="csl-entry quarto-appendix-citeas">
Nowosad, Jakub. 2018. <span>“Pattern-Based Regionalization.”</span>
August 13. <a href="https://doi.org/10.59350/fmntx-66n69">https://doi.org/10.59350/fmntx-66n69</a>.
</div></div></section></div> ]]></description>
  <category>spatial</category>
  <category>pattern</category>
  <category>spatiotemporal</category>
  <category>geopat</category>
  <category>sil</category>
  <guid>https://jakubnowosad.com/posts/2018-08-13-geopat-2-segmentation/</guid>
  <pubDate>Mon, 13 Aug 2018 00:00:00 GMT</pubDate>
</item>
<item>
  <title>Quantifying temporal change of landscape pattern</title>
  <dc:creator>Jakub Nowosad</dc:creator>
  <link>https://jakubnowosad.com/posts/2018-07-24-geopat-2-compare/</link>
  <description><![CDATA[ 





<hr>
<p>Blog posts in the series introducing <a href="http://sil.home.amu.edu.pl/index.php?id=geopat2">GeoPAT 2 - a software for pattern-based spatial and temporal analysis</a>:</p>
<ol type="1">
<li><a href="../../posts/2018-06-18-geopat-2-software-for-pattern-based-spatial-and-temporal-analysis/index.html">GeoPAT 2: Software for Pattern-Based Spatial and Temporal Analysis</a></li>
<li><a href="../../posts/2018-07-06-pattern-based-spatial-analysis-core-ideas/index.html">Pattern-based Spatial Analysis - core ideas</a></li>
<li><a href="../../posts/2018-07-16-geopat-2-search/index.html">Finding similar local landscapes</a></li>
<li><a href="../../posts/2018-07-24-geopat-2-compare/index.html">Quantifying temporal change of landscape pattern</a></li>
<li><a href="../../posts/2018-08-13-geopat-2-segmentation/index.html">Pattern-based regionalization</a></li>
<li><a href="../../posts/2018-08-28-geopat-2-extend/index.html">Moving beyond pattern-based analysis: Additional applications of GeoPAT 2</a>
<hr></li>
</ol>
<p>This is a fourth blog post in the series introducing <a href="http://sil.home.amu.edu.pl/index.php?id=geopat2">GeoPAT 2 - a software for pattern-based spatial and temporal analysis</a>. <a href="../../posts/2018-07-16-geopat-2-search/index.html">The previous one</a> focused on finding local landscapes with similar patterns. I had shown a small location of interest located in Australia and found a set of areas with very similar spatial patterns to it. Here, I will apply the concepts of the pattern-based spatial analysis to quantify temporal change of pattern in a fixed spatial location.</p>
<section id="introduction" class="level2">
<h2 class="anchored" data-anchor-id="introduction">Introduction</h2>
<!-- Imagine you have two images and you want to compare how a person on the image changed. -->
<!-- You would take a look and see if that person become taller, change their hairstyle, get a few new wrinkles. -->
<p>Imagine you have two values expressing the world population in 1950 (2.5 billion people) and 2012 (7.1 billion people). How would you compare the change in the world population? The easiest (and correct) approach is just to subtract the past value from the more recent one:</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A7.1%20-%202.5%20=%204.6%0A"></p>
<p>We can conclude that the world population between 1950 and 2015 increased by 4.6 billion people.</p>
<p>Now, let’s move into a more spatial problem. Think about how you can compare two satellite images or raster maps. Usually, the process is the same as in the example above - values in the image B are subtracted by values in the corresponding cells in the image A. This can be used to determine how land cover changes. Two raster land cover maps are just analyzed pixel by pixel, e.g.&nbsp;this pixel did change from forest to agriculture and that pixel did not change as it is classified as forest on both maps. It a good approach when we are interested in, e.g.&nbsp;overall changes over an extended area. This way you can say, for example, that globally in the last 25 year we lost this area of forest, but gain this area of agriculture.</p>
<p>However, while this approach is helpful in many cases, it cannot solve all of the problems. Let’s consider these two areas, the left one represents an earlier time A and the right one represents a later time B.</p>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://jakubnowosad.com/posts/2018-07-24-geopat-2-compare/figs/compare01a.png" class="img-fluid figure-img"></p>
<figcaption>Example landscape in two times A and B</figcaption>
</figure>
</div>
<p>When we compare it using the standard method (pixel by pixel) we get the result below:</p>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://jakubnowosad.com/posts/2018-07-24-geopat-2-compare/figs/compare01b.png" class="img-fluid figure-img"></p>
<figcaption>Pixel by pixel change in the example landscape between time A and time B</figcaption>
</figure>
</div>
<p>The changes analyzed this way are very prominent - about a half of the area had changed the category. However, when you look back at the whole landscape in time A and B, you should notice that both images are very similar if one disregards details and orientations. They both have the same number of categories, their share in the landscape is comparable and they are arranged in a similar spatial pattern. While we can say that many pixels have changed their values, we cannot say that this landscape’s pattern changed a lot. Therefore, to be able to fully describe the change of a landscape, we need to be able not only to see the changes in single pixels but also changes in spatial patterns. <!-- error resistance --> <!-- it could have important implications --></p>
<p>Pixel by pixel analyses are a part of standard raster calculators, but comparing spatial patterns is a role for GeoPAT 2. This consists of two steps:</p>
<ol type="1">
<li>Creating a grid of motifels.</li>
<li>Comparing a query and a grid of motifels.</li>
</ol>
<p>As an example, we are going to use land cover data of Australia from the year 2000 and 2015. To follow the subsequent calculations, you need to download both datasets, <a href="https://osf.io/9gdvj/download">cci_lc2000.tif</a> and <a href="https://osf.io/5s37z/download">cci_lc2015.tif</a>, and install <a href="http://sil.home.amu.edu.pl/index.php?id=geopat2">GeoPAT 2</a>.</p>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://jakubnowosad.com/posts/2018-07-24-geopat-2-compare/figs/compare02.gif" class="img-fluid figure-img"></p>
<figcaption>Animation showing land cover differences in Australia between 2000 and 2015</figcaption>
</figure>
</div>
</section>
<section id="creating-grids-of-motifels" class="level2">
<h2 class="anchored" data-anchor-id="creating-grids-of-motifels">Creating grids of motifels</h2>
<p>In the first step, we need to create two grids of motifels - one for each input raster.</p>
<p><code>{bash, eval = FALSE} gpat_gridhis -i cci_lc2000.tif -o patterns2000.grd -z 50 -f 50 -s cooc  gpat_gridhis -i cci_lc2015.tif -o patterns2015.grd -z 50 -f 50 -s cooc</code></p>
<p>These grids, <code>patterns2000.grd</code> and <code>patterns2015.grd</code>, contain a large number of <a href="../../posts/2018-07-06-pattern-based-spatial-analysis-core-ideas/index.html#motiel">motifels</a> that are represented by <a href="../../posts/2018-07-06-pattern-based-spatial-analysis-core-ideas/index.html#signature">signatures</a>. In the code above, we specify the input (<code>-i</code>) and the outputs (<code>-o</code>) files, while other parameters control a size of motifels and a signature used. Here, each motifel is a rectangle of 15,000 by 15,000 meters (<code>-z 50</code>: 50 pixels x map resolution of 300 meters). Importantly, motifels should not overlap, therefore we shift the subsequent motifels by 15,000 meters (<code>-f 50</code>: 50 pixels x map resolution of 300 meters). The last argument, <code>-s</code>, is crucial here. We can use <code>-s prod</code> that only represents shares of land cover categories or <code>-s cooc</code>, a spatial co-occurrence, that represents a spatial pattern.</p>
</section>
<section id="compare-two-maps" class="level2">
<h2 class="anchored" data-anchor-id="compare-two-maps">Compare two maps</h2>
<p>The second and final step is to use the outputs of the first step and compare their signatures. Three parameters are required here:</p>
<ul>
<li><code>-i</code> - this parameter is used twice, for the first and the second input grid (results of the first step)</li>
<li><code>-o</code> - a name of the output GeoTIFF file</li>
<li><code>-m</code> - selected <a href="read [the previous blog post](/posts/2018-07-06-pattern-based-spatial-analysis-core-ideas/index.html) to learn more about **similarity metrics**"><strong>similarity metric</strong></a></li>
</ul>
<p><code>{bash, eval = FALSE} gpat_compare -i patterns2000.grd -i patterns2015.grd -o similarity0015.tif -m jsd</code></p>
<p>The result is a raster map of similarities between Australia’s land cover in 2000 and 2015. Each pixel in this raster has a resolution of 15,000 meters and a value between 0 and 1. The largest value, 1, means that the spatial pattern in these two years is identical (no change in pattern has occured), while lower values represent areas with more and more dissimilar patterns (increasing degree of change in pattern has occured).</p>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://jakubnowosad.com/posts/2018-07-24-geopat-2-compare/figs/compare03.png" class="img-fluid figure-img"></p>
<figcaption>Similarity of land cover patterns (a magnitude of pattern change) between the years 2000 and 2015</figcaption>
</figure>
</div>
<p>Large areas in northern and south-eastern Australia showed no change or very small change of spatial patterns of land cover (values of similarity close to 1). At the same time, there are a visible number of changed landscapes and their spatial distribution is not random. Now, they can be analyzed in more detail - we can look at what kind of landscapes changed the most (e.g.&nbsp;mosaic of forest and grassland), what is a trend in patterns changes (replacing grasslands with croplands), or what is the degree of spatial pattern changes.</p>
<p>Just to give you an example, below are six pairs of landscapes of different levels of similarity:</p>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://jakubnowosad.com/posts/2018-07-24-geopat-2-compare/figs/compare04.png" class="img-fluid figure-img"></p>
<figcaption>Six examples of pairs of motifels with different levels of similarity</figcaption>
</figure>
</div>
<p>The most extreme example in top right corner represents an almost complete change from a bare area into a sparse vegetation. The next ones show decreasing levels of change, with the fifth example where just a small percentage of the area actually had changed. Finally, the last example has a similarity of 1, which means that its spatial pattern did not change.</p>
</section>
<section id="whats-next" class="level2">
<h2 class="anchored" data-anchor-id="whats-next">What’s next</h2>
<p>The next blog post in the GeoPAT 2 series is going to focus on automatic map regionalizations. In the meantime, you can find another simple example in <a href="https://zenodo.org/record/1291123">the GeoPAT 2 user’s manual</a> and if you want to see spatial pattern comparison in action, read a paper <a href="https://eartharxiv.org/k3rmn/">“Global mapping of changes in landscapes and coverages of vegetation types from the ESA land cover 1992-2015 time series”</a>. It applies the pattern-based spatial analysis to describe global land cover changes during the 1992–2015 period.</p>


</section>

<div id="quarto-appendix" class="default"><section class="quarto-appendix-contents" id="quarto-reuse"><h2 class="anchored quarto-appendix-heading">Reuse</h2><div class="quarto-appendix-contents"><div><a rel="license" href="https://creativecommons.org/licenses/by/4.0/">CC BY 4.0</a></div></div></section><section class="quarto-appendix-contents" id="quarto-citation"><h2 class="anchored quarto-appendix-heading">Citation</h2><div><div class="quarto-appendix-secondary-label">BibTeX citation:</div><div class="code-copy-outer-scaffold"><pre class="sourceCode quarto-appendix-bibtex code-with-copy"><code class="sourceCode bibtex">@online{nowosad2018,
  author = {Nowosad, Jakub},
  title = {Quantifying Temporal Change of Landscape Pattern},
  date = {2018-07-24},
  url = {https://jakubnowosad.com/posts/2018-07-24-geopat-2-compare/},
  doi = {10.59350/mzbrk-fgy16},
  langid = {en}
}
</code></pre></div><div class="quarto-appendix-secondary-label">For attribution, please cite this work as:</div><div id="ref-nowosad2018" class="csl-entry quarto-appendix-citeas">
Nowosad, Jakub. 2018. <span>“Quantifying Temporal Change of Landscape
Pattern.”</span> July 24. <a href="https://doi.org/10.59350/mzbrk-fgy16">https://doi.org/10.59350/mzbrk-fgy16</a>.
</div></div></section></div> ]]></description>
  <category>spatial</category>
  <category>pattern</category>
  <category>spatiotemporal</category>
  <category>geopat</category>
  <category>sil</category>
  <guid>https://jakubnowosad.com/posts/2018-07-24-geopat-2-compare/</guid>
  <pubDate>Tue, 24 Jul 2018 00:00:00 GMT</pubDate>
</item>
<item>
  <title>Finding similar local landscapes</title>
  <dc:creator>Jakub Nowosad</dc:creator>
  <link>https://jakubnowosad.com/posts/2018-07-16-geopat-2-search/</link>
  <description><![CDATA[ 





<hr>
<p>Blog posts in the series introducing <a href="http://sil.home.amu.edu.pl/index.php?id=geopat2">GeoPAT 2 - a software for pattern-based spatial and temporal analysis</a>:</p>
<ol type="1">
<li><a href="../../posts/2018-06-18-geopat-2-software-for-pattern-based-spatial-and-temporal-analysis/index.html">GeoPAT 2: Software for Pattern-Based Spatial and Temporal Analysis</a></li>
<li><a href="../../posts/2018-07-06-pattern-based-spatial-analysis-core-ideas/index.html">Pattern-based Spatial Analysis - core ideas</a></li>
<li><a href="../../posts/2018-07-16-geopat-2-search/index.html">Finding similar local landscapes</a></li>
<li><a href="../../posts/2018-07-24-geopat-2-compare/index.html">Quantifying temporal change of landscape pattern</a></li>
<li><a href="../../posts/2018-08-13-geopat-2-segmentation/index.html">Pattern-based regionalization</a></li>
<li><a href="../../posts/2018-08-28-geopat-2-extend/index.html">Moving beyond pattern-based analysis: Additional applications of GeoPAT 2</a>
<hr></li>
</ol>
<p>This is a third blog post in the series introducing <a href="http://sil.home.amu.edu.pl/index.php?id=geopat2">GeoPAT 2 - a software for pattern-based spatial and temporal analysis</a>. <a href="../../posts/2018-07-06-pattern-based-spatial-analysis-core-ideas/">The previous two</a> laid a foundation for pattern-based spatial analysis by explaining the three core ideas - a <strong>motifel</strong>, <strong>signature</strong>, and <strong>similarity metric</strong>. Now, I will apply these ideas to real-life problems. The first one is finding local landscapes with similar patterns.</p>
<section id="introduction" class="level2">
<h2 class="anchored" data-anchor-id="introduction">Introduction</h2>
<p>Nowadays, convenient search engines (such as <a href="https://www.google.com/">Google</a> or <a href="https://duckduckgo.com/">DuckDuckGo</a>) are essential tools in everyday life. We check the news, look for the products reviews, or find the way <a href="https://trends.google.com/trends/yis/2017/GLOBAL/">how to make solar eclipse glasses</a>. Most of our searches have one thing in common though - they are based on text phrases. Additionally, some less common search methods are possible, including <a href="https://support.google.com/websearch/answer/1325808">reverse image search</a>.</p>
<p>Spatial data analysis also offers several distinct ways to search. Some only depends on <a href="http://geocompr.robinlovelace.net/attr.html#vector-attribute-subsetting">values in a vector’s attribute table</a> or <a href="http://geocompr.robinlovelace.net/attr.html#raster-subsetting">raster’s values</a>. There are also methods that search based on spatial properties of the data, for example, how <a href="http://geocompr.robinlovelace.net/spatial-operations.html#spatial-subsetting">an object relate in space to another one</a>. You can look for intersections, touches, is within, etc. Finally, it is possible to search by distance.</p>
<p>Can you guess what are we missing in this long list? Yes, you’re (probably) right! Finding places with similar spatial patterns is not a standard spatial operation. However, it could be useful to solve many potential questions. For example, you observed some ecological process and assume that it depends on a pattern of land cover. Next, you need to find and study areas of similar land cover patterns to validate your results. Fortunately, finding similar spatial patterns is one of the GeoPAT 2 main features.</p>
<p>It consists of three steps:</p>
<ol type="1">
<li>Extracting local pattern features of a query (location of interest).</li>
<li>Creating a grid of motifels.</li>
<li>Comparing a query and a grid of motifels.</li>
</ol>
<p>We will use a data from Australia as an example. You just need to download <a href="https://osf.io/5s37z/download">the cci_lc2015.tif land cover dataset</a> and install <a href="http://sil.home.amu.edu.pl/index.php?id=geopat2">GeoPAT 2</a> to follow the calculations below.</p>
</section>
<section id="extracting-local-pattern-features" class="level2">
<h2 class="anchored" data-anchor-id="extracting-local-pattern-features">Extracting local pattern features</h2>
<p>Spatial pattern search requires just two things - a query (location of interest) and a context (the same data of a larger area). In this example, we use a 6 by 6 km site in Pilliga Forests in New South Wales, Australia as our location of interest:</p>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://jakubnowosad.com/posts/2018-07-16-geopat-2-search/figs/search01.png" class="img-fluid figure-img"></p>
<figcaption>Location of interest. 6 x 6 km area in New South Wales, Australia</figcaption>
</figure>
</div>
<p>This area is mostly occupied by evergreen and deciduous broad-leaved forest, but also has a few smaller patches of shrublands.</p>
<p>In the first step, we need to convert this map (spatial patterns of categories) into a numerical <strong>signature</strong> (read <a href="../../posts/2018-07-06-pattern-based-spatial-analysis-core-ideas/">the previous blog post</a> to learn more about <strong>signatures</strong>) using the <code>gpat_pointshis</code> module. It requires a number of parameters:</p>
<ul>
<li><code>-i</code> - an input map covering the whole area that we want to be searched</li>
<li><code>-o</code> - a name of the output text file containing <strong>a signature</strong> of the query</li>
<li><code>-s</code> - a representation of the query (a type of <strong>signature</strong>)</li>
<li><code>-x</code> and <code>-y</code> - x and y coordinates of the query (alternatively, a file with coordinates can be provided with <code>--xy_file=&lt;file_name&gt;</code>)</li>
<li><code>-z</code> - a size of the query in pixels</li>
</ul>
<p><code>{bash, eval = FALSE} gpat_pointshis -i cci_lc2015.tif -o query_signature.txt -s cooc -x 1457570 -y -3562186  -z 20</code></p>
<p>In this example, a land cover dataset with a resolution of 300 meters and covering Australia is our input map. We selected the spatial co-occurrence of categories (<code>cooc</code>) as a type of <strong>signature</strong> because we are interested not only in a composition of land cover classes but also in its pattern. The last parameters determine coordinates of a central point (<code>-x 1457570 -y -3562186</code>) and the size of a query (<code>-z 20</code>). In other words, the area of interest is a rectangle of 6000 by 6000 meters (20 pixels x map resolution of 300 meters) with the above coordinates in the middle.</p>
</section>
<section id="creating-a-grid-of-motifels" class="level2">
<h2 class="anchored" data-anchor-id="creating-a-grid-of-motifels">Creating a grid of motifels</h2>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://jakubnowosad.com/posts/2018-07-16-geopat-2-search/figs/australia_ccilc.png" class="img-fluid figure-img"></p>
<figcaption>Land cover raster map at 300 m resolution created by the European Space Agency (ESA)</figcaption>
</figure>
</div>
<p>In the second step, we create a grid of motifels - a database of patterns for the area of Australia.</p>
<p>Using the <code>gpat_gridhis</code> module, we need to provide:</p>
<ul>
<li><code>-i</code> - an input map covering the whole area that we want to be searched</li>
<li><code>-o</code> - a name of the output file containing signatures of each landscape in the input map (a grid of motifels)</li>
<li><code>-s</code> - a representation of the query (a type of <strong>signature</strong>)</li>
<li><code>-z</code> and <code>-f</code> - a size of the landscapes from which signatures will be extracted</li>
</ul>
<p><code>{bash, eval = FALSE} gpat_gridhis -i cci_lc2015.tif -o reference.grd -s cooc -z 20 -f 20</code></p>
</section>
<section id="finding-similar-local-landscapes" class="level2">
<h2 class="anchored" data-anchor-id="finding-similar-local-landscapes">Finding similar local landscapes</h2>
<p>Finally, we compare pattern in our query (step 1) with every local 6 by 6 km patterns on the whole continent (step 2). Therefore, only four parameters are required by <code>gpat_search</code>:</p>
<ul>
<li><code>-i</code> - a grid of motifels</li>
<li><code>-o</code> - a final map of similarity between <code>-i</code> and <code>-r</code></li>
<li><code>-r</code> - a text file with a numerical <strong>signature</strong> of our query</li>
<li><code>-m</code> - a selected <a href="read [the previous blog post](/posts/2018-07-06-pattern-based-spatial-analysis-core-ideas/) to learn more about **similarity metrics**"><strong>similarity metric</strong></a></li>
</ul>
<p><code>{bash, eval = FALSE} gpat_search -i reference.grd -o similarity_output -r query_signature.txt -m jsd</code></p>
<p>The output map is a raster of similarity between our query and the reference map: <img src="https://jakubnowosad.com/posts/2018-07-16-geopat-2-search/figs/search03.png" class="img-fluid" alt="Similarity of land cover patterns between the query and the reference map"></p>
<p>Most of the reference map’s area is very different than our query (low values of similarity indicated by dark colors). However, there is also a visible number of places with a very similar local pattern. They are located in the south-west and east Australia and on Tasmania.</p>
<p>Finally, we can just use standard GIS methods and extract areas with the most similar land cover pattern to our query:</p>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://jakubnowosad.com/posts/2018-07-16-geopat-2-search/figs/search04.png" class="img-fluid figure-img"></p>
<figcaption>The query and six the most similar local landscapes to it</figcaption>
</figure>
</div>
<p>They do not only have the same land cover classes, but also its composition and configuration are very similar.</p>
</section>
<section id="whats-next" class="level2">
<h2 class="anchored" data-anchor-id="whats-next">What’s next</h2>
<p>Another example of the spatial pattern search in action can be found in <a href="https://zenodo.org/record/1291123">the GeoPAT 2 user’s manual</a>. Now, finding similar local landscapes is easy. However, how to describe spatial pattern’s changes in time? That will be a topic for the next blog post in the GeoPAT 2 series.</p>


</section>

<div id="quarto-appendix" class="default"><section class="quarto-appendix-contents" id="quarto-reuse"><h2 class="anchored quarto-appendix-heading">Reuse</h2><div class="quarto-appendix-contents"><div><a rel="license" href="https://creativecommons.org/licenses/by/4.0/">CC BY 4.0</a></div></div></section><section class="quarto-appendix-contents" id="quarto-citation"><h2 class="anchored quarto-appendix-heading">Citation</h2><div><div class="quarto-appendix-secondary-label">BibTeX citation:</div><div class="code-copy-outer-scaffold"><pre class="sourceCode quarto-appendix-bibtex code-with-copy"><code class="sourceCode bibtex">@online{nowosad2018,
  author = {Nowosad, Jakub},
  title = {Finding Similar Local Landscapes},
  date = {2018-07-16},
  url = {https://jakubnowosad.com/posts/2018-07-16-geopat-2-search/},
  doi = {10.59350/whb56-qh683},
  langid = {en}
}
</code></pre></div><div class="quarto-appendix-secondary-label">For attribution, please cite this work as:</div><div id="ref-nowosad2018" class="csl-entry quarto-appendix-citeas">
Nowosad, Jakub. 2018. <span>“Finding Similar Local Landscapes.”</span>
July 16. <a href="https://doi.org/10.59350/whb56-qh683">https://doi.org/10.59350/whb56-qh683</a>.
</div></div></section></div> ]]></description>
  <category>spatial</category>
  <category>pattern</category>
  <category>spatiotemporal</category>
  <category>geopat</category>
  <category>sil</category>
  <guid>https://jakubnowosad.com/posts/2018-07-16-geopat-2-search/</guid>
  <pubDate>Mon, 16 Jul 2018 00:00:00 GMT</pubDate>
</item>
<item>
  <title>Pattern-based Spatial Analysis - core ideas</title>
  <dc:creator>Jakub Nowosad</dc:creator>
  <link>https://jakubnowosad.com/posts/2018-07-06-pattern-based-spatial-analysis-core-ideas/</link>
  <description><![CDATA[ 





<hr>
<p>Blog posts in the series introducing <a href="http://sil.home.amu.edu.pl/index.php?id=geopat2">GeoPAT 2 - a software for pattern-based spatial and temporal analysis</a>:</p>
<ol type="1">
<li><a href="../../posts/2018-06-18-geopat-2-software-for-pattern-based-spatial-and-temporal-analysis/index.html">GeoPAT 2: Software for Pattern-Based Spatial and Temporal Analysis</a></li>
<li><a href="../../posts/2018-07-06-pattern-based-spatial-analysis-core-ideas/index.html">Pattern-based Spatial Analysis - core ideas</a></li>
<li><a href="../../posts/2018-07-16-geopat-2-search/index.html">Finding similar local landscapes</a></li>
<li><a href="../../posts/2018-07-24-geopat-2-compare/index.html">Quantifying temporal change of landscape pattern</a></li>
<li><a href="../../posts/2018-08-13-geopat-2-segmentation/index.html">Pattern-based regionalization</a></li>
<li><a href="../../posts/2018-08-28-geopat-2-extend/index.html">Moving beyond pattern-based analysis: Additional applications of GeoPAT 2</a>
<hr></li>
</ol>
<p>This is a second blog post in the series introducing GeoPAT 2 - a software for pattern-based spatial and temporal analysis. <a href="../../posts/2018-06-18-geopat-2-software-for-pattern-based-spatial-and-temporal-analysis/">The first one</a> focused on providing a background of this open-source software. Here, I will explain the three core ideas behind the pattern-based spatial analysis including what is a <strong>motifel</strong>, <strong>signature</strong>, and <strong>similarity metric</strong>.</p>
<section id="motifel" class="level2">
<h2 class="anchored" data-anchor-id="motifel">Motifel</h2>
<p>Most of the spatial raster analyses use single pixels <!--(often from many spectral/image bands)--> as its main unit of analysis. Each pixel has one value (e.g.&nbsp;land cover class) and relatively small size (e.g.&nbsp;90 meters). The value of a single pixel can show a local property, however, it says nothing about a local spatial pattern.</p>
<p>Therefore, the question is how a local pattern can be depicted? In our approach, we consider a square block of pixels as a representation of a local pattern (“motif”). This block of pixels is called a <strong>motifel</strong> and it is an elementary unit of the pattern-based spatial analysis.</p>
<p>In the figure below, you can see a land cover map divided into a set of <strong>motifels</strong>. Each <strong>motifel</strong> consists of a large number of pixels and depicts a local pattern.</p>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://jakubnowosad.com/posts/2018-07-06-pattern-based-spatial-analysis-core-ideas/figs/01_motifel_grid.png" class="img-fluid figure-img"></p>
<figcaption>An example of a grid divided into motifels</figcaption>
</figure>
</div>
<p>It is worth to mention that a simple rule to decide on a size of a <strong>motifel</strong> doesn’t exist. The decision depends on the input data (e.g.&nbsp;its resolution, number of classes), type of a pattern, its variability, etc. However, a rule of thumb is to look closely at your data and keep in mind two things:</p>
<ol type="1">
<li>There should be enough pixels to create a pattern. That is why GeoPAT 2 does not allow for a mofitels smaller than 10 by 10 pixels.</li>
<li>Single <strong>motifels</strong> usually should not encapsulate many different patterns.</li>
</ol>
</section>
<section id="signature" class="level2">
<h2 class="anchored" data-anchor-id="signature">Signature</h2>
<p><strong>Motifel</strong> is a spatial data representation preferable by humans as our pattern processing capabilities were improved in millions of years of evolution. However, computers cannot see patterns in the same way as we do, and we need to transform the spatial pattern data into a form recognizable by machines. Fortunately, there are many ways to do so and GeoPAT 2 offers some of them. They are called <strong>signatures</strong>.</p>
<p>The simplest <strong>signature</strong> of a motifel is its composition (Cartesian product, <code>prod</code>) - a number of cells of each map category. It is a very compact representation, although it doesn’t contain information related to the configuration of categories. This is a role for the following <strong>signature</strong> - spatial co-occurrence of categories (<code>cooc</code>). Spatial co-occurrence of categories is a <img src="https://latex.codecogs.com/png.latex?k"> by <img src="https://latex.codecogs.com/png.latex?k"> square matrix, where <img src="https://latex.codecogs.com/png.latex?k"> is a number of classes in a landscape. Class co-occurrence matrix counts a number of pairs of classes assigned to neighboring cells. Next, the co-occurrence matrix is transformed into a normalized co-occurrence histogram. In this <strong>signature</strong>, a landscape with <img src="https://latex.codecogs.com/png.latex?k"> classes can be represented by the co-occurrence histogram of <img src="https://latex.codecogs.com/png.latex?(k%5E2%20+%20k)/2"> elements. Importantly, a <img src="https://latex.codecogs.com/png.latex?k"> number of them is related to the class composition and <img src="https://latex.codecogs.com/png.latex?(k%5E2%20-%20k)/2"> is related to the class configuration.</p>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://jakubnowosad.com/posts/2018-07-06-pattern-based-spatial-analysis-core-ideas/figs/02_signatures_examples.png" class="img-fluid figure-img"></p>
<figcaption>Two representations of a motifel: (a) composition (Cartesian product), (b) spatial co-occurrence of categories</figcaption>
</figure>
</div>
<p>Take a look at the example above and compare information depicted by the compositional and co-occurrence histograms. The first one shows that each land cover category occupies a very similar proportion of the area. The second one provides more information - it counts how often different categories are adjacent to each other. These histograms also have some important properties - rotated or mirrored version of a landscape will still have the same non-spatial representation.</p>
<p>The <strong>signatures</strong> mentioned above are suitable for a numeric comparison between <strong>motifels</strong> using <strong>similarity metrics</strong> (more about them below). There are three additionals <strong>signatures</strong> - “landscape indices” (<code>lind</code>), “selected landscape indices” (<code>linds</code>), and “Shannon entropy” (<code>ent</code>), and their role is mostly limited to describing motifels.</p>
</section>
<section id="similarity-metric" class="level2">
<h2 class="anchored" data-anchor-id="similarity-metric">Similarity metric</h2>
<p>Two of the <strong>signatures</strong> mentioned above, <code>prod</code> and <code>cooc</code>, have a special power - they allow for comparisons between any pair of motifels. In a similar way, as a human can look at two images and decide if they are similar or not, GeoPAT 2 can take two motifels and compare their <strong>signatures</strong>. It also has important advantages comparing to a human perception. Firstly, the GeoPAT 2 results are consistent and reproducible, while human perception can be erratic and differs between individuals. Secondly, GeoPAT 2 gives a numerical value of similarity between two motifels.</p>
<p>As an example, consider the three <strong>motifels</strong> below. They are represented using the co-occurrence histograms.</p>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://jakubnowosad.com/posts/2018-07-06-pattern-based-spatial-analysis-core-ideas/figs/03_motifel_signatures.png" class="img-fluid figure-img"></p>
<figcaption>Comparision of three motifels (landscapes) using spatial co-occurrence of categories</figcaption>
</figure>
</div>
<p>To calculate a similarity between them we need to have a way to measure how these histograms are alike. GeoPAT 2 has several <strong>similarity metrics</strong> that were build to compare two histograms. <!--This numerical value depends on a selected similarity metric.--> It includes Jensen Shannon divergence (<code>jsd</code>), Triangular (<code>tri</code>), Wave-Hedges distance (<code>wh</code>), and Jaccard distance (<code>jac</code>). They are explained in detail in <a href="https://zenodo.org/record/1291123">the GeoPAT 2 manual</a>.</p>
<p>I’ve calculated the Jensen Shannon divergence values between the three examples from the above figure. The smaller this value is the more similar are two <strong>motifels</strong>. The results are, as expected, consistent with the human perception. The first and third motifel are the most similar with the <code>jsd</code> value of 0.003 - both motifels have only three categories and their patterns are analogous. The second motifel is less similar to both the first one (the <code>jsd</code> value of 0.115) and the third one (the <code>jsd</code> value of 0.089). It has an additional land cover category (“grassland”), different proportions of land cover categories and their configurations.</p>
<div class="cell">
<div class="cell-output-display">
<table class="caption-top table table-sm table-striped small">
<caption>Comparision of the Jensen Shannon divergence values between the example motifels (landscapes)</caption>
<thead>
<tr class="header">
<th style="text-align: left;"></th>
<th style="text-align: right;">1</th>
<th style="text-align: right;">2</th>
<th style="text-align: right;">3</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td style="text-align: left;">1</td>
<td style="text-align: right;">0.000</td>
<td style="text-align: right;">0.115</td>
<td style="text-align: right;">0.003</td>
</tr>
<tr class="even">
<td style="text-align: left;">2</td>
<td style="text-align: right;">0.115</td>
<td style="text-align: right;">0.000</td>
<td style="text-align: right;">0.089</td>
</tr>
<tr class="odd">
<td style="text-align: left;">3</td>
<td style="text-align: right;">0.003</td>
<td style="text-align: right;">0.089</td>
<td style="text-align: right;">0.000</td>
</tr>
</tbody>
</table>
</div>
</div>
</section>
<section id="whats-next" class="level2">
<h2 class="anchored" data-anchor-id="whats-next">What’s next</h2>
<p>Understanding the three main ideas behind pattern-based spatial analysis - what is a <strong>motifel</strong>, <strong>signature</strong>, and <strong>similarity metric</strong> - opens a large number of possibilities. In the next blog posts, I will apply them to answer several questions using the real data.</p>


</section>

<div id="quarto-appendix" class="default"><section class="quarto-appendix-contents" id="quarto-reuse"><h2 class="anchored quarto-appendix-heading">Reuse</h2><div class="quarto-appendix-contents"><div><a rel="license" href="https://creativecommons.org/licenses/by/4.0/">CC BY 4.0</a></div></div></section><section class="quarto-appendix-contents" id="quarto-citation"><h2 class="anchored quarto-appendix-heading">Citation</h2><div><div class="quarto-appendix-secondary-label">BibTeX citation:</div><div class="code-copy-outer-scaffold"><pre class="sourceCode quarto-appendix-bibtex code-with-copy"><code class="sourceCode bibtex">@online{nowosad2018,
  author = {Nowosad, Jakub},
  title = {Pattern-Based {Spatial} {Analysis} - Core Ideas},
  date = {2018-07-06},
  url = {https://jakubnowosad.com/posts/2018-07-06-pattern-based-spatial-analysis-core-ideas/},
  doi = {10.59350/kkvsr-2am60},
  langid = {en}
}
</code></pre></div><div class="quarto-appendix-secondary-label">For attribution, please cite this work as:</div><div id="ref-nowosad2018" class="csl-entry quarto-appendix-citeas">
Nowosad, Jakub. 2018. <span>“Pattern-Based Spatial Analysis - Core
Ideas.”</span> July 6. <a href="https://doi.org/10.59350/kkvsr-2am60">https://doi.org/10.59350/kkvsr-2am60</a>.
</div></div></section></div> ]]></description>
  <category>spatial</category>
  <category>pattern</category>
  <category>spatiotemporal</category>
  <category>geopat</category>
  <category>sil</category>
  <guid>https://jakubnowosad.com/posts/2018-07-06-pattern-based-spatial-analysis-core-ideas/</guid>
  <pubDate>Fri, 06 Jul 2018 00:00:00 GMT</pubDate>
</item>
<item>
  <title>Life (expectancy), animated</title>
  <dc:creator>Jakub Nowosad</dc:creator>
  <link>https://jakubnowosad.com/posts/2018-07-05-life-expectancy-animated/</link>
  <description><![CDATA[ 





<p>Global socio-economic data is easily accessible nowadays. Just type the indicator of interest and the name of the country in your preferred search engine and you can find its value, sometimes also an additional plot or a map. But what about when you want to go further and (for example):</p>
<ul>
<li>Want to compare many countries?</li>
<li>Get data just for a specific year?</li>
<li>See changes in time?</li>
<li>Just want to create a very specific plot or a map?</li>
</ul>
<p>In this blog post, I will show how to download, process, and present global socio-economic data, using the following packages:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(wbstats) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># the World Bank data downloading</span></span>
<span id="cb1-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(dplyr) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># data manipulation </span></span>
<span id="cb1-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(tidyr) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># data manipulation </span></span>
<span id="cb1-4"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(purrr) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># data manipulation </span></span>
<span id="cb1-5"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(ggplot2) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># data visualization </span></span>
<span id="cb1-6"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(sf) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># spatial data manipulation</span></span>
<span id="cb1-7"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(rnaturalearth) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># access to spatial data</span></span>
<span id="cb1-8"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># devtools::install_github("mtennekes/tmap")</span></span>
<span id="cb1-9"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(tmap) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># spatial data visualization</span></span></code></pre></div></div>
</div>
<section id="world-bank-data" class="level2">
<h2 class="anchored" data-anchor-id="world-bank-data">World Bank Data</h2>
<p>The World Bank Data is a source of various global socio-economic indicators with a large database of historical records. Its API is, for instance, accessible through the R package <strong>wbstats</strong> package. The two most important functions of this package are <code>wbsearch()</code> and <code>wb()</code>. The first one allows for searching for available indicators, and the second one is used for downloading the data. Read <a href="https://cran.r-project.org/web/packages/wbstats/README.html">the vignette “wbstats: An R package for searching and downloading data from the World Bank API”</a> to learn more about this package.</p>
<p>Here, I would like to create a time-series of a selected indicator. The function below downloads data for a given indicator and a year. Secondly, it selects only the variables of interest and reshapes them into the wide format.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb2-1">wb_data_create <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(indicator, new_name, year, ...){</span>
<span id="cb2-2">  df <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">wb</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">indicator =</span> indicator,</span>
<span id="cb2-3">          <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">startdate =</span> year, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">enddate =</span> year, ...) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb2-4">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">select</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">iso_a2 =</span> iso2c, value, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">year =</span> date) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb2-5">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">indicator =</span> new_name) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb2-6">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">spread</span>(indicator, value) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb2-7">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as_tibble</span>()</span>
<span id="cb2-8">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># return the dataframe</span></span>
<span id="cb2-9">  df</span>
<span id="cb2-10">}</span></code></pre></div></div>
</div>
<p>Importantly, this function can be also used to download data from many years using the <code>map_dfr()</code> function. As an example, let’s download a data of life expectancy at birth for every five years from 1963 to 2013 on a country level:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb3-1">data_life_exp <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">seq</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1963</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2013</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">by =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb3-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">map_dfr</span>(wb_data_create,</span>
<span id="cb3-3">          <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">indicator =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"SP.DYN.LE00.IN"</span>,</span>
<span id="cb3-4">          <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">new_name =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"life_exp"</span>,</span>
<span id="cb3-5">          <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">country =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"countries_only"</span>)</span></code></pre></div></div>
</div>
<p>This new dataset, <code>data_life_exp</code>, already allows analyzing temporal changes in life expectancy. For example, we can calculate the mean life expectancy of all countries:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb4-1">data_life_exp_avg <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> data_life_exp <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb4-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">group_by</span>(year) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb4-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">summarise</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mean_life_exp =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mean</span>(life_exp, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">na.rm =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>))</span></code></pre></div></div>
</div>
<p>Let’s visualize the life expectancy. Each grey line represents one country, and the red one shows the mean life expectancy.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb5-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb5-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_line</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> data_life_exp, </span>
<span id="cb5-3">            <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(year, life_exp, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">group =</span> iso_a2), </span>
<span id="cb5-4">            <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"gray40"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb5-5">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_line</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> data_life_exp_avg,</span>
<span id="cb5-6">            <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(year, mean_life_exp, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">group =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>),</span>
<span id="cb5-7">            <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"red"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb5-8">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Year"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Life expectancy"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb5-9">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_minimal</span>()</span></code></pre></div></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://jakubnowosad.com/posts/2018-07-05-life-expectancy-animated/index_files/figure-html/unnamed-chunk-5-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
</section>
<section id="spatialize-it" class="level2">
<h2 class="anchored" data-anchor-id="spatialize-it">Spatialize it</h2>
<p>Overall, we can see a stable increase in life expectancy over the last 55 years. However, are there any differences between countries? What is the spatial distribution of the life expectancy? To answer that, we can download spatial data of the world using the <strong>rnaturalearth</strong> package:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb6" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb6-1">world <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ne_countries</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">returnclass =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"sf"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb6-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">select</span>(iso_a2, name_long, continent)</span></code></pre></div></div>
</div>
<p>Next, we need to combine the new spatial data with our non-spatial information of life expectancy. First, the non-spatial data is reshaped into the wide form. This way we are filling missing data with <code>NA</code> instead of removing them As a result, we will have the same number of countries (borders) for each year in our database. Secondly, we are joining the spatial <code>world</code> dataset with the non-spatial <code>data_life_exp_wide</code> using the variable <code>"iso_a2"</code> common to both datasets. The last step is to convert the data back into a long form, where each pair of country and year corresponds to one row. This also means that the spatial geometry for each country will be repeated for each time step.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb7" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb7-1">data_life_exp_wide <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> data_life_exp <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb7-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">spread</span>(year, life_exp)</span>
<span id="cb7-3">world_temporal <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> world <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb7-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">left_join</span>(data_life_exp_wide, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">by =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"iso_a2"</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb7-5">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">gather</span>(year, life_exp, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">`</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">1963</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">`</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">`</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">2013</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">`</span>)</span></code></pre></div></div>
</div>
</section>
<section id="facet-map" class="level2">
<h2 class="anchored" data-anchor-id="facet-map">Facet map</h2>
<p>How to create a map showing temporal changes? One of the most straigforward ways is to use a facet plot:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb8" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb8-1">robinson_projection <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"+proj=robin +lon_0=0 +x_0=0 +y_0=0 +ellps=WGS84 +datum=WGS84 +units=m no_defs"</span></span>
<span id="cb8-2">my_map <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tm_shape</span>(world_temporal, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">projection =</span> robinson_projection) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb8-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tm_fill</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"life_exp"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">title =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Life expectancy"</span>,</span>
<span id="cb8-4">          <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">palette =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"viridis"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb8-5">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tm_facets</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">by =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"year"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ncol =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>)</span>
<span id="cb8-6">my_map</span></code></pre></div></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://jakubnowosad.com/posts/2018-07-05-life-expectancy-animated/index_files/figure-html/unnamed-chunk-8-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<p>Each map facet represents a different year. We can see that, on overall, the map colors shift from darker to more bright, indicating an increase in life expectancy. There is a noticeable spatial similarity (“spatial autocorrelation”) - European countries tend to have a longer life expectancy, while the lowest life expectancy is in Africa. This visualization also reveals (or just helps to see) a lot more, for example, life expectancy in China increased visibly faster than in neighboring countries or that there are missing values for Greenland before 1978.</p>
<p>There is, however, a downside of facet maps - each facet is relatively small and it is hard to see any details without zooming in. Alternatively, an animated map can be created, where each facet would be a next image frame.</p>
</section>
<section id="animated-map" class="level2">
<h2 class="anchored" data-anchor-id="animated-map">Animated map</h2>
<p>The code to create an animated map is almost identical to the facet map code. There is only one important difference - the <code>along</code> parameter should be used instead of <code>by</code>:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb9" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb9-1">my_ani <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tm_shape</span>(world_temporal, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">projection =</span> robinson_projection) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb9-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tm_fill</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"life_exp"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">title =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Life expectation"</span>,</span>
<span id="cb9-3">          <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">palette =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"viridis"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb9-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tm_facets</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">along =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"year"</span>)</span></code></pre></div></div>
</div>
<p>The output, <code>my_ani</code>, is a set of maps - one map per year. It can be further converted into a proper animation with the <code>tmap_animation()</code> function:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb10" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb10-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tmap_animation</span>(my_ani, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">filename =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"life_exp_animation.gif"</span>,</span>
<span id="cb10-2">               <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">width =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1500</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">height =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">600</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">delay =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">60</span>)</span></code></pre></div></div>
</div>
<p><img src="https://jakubnowosad.com/posts/2018-07-05-life-expectancy-animated/figs/life_exp_animation.gif" class="img-fluid"></p>
<p>The output presents the animated changes in life expectancy. The overall trend is still visible, but also small changes can be seen. It includes, for example, decreases in life expectancy in Cambodia in the 1970s and Rwanda in the 1990s.</p>
</section>
<section id="bonus" class="level2">
<h2 class="anchored" data-anchor-id="bonus">Bonus</h2>
<p>It is also possible to combine the two above methods - facet and animated map. Let’s try it on the data from South America:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb11" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb11-1">world_temporal_sa <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</span>(world_temporal,</span>
<span id="cb11-2">                           continent <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"South America"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb11-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">na.omit</span>()</span></code></pre></div></div>
</div>
<p>To enable animated facet map, both arguments - <code>by</code> and <code>along</code> need to be set. Here, countries will be presented on facets and each animation frame is a different year. Feel free to test what would happen if you switch the arguments of <code>by</code> and <code>along</code>.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb12" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb12-1">my_ani2 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tm_shape</span>(world_temporal_sa) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb12-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tm_polygons</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"life_exp"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">title =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Life expectation"</span>,</span>
<span id="cb12-3">          <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">palette =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"viridis"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb12-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tm_facets</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">by =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"name_long"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">along =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"year"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">nrow =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>)</span>
<span id="cb12-5"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tmap_animation</span>(my_ani2, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">filename =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"life_exp_sa_animation.gif"</span>,</span>
<span id="cb12-6">               <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">width =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1600</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">height =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1000</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">delay =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">60</span>)</span></code></pre></div></div>
</div>
<p><img src="https://jakubnowosad.com/posts/2018-07-05-life-expectancy-animated/figs/life_exp_sa_animation.gif" class="img-fluid"></p>
<p>This way of presentation has a nice property - it allows for focus on values, instead of countries location or size. You can easily see values of the smallest countries and compare them to the largest ones.</p>
<p>I must admit that the bonus visualization example might not be the most appropriate. This visualization method is the best suited for comparing continuous features. For example, imagine animated facet maps comparing continuous surface phenomena (e.g.&nbsp;temperature or air quality) for each country through time. Try this method for yourself!</p>
</section>
<section id="final-words" class="level2">
<h2 class="anchored" data-anchor-id="final-words">Final words</h2>
<p>This blog post builds on the content in Chapter <a href="http://geocompr.robinlovelace.net/adv-map.html#inset-maps">8</a> of the book <strong>Geocomputation with R</strong>. To learn more about spatial data analysis, visualization and modeling using R visit the online version of the <strong>Geocomputation with R</strong> book at https://geocompr.robinlovelace.net/.</p>
</section>
<section id="acknowledgement" class="level2">
<h2 class="anchored" data-anchor-id="acknowledgement">Acknowledgement</h2>
<p>I’d like to thanks to Tal Galili (creator of https://www.r-bloggers.com/ and https://www.r-users.com/), and Maëlle Salmon, Bruce Zhao, Colin Fay, Eric Nantz, Hao Zhu, Jasmine Dumas, Jon Calder, Jonathan Carroll, Kun Ren, Tracy Shen, Wolfram Qin (team members of https://rweekly.org) for doing a great work of gathering and spreading blog posts, tutorials, and many other R-related resources. Keep your awesome work!</p>


</section>

<div id="quarto-appendix" class="default"><section class="quarto-appendix-contents" id="quarto-reuse"><h2 class="anchored quarto-appendix-heading">Reuse</h2><div class="quarto-appendix-contents"><div><a rel="license" href="https://creativecommons.org/licenses/by/4.0/">CC BY 4.0</a></div></div></section><section class="quarto-appendix-contents" id="quarto-citation"><h2 class="anchored quarto-appendix-heading">Citation</h2><div><div class="quarto-appendix-secondary-label">BibTeX citation:</div><div class="code-copy-outer-scaffold"><pre class="sourceCode quarto-appendix-bibtex code-with-copy"><code class="sourceCode bibtex">@online{nowosad2018,
  author = {Nowosad, Jakub},
  title = {Life (Expectancy), Animated},
  date = {2018-07-05},
  url = {https://jakubnowosad.com/posts/2018-07-05-life-expectancy-animated/},
  doi = {10.59350/w209f-vfa90},
  langid = {en}
}
</code></pre></div><div class="quarto-appendix-secondary-label">For attribution, please cite this work as:</div><div id="ref-nowosad2018" class="csl-entry quarto-appendix-citeas">
Nowosad, Jakub. 2018. <span>“Life (Expectancy), Animated.”</span> July
5. <a href="https://doi.org/10.59350/w209f-vfa90">https://doi.org/10.59350/w209f-vfa90</a>.
</div></div></section></div> ]]></description>
  <category>book</category>
  <category>geocompr</category>
  <category>rstats</category>
  <category>spatial</category>
  <category>vis</category>
  <guid>https://jakubnowosad.com/posts/2018-07-05-life-expectancy-animated/</guid>
  <pubDate>Thu, 05 Jul 2018 00:00:00 GMT</pubDate>
</item>
<item>
  <title>GeoPAT 2: Software for Pattern-Based Spatial and Temporal Analysis</title>
  <dc:creator>Jakub Nowosad</dc:creator>
  <link>https://jakubnowosad.com/posts/2018-06-18-geopat-2-software-for-pattern-based-spatial-and-temporal-analysis/</link>
  <description><![CDATA[ 





<hr>
<p>Blog posts in the series introducing <a href="http://sil.home.amu.edu.pl/index.php?id=geopat2">GeoPAT 2 - a software for pattern-based spatial and temporal analysis</a>:</p>
<ol type="1">
<li><a href="../../posts/2018-06-18-geopat-2-software-for-pattern-based-spatial-and-temporal-analysis/index.html">GeoPAT 2: Software for Pattern-Based Spatial and Temporal Analysis</a></li>
<li><a href="../../posts/2018-07-06-pattern-based-spatial-analysis-core-ideas/index.html">Pattern-based Spatial Analysis - core ideas</a></li>
<li><a href="../../posts/2018-07-16-geopat-2-search/index.html">Finding similar local landscapes</a></li>
<li><a href="../../posts/2018-07-24-geopat-2-compare/index.html">Quantifying temporal change of landscape pattern</a></li>
<li><a href="../../posts/2018-08-13-geopat-2-segmentation/index.html">Pattern-based regionalization</a></li>
<li><a href="../../posts/2018-08-28-geopat-2-extend/index.html">Moving beyond pattern-based analysis: Additional applications of GeoPAT 2</a>
<hr></li>
</ol>
<p>Take a look at the satellite image below - do you see buildings? Roads? A river? Are you able to draw a line separating a building and a road? It is a rather easy, but time-consuming, task for a person. Gladly, there is a whole discipline <a href="https://gisgeography.com/obia-object-based-image-analysis-geobia/">GEOBIA (Geographic Object-Based Image Analysis)</a>, focused on extracting meaningful objects from remote sensing imagery.</p>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://jakubnowosad.com/posts/2018-06-18-geopat-2-software-for-pattern-based-spatial-and-temporal-analysis/figs/fig1.png" class="img-fluid figure-img" width="600"></p>
<figcaption>Example of a satellite image. Source: https://www.orfeo-toolbox.org/documentation/</figcaption>
</figure>
</div>
<p>Now, try to do the same with the image below representing a land cover over a part of Eastern Australia. Do you see any objects? If not, are you able to identify different types of patterns? Can you partition this image into meaningful, consistent groups of patterns? How about distinguishing spatial patterns over the data for the entire continent or the whole world?</p>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://jakubnowosad.com/posts/2018-06-18-geopat-2-software-for-pattern-based-spatial-and-temporal-analysis/figs/fig2.png" class="img-fluid figure-img" width="600"></p>
<figcaption>Land cover map of Eastern Australia</figcaption>
</figure>
</div>
<p>This is not an easy task for a human eye - not only it is time-consuming, but it is also hardly reproducible. Those are some of the reasons why we developed GeoPAT 2 - an open-source software for pattern-based spatial and temporal analysis. It works on categorical maps (e.g.&nbsp;land cover, <a href="https://grass.osgeo.org/grass78/manuals/r.geomorphon.html">geomorphons</a>) and its main focus is on blocks of pixels (and therefore patterns), rather than values of single pixels. This approach makes it possible to asses characteristics of a local pattern (the block of pixels), but also compare its pattern with other “blocks”.</p>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://jakubnowosad.com/posts/2018-06-18-geopat-2-software-for-pattern-based-spatial-and-temporal-analysis/figs/fig3.png" class="img-fluid figure-img" width="600"></p>
<figcaption>Pattern-based segmentation of a land cover in Eastern Australia</figcaption>
</figure>
</div>
<p>Somebody can ask - is there GeoPAT 1? What is the difference between GeoPAT and GeoPAT 2? The first version of the GeoPAT software was developed for GRASS GIS and its main role was to test the ideas and <a href="https://www.sciencedirect.com/science/article/pii/S0098300415000758">build the foundations of to the pattern-based spatial analysis</a>. There are several changes between the first and the second version, but the most prominent one is GeoPAT 2 do not require GRASS GIS to be run. It a standalone software, which the only requirement is <a href="http://www.gdal.org/">GDAL</a>. There is also a number of additional changes between the versions, including experimental support for <a href="http://sil.home.amu.edu.pl/data/uploads/pdf/conference_papers/gc17-netzelstepinskileeds2017.pdf">analysis of patterns in time-series spatial data</a>.</p>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://jakubnowosad.com/posts/2018-06-18-geopat-2-software-for-pattern-based-spatial-and-temporal-analysis/figs/fig4.png" class="img-fluid figure-img" width="600"></p>
<figcaption>Command-line interface of GeoPAT 2</figcaption>
</figure>
</div>
<p>The pattern-based analysis allows for four main types of analysis:</p>
<ul>
<li><strong>Search</strong> - finding areas similar to a queried local landscape</li>
<li><strong>Change detection</strong> - comparing local pattern between two maps</li>
<li><strong>Segmentation</strong> - partitioning a map into regions of cohesive patterns</li>
<li><strong>Clustering</strong> - grouping patterns that are similar</li>
</ul>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://jakubnowosad.com/posts/2018-06-18-geopat-2-software-for-pattern-based-spatial-and-temporal-analysis/figs/fig5.png" class="img-fluid figure-img" width="600"></p>
<figcaption>Basic types of the pattern-based analysis</figcaption>
</figure>
</div>
<p>In a series of blog posts, I will explain basic concepts of pattern-based spatial analysis and give practical examples of the GeoPAT 2 use. The next blog post will focus on the core ideas - what is a motifel, spatial signature and similarity metric. In the meantime, you can download the latest version of this software (either as a Windows installer or Linux source code) from <a href="https://github.com/Nowosad/geopat2">our Github webpage</a>. Additional information can be also found in <a href="https://zenodo.org/record/1291123">the official GeoPAT 2 manual</a> and presentation <a href="https://zenodo.org/record/1209418">Describing landscapes by statistics of local pattern features</a>.</p>



<div id="quarto-appendix" class="default"><section class="quarto-appendix-contents" id="quarto-reuse"><h2 class="anchored quarto-appendix-heading">Reuse</h2><div class="quarto-appendix-contents"><div><a rel="license" href="https://creativecommons.org/licenses/by/4.0/">CC BY 4.0</a></div></div></section><section class="quarto-appendix-contents" id="quarto-citation"><h2 class="anchored quarto-appendix-heading">Citation</h2><div><div class="quarto-appendix-secondary-label">BibTeX citation:</div><div class="code-copy-outer-scaffold"><pre class="sourceCode quarto-appendix-bibtex code-with-copy"><code class="sourceCode bibtex">@online{nowosad2018,
  author = {Nowosad, Jakub},
  title = {GeoPAT 2: {Software} for {Pattern-Based} {Spatial} and
    {Temporal} {Analysis}},
  date = {2018-06-18},
  url = {https://jakubnowosad.com/posts/2018-06-18-geopat-2-software-for-pattern-based-spatial-and-temporal-analysis/},
  doi = {10.59350/1qge1-w4689},
  langid = {en}
}
</code></pre></div><div class="quarto-appendix-secondary-label">For attribution, please cite this work as:</div><div id="ref-nowosad2018" class="csl-entry quarto-appendix-citeas">
Nowosad, Jakub. 2018. <span>“GeoPAT 2: Software for Pattern-Based
Spatial and Temporal Analysis.”</span> June 18. <a href="https://doi.org/10.59350/1qge1-w4689">https://doi.org/10.59350/1qge1-w4689</a>.
</div></div></section></div> ]]></description>
  <category>spatial</category>
  <category>pattern</category>
  <category>spatiotemporal</category>
  <category>geopat</category>
  <category>sil</category>
  <guid>https://jakubnowosad.com/posts/2018-06-18-geopat-2-software-for-pattern-based-spatial-and-temporal-analysis/</guid>
  <pubDate>Mon, 18 Jun 2018 00:00:00 GMT</pubDate>
</item>
<item>
  <title>Making maps of the USA with R: alternative layout</title>
  <dc:creator>Jakub Nowosad</dc:creator>
  <link>https://jakubnowosad.com/posts/2018-03-11-making-alternative-inset-maps-of-the-usa/</link>
  <description><![CDATA[ 





<p><strong>Update (2018-12-12):</strong> Alternative approach to this problem can be found at https://github.com/Nowosad/us-map-alternative-layout.</p>
<section id="introduction" class="level2">
<h2 class="anchored" data-anchor-id="introduction">Introduction</h2>
<p>Maps of United States often focus only on the contiguous 48 states. In many maps Alaska and Hawaii <a href="http://www.freeworldmaps.net/northamerica/united-states/us-political-map-big.jpg">are simply not shown</a> or are displayed at <a href="https://upload.wikimedia.org/wikipedia/commons/a/a5/Map_of_USA_with_state_names.svg">different geographic scales</a> than the main map. This article shows how to create inset maps of the USA, building on a chapter in the in-development book <a href="http://geocompr.robinlovelace.net/">Geocomputation with R</a> that shows all its states and ensures relative sizes are preserved.</p>
<p>It requires up-to-date versions of the following packages to be loaded:<sup>1</sup></p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># devtools::install_github("robinlovelace/geocompr") # install pkgs</span></span>
<span id="cb1-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(spData) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># contains datasets used in this example</span></span>
<span id="cb1-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(sf)     <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># for handling spatial (vector) data</span></span>
<span id="cb1-4"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(tmap)   <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># map making package</span></span>
<span id="cb1-5"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(grid)   <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># to re-arrange maps</span></span></code></pre></div></div>
</div>
</section>
<section id="data-preparation" class="level2">
<h2 class="anchored" data-anchor-id="data-preparation">Data preparation</h2>
<p>First, we must decide on the best projection for each inset. For this case, we will use equal area projection. Hawaii and Alaska already come with an appropriate projection (US National Atlas Equal Area) but <code>us_states</code> (provided by <strong>spData</strong>) must be reprojected:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb2-1">us_states2163 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">st_transform</span>(us_states, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2163</span>)</span></code></pre></div></div>
</div>
</section>
<section id="ratio-calculation" class="level2">
<h2 class="anchored" data-anchor-id="ratio-calculation">Ratio calculation</h2>
<p>The next step is to calculate scale relations between the mainland and Hawaii as well as Alaska. To do so we can calculate latitude ranges of the bounding box of each object:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb3-1">us_states_range <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">st_bbox</span>(us_states2163)[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>] <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">st_bbox</span>(us_states2163)[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>]</span>
<span id="cb3-2">hawaii_range <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">st_bbox</span>(hawaii)[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>] <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">st_bbox</span>(hawaii)[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>]</span>
<span id="cb3-3">alaska_range <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">st_bbox</span>(alaska)[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>] <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">st_bbox</span>(alaska)[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>]</span></code></pre></div></div>
</div>
<p>Next, we can calculate the ratios between their areas:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb4-1">us_states_hawaii_ratio <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> hawaii_range <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> us_states_range</span>
<span id="cb4-2">us_states_alaska_ratio <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> alaska_range <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> us_states_range</span></code></pre></div></div>
</div>
</section>
<section id="map-and-inset-map-creation" class="level2">
<h2 class="anchored" data-anchor-id="map-and-inset-map-creation">Map and inset map creation</h2>
<p>In the third step, we need to create single maps for each object. The <code>tm_layout()</code> function is used here to remove map frames and backgrounds or to add titles to our insets:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb5-1">us_states_map <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tm_shape</span>(us_states2163) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb5-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tm_polygons</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb5-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tm_layout</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">frame =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>)</span></code></pre></div></div>
</div>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb6" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb6-1">hawaii_map <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tm_shape</span>(hawaii) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb6-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tm_polygons</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb6-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tm_layout</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">title =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Hawaii"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">frame =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">bg.color =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NA</span>, </span>
<span id="cb6-4">            <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">title.position =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"LEFT"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"BOTTOM"</span>)) </span></code></pre></div></div>
</div>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb7" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb7-1">alaska_map <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tm_shape</span>(alaska) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb7-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tm_polygons</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb7-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tm_layout</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">title =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Alaska"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">frame =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">bg.color =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NA</span>, </span>
<span id="cb7-4">            <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">title.position =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"left"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"TOP"</span>)) </span></code></pre></div></div>
</div>
</section>
<section id="map-arrangement" class="level2">
<h2 class="anchored" data-anchor-id="map-arrangement">Map arrangement</h2>
<p>The final step is to arrange the map, using the <strong>grid</strong> package. The code below creates a new ‘page’ and specifies its layout with two rows - one smaller (for Alaska) and one larger (for the contiguous 48 states). Each inset map is printed in its corresponding viewport, with Hawaii is printed in the bottom of the second row. Note the use of ratios to make the size of Alaska and Hawaii roughly comparable with that of the main land. The final step draws dashed lines (<code>gp = gpar(lty = 2)</code>) to separate mainland from Alaska and Hawaii.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb8" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb8-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">grid.newpage</span>()</span>
<span id="cb8-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">pushViewport</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">viewport</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">layout =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">grid.layout</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>,</span>
<span id="cb8-3">                                           <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">heights =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">unit</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(us_states_alaska_ratio, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>), <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"null"</span>))))</span>
<span id="cb8-4"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">print</span>(alaska_map, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">vp =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">viewport</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">layout.pos.row =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>))</span>
<span id="cb8-5"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">print</span>(us_states_map, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">vp =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">viewport</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">layout.pos.row =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>))</span>
<span id="cb8-6"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">print</span>(hawaii_map, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">vp =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">viewport</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.3</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.07</span>,</span>
<span id="cb8-7">                                <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">height =</span> us_states_hawaii_ratio <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sum</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(us_states_alaska_ratio, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>))))</span>
<span id="cb8-8"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">grid.lines</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.58</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.58</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">gp =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">gpar</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">lty =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>))</span>
<span id="cb8-9"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">grid.lines</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.33</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">gp =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">gpar</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">lty =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>))</span></code></pre></div></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://jakubnowosad.com/posts/2018-03-11-making-alternative-inset-maps-of-the-usa/index_files/figure-html/unnamed-chunk-7-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
</section>
<section id="final-words" class="level2">
<h2 class="anchored" data-anchor-id="final-words">Final words</h2>
<p>The final map is an approximation of areal relationships between the mainland, Alaska and Hawaii. This improves on maps of the United States that only show the mainland or greatly reduce the relative size of Hawaii and Alaska.</p>
<p>But there is scope for further improvement building on the reproducible code above (a fun challenge is to recreate the map without any copy-pasting): I look forward to any suggestions how this map can be improved! Contact me on <a href="https://twitter.com/jakub_nowosad">twitter</a> or github - see information below.</p>
<p>The blogpost builds on the content in Chapter <a href="http://geocompr.robinlovelace.net/adv-map.html#inset-maps">8</a> of the book Geocomputation with R. A regularly updated online version of the Geocomputation with R book can be found at https://geocompr.robinlovelace.net/. It can be edited from <a href="https://github.com/Robinlovelace/geocompr">our github repo</a> or by clicking on the ‘edit’ pencil-shaped symbol at the top left of any part of the book:</p>
<p><a href="https://github.com/Robinlovelace/geocompr/edit/master/README.Rmd"><img src="https://github.com/Robinlovelace/geocompr/raw/master/figures/editme.png" class="img-fluid"></a></p>
<p>I also encourage community contributions on any part of the book, for example by using the <a href="https://github.com/Robinlovelace/geocompr/issues">issue tracker</a>.</p>


</section>


<div id="quarto-appendix" class="default"><section id="footnotes" class="footnotes footnotes-end-of-document"><h2 class="anchored quarto-appendix-heading">Footnotes</h2>

<ol>
<li id="fn1"><p> These packages can be installed individually with <code>install.packages("spData")</code> etc or by uncommenting and executing the first command in the code chunk, which installs all packages needed to reproduce the code in Geocomputation with R. ↩︎</p></li>
</ol>
</section><section class="quarto-appendix-contents" id="quarto-reuse"><h2 class="anchored quarto-appendix-heading">Reuse</h2><div class="quarto-appendix-contents"><div><a rel="license" href="https://creativecommons.org/licenses/by/4.0/">CC BY 4.0</a></div></div></section><section class="quarto-appendix-contents" id="quarto-citation"><h2 class="anchored quarto-appendix-heading">Citation</h2><div><div class="quarto-appendix-secondary-label">BibTeX citation:</div><div class="code-copy-outer-scaffold"><pre class="sourceCode quarto-appendix-bibtex code-with-copy"><code class="sourceCode bibtex">@online{nowosad2018,
  author = {Nowosad, Jakub},
  title = {Making Maps of the {USA} with {R:} Alternative Layout},
  date = {2018-03-13},
  url = {https://jakubnowosad.com/posts/2018-03-11-making-alternative-inset-maps-of-the-usa/},
  doi = {10.59350/ctde5-rmv81},
  langid = {en}
}
</code></pre></div><div class="quarto-appendix-secondary-label">For attribution, please cite this work as:</div><div id="ref-nowosad2018" class="csl-entry quarto-appendix-citeas">
Nowosad, Jakub. 2018. <span>“Making Maps of the USA with R: Alternative
Layout.”</span> March 13. <a href="https://doi.org/10.59350/ctde5-rmv81">https://doi.org/10.59350/ctde5-rmv81</a>.
</div></div></section></div> ]]></description>
  <category>book</category>
  <category>geocompr</category>
  <category>rstats</category>
  <category>spatial</category>
  <category>vis</category>
  <guid>https://jakubnowosad.com/posts/2018-03-11-making-alternative-inset-maps-of-the-usa/</guid>
  <pubDate>Tue, 13 Mar 2018 00:00:00 GMT</pubDate>
</item>
<item>
  <title>Geocomputation with R - the intermission</title>
  <dc:creator>Jakub Nowosad</dc:creator>
  <link>https://jakubnowosad.com/posts/2018-01-20-geocomputation-with-r-the-intermission/</link>
  <description><![CDATA[ 





<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><a href="https://geocompr.robinlovelace.net"><img src="https://jakubnowosad.com/posts/2018-01-20-geocomputation-with-r-the-intermission/geocompr_second_post.png" class="img-fluid figure-img"></a></p>
<figcaption>Foo</figcaption>
</figure>
</div>
<p>Hello everybody! A lot of things have changed since <a href="../../posts/2017-08-10-geocompr/index.html">the last blogpost about Geocomputation with R</a>. In this post I’ll give an update of our progress and our plans for the next chapters.</p>
<section id="third-author" class="level2">
<h2 class="anchored" data-anchor-id="third-author">Third author</h2>
<p>Probably the most important change is having a third author - <a href="http://www.geographie.uni-jena.de/en/Muenchow.html">Jannes Muenchow</a>. He is a GIScientist based at the University of Jena with a keen interest in spatial and geostatistical modeling, algorithm automation and geocomputation. As lead developer of the recent <a href="https://github.com/jannes-m/RQGIS">RQGIS</a> package, he also brings expertise in desktop GIS programs and how we can build ‘bridges’ to them from R.</p>
</section>
<section id="improvements" class="level2">
<h2 class="anchored" data-anchor-id="improvements">Improvements</h2>
<p>A huge amount of effort has gone into creating the Foundations part of the book (Chapters <a href="http://geocompr.robinlovelace.net/intro.html">1</a> to <a href="http://geocompr.robinlovelace.net/read-write.html">6</a>) . These chapters contain tons of useful information about, among other things, <a href="https://geocompr.robinlovelace.net/spatial-class.html#vector-data">vector</a> and <a href="https://geocompr.robinlovelace.net/spatial-class.html#raster-data">raster</a> data structures, <a href="https://geocompr.robinlovelace.net/read-write.html">spatial data import and export</a> and <a href="https://geocompr.robinlovelace.net/attr.html">attribute</a>, <a href="https://geocompr.robinlovelace.net/spatial-operations.html">spatial</a>, and <a href="https://geocompr.robinlovelace.net/transform.html">geometric</a> operations. The latter also gives an overview of one of the corner stones of geocomputation - coordinate reference systems (CRSs), explaining how to make maps like the global map of life expectancy generated by this code:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(sf)</span>
<span id="cb1-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(spData)</span>
<span id="cb1-3">world_mollweide <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">st_transform</span>(world, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">crs =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"+proj=moll"</span>)</span>
<span id="cb1-4"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">plot</span>(world_mollweide[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"lifeExp"</span>], <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">graticule =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">main =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Life Expectancy"</span>)</span></code></pre></div></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://jakubnowosad.com/posts/2018-01-20-geocomputation-with-r-the-intermission/index_files/figure-html/lifeExpplot-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
</section>
<section id="basic-applications" class="level2">
<h2 class="anchored" data-anchor-id="basic-applications">Basic applications</h2>
<p>The second part of the book focuses on basic applications with examples of <a href="https://geocompr.robinlovelace.net/transport.html">transport</a> and <a href="https://geocompr.robinlovelace.net/location.html">location</a> analysis.</p>
<p>The <a href="https://geocompr.robinlovelace.net/transport.html">transport</a> chapter gives an overview of geographic analysis of transport systems and shows it in practice using a city of Bristol as an example. What are patterns of transport behavior in the city? What can be changed? Where the new improvements could be located?</p>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><a href="https://geocompr.robinlovelace.net/transport.html"><img src="https://jakubnowosad.com/posts/2018-01-20-geocomputation-with-r-the-intermission/transport_chapter.png" class="img-fluid figure-img"></a></p>
<figcaption>Foo2</figcaption>
</figure>
</div>
<p>The <a href="https://geocompr.robinlovelace.net/location.html">location</a> chapter shows how to find optimal locations for selected services. The corresponding case study explores where to establish bike stores in metropolitan areas of Germany for a specific target customer group while using freely available census data.</p>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><a href="https://geocompr.robinlovelace.net/location.html"><img src="https://jakubnowosad.com/posts/2018-01-20-geocomputation-with-r-the-intermission/location_chapter.png" class="img-fluid figure-img"></a></p>
<figcaption>Foo3</figcaption>
</figure>
</div>
<p>Both chapters apply command-line based geocomputation introduced in chapters 1-6 to the real world, and answer relevant questions in a reproducible manner with the help of open data and R.</p>
</section>
<section id="our-plans" class="level2">
<h2 class="anchored" data-anchor-id="our-plans">Our plans</h2>
<p>The next part of the book will be about more advanced geocomputational methods. It includes interactions between vector and raster data and spatial visualizations. While, we have a lot of ideas for the next chapters, we are also open to any suggestions about future content of the book so feel free to contact us on twitter - <a href="https://twitter.com/jakub_nowosad">Jakub</a> and <a href="https://twitter.com/robinlovelace">Robin</a> or github - see information below.</p>
<p>We also have the pleasure to be part of <a href="https://geostat-course.org/2018">GEOSTAT 2018</a> - a summer school organized by R and Open Source (OS) GIS developers and enthusiasts. This year, lecturers are (in alphabetical order), <a href="https://twitter.com/VeronicaAndreo">Veronica Andreo</a>, <a href="https://twitter.com/TimSalabim3">Tim Appelhans</a>, <a href="https://twitter.com/RogerBivand">Roger Bivand</a>, <a href="https://twitter.com/tom_hengl">Tomislav Hengl</a>, <a href="https://www.uni-marburg.de/fb19/fachgebiete/umweltinformatik/meyerh/index_html">Hanna Meyer</a>, <a href="https://twitter.com/MarkusNeteler">Markus Neteler</a>, <a href="https://twitter.com/edzerpebesma">Edzer Pebesma</a>, and <a href="https://twitter.com/geospacedman">Barry Rowlingson</a>. Registration closes on 1 February, so don’t hesitate and join us to learn about various aspects of statistical analysis of spatial and spatio-temporal data using Open Source tools! More information at https://geostat-course.org/2018.</p>
</section>
<section id="your-contibution" class="level2">
<h2 class="anchored" data-anchor-id="your-contibution">Your contibution</h2>
<p>We decided early on to develop this book in the open to remove financial barriers to learning open source software. It also enables <a href="https://github.com/Robinlovelace/geocompr/graphs/contributors">community contributions</a>, which we encourage on any part of the book, for example:</p>
<ul>
<li>Improvements to the text, e.g.&nbsp;clarifying unclear sentences, fixing typos</li>
<li>Changes to the code, e.g.&nbsp;to do things in a more efficient way</li>
<li>Suggestions on content in the <a href="https://github.com/Robinlovelace/geocompr/issues">issue tracker</a></li>
</ul>
<p>A regularly updated online version of the book can be found at https://geocompr.robinlovelace.net/ which can be edited from <a href="https://github.com/Robinlovelace/geocompr">our github repo</a> or by clicking on the ‘edit’ pencil-shaped symbol at the top left of any part of the book:</p>
<p><a href="https://github.com/Robinlovelace/geocompr/edit/master/README.Rmd"><img src="https://github.com/Robinlovelace/geocompr/raw/master/figures/editme.png" class="img-fluid"></a></p>


</section>

<div id="quarto-appendix" class="default"><section class="quarto-appendix-contents" id="quarto-reuse"><h2 class="anchored quarto-appendix-heading">Reuse</h2><div class="quarto-appendix-contents"><div><a rel="license" href="https://creativecommons.org/licenses/by/4.0/">CC BY 4.0</a></div></div></section><section class="quarto-appendix-contents" id="quarto-citation"><h2 class="anchored quarto-appendix-heading">Citation</h2><div><div class="quarto-appendix-secondary-label">BibTeX citation:</div><div class="code-copy-outer-scaffold"><pre class="sourceCode quarto-appendix-bibtex code-with-copy"><code class="sourceCode bibtex">@online{nowosad2018,
  author = {Nowosad, Jakub},
  title = {Geocomputation with {R} - the Intermission},
  date = {2018-01-25},
  url = {https://jakubnowosad.com/posts/2018-01-20-geocomputation-with-r-the-intermission/},
  doi = {10.59350/vd1k7-m7k43},
  langid = {en}
}
</code></pre></div><div class="quarto-appendix-secondary-label">For attribution, please cite this work as:</div><div id="ref-nowosad2018" class="csl-entry quarto-appendix-citeas">
Nowosad, Jakub. 2018. <span>“Geocomputation with R - the
Intermission.”</span> January 25. <a href="https://doi.org/10.59350/vd1k7-m7k43">https://doi.org/10.59350/vd1k7-m7k43</a>.
</div></div></section></div> ]]></description>
  <category>book</category>
  <category>geocompr</category>
  <category>rstats</category>
  <category>spatial</category>
  <guid>https://jakubnowosad.com/posts/2018-01-20-geocomputation-with-r-the-intermission/</guid>
  <pubDate>Thu, 25 Jan 2018 00:00:00 GMT</pubDate>
</item>
<item>
  <title>Geocomputation with R - the beginnings</title>
  <dc:creator>Jakub Nowosad</dc:creator>
  <link>https://jakubnowosad.com/posts/2017-08-10-geocompr/</link>
  <description><![CDATA[ 





<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><a href="https://geocompr.robinlovelace.net/"><img src="https://jakubnowosad.com/posts/2017-08-10-geocompr/geocompr_intro.png" class="img-fluid figure-img"></a></p>
<figcaption>Foo</figcaption>
</figure>
</div>
<p>Just over one year ago, I met <a href="https://twitter.com/robinlovelace">Robin</a> in Poznan, Poland, who was teaching as part of the <a href="http://geocentrum.home.amu.edu.pl/">GEOCENTRUM</a> project at the Adam Mickiewicz University. To our surprise, we found that we shared an idea of writing a book explaining how to use R for spatial data analysis.</p>
<p>The timing worked well. Over the next few months, Robin finished Phase II of his project (<a href="http://pct.bike/">the Propensity to Cycle Tool (PCT)</a>) and I completed my PhD and moved to Cincinnati to work in <a href="http://sil.uc.edu">Space Informatics Lab</a>. At the same time, many things developed in R’s spatial ecosystem, most notably the development of the <code>sf</code> package dealing with vector data in R.</p>
<p>At the beginning of this year, we wrote a book proposal which led to a contract with CRC Press to write <em>Geocomputation with R</em>. As of today, we are working on the first five chapters.</p>
<ul>
<li><a href="http://robinlovelace.net/geocompr/intro.html">Introduction</a> - an introduction to this book, our motivation, overview of R’s spatial ecosystem, and R’s spatial history</li>
<li><a href="http://robinlovelace.net/geocompr/spatial-class.html">Geographic data in R</a> - basis of spatial data representation in R, including vector data, raster data, and coordinate reference systems</li>
<li><a href="http://robinlovelace.net/geocompr/attr.html">Attribute data operations</a> - non-spatial operations on spatial data</li>
<li><a href="http://robinlovelace.net/geocompr/spatial-data-operations.html">Spatial data operations</a> - spatial counterparts of the operation in the previous chapter</li>
<li><a href="http://robinlovelace.net/geocompr/read-write.html">Geographic data I/O</a> - ways to read and write spatial data in R</li>
</ul>
<p>The hard copy of the book will be published in late 2018.</p>
<p>We decided early on to develop this book in the open to remove financial barriers to learning open source software. It also enables <a href="https://github.com/Robinlovelace/geocompr/graphs/contributors">community contributions</a>, which we encourage on any part of the book, for example:</p>
<ul>
<li>Improvements to the text, e.g.&nbsp;clarifying unclear sentences, fixing typos</li>
<li>Changes to the code, e.g.&nbsp;to do things in a more efficient way</li>
<li>Suggestions on content in the <a href="https://github.com/Robinlovelace/geocompr/issues">issue tracker</a></li>
</ul>
<p>An up-to-date version of the book can be found at http://robinlovelace.net/geocompr/ which can be edited from <a href="https://github.com/Robinlovelace/geocompr">our github repo</a> or by clicking on the ‘edit’ pencil-shaped symbol at the top left of any part of the book:</p>
<p><a href="https://github.com/Robinlovelace/geocompr/edit/master/README.Rmd"><img src="https://github.com/Robinlovelace/geocompr/raw/master/figures/editme.png" class="img-fluid"></a></p>



<div id="quarto-appendix" class="default"><section class="quarto-appendix-contents" id="quarto-reuse"><h2 class="anchored quarto-appendix-heading">Reuse</h2><div class="quarto-appendix-contents"><div><a rel="license" href="https://creativecommons.org/licenses/by/4.0/">CC BY 4.0</a></div></div></section><section class="quarto-appendix-contents" id="quarto-citation"><h2 class="anchored quarto-appendix-heading">Citation</h2><div><div class="quarto-appendix-secondary-label">BibTeX citation:</div><div class="code-copy-outer-scaffold"><pre class="sourceCode quarto-appendix-bibtex code-with-copy"><code class="sourceCode bibtex">@online{nowosad2017,
  author = {Nowosad, Jakub},
  title = {Geocomputation with {R} - the Beginnings},
  date = {2017-08-10},
  url = {https://jakubnowosad.com/posts/2017-08-10-geocompr/},
  doi = {10.59350/z20kr-gd268},
  langid = {en}
}
</code></pre></div><div class="quarto-appendix-secondary-label">For attribution, please cite this work as:</div><div id="ref-nowosad2017" class="csl-entry quarto-appendix-citeas">
Nowosad, Jakub. 2017. <span>“Geocomputation with R - the
Beginnings.”</span> August 10. <a href="https://doi.org/10.59350/z20kr-gd268">https://doi.org/10.59350/z20kr-gd268</a>.
</div></div></section></div> ]]></description>
  <category>book</category>
  <category>geocompr</category>
  <category>rstats</category>
  <category>spatial</category>
  <guid>https://jakubnowosad.com/posts/2017-08-10-geocompr/</guid>
  <pubDate>Thu, 10 Aug 2017 00:00:00 GMT</pubDate>
</item>
<item>
  <title>Intro to R workshop</title>
  <dc:creator>Jakub Nowosad</dc:creator>
  <link>https://jakubnowosad.com/posts/2017-04-29-intro-to-r/</link>
  <description><![CDATA[ 





<p><img src="https://jakubnowosad.com/posts/2017-04-29-intro-to-r/introtor.png" class="img-fluid"></p>
<p>Last week, I had a pleasure to conduct a workshop for graduate students and faculty in the Department of Geography and GIS at the University of Cincinnati. In two afternoons, a group of mostly beginners, learned a little bit about R, RStudio, data processing, and visualisation, as well as about spatial data analysis in R.</p>
<p>The workshop had four parts:</p>
<ol type="1">
<li><strong>Intro to R</strong>. It introduces basics of R, such as assignment operator, functions, objects, classes, and data types. In this part participants also learn about very useful bits - keyboard shortcuts, how to get help, and what is RStudio project.</li>
<li><strong>Intro to data processing</strong>. It starts with how to index vectors and data frames. Afterwards, basic use of <strong>dplyr</strong> and <strong>tidyr</strong> are presented. This part ends with the introduction of the <em>%&gt;%</em> operator.</li>
<li><strong>Intro to data visualisation</strong>. It explains idea behind <strong>ggplot2</strong> and shows how to create and modify plots in this package. At the end, two packages for interactive plots are introduced - <strong>plotly</strong> and <strong>dygraphs</strong>.</li>
<li><strong>Intro to spatial analysis</strong>. The last part give an introduction to spatial capabilities of R. Basics of vector data processing are shown with examples in <strong>sf</strong> and <strong>sp</strong>, and basic of raster data with the <strong>raster</strong> package. Spatial data visualisation is presented with <strong>rasterVis</strong>, <strong>tmap</strong>, and <strong>leaflet</strong>. This part ends with a description of many R packages for downloading of spatial data, such as <strong>tigris</strong> and <strong>rnaturalearth</strong>.</li>
</ol>
<p><a href="https://nowosad.github.io/presentations.html#workshops-archive">Slides and data are available here.</a></p>
<p>Moreover, each part ends with a list of useful resources - let me know if I missed something.</p>



<div id="quarto-appendix" class="default"><section class="quarto-appendix-contents" id="quarto-reuse"><h2 class="anchored quarto-appendix-heading">Reuse</h2><div class="quarto-appendix-contents"><div><a rel="license" href="https://creativecommons.org/licenses/by/4.0/">CC BY 4.0</a></div></div></section><section class="quarto-appendix-contents" id="quarto-citation"><h2 class="anchored quarto-appendix-heading">Citation</h2><div><div class="quarto-appendix-secondary-label">BibTeX citation:</div><div class="code-copy-outer-scaffold"><pre class="sourceCode quarto-appendix-bibtex code-with-copy"><code class="sourceCode bibtex">@online{nowosad2017,
  author = {Nowosad, Jakub},
  title = {Intro to {R} Workshop},
  date = {2017-04-29},
  url = {https://jakubnowosad.com/posts/2017-04-29-intro-to-r/},
  doi = {10.59350/41663-a5j07},
  langid = {en}
}
</code></pre></div><div class="quarto-appendix-secondary-label">For attribution, please cite this work as:</div><div id="ref-nowosad2017" class="csl-entry quarto-appendix-citeas">
Nowosad, Jakub. 2017. <span>“Intro to R Workshop.”</span> April 29. <a href="https://doi.org/10.59350/41663-a5j07">https://doi.org/10.59350/41663-a5j07</a>.
</div></div></section></div> ]]></description>
  <category>rstats</category>
  <category>workshop</category>
  <category>spatial</category>
  <category>visualization</category>
  <category>processing</category>
  <guid>https://jakubnowosad.com/posts/2017-04-29-intro-to-r/</guid>
  <pubDate>Sat, 29 Apr 2017 00:00:00 GMT</pubDate>
</item>
</channel>
</rss>
