<!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>Conditional Permutations with sfdep • 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="Conditional Permutations with sfdep">
<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>Conditional Permutations with sfdep</h1>
            
      
      <small class="dont-index">Source: <a href="https://github.com/josiahparry/sfdep/blob/HEAD/vignettes/articles/conditional-permutation.Rmd" class="external-link"><code>vignettes/articles/conditional-permutation.Rmd</code></a></small>
      <div class="d-none name"><code>conditional-permutation.Rmd</code></div>
    </div>

    
    
<p>In the lattice approach to spatial analysis, calculating p-values is
often done based on a conditional permutation approach as outlined by
Anselin 1995.</p>
<p>Conditional permutation can be summed up with the question “if I were
to hold this observation constant, and change it’s neighbors, will my
statistic be the same?”</p>
<p>To understand this a bit better, lets first obtain our neighbors and
weights lists, as well as our numeric variable.</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>
<span><span class="va">nb</span> <span class="op">&lt;-</span> <span class="fu"><a href="../reference/st_contiguity.html">st_contiguity</a></span><span class="op">(</span><span class="va">guerry</span><span class="op">)</span></span>
<span><span class="va">wt</span> <span class="op">&lt;-</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="va">x</span> <span class="op">&lt;-</span> <span class="va">guerry</span><span class="op">$</span><span class="va">crime_pers</span></span></code></pre></div>
<p>sfdep exports a function <code><a href="../reference/cond_permute_nb.html">cond_permute_nb()</a></code> to allow
users to create conditional permutations of their nb lists. This works
by first identifying the cardinality (number of neighbors) of each
observation. Then, for each location from i through n—where i is the
position index and n is the number of observations—we sample k values,
where k is the number of neighbors for observation i, from the set of
values containing 1 through n minus i <span class="math inline">\(\{1,
..., n\}\setminus i\)</span>.</p>
<div class="section level2">
<h2 id="inference-using-conditional-permutation">Inference using conditional permutation<a class="anchor" aria-label="anchor" href="#inference-using-conditional-permutation"></a>
</h2>
<p>With spatial data, we always start from the assumption of spatial
randomness. Traditional hypothesis testing like t-test often use an
assumption of normality. This assumption, however, is often broken in
spatial data. So <em>analytical</em> p-values (p-values that come from a
reference distribution such as the normal distribution) are often
unreliable, or inaccurate measures of significance. For this reason, in
ESDA, we often use simulated, or pseudo, p-values.</p>
<p>Simulated p-values are calculated by creating a reference
distribution and comparing our observed statistic to the reference
distribution. The reference is created by making <em>M</em> conditional
permutations of our data set and calculating a test statistic. Then the
ratio of times the simulated statistic is greater than the observed
statistic (in either direction) to the number of simulations becomes our
simulated p-value.</p>
<div class="section level3">
<h3 id="worked-example">Worked Example<a class="anchor" aria-label="anchor" href="#worked-example"></a>
</h3>
<p>We can use <code><a href="../reference/cond_permute_nb.html">cond_permute_nb()</a></code> to create a conditional
permutation of the neighbors list. From that permuted neighbor list we
can then create a new weights list and calculate the global Geary C for
each of the permutation.</p>
<div class="sourceCode" id="cb2"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span> <span class="va">p_nb</span> <span class="op">&lt;-</span> <span class="fu"><a href="../reference/cond_permute_nb.html">cond_permute_nb</a></span><span class="op">(</span><span class="va">nb</span><span class="op">)</span></span>
<span> <span class="va">p_wt</span> <span class="op">&lt;-</span> <span class="fu"><a href="../reference/st_weights.html">st_weights</a></span><span class="op">(</span><span class="va">p_nb</span><span class="op">)</span></span>
<span> <span class="va">observed</span> <span class="op">&lt;-</span> <span class="fu"><a href="../reference/global_c.html">global_c</a></span><span class="op">(</span><span class="va">x</span>, <span class="va">p_nb</span>, <span class="va">p_wt</span><span class="op">)</span></span>
<span> </span>
<span> <span class="va">observed</span></span>
<span><span class="co">#&gt; $C</span></span>
<span><span class="co">#&gt; [1] 1.010342</span></span>
<span><span class="co">#&gt; </span></span>
<span><span class="co">#&gt; $K</span></span>
<span><span class="co">#&gt; [1] 2.400641</span></span></code></pre></div>
<p>If we did this say, 199 times, we can make a fairly robust reference
distribution. We will do this by putting the above code in a
<code><a href="https://rdrr.io/r/base/lapply.html" class="external-link">replicate()</a></code> call. <code><a href="https://rdrr.io/r/base/lapply.html" class="external-link">replicate()</a></code> execute some
code a number of times as determined by the first argument. This is how
I calculate simulated p-values for many measures in sfdep.</p>
<div class="sourceCode" id="cb3"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="va">reps</span> <span class="op">&lt;-</span> <span class="fu"><a href="https://rdrr.io/r/base/lapply.html" class="external-link">replicate</a></span><span class="op">(</span><span class="fl">199</span>, <span class="op">{</span></span>
<span>  <span class="va">p_nb</span> <span class="op">&lt;-</span> <span class="fu"><a href="../reference/cond_permute_nb.html">cond_permute_nb</a></span><span class="op">(</span><span class="va">nb</span><span class="op">)</span></span>
<span>  <span class="va">p_wt</span> <span class="op">&lt;-</span> <span class="fu"><a href="../reference/st_weights.html">st_weights</a></span><span class="op">(</span><span class="va">p_nb</span><span class="op">)</span></span>
<span>  <span class="fu"><a href="../reference/global_c.html">global_c</a></span><span class="op">(</span><span class="va">x</span>, <span class="va">p_nb</span>, <span class="va">p_wt</span><span class="op">)</span><span class="op">[[</span><span class="st">"C"</span><span class="op">]</span><span class="op">]</span></span>
<span><span class="op">}</span><span class="op">)</span></span></code></pre></div>
<p>Here we can plot the reference distribution and the observed value
over the reference distribution.</p>
<div class="sourceCode" id="cb4"><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="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://rdrr.io/r/base/data.frame.html" class="external-link">data.frame</a></span><span class="op">(</span>sim_val <span class="op">=</span> <span class="va">reps</span><span class="op">)</span>, </span>
<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">sim_val</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_histogram.html" class="external-link">geom_histogram</a></span><span class="op">(</span>bins <span class="op">=</span> <span class="fl">20</span><span class="op">)</span> <span class="op">+</span></span>
<span>  <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/geom_abline.html" class="external-link">geom_vline</a></span><span class="op">(</span>xintercept <span class="op">=</span> <span class="va">observed</span><span class="op">[[</span><span class="st">"C"</span><span class="op">]</span><span class="op">]</span>, </span>
<span>             color <span class="op">=</span> <span class="st">"#6fb381"</span>, </span>
<span>             lty <span class="op">=</span> <span class="fl">2</span>,</span>
<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="conditional-permutation_files/figure-html/unnamed-chunk-4-1.png" width="700"></p>
<p>We can see that the observed Geary C value is not very extreme and
falls some what close to the center of our distribution.</p>
<p>We can now calculate the pseudo p-value using the formula <span class="math inline">\((M + 1) / (R + 1)\)</span></p>
<div class="sourceCode" id="cb5"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="co"># simulated p-value</span></span>
<span><span class="op">(</span><span class="fu"><a href="https://rdrr.io/r/base/sum.html" class="external-link">sum</a></span><span class="op">(</span><span class="va">observed</span><span class="op">[[</span><span class="st">"C"</span><span class="op">]</span><span class="op">]</span> <span class="op">&lt;=</span> <span class="va">reps</span><span class="op">)</span> <span class="op">+</span> <span class="fl">1</span><span class="op">)</span> <span class="op">/</span> <span class="op">(</span><span class="fl">199</span> <span class="op">+</span> <span class="fl">1</span><span class="op">)</span></span>
<span><span class="co">#&gt; [1] 0.45</span></span></code></pre></div>
<p>This is the approach taken by Pysal and by sfdep where other methods
do not apply or are not provided by spdep.</p>
</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>
