<!DOCTYPE html>
<!-- Generated by pkgdown: do not edit by hand --><html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<meta name="description" content="sfdep">
<title>Emerging Hot Spot Analysis • sfdep</title>
<script src="../deps/jquery-3.6.0/jquery-3.6.0.min.js"></script><meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<link href="../deps/bootstrap-5.3.1/bootstrap.min.css" rel="stylesheet">
<script src="../deps/bootstrap-5.3.1/bootstrap.bundle.min.js"></script><link href="../deps/Noto_Sans_Display-0.4.8/font.css" rel="stylesheet">
<link href="../deps/Noto_Sans_Mono-0.4.8/font.css" rel="stylesheet">
<link href="../deps/Merriweather-0.4.8/font.css" rel="stylesheet">
<!-- Font Awesome icons --><link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.12.1/css/all.min.css" integrity="sha256-mmgLkCYLUQbXn0B1SRqzHar6dCnv9oZFPEC1g1cwlkk=" crossorigin="anonymous">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.12.1/css/v4-shims.min.css" integrity="sha256-wZjR52fzng1pJHwx4aV2AO3yyTOXrcDW7jBpJtTwVxw=" crossorigin="anonymous">
<!-- bootstrap-toc --><script src="https://cdn.jsdelivr.net/gh/afeld/bootstrap-toc@v1.0.1/dist/bootstrap-toc.min.js" integrity="sha256-4veVQbu7//Lk5TSmc7YV48MxtMy98e26cf5MrgZYnwo=" crossorigin="anonymous"></script><!-- headroom.js --><script src="https://cdnjs.cloudflare.com/ajax/libs/headroom/0.11.0/headroom.min.js" integrity="sha256-AsUX4SJE1+yuDu5+mAVzJbuYNPHj/WroHuZ8Ir/CkE0=" crossorigin="anonymous"></script><script src="https://cdnjs.cloudflare.com/ajax/libs/headroom/0.11.0/jQuery.headroom.min.js" integrity="sha256-ZX/yNShbjqsohH1k95liqY9Gd8uOiE1S4vZc+9KQ1K4=" crossorigin="anonymous"></script><!-- clipboard.js --><script src="https://cdnjs.cloudflare.com/ajax/libs/clipboard.js/2.0.6/clipboard.min.js" integrity="sha256-inc5kl9MA1hkeYUt+EC3BhlIgyp/2jDIyBLS6k3UxPI=" crossorigin="anonymous"></script><!-- search --><script src="https://cdnjs.cloudflare.com/ajax/libs/fuse.js/6.4.6/fuse.js" integrity="sha512-zv6Ywkjyktsohkbp9bb45V6tEMoWhzFzXis+LrMehmJZZSys19Yxf1dopHx7WzIKxr5tK2dVcYmaCk2uqdjF4A==" crossorigin="anonymous"></script><script src="https://cdnjs.cloudflare.com/ajax/libs/autocomplete.js/0.38.0/autocomplete.jquery.min.js" integrity="sha512-GU9ayf+66Xx2TmpxqJpliWbT5PiGYxpaG8rfnBEk1LL8l1KGkRShhngwdXK1UgqhAzWpZHSiYPc09/NwDQIGyg==" crossorigin="anonymous"></script><script src="https://cdnjs.cloudflare.com/ajax/libs/mark.js/8.11.1/mark.min.js" integrity="sha512-5CYOlHXGh6QpOFA/TeTylKLWfB3ftPsde7AnmhuitiTX4K5SqCLBeKro6sPS8ilsz1Q4NRx3v8Ko2IBiszzdww==" crossorigin="anonymous"></script><!-- pkgdown --><script src="../pkgdown.js"></script><meta property="og:title" content="Emerging Hot Spot Analysis">
<meta property="og:description" content="sfdep">
<!-- mathjax --><script src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/MathJax.js" integrity="sha256-nvJJv9wWKEm88qvoQl9ekL2J+k/RWIsaSScxxlsrv8k=" crossorigin="anonymous"></script><script src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/config/TeX-AMS-MML_HTMLorMML.js" integrity="sha256-84DKXVJXs0/F8OTMzX4UR909+jtl4G7SPypPavF+GfA=" crossorigin="anonymous"></script><!--[if lt IE 9]>
<script src="https://oss.maxcdn.com/html5shiv/3.7.3/html5shiv.min.js"></script>
<script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
<![endif]-->
</head>
<body>
    <a href="#main" class="visually-hidden-focusable">Skip to contents</a>
    

    <nav class="navbar fixed-top navbar-light navbar-expand-lg bg-light"><div class="container">
    
    <a class="navbar-brand me-2" href="../index.html">sfdep</a>

    <small class="nav-text text-muted me-auto" data-bs-toggle="tooltip" data-bs-placement="bottom" title="">0.2.4</small>

    
    <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbar" aria-controls="navbar" aria-expanded="false" aria-label="Toggle navigation">
      <span class="navbar-toggler-icon"></span>
    </button>

    <div id="navbar" class="collapse navbar-collapse ms-3">
      <ul class="navbar-nav me-auto">
<li class="nav-item">
  <a class="nav-link" href="../reference/index.html">Reference</a>
</li>
<li class="active nav-item dropdown">
  <a href="#" class="nav-link dropdown-toggle" data-bs-toggle="dropdown" role="button" aria-expanded="false" aria-haspopup="true" id="dropdown-vignettes">Vignettes</a>
  <div class="dropdown-menu" aria-labelledby="dropdown-vignettes">
    <a class="dropdown-item" href="../articles/basics-of-sfdep.html">Basics of sfdep</a>
    <a class="dropdown-item" href="../articles/spacetime-s3.html">spacetime and spacetime cubes</a>
    <a class="dropdown-item" href="../articles/colocation-analysis.html">Colocation Quotients</a>
    <a class="dropdown-item" href="../articles/conditional-permutation.html">Conditional permutation</a>
    <a class="dropdown-item" href="../articles/understanding-emerging-hotspots.html">Emerging Hot Spot Analysis</a>
  </div>
</li>
<li class="nav-item">
  <a class="nav-link" href="../news/index.html">Changelog</a>
</li>
<li class="nav-item dropdown">
  <a href="#" class="nav-link dropdown-toggle" data-bs-toggle="dropdown" role="button" aria-expanded="false" aria-haspopup="true" id="dropdown-releases">Releases</a>
  <div class="dropdown-menu" aria-labelledby="dropdown-releases">
    <a class="dropdown-item" href="../articles/Version-0-2-0.html">Version 0.2.0</a>
  </div>
</li>
      </ul>
<form class="form-inline my-2 my-lg-0" role="search">
        <input type="search" class="form-control me-sm-2" aria-label="Toggle navigation" name="search-input" data-search-index="../search.json" id="search-input" placeholder="Search for" autocomplete="off">
</form>

      <ul class="navbar-nav">
<li class="nav-item">
  <a class="external-link nav-link" href="https://github.com/josiahparry/sfdep/" aria-label="github">
    <span class="fab fa fab fa-github fa-lg"></span>
     
  </a>
</li>
      </ul>
</div>

    
  </div>
</nav><div class="container template-article">




<div class="row">
  <main id="main" class="col-md-9"><div class="page-header">
      <img src="" class="logo" alt=""><h1>Emerging Hot Spot Analysis</h1>
            
      
      <small class="dont-index">Source: <a href="https://github.com/josiahparry/sfdep/blob/HEAD/vignettes/articles/understanding-emerging-hotspots.Rmd" class="external-link"><code>vignettes/articles/understanding-emerging-hotspots.Rmd</code></a></small>
      <div class="d-none name"><code>understanding-emerging-hotspots.Rmd</code></div>
    </div>

    
    
<p>Emerging hot spot Analysis (EHSA) is a technique that falls under
exploratory spatial data analysis (ESDA). It combines the traditional
ESDA technique of hot spot analysis using the Getis-Ord Gi* statistic
with the traditional time-series Mann-Kendall test for monotonic
trends.</p>
<p>The goal of EHSA is to evaluate how hot and cold spots are changing
over time. It helps us answer the questions: are they becoming
increasingly hotter, are they cooling down, or are they staying the
same?</p>
<p>In brief, EHSA works by calculating the Gi* for each time period. The
series of Gi* at each location is treated as a time-series and evaluated
for a trend using the Mann-Kendall statistic. The Gi* and the
Mann-Kendall are compared together to create 17 unique classifications
to help better understand how the locations have changed over time.</p>
<p>In this vignette we walk through the Getis-Ord Gi*, the Mann-Kendall,
and how the two work together to in EHSA.</p>
<div class="section level2">
<h2 id="gettis-ord-gi">Gettis-Ord Gi*<a class="anchor" aria-label="anchor" href="#gettis-ord-gi"></a>
</h2>
<p>The Gettis-Ord Gi and Gi* (pronounced gee-eye-star) are one of the
earliest LISAs. The Gi and Gi* measures are typically reported as a
Z-score where high values indicate a high-high cluster and negative
Z-scores indicate a low-low cluster. There are no high-low and low-high
classifications like the local Moran.</p>
<p>“The Gi statistic consist of a ratio of the weighted average of the
values in the neighboring locations, to the sum of all values, not
including the value at the location (<span class="math inline">\(x_i\)</span>)” (<a href="https://geodacenter.github.io/workbook/6b_local_adv/lab6b.html#getis-ord-statistics" class="external-link">Local
Spatial Autocorrelation (2), GeoDa Center</a>).</p>
<p><span class="math display">\[
G_i = \frac{\sum_{j \ne i}W_{ij}X_j}
          {\sum_{j \ne i}X_j}
\]</span></p>
<p>The Gi* statistic includes the focal (or self, or i<sup>th</sup>)
observation in the neighborhood.</p>
<p><span class="math display">\[
G_i* = \frac{\sum_{j}W_{ij}X_j}
          {\sum_{j}X_j}
\]</span></p>
<div class="section level3">
<h3 id="calculating-the-local-gi">Calculating the local Gi*<a class="anchor" aria-label="anchor" href="#calculating-the-local-gi"></a>
</h3>
<p>To calculate the local Gi* using sfdep, we have to be especially
aware of the neighbors list when we create it. By default, when we
create a neighbors list, we exclude the self from the neighbors list
because it seems a little silly to say “I am my own neighbor.” However,
if we want to calculate the local Gi*, we must be sure to explicitly add
it using <code><a href="../reference/include_self.html">include_self()</a></code></p>
<p>Here, we follow the example laid out by the GeoDa center
documentation to calculate and plot the local Gi* statistic for
donations using the Guerry dataset.</p>
<p>First we create a neighbor list ensuring that the self is included
and then create the weights list from the new neighbors list.</p>
<div class="sourceCode" id="cb1"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="kw"><a href="https://rdrr.io/r/base/library.html" class="external-link">library</a></span><span class="op">(</span><span class="va"><a href="https://sfdep.josiahparry.com" class="external-link">sfdep</a></span><span class="op">)</span></span>
<span><span class="kw"><a href="https://rdrr.io/r/base/library.html" class="external-link">library</a></span><span class="op">(</span><span class="va"><a href="https://dplyr.tidyverse.org" class="external-link">dplyr</a></span><span class="op">)</span></span>
<span></span>
<span><span class="va">guerry_nb</span> <span class="op">&lt;-</span> <span class="va">guerry</span> <span class="op">|&gt;</span> </span>
<span>  <span class="fu"><a href="https://dplyr.tidyverse.org/reference/mutate.html" class="external-link">mutate</a></span><span class="op">(</span></span>
<span>    nb <span class="op">=</span> <span class="fu"><a href="../reference/include_self.html">include_self</a></span><span class="op">(</span><span class="fu"><a href="../reference/st_contiguity.html">st_contiguity</a></span><span class="op">(</span><span class="va">geometry</span><span class="op">)</span><span class="op">)</span>,</span>
<span>         wt <span class="op">=</span> <span class="fu"><a href="../reference/st_weights.html">st_weights</a></span><span class="op">(</span><span class="va">nb</span><span class="op">)</span></span>
<span>    <span class="op">)</span> </span></code></pre></div>
<p>Following, we calculate the local Gi* using
<code><a href="../reference/local_gstar.html">local_gstar_perm()</a></code> on the <code>donations</code> column
which creates a new data frame column called <code>gi_star</code>. We
then unnest it using <code><a href="https://tidyr.tidyverse.org/reference/unnest.html" class="external-link">tidyr::unnest()</a></code>.</p>
<div class="sourceCode" id="cb2"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="va">donat_gistar</span> <span class="op">&lt;-</span> <span class="va">guerry_nb</span> <span class="op">|&gt;</span> </span>
<span>  <span class="fu"><a href="https://dplyr.tidyverse.org/reference/transmute.html" class="external-link">transmute</a></span><span class="op">(</span>gi_star <span class="op">=</span> <span class="fu"><a href="../reference/local_gstar.html">local_gstar_perm</a></span><span class="op">(</span><span class="va">donations</span>, <span class="va">nb</span>, <span class="va">wt</span>, nsim <span class="op">=</span> <span class="fl">199</span><span class="op">)</span><span class="op">)</span> <span class="op">|&gt;</span> </span>
<span>  <span class="fu">tidyr</span><span class="fu">::</span><span class="fu"><a href="https://tidyr.tidyverse.org/reference/unnest.html" class="external-link">unnest</a></span><span class="op">(</span><span class="va">gi_star</span><span class="op">)</span></span>
<span></span>
<span><span class="va">donat_gistar</span></span>
<span><span class="co">#&gt; Simple feature collection with 85 features and 10 fields</span></span>
<span><span class="co">#&gt; Geometry type: MULTIPOLYGON</span></span>
<span><span class="co">#&gt; Dimension:     XY</span></span>
<span><span class="co">#&gt; Bounding box:  xmin: 47680 ymin: 1703258 xmax: 1031401 ymax: 2677441</span></span>
<span><span class="co">#&gt; CRS:           NA</span></span>
<span><span class="co">#&gt; <span style="color: #949494;"># A tibble: 85 × 11</span></span></span>
<span><span class="co">#&gt;    gi_star cluster   e_gi     var_gi std_dev p_value p_sim p_folded_sim skewness</span></span>
<span><span class="co">#&gt;      <span style="color: #949494; font-style: italic;">&lt;dbl&gt;</span> <span style="color: #949494; font-style: italic;">&lt;fct&gt;</span>    <span style="color: #949494; font-style: italic;">&lt;dbl&gt;</span>      <span style="color: #949494; font-style: italic;">&lt;dbl&gt;</span>   <span style="color: #949494; font-style: italic;">&lt;dbl&gt;</span>   <span style="color: #949494; font-style: italic;">&lt;dbl&gt;</span> <span style="color: #949494; font-style: italic;">&lt;dbl&gt;</span>        <span style="color: #949494; font-style: italic;">&lt;dbl&gt;</span>    <span style="color: #949494; font-style: italic;">&lt;dbl&gt;</span></span></span>
<span><span class="co">#&gt; <span style="color: #BCBCBC;"> 1</span>  -<span style="color: #BB0000;">1.49</span>  Low     0.011<span style="text-decoration: underline;">1</span> 0.000<span style="text-decoration: underline;">011</span>8   -<span style="color: #BB0000;">1.42</span>   0.156   0.08        0.04     0.912</span></span>
<span><span class="co">#&gt; <span style="color: #BCBCBC;"> 2</span>  -<span style="color: #BB0000;">0.482</span> High    0.012<span style="text-decoration: underline;">2</span> 0.000<span style="text-decoration: underline;">008</span>87  -<span style="color: #BB0000;">0.638</span>  0.523   0.6         0.3      0.813</span></span>
<span><span class="co">#&gt; <span style="color: #BCBCBC;"> 3</span>   0.652 High    0.012<span style="text-decoration: underline;">9</span> 0.000<span style="text-decoration: underline;">008</span>91   0.296  0.768   0.66        0.33     1.00 </span></span>
<span><span class="co">#&gt; <span style="color: #BCBCBC;"> 4</span>  -<span style="color: #BB0000;">1.65</span>  Low     0.010<span style="text-decoration: underline;">2</span> 0.000<span style="text-decoration: underline;">011</span>4   -<span style="color: #BB0000;">1.34</span>   0.181   0.08        0.04     1.72 </span></span>
<span><span class="co">#&gt; <span style="color: #BCBCBC;"> 5</span>  -<span style="color: #BB0000;">1.08</span>  High    0.011<span style="text-decoration: underline;">9</span> 0.000<span style="text-decoration: underline;">014</span>9   -<span style="color: #BB0000;">1.20</span>   0.232   0.11        0.055    1.13 </span></span>
<span><span class="co">#&gt; <span style="color: #BCBCBC;"> 6</span>  -<span style="color: #BB0000;">2.38</span>  Low     0.011<span style="text-decoration: underline;">1</span> 0.000<span style="text-decoration: underline;">007</span>81  -<span style="color: #BB0000;">2.19</span>   0.028<span style="text-decoration: underline;">9</span>  0.01        0.005    0.859</span></span>
<span><span class="co">#&gt; <span style="color: #BCBCBC;"> 7</span>  -<span style="color: #BB0000;">0.362</span> Low     0.011<span style="text-decoration: underline;">4</span> 0.000<span style="text-decoration: underline;">011</span>9   -<span style="color: #BB0000;">0.340</span>  0.734   0.82        0.41     0.744</span></span>
<span><span class="co">#&gt; <span style="color: #BCBCBC;"> 8</span>  -<span style="color: #BB0000;">0.720</span> Low     0.010<span style="text-decoration: underline;">3</span> 0.000<span style="text-decoration: underline;">011</span>8   -<span style="color: #BB0000;">0.451</span>  0.652   0.77        0.385    0.920</span></span>
<span><span class="co">#&gt; <span style="color: #BCBCBC;"> 9</span>  -<span style="color: #BB0000;">1.45</span>  Low     0.011<span style="text-decoration: underline;">2</span> 0.000<span style="text-decoration: underline;">011</span>1   -<span style="color: #BB0000;">1.28</span>   0.201   0.11        0.055    0.916</span></span>
<span><span class="co">#&gt; <span style="color: #BCBCBC;">10</span>  -<span style="color: #BB0000;">1.32</span>  Low     0.010<span style="text-decoration: underline;">5</span> 0.000<span style="text-decoration: underline;">009</span>51  -<span style="color: #BB0000;">1.04</span>   0.300   0.27        0.135    0.982</span></span>
<span><span class="co">#&gt; <span style="color: #949494;"># ℹ 75 more rows</span></span></span>
<span><span class="co">#&gt; <span style="color: #949494;"># ℹ 2 more variables: kurtosis &lt;dbl&gt;, geometry &lt;MULTIPOLYGON&gt;</span></span></span></code></pre></div>
<p>Lastly, we classify the clusters using a combination of
<code><a href="https://dplyr.tidyverse.org/reference/mutate.html" class="external-link">mutate()</a></code> and <code><a href="https://dplyr.tidyverse.org/reference/case_when.html" class="external-link">case_when()</a></code> which is then piped
into a ggplot map. While not a perfect recreation of the GeoDa map, it
is very close—the differences likely due to conditional permutation (see
<a href="/articles/conditional-permutation.html">conditional permutation
vignette</a> for more on significance calculation).</p>
<div class="sourceCode" id="cb3"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="kw"><a href="https://rdrr.io/r/base/library.html" class="external-link">library</a></span><span class="op">(</span><span class="va"><a href="https://ggplot2.tidyverse.org" class="external-link">ggplot2</a></span><span class="op">)</span></span>
<span></span>
<span><span class="va">donat_gistar</span> <span class="op">|&gt;</span> </span>
<span>  <span class="fu"><a href="https://dplyr.tidyverse.org/reference/mutate.html" class="external-link">mutate</a></span><span class="op">(</span>cluster <span class="op">=</span> <span class="fu"><a href="https://dplyr.tidyverse.org/reference/case_when.html" class="external-link">case_when</a></span><span class="op">(</span></span>
<span>    <span class="va">p_folded_sim</span> <span class="op">&gt;</span> <span class="fl">0.05</span> <span class="op">~</span> <span class="st">"Not Significant"</span>,</span>
<span>    <span class="va">p_folded_sim</span> <span class="op">&lt;=</span> <span class="fl">0.05</span> <span class="op">&amp;</span> <span class="va">gi_star</span> <span class="op">&lt;</span> <span class="fl">0</span> <span class="op">~</span> <span class="st">"Low"</span>,</span>
<span>    <span class="va">p_folded_sim</span> <span class="op">&lt;=</span> <span class="fl">0.05</span> <span class="op">&amp;</span> <span class="va">gi_star</span> <span class="op">&gt;</span> <span class="fl">0</span> <span class="op">~</span> <span class="st">"High"</span></span>
<span>  <span class="op">)</span><span class="op">)</span> <span class="op">|&gt;</span> </span>
<span>  <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/ggplot.html" class="external-link">ggplot</a></span><span class="op">(</span><span class="fu"><a href="https://ggplot2.tidyverse.org/reference/aes.html" class="external-link">aes</a></span><span class="op">(</span>fill <span class="op">=</span> <span class="va">cluster</span><span class="op">)</span><span class="op">)</span> <span class="op">+</span></span>
<span>  <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/ggsf.html" class="external-link">geom_sf</a></span><span class="op">(</span>lwd <span class="op">=</span> <span class="fl">0.2</span>, color <span class="op">=</span> <span class="st">"black"</span><span class="op">)</span> <span class="op">+</span></span>
<span>  <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/scale_manual.html" class="external-link">scale_fill_manual</a></span><span class="op">(</span>values <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/base/c.html" class="external-link">c</a></span><span class="op">(</span><span class="st">"High"</span> <span class="op">=</span> <span class="st">"red"</span>,</span>
<span>                               <span class="st">"Low"</span> <span class="op">=</span> <span class="st">"Blue"</span>, </span>
<span>                               <span class="st">"Not Significant"</span> <span class="op">=</span> <span class="st">"white"</span><span class="op">)</span><span class="op">)</span> <span class="op">+</span></span>
<span>  <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/ggtheme.html" class="external-link">theme_void</a></span><span class="op">(</span><span class="op">)</span></span></code></pre></div>
<p><img src="understanding-emerging-hotspots_files/figure-html/unnamed-chunk-4-1.png" width="700"></p>
<p>In EHSA, we calculate this statistic for each time period.</p>
</div>
</div>
<div class="section level2">
<h2 id="mann-kendall-test">Mann-Kendall Test<a class="anchor" aria-label="anchor" href="#mann-kendall-test"></a>
</h2>
<p>With the calculations of the local Gi* for each unit of time for
every geography, we can evaluate how the hotspots change over time. We
incorporate time-series analysis through the use of the Mann-Kendall
(MK) Trend test. The MK test is a nonparametric test that checks for
monotonic trends.</p>
<p>If you don’t recall the term monotonic from your calculus class,
that’s okay. A monotonic series or function is one that only increases
(or decreases) and never changes direction. So long as the function
either stays flat or continues to increase, it is monotonic.</p>
<p>The benefit of the test being nonparametric is that the trend can be
non-linear. And further, the MK test does not check if a time-series is
strictly monotonic—e.g. every subsequent unit is increasing or
decreasing—but if the overall series is increasing or decreasing.</p>
<p>Below is an example of a monotonic upward trend which is not
linear.</p>
<div class="sourceCode" id="cb4"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="va">series</span> <span class="op">&lt;-</span> <span class="fu"><a href="https://rdrr.io/r/base/c.html" class="external-link">c</a></span><span class="op">(</span><span class="fl">0</span>, <span class="fl">1</span>, <span class="fl">1.4</span>, <span class="fl">1.5</span>, <span class="fl">1.6</span>, <span class="fl">5</span>, <span class="fl">5</span>, <span class="fl">5.5</span>, <span class="fl">8</span>, <span class="fu"><a href="https://rdrr.io/r/base/seq.html" class="external-link">seq</a></span><span class="op">(</span><span class="fl">8</span>, <span class="fl">8.5</span>, length.out <span class="op">=</span> <span class="fl">5</span><span class="op">)</span><span class="op">)</span></span>
<span></span>
<span><span class="fu"><a href="https://rdrr.io/r/graphics/plot.default.html" class="external-link">plot</a></span><span class="op">(</span><span class="va">series</span>, type <span class="op">=</span> <span class="st">"l"</span><span class="op">)</span></span></code></pre></div>
<p><img src="understanding-emerging-hotspots_files/figure-html/unnamed-chunk-5-1.png" width="700"></p>
<p>For a more realistic example, let’s take a look a the
<code>economics</code> dataset from <code>ggplot2</code> which contains
data from FRED. We’ll use the <code>psavert</code> variable which
contains data about personal savings rates in the United States.</p>
<div class="sourceCode" id="cb5"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="va">econ</span> <span class="op">&lt;-</span> <span class="fu">ggplot2</span><span class="fu">::</span><span class="va"><a href="https://ggplot2.tidyverse.org/reference/economics.html" class="external-link">economics</a></span></span>
<span></span>
<span><span class="fu"><a href="https://ggplot2.tidyverse.org/reference/ggplot.html" class="external-link">ggplot</a></span><span class="op">(</span><span class="va">econ</span>, <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/aes.html" class="external-link">aes</a></span><span class="op">(</span><span class="va">date</span>, <span class="va">psavert</span><span class="op">)</span><span class="op">)</span> <span class="op">+</span></span>
<span>  <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/geom_path.html" class="external-link">geom_line</a></span><span class="op">(</span><span class="op">)</span> <span class="op">+</span></span>
<span>  <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/ggtheme.html" class="external-link">theme_light</a></span><span class="op">(</span><span class="op">)</span></span></code></pre></div>
<p><img src="understanding-emerging-hotspots_files/figure-html/unnamed-chunk-6-1.png" width="700"></p>
<p>From a visual inspection, we can tell that overall the trend is
decreasing. The MK test can confirm this. We use
<code><a href="https://rdrr.io/pkg/Kendall/man/MannKendall.html" class="external-link">Kendall::MannKendall()</a></code> to test if the trend is
significantly decreasing.</p>
<div class="sourceCode" id="cb6"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="fu">Kendall</span><span class="fu">::</span><span class="fu"><a href="https://rdrr.io/pkg/Kendall/man/MannKendall.html" class="external-link">MannKendall</a></span><span class="op">(</span><span class="va">econ</span><span class="op">$</span><span class="va">psavert</span><span class="op">)</span></span>
<span><span class="co">#&gt; tau = -0.644, 2-sided pvalue =&lt; 2.22e-16</span></span></code></pre></div>
<p>The above results show the <code>tau</code> and p-value. Tau ranges
between -1 and 1 where -1 is a perfectly decreasing series and 1 is a
perfectly increasing series. The p-value is extremely small indicating
that the null-hypothesis (that there is no trend) can be rejected. The
tau is -0.64 which indicates a moderate downward trend.</p>
</div>
<div class="section level2">
<h2 id="the-whole-game">The whole game<a class="anchor" aria-label="anchor" href="#the-whole-game"></a>
</h2>
<p>We’ve gone over briefly the local Gi* and the Mann-Kendall Test. EHSA
combines the both of these to evaluate if there are trends in hot or
cold spots over time.</p>
<p>EHSA utilizes a spacetime cube, or spatio-temporal full grid. Please
see <a href="/articles/spacetime-s3.html">spacetime vignette</a> for
more on spacetime representations. In short, a spacetime cube contains a
regular time-series for each location in a dataset—i.e. there are <span class="math inline">\(n \times m\)</span> observations where n is the
number of locations and m is the number of times.</p>
<p>For each time-slice (i.e. the complete set of geometries for a given
time) the local Gi* is calculated. Say we have 10 times, we will have
for each i <span class="math inline">\(G_{i,t=1}^*, \ldots,
G_{i,t=10}^*\)</span>.</p>
<p>Then, given each location’s time-series of Gi*, they are assessed
using the MK test.</p>
</div>
<div class="section level2">
<h2 id="a-hands-on-example">A hands on example<a class="anchor" aria-label="anchor" href="#a-hands-on-example"></a>
</h2>
<p>We can compute the EHSA manually using sfdep and spacetime classes.
In this example we utilize <a href="https://ui.josiahparry.com/ecometrics.html#ecometrics" class="external-link">Ecometric</a>
Data from the Boston Area Research Initiative (<a href="https://dataverse.harvard.edu/dataset.xhtml?persistentId=doi:10.7910/DVN/XTEJRE" class="external-link">data
and documentation)</a>. These data are generated from 911 calls in
Boston from 2010 - 2020 and aggregated to the Census Block Group (CBG)
level. The column <code>value</code> indicates the calculated prevalence
which is derived from Exploratory Factor Analysis and population
projections.</p>
<div class="section level3">
<h3 id="data">Data<a class="anchor" aria-label="anchor" href="#data"></a>
</h3>
<p>The data are stored in the internal data file
<code>bos-ecometric.csv</code> and <code>bos-ecometric.geojson</code>.
We will read these in to <code>df</code> and <code>geo</code> and will
create a new spacetime object called <code>bos</code> with them.</p>
<blockquote>
<p>Note: 2022-05-31 this data is available only in the development
version, so please install with
<code>remotes::install_github("josiahparry/sfdep")</code>.</p>
</blockquote>
<div class="sourceCode" id="cb7"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="co"># Create objects to store file paths </span></span>
<span><span class="va">df_fp</span> <span class="op">&lt;-</span> <span class="fu"><a href="https://rdrr.io/r/base/system.file.html" class="external-link">system.file</a></span><span class="op">(</span><span class="st">"extdata"</span>, <span class="st">"bos-ecometric.csv"</span>, package <span class="op">=</span> <span class="st">"sfdep"</span><span class="op">)</span></span>
<span><span class="va">geo_fp</span> <span class="op">&lt;-</span> <span class="fu"><a href="https://rdrr.io/r/base/system.file.html" class="external-link">system.file</a></span><span class="op">(</span><span class="st">"extdata"</span>, <span class="st">"bos-ecometric.geojson"</span>, package <span class="op">=</span> <span class="st">"sfdep"</span><span class="op">)</span></span>
<span></span>
<span><span class="co"># read in data </span></span>
<span><span class="va">df</span> <span class="op">&lt;-</span> <span class="fu">readr</span><span class="fu">::</span><span class="fu"><a href="https://readr.tidyverse.org/reference/read_delim.html" class="external-link">read_csv</a></span><span class="op">(</span><span class="va">df_fp</span>, col_types <span class="op">=</span> <span class="st">"ccidD"</span><span class="op">)</span></span>
<span><span class="va">geo</span> <span class="op">&lt;-</span> <span class="fu">sf</span><span class="fu">::</span><span class="fu"><a href="https://r-spatial.github.io/sf/reference/st_read.html" class="external-link">st_read</a></span><span class="op">(</span><span class="va">geo_fp</span><span class="op">)</span></span>
<span><span class="co">#&gt; Reading layer `bos-ecometric' from data source </span></span>
<span><span class="co">#&gt;   `/private/var/folders/zc/tg2xwck53dg9sls8x4ndzdc00000gn/T/Rtmp2RGXTr/temp_libpath2b6f5ea0db20/sfdep/extdata/bos-ecometric.geojson' </span></span>
<span><span class="co">#&gt;   using driver `GeoJSON'</span></span>
<span><span class="co">#&gt; Simple feature collection with 168 features and 1 field</span></span>
<span><span class="co">#&gt; Geometry type: POLYGON</span></span>
<span><span class="co">#&gt; Dimension:     XY</span></span>
<span><span class="co">#&gt; Bounding box:  xmin: -71.19115 ymin: 42.22788 xmax: -70.99445 ymax: 42.3974</span></span>
<span><span class="co">#&gt; Geodetic CRS:  NAD83</span></span>
<span></span>
<span></span>
<span><span class="co"># Create spacetime object called `bos`</span></span>
<span><span class="va">bos</span> <span class="op">&lt;-</span> <span class="fu"><a href="../reference/spacetime.html">spacetime</a></span><span class="op">(</span><span class="va">df</span>, <span class="va">geo</span>, </span>
<span>                 .loc_col <span class="op">=</span> <span class="st">".region_id"</span>,</span>
<span>                 .time_col <span class="op">=</span> <span class="st">"time_period"</span><span class="op">)</span></span>
<span></span>
<span><span class="va">bos</span></span>
<span><span class="co">#&gt; <span style="font-weight: bold; font-style: italic;">spacetime ────</span></span></span>
<span><span class="co">#&gt; <span style="color: #555555;">Context:</span><span style="color: #555555; font-style: italic;">`data`</span></span></span>
<span><span class="co">#&gt; <span style="color: #555555; font-style: italic;">168</span><span style="color: #555555;"> locations `.region_id`</span></span></span>
<span><span class="co">#&gt; <span style="color: #555555; font-style: italic;">10</span><span style="color: #555555;"> time periods `time_period`</span></span></span>
<span><span class="co">#&gt; <span style="color: #B2B2B2;">── </span><span style="color: #555555; font-style: italic;">data context</span><span style="color: #B2B2B2;"> ────────────────────────────────────────────────────────────────</span></span></span>
<span><span class="co">#&gt; <span style="color: #949494;"># A tibble: 1,680 × 5</span></span></span>
<span><span class="co">#&gt;    .region_id  ecometric  year value time_period</span></span>
<span><span class="co">#&gt;  <span style="color: #BCBCBC;">*</span> <span style="color: #949494; font-style: italic;">&lt;chr&gt;</span>       <span style="color: #949494; font-style: italic;">&lt;chr&gt;</span>     <span style="color: #949494; font-style: italic;">&lt;int&gt;</span> <span style="color: #949494; font-style: italic;">&lt;dbl&gt;</span> <span style="color: #949494; font-style: italic;">&lt;date&gt;</span>     </span></span>
<span><span class="co">#&gt; <span style="color: #BCBCBC;"> 1</span> 25025010405 Guns       <span style="text-decoration: underline;">2</span>010  0.35 2010-01-01 </span></span>
<span><span class="co">#&gt; <span style="color: #BCBCBC;"> 2</span> 25025010405 Guns       <span style="text-decoration: underline;">2</span>011  0.89 2011-01-01 </span></span>
<span><span class="co">#&gt; <span style="color: #BCBCBC;"> 3</span> 25025010405 Guns       <span style="text-decoration: underline;">2</span>012  1.2  2012-01-01 </span></span>
<span><span class="co">#&gt; <span style="color: #BCBCBC;"> 4</span> 25025010405 Guns       <span style="text-decoration: underline;">2</span>013  0.84 2013-01-01 </span></span>
<span><span class="co">#&gt; <span style="color: #BCBCBC;"> 5</span> 25025010405 Guns       <span style="text-decoration: underline;">2</span>014  1.5  2014-01-01 </span></span>
<span><span class="co">#&gt; <span style="color: #BCBCBC;"> 6</span> 25025010405 Guns       <span style="text-decoration: underline;">2</span>015  1.15 2015-01-01 </span></span>
<span><span class="co">#&gt; <span style="color: #BCBCBC;"> 7</span> 25025010405 Guns       <span style="text-decoration: underline;">2</span>016  1.48 2016-01-01 </span></span>
<span><span class="co">#&gt; <span style="color: #BCBCBC;"> 8</span> 25025010405 Guns       <span style="text-decoration: underline;">2</span>017  1.64 2017-01-01 </span></span>
<span><span class="co">#&gt; <span style="color: #BCBCBC;"> 9</span> 25025010405 Guns       <span style="text-decoration: underline;">2</span>018  0.49 2018-01-01 </span></span>
<span><span class="co">#&gt; <span style="color: #BCBCBC;">10</span> 25025010405 Guns       <span style="text-decoration: underline;">2</span>019  0.17 2019-01-01 </span></span>
<span><span class="co">#&gt; <span style="color: #949494;"># ℹ 1,670 more rows</span></span></span></code></pre></div>
</div>
<div class="section level3">
<h3 id="local-gi">Local Gi*<a class="anchor" aria-label="anchor" href="#local-gi"></a>
</h3>
<p>The first step in EHSA is to calculate the local Gi* for each time
period. But that, however, requires a couple of preceding steps. Before
we calculate Gi* we need to identify neighbors and weights.</p>
<p>To do this, we activate the geometry context and create two new
columns <code>nb</code> and <code>wt</code>. Then we will activate the
data context again and copy over the nb and wt columns to each
time-slice using <code><a href="../reference/set_col.html">set_nbs()</a></code> and
<code><a href="../reference/set_col.html">set_wts()</a></code>—<strong>row order is very important</strong> so
do not rearrange the observations after using <code><a href="../reference/set_col.html">set_nbs()</a></code> or
<code><a href="../reference/set_col.html">set_wts()</a></code>.</p>
<div class="sourceCode" id="cb8"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="va">bos_nb</span> <span class="op">&lt;-</span> <span class="va">bos</span> <span class="op">|&gt;</span> </span>
<span>  <span class="fu"><a href="../reference/activate.html">activate</a></span><span class="op">(</span><span class="st">"geometry"</span><span class="op">)</span> <span class="op">|&gt;</span> </span>
<span>  <span class="fu"><a href="https://dplyr.tidyverse.org/reference/mutate.html" class="external-link">mutate</a></span><span class="op">(</span></span>
<span>    nb <span class="op">=</span> <span class="fu"><a href="../reference/include_self.html">include_self</a></span><span class="op">(</span><span class="fu"><a href="../reference/st_contiguity.html">st_contiguity</a></span><span class="op">(</span><span class="va">geometry</span><span class="op">)</span><span class="op">)</span>,</span>
<span>    wt <span class="op">=</span> <span class="fu"><a href="../reference/st_weights.html">st_weights</a></span><span class="op">(</span><span class="va">nb</span><span class="op">)</span></span>
<span>  <span class="op">)</span> <span class="op">|&gt;</span> </span>
<span>  <span class="fu"><a href="../reference/set_col.html">set_nbs</a></span><span class="op">(</span><span class="st">"nb"</span><span class="op">)</span> <span class="op">|&gt;</span> </span>
<span>  <span class="fu"><a href="../reference/set_col.html">set_wts</a></span><span class="op">(</span><span class="st">"wt"</span><span class="op">)</span></span>
<span></span>
<span><span class="fu"><a href="https://rdrr.io/r/utils/head.html" class="external-link">head</a></span><span class="op">(</span><span class="va">bos_nb</span><span class="op">)</span></span>
<span><span class="co">#&gt; <span style="font-weight: bold; font-style: italic;">spacetime ────</span></span></span>
<span><span class="co">#&gt; <span style="color: #555555;">Context:</span><span style="color: #555555; font-style: italic;">`data`</span></span></span>
<span><span class="co">#&gt; <span style="color: #555555; font-style: italic;">168</span><span style="color: #555555;"> locations `.region_id`</span></span></span>
<span><span class="co">#&gt; <span style="color: #555555; font-style: italic;">10</span><span style="color: #555555;"> time periods `time_period`</span></span></span>
<span><span class="co">#&gt; <span style="color: #B2B2B2;">── </span><span style="color: #555555; font-style: italic;">data context</span><span style="color: #B2B2B2;"> ────────────────────────────────────────────────────────────────</span></span></span>
<span><span class="co">#&gt; <span style="color: #949494;"># A tibble: 6 × 7</span></span></span>
<span><span class="co">#&gt;   .region_id  ecometric  year value time_period nb        wt       </span></span>
<span><span class="co">#&gt;   <span style="color: #949494; font-style: italic;">&lt;chr&gt;</span>       <span style="color: #949494; font-style: italic;">&lt;chr&gt;</span>     <span style="color: #949494; font-style: italic;">&lt;int&gt;</span> <span style="color: #949494; font-style: italic;">&lt;dbl&gt;</span> <span style="color: #949494; font-style: italic;">&lt;date&gt;</span>      <span style="color: #949494; font-style: italic;">&lt;list&gt;</span>    <span style="color: #949494; font-style: italic;">&lt;list&gt;</span>   </span></span>
<span><span class="co">#&gt; <span style="color: #BCBCBC;">1</span> 25025010405 Guns       <span style="text-decoration: underline;">2</span>010  0.35 2010-01-01  <span style="color: #949494;">&lt;int [9]&gt;</span> <span style="color: #949494;">&lt;dbl [9]&gt;</span></span></span>
<span><span class="co">#&gt; <span style="color: #BCBCBC;">2</span> 25025010404 Guns       <span style="text-decoration: underline;">2</span>010  0    2010-01-01  <span style="color: #949494;">&lt;int [4]&gt;</span> <span style="color: #949494;">&lt;dbl [4]&gt;</span></span></span>
<span><span class="co">#&gt; <span style="color: #BCBCBC;">3</span> 25025010801 Guns       <span style="text-decoration: underline;">2</span>010  0    2010-01-01  <span style="color: #949494;">&lt;int [5]&gt;</span> <span style="color: #949494;">&lt;dbl [5]&gt;</span></span></span>
<span><span class="co">#&gt; <span style="color: #BCBCBC;">4</span> 25025010702 Guns       <span style="text-decoration: underline;">2</span>010  0.46 2010-01-01  <span style="color: #949494;">&lt;int [6]&gt;</span> <span style="color: #949494;">&lt;dbl [6]&gt;</span></span></span>
<span><span class="co">#&gt; <span style="color: #BCBCBC;">5</span> 25025010204 Guns       <span style="text-decoration: underline;">2</span>010  0    2010-01-01  <span style="color: #949494;">&lt;int [4]&gt;</span> <span style="color: #949494;">&lt;dbl [4]&gt;</span></span></span>
<span><span class="co">#&gt; <span style="color: #BCBCBC;">6</span> 25025010802 Guns       <span style="text-decoration: underline;">2</span>010  0    2010-01-01  <span style="color: #949494;">&lt;int [5]&gt;</span> <span style="color: #949494;">&lt;dbl [5]&gt;</span></span></span></code></pre></div>
<p>This dataset now has neighbors and weights for each time-slice. We
can use these new columns to manually calculate the local Gi* for each
location. We can do this by grouping by <code>time_period</code> and
using <code><a href="../reference/local_gstar.html">local_gstar_perm()</a></code>. After which, we unnest the new
data frame column <code>gi_star</code>.</p>
<div class="sourceCode" id="cb9"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="va">gi_stars</span> <span class="op">&lt;-</span> <span class="va">bos_nb</span> <span class="op">|&gt;</span> </span>
<span>  <span class="fu"><a href="https://dplyr.tidyverse.org/reference/group_by.html" class="external-link">group_by</a></span><span class="op">(</span><span class="va">time_period</span><span class="op">)</span> <span class="op">|&gt;</span> </span>
<span>  <span class="fu"><a href="https://dplyr.tidyverse.org/reference/mutate.html" class="external-link">mutate</a></span><span class="op">(</span>gi_star <span class="op">=</span> <span class="fu"><a href="../reference/local_gstar.html">local_gstar_perm</a></span><span class="op">(</span><span class="va">value</span>, <span class="va">nb</span>, <span class="va">wt</span><span class="op">)</span><span class="op">)</span> <span class="op">|&gt;</span> </span>
<span>  <span class="fu">tidyr</span><span class="fu">::</span><span class="fu"><a href="https://tidyr.tidyverse.org/reference/unnest.html" class="external-link">unnest</a></span><span class="op">(</span><span class="va">gi_star</span><span class="op">)</span></span>
<span></span>
<span></span>
<span><span class="va">gi_stars</span> <span class="op">|&gt;</span> </span>
<span>  <span class="fu"><a href="https://dplyr.tidyverse.org/reference/select.html" class="external-link">select</a></span><span class="op">(</span><span class="va">.region_id</span>, <span class="va">time_period</span>, <span class="va">gi_star</span>, <span class="va">p_folded_sim</span><span class="op">)</span></span>
<span><span class="co">#&gt; <span style="color: #949494;"># A tibble: 1,680 × 4</span></span></span>
<span><span class="co">#&gt; <span style="color: #949494;"># Groups:   time_period [10]</span></span></span>
<span><span class="co">#&gt;    .region_id  time_period gi_star p_folded_sim</span></span>
<span><span class="co">#&gt;    <span style="color: #949494; font-style: italic;">&lt;chr&gt;</span>       <span style="color: #949494; font-style: italic;">&lt;date&gt;</span>        <span style="color: #949494; font-style: italic;">&lt;dbl&gt;</span>        <span style="color: #949494; font-style: italic;">&lt;dbl&gt;</span></span></span>
<span><span class="co">#&gt; <span style="color: #BCBCBC;"> 1</span> 25025010405 2010-01-01   -<span style="color: #BB0000;">0.987</span>        0.138</span></span>
<span><span class="co">#&gt; <span style="color: #BCBCBC;"> 2</span> 25025010404 2010-01-01   -<span style="color: #BB0000;">1.37</span>         0.152</span></span>
<span><span class="co">#&gt; <span style="color: #BCBCBC;"> 3</span> 25025010801 2010-01-01   -<span style="color: #BB0000;">1.48</span>         0.134</span></span>
<span><span class="co">#&gt; <span style="color: #BCBCBC;"> 4</span> 25025010702 2010-01-01   -<span style="color: #BB0000;">0.863</span>        0.14 </span></span>
<span><span class="co">#&gt; <span style="color: #BCBCBC;"> 5</span> 25025010204 2010-01-01   -<span style="color: #BB0000;">1.32</span>         0.194</span></span>
<span><span class="co">#&gt; <span style="color: #BCBCBC;"> 6</span> 25025010802 2010-01-01   -<span style="color: #BB0000;">1.48</span>         0.114</span></span>
<span><span class="co">#&gt; <span style="color: #BCBCBC;"> 7</span> 25025010104 2010-01-01   -<span style="color: #BB0000;">1.68</span>         0.066</span></span>
<span><span class="co">#&gt; <span style="color: #BCBCBC;"> 8</span> 25025000703 2010-01-01   -<span style="color: #BB0000;">1.44</span>         0.104</span></span>
<span><span class="co">#&gt; <span style="color: #BCBCBC;"> 9</span> 25025000504 2010-01-01   -<span style="color: #BB0000;">1.42</span>         0.058</span></span>
<span><span class="co">#&gt; <span style="color: #BCBCBC;">10</span> 25025000704 2010-01-01   -<span style="color: #BB0000;">1.62</span>         0.062</span></span>
<span><span class="co">#&gt; <span style="color: #949494;"># ℹ 1,670 more rows</span></span></span></code></pre></div>
<p>With these Gi* measures we can then evaluate each location for a
trend using the Mann-Kendall test.</p>
</div>
<div class="section level3">
<h3 id="mann-kendall-test-1">Mann-Kendall Test<a class="anchor" aria-label="anchor" href="#mann-kendall-test-1"></a>
</h3>
<p>Let’s first take only a single location, <code>25025010403</code>,
and evaluate the Mann-Kendall statistic for it by hand. Then we can
extend this to each location.</p>
<blockquote>
<p>Note that there is a bit of finagling the MK test to make it work
nicely in a dplyr::summarise() call.</p>
</blockquote>
<div class="sourceCode" id="cb10"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="va">cbg</span> <span class="op">&lt;-</span> <span class="va">gi_stars</span> <span class="op">|&gt;</span> </span>
<span>  <span class="fu"><a href="https://dplyr.tidyverse.org/reference/group_by.html" class="external-link">ungroup</a></span><span class="op">(</span><span class="op">)</span> <span class="op">|&gt;</span> </span>
<span>  <span class="fu"><a href="https://dplyr.tidyverse.org/reference/filter.html" class="external-link">filter</a></span><span class="op">(</span><span class="va">.region_id</span> <span class="op">==</span> <span class="st">"25025010403"</span><span class="op">)</span> <span class="op">|&gt;</span> </span>
<span>  <span class="fu"><a href="https://dplyr.tidyverse.org/reference/select.html" class="external-link">select</a></span><span class="op">(</span><span class="va">.region_id</span>, <span class="va">time_period</span>, <span class="va">year</span>, <span class="va">gi_star</span><span class="op">)</span></span></code></pre></div>
<p>We can also visualized this trend.</p>
<div class="sourceCode" id="cb11"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="co"># visualize trend</span></span>
<span><span class="fu"><a href="https://ggplot2.tidyverse.org/reference/ggplot.html" class="external-link">ggplot</a></span><span class="op">(</span><span class="va">cbg</span>, <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/aes.html" class="external-link">aes</a></span><span class="op">(</span><span class="va">time_period</span>, <span class="va">gi_star</span><span class="op">)</span><span class="op">)</span> <span class="op">+</span></span>
<span>  <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/geom_path.html" class="external-link">geom_line</a></span><span class="op">(</span><span class="op">)</span> <span class="op">+</span></span>
<span>  <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/ggtheme.html" class="external-link">theme_light</a></span><span class="op">(</span><span class="op">)</span></span></code></pre></div>
<p><img src="understanding-emerging-hotspots_files/figure-html/unnamed-chunk-12-1.png" width="700"></p>
<div class="sourceCode" id="cb12"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="va">cbg</span> <span class="op">|&gt;</span> </span>
<span>  <span class="fu"><a href="https://dplyr.tidyverse.org/reference/summarise.html" class="external-link">summarise</a></span><span class="op">(</span>mk <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/base/list.html" class="external-link">list</a></span><span class="op">(</span><span class="fu"><a href="https://rdrr.io/r/base/class.html" class="external-link">unclass</a></span><span class="op">(</span><span class="fu">Kendall</span><span class="fu">::</span><span class="fu"><a href="https://rdrr.io/pkg/Kendall/man/MannKendall.html" class="external-link">MannKendall</a></span><span class="op">(</span><span class="va">gi_star</span><span class="op">)</span><span class="op">)</span><span class="op">)</span><span class="op">)</span> <span class="op">|&gt;</span> </span>
<span>  <span class="fu">tidyr</span><span class="fu">::</span><span class="fu"><a href="https://tidyr.tidyverse.org/reference/unnest_wider.html" class="external-link">unnest_wider</a></span><span class="op">(</span><span class="va">mk</span><span class="op">)</span></span>
<span><span class="co">#&gt; <span style="color: #949494;"># A tibble: 1 × 5</span></span></span>
<span><span class="co">#&gt;     tau    sl     S     D  varS</span></span>
<span><span class="co">#&gt;   <span style="color: #949494; font-style: italic;">&lt;dbl&gt;</span> <span style="color: #949494; font-style: italic;">&lt;dbl&gt;</span> <span style="color: #949494; font-style: italic;">&lt;dbl&gt;</span> <span style="color: #949494; font-style: italic;">&lt;dbl&gt;</span> <span style="color: #949494; font-style: italic;">&lt;dbl&gt;</span></span></span>
<span><span class="co">#&gt; <span style="color: #BCBCBC;">1</span> 0.378 0.152    17    45   125</span></span></code></pre></div>
<p>In the above result, <code>sl</code> is the p-value. This result
tells us that there is a slight upward but insignificant trend.</p>
<p>We can replicate this for each location by using
<code><a href="https://dplyr.tidyverse.org/reference/group_by.html" class="external-link">group_by()</a></code>.</p>
<div class="sourceCode" id="cb13"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="va">ehsa_manual</span> <span class="op">&lt;-</span> <span class="va">gi_stars</span> <span class="op">|&gt;</span> </span>
<span>  <span class="fu"><a href="https://dplyr.tidyverse.org/reference/group_by.html" class="external-link">group_by</a></span><span class="op">(</span><span class="va">.region_id</span><span class="op">)</span> <span class="op">|&gt;</span> </span>
<span>  <span class="fu"><a href="https://dplyr.tidyverse.org/reference/summarise.html" class="external-link">summarise</a></span><span class="op">(</span>mk <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/base/list.html" class="external-link">list</a></span><span class="op">(</span><span class="fu"><a href="https://rdrr.io/r/base/class.html" class="external-link">unclass</a></span><span class="op">(</span><span class="fu">Kendall</span><span class="fu">::</span><span class="fu"><a href="https://rdrr.io/pkg/Kendall/man/MannKendall.html" class="external-link">MannKendall</a></span><span class="op">(</span><span class="va">gi_star</span><span class="op">)</span><span class="op">)</span><span class="op">)</span><span class="op">)</span> <span class="op">|&gt;</span> </span>
<span>  <span class="fu">tidyr</span><span class="fu">::</span><span class="fu"><a href="https://tidyr.tidyverse.org/reference/unnest_wider.html" class="external-link">unnest_wider</a></span><span class="op">(</span><span class="va">mk</span><span class="op">)</span></span>
<span></span>
<span><span class="co"># arrange to show significant emerging hot/cold spots</span></span>
<span><span class="va">emerging</span> <span class="op">&lt;-</span> <span class="va">ehsa_manual</span> <span class="op">|&gt;</span> </span>
<span>  <span class="fu"><a href="https://dplyr.tidyverse.org/reference/arrange.html" class="external-link">arrange</a></span><span class="op">(</span><span class="va">sl</span>, <span class="fu"><a href="https://rdrr.io/r/base/MathFun.html" class="external-link">abs</a></span><span class="op">(</span><span class="va">tau</span><span class="op">)</span><span class="op">)</span> <span class="op">|&gt;</span> </span>
<span>  <span class="fu"><a href="https://dplyr.tidyverse.org/reference/slice.html" class="external-link">slice</a></span><span class="op">(</span><span class="fl">1</span><span class="op">:</span><span class="fl">5</span><span class="op">)</span></span>
<span></span>
<span><span class="co"># show top 5 locations</span></span>
<span><span class="va">emerging</span></span>
<span><span class="co">#&gt; <span style="color: #949494;"># A tibble: 5 × 6</span></span></span>
<span><span class="co">#&gt;   .region_id     tau        sl     S     D  varS</span></span>
<span><span class="co">#&gt;   <span style="color: #949494; font-style: italic;">&lt;chr&gt;</span>        <span style="color: #949494; font-style: italic;">&lt;dbl&gt;</span>     <span style="color: #949494; font-style: italic;">&lt;dbl&gt;</span> <span style="color: #949494; font-style: italic;">&lt;dbl&gt;</span> <span style="color: #949494; font-style: italic;">&lt;dbl&gt;</span> <span style="color: #949494; font-style: italic;">&lt;dbl&gt;</span></span></span>
<span><span class="co">#&gt; <span style="color: #BCBCBC;">1</span> 25025120201 -<span style="color: #BB0000;">1</span>     0.000<span style="text-decoration: underline;">083</span>0   -<span style="color: #BB0000;">45</span>    45   125</span></span>
<span><span class="co">#&gt; <span style="color: #BCBCBC;">2</span> 25025090700  0.822 0.001<span style="text-decoration: underline;">28</span>      37    45   125</span></span>
<span><span class="co">#&gt; <span style="color: #BCBCBC;">3</span> 25025060600  0.733 0.004<span style="text-decoration: underline;">21</span>      33    45   125</span></span>
<span><span class="co">#&gt; <span style="color: #BCBCBC;">4</span> 25025100601 -<span style="color: #BB0000;">0.733</span> 0.004<span style="text-decoration: underline;">21</span>     -<span style="color: #BB0000;">33</span>    45   125</span></span>
<span><span class="co">#&gt; <span style="color: #BCBCBC;">5</span> 25025000201 -<span style="color: #BB0000;">0.689</span> 0.007<span style="text-decoration: underline;">29</span>     -<span style="color: #BB0000;">31</span>    45   125</span></span></code></pre></div>
<p>Here we can see there are a few locations that are becoming
significantly colder and others that are becoming significantly
warmer.</p>
</div>
</div>
<div class="section level2">
<h2 id="using-emerging_hotspot_analysis">Using <code>emerging_hotspot_analysis()</code><a class="anchor" aria-label="anchor" href="#using-emerging_hotspot_analysis"></a>
</h2>
<p>While we can do the calculations manually as above, this is limited
in two ways. Primarily that in the above example we used spatial
neighbors only. Whereas in EHSA we can—and likely should—incorporate the
time-lag of our spatial neighbors. Secondly, there are classifications
proposed by ESRI which help us understand how each location is changing
over time. Both of these are handled by the
<code><a href="../reference/emerging_hotspot_analysis.html">emerging_hotspot_analysis()</a></code> function.</p>
<p>This <code><a href="../reference/emerging_hotspot_analysis.html">emerging_hotspot_analysis()</a></code> takes a spacetime
object <code>x</code>, and the quoted name of the variable of interested
in <code>.var</code> at minimum. We can specify the number of time lags
using the argument <code>k</code> which is set to 1 by default.</p>
<div class="sourceCode" id="cb14"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="co"># conduct EHSA</span></span>
<span><span class="va">ehsa</span> <span class="op">&lt;-</span> <span class="fu"><a href="../reference/emerging_hotspot_analysis.html">emerging_hotspot_analysis</a></span><span class="op">(</span></span>
<span>  x <span class="op">=</span> <span class="va">bos</span>, </span>
<span>  .var <span class="op">=</span> <span class="st">"value"</span>, </span>
<span>  k <span class="op">=</span> <span class="fl">1</span>, </span>
<span>  nsim <span class="op">=</span> <span class="fl">99</span></span>
<span><span class="op">)</span></span>
<span></span>
<span><span class="co"># preview some values </span></span>
<span><span class="fu"><a href="https://rdrr.io/r/utils/head.html" class="external-link">head</a></span><span class="op">(</span><span class="va">ehsa</span><span class="op">)</span></span>
<span><span class="co">#&gt; <span style="color: #949494;"># A tibble: 6 × 4</span></span></span>
<span><span class="co">#&gt;   location       tau p_value classification     </span></span>
<span><span class="co">#&gt;   <span style="color: #949494; font-style: italic;">&lt;chr&gt;</span>        <span style="color: #949494; font-style: italic;">&lt;dbl&gt;</span>   <span style="color: #949494; font-style: italic;">&lt;dbl&gt;</span> <span style="color: #949494; font-style: italic;">&lt;chr&gt;</span>              </span></span>
<span><span class="co">#&gt; <span style="color: #BCBCBC;">1</span> 25025010405  0.111  0.721  sporadic coldspot  </span></span>
<span><span class="co">#&gt; <span style="color: #BCBCBC;">2</span> 25025010404 -<span style="color: #BB0000;">0.333</span>  0.210  sporadic coldspot  </span></span>
<span><span class="co">#&gt; <span style="color: #BCBCBC;">3</span> 25025010801 -<span style="color: #BB0000;">0.200</span>  0.474  sporadic coldspot  </span></span>
<span><span class="co">#&gt; <span style="color: #BCBCBC;">4</span> 25025010702 -<span style="color: #BB0000;">0.600</span>  0.020<span style="text-decoration: underline;">0</span> sporadic coldspot  </span></span>
<span><span class="co">#&gt; <span style="color: #BCBCBC;">5</span> 25025010204 -<span style="color: #BB0000;">0.467</span>  0.073<span style="text-decoration: underline;">6</span> persistent coldspot</span></span>
<span><span class="co">#&gt; <span style="color: #BCBCBC;">6</span> 25025010802 -<span style="color: #BB0000;">0.333</span>  0.210  sporadic coldspot</span></span>
<span></span>
<span><span class="co"># evaluate the classifications of our hotspots</span></span>
<span><span class="fu"><a href="https://dplyr.tidyverse.org/reference/count.html" class="external-link">count</a></span><span class="op">(</span><span class="va">ehsa</span>, <span class="va">classification</span><span class="op">)</span></span>
<span><span class="co">#&gt; <span style="color: #949494;"># A tibble: 7 × 2</span></span></span>
<span><span class="co">#&gt;   classification           n</span></span>
<span><span class="co">#&gt;   <span style="color: #949494; font-style: italic;">&lt;chr&gt;</span>                <span style="color: #949494; font-style: italic;">&lt;int&gt;</span></span></span>
<span><span class="co">#&gt; <span style="color: #BCBCBC;">1</span> consecutive hotspot     19</span></span>
<span><span class="co">#&gt; <span style="color: #BCBCBC;">2</span> intensifying hotspot     2</span></span>
<span><span class="co">#&gt; <span style="color: #BCBCBC;">3</span> no pattern detected     14</span></span>
<span><span class="co">#&gt; <span style="color: #BCBCBC;">4</span> persistent coldspot      8</span></span>
<span><span class="co">#&gt; <span style="color: #BCBCBC;">5</span> persistent hotspot       3</span></span>
<span><span class="co">#&gt; <span style="color: #BCBCBC;">6</span> sporadic coldspot       92</span></span>
<span><span class="co">#&gt; <span style="color: #BCBCBC;">7</span> sporadic hotspot        30</span></span></code></pre></div>
</div>
  </main><aside class="col-md-3"><nav id="toc"><h2>On this page</h2>
    </nav></aside>
</div>



    <footer><div class="pkgdown-footer-left">
  <p></p>
<p>Developed by Josiah Parry, Dexter H Locke.</p>
</div>

<div class="pkgdown-footer-right">
  <p></p>
<p>Site built with <a href="https://pkgdown.r-lib.org/" class="external-link">pkgdown</a> 2.0.7.</p>
</div>

    </footer>
</div>

  

  

  </body>
</html>
