<!-- Generated by pkgdown: do not edit by hand -->
<!DOCTYPE html>
<html lang="en">
  <head>
  <meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">

<title>Optimized sample variance — optimized.sample.variance • spatialEco</title>


<!-- jquery -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.4.1/jquery.min.js" integrity="sha256-CSXorXvZcTkaix6Yvo6HppcZGetbYMGWSFlBw8HfCJo=" crossorigin="anonymous"></script>
<!-- Bootstrap -->

<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.4.1/css/bootstrap.min.css" integrity="sha256-bZLfwXAP04zRMK2BjiO8iu9pf4FbLqX6zitd+tIvLhE=" crossorigin="anonymous" />

<script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.4.1/js/bootstrap.min.js" integrity="sha256-nuL8/2cJ5NDSSwnKD8VqreErSWHtnEP9E7AySL+1ev4=" crossorigin="anonymous"></script>

<!-- bootstrap-toc -->
<link rel="stylesheet" href="../bootstrap-toc.css">
<script src="../bootstrap-toc.js"></script>

<!-- 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" />

<!-- 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>

<!-- 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>

<!-- pkgdown -->
<link href="../pkgdown.css" rel="stylesheet">
<script src="../pkgdown.js"></script>




<meta property="og:title" content="Optimized sample variance — optimized.sample.variance" />
<meta property="og:description" content="Draws an optimal sample that minimizes or maximizes the 
             sample variance" />




<!-- 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 data-spy="scroll" data-target="#toc">
    <div class="container template-reference-topic">
      <header>
      <div class="navbar navbar-default navbar-fixed-top" role="navigation">
  <div class="container">
    <div class="navbar-header">
      <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar" aria-expanded="false">
        <span class="sr-only">Toggle navigation</span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
      </button>
      <span class="navbar-brand">
        <a class="navbar-link" href="../index.html">spatialEco</a>
        <span class="version label label-default" data-toggle="tooltip" data-placement="bottom" title="Released version">1.3-7</span>
      </span>
    </div>

    <div id="navbar" class="navbar-collapse collapse">
      <ul class="nav navbar-nav">
        <li>
  <a href="../index.html">
    <span class="fas fa-home fa-lg"></span>
     
  </a>
</li>
<li>
  <a href="../reference/index.html">Reference</a>
</li>
      </ul>
      <ul class="nav navbar-nav navbar-right">
        <li>
  <a href="https://github.com/jeffreyevans/spatialEco/">
    <span class="fab fa-github fa-lg"></span>
     
  </a>
</li>
      </ul>
      
    </div><!--/.nav-collapse -->
  </div><!--/.container -->
</div><!--/.navbar -->

      

      </header>

<div class="row">
  <div class="col-md-9 contents">
    <div class="page-header">
    <h1>Optimized sample variance</h1>
    <small class="dont-index">Source: <a href='https://github.com/jeffreyevans/spatialEco/blob/master/R/optimized.sample.variance.R'><code>R/optimized.sample.variance.R</code></a></small>
    <div class="hidden name"><code>optimized.sample.variance.Rd</code></div>
    </div>

    <div class="ref-description">
    <p>Draws an optimal sample that minimizes or maximizes the 
             sample variance</p>
    </div>

    <pre class="usage"><span class='fu'>optimized.sample.variance</span><span class='op'>(</span><span class='va'>x</span>, <span class='va'>n</span>, type <span class='op'>=</span> <span class='st'>"maximized"</span><span class='op'>)</span></pre>

    <h2 class="hasAnchor" id="arguments"><a class="anchor" href="#arguments"></a>Arguments</h2>
    <table class="ref-arguments">
    <colgroup><col class="name" /><col class="desc" /></colgroup>
    <tr>
      <th>x</th>
      <td><p>A vector to draw a sample from</p></td>
    </tr>
    <tr>
      <th>n</th>
      <td><p>Number of samples to draw</p></td>
    </tr>
    <tr>
      <th>type</th>
      <td><p>Type of sample variance optimization 
c("maximized", "minimized")</p></td>
    </tr>
    </table>

    <h2 class="hasAnchor" id="value"><a class="anchor" href="#value"></a>Value</h2>

    <p>A data.frame with "idx" representing the index of the original vector  
and "y" is the value of the sampled data</p>
    <h2 class="hasAnchor" id="author"><a class="anchor" href="#author"></a>Author</h2>

    <p>Jeffrey S. Evans  &lt;jeffrey_evans@tnc.org&gt;</p>

    <h2 class="hasAnchor" id="examples"><a class="anchor" href="#examples"></a>Examples</h2>
    <pre class="examples"><div class='input'> <span class='kw'><a href='https://rdrr.io/r/base/library.html'>library</a></span><span class='op'>(</span><span class='va'><a href='https://github.com/edzer/sp/'>sp</a></span><span class='op'>)</span>
   <span class='fu'><a href='https://rdrr.io/r/utils/data.html'>data</a></span><span class='op'>(</span><span class='va'>meuse</span><span class='op'>)</span>
   <span class='fu'><a href='https://rdrr.io/pkg/sp/man/coordinates.html'>coordinates</a></span><span class='op'>(</span><span class='va'>meuse</span><span class='op'>)</span> <span class='op'>&lt;-</span> <span class='op'>~</span><span class='va'>x</span><span class='op'>+</span><span class='va'>y</span>

 <span class='va'>n</span> <span class='op'>=</span> <span class='fl'>15</span>
 <span class='co'># Draw n samples that maximize the variance of y</span>
 <span class='op'>(</span> <span class='va'>max.sv</span> <span class='op'>&lt;-</span> <span class='fu'>optimized.sample.variance</span><span class='op'>(</span><span class='va'>meuse</span><span class='op'>$</span><span class='va'>zinc</span>, <span class='fl'>15</span><span class='op'>)</span> <span class='op'>)</span>
</div><div class='output co'>#&gt; <span class='message'></span>
#&gt; <span class='message'>maximized sample variance: 568741.542857143</span></div><div class='output co'>#&gt;    idx    y
#&gt; 1   54 1839
#&gt; 2  107  113
#&gt; 3  106  117
#&gt; 4   82 1672
#&gt; 5   68  119
#&gt; 6   59 1571
#&gt; 7  127  119
#&gt; 8   53 1548
#&gt; 9  131  126
#&gt; 10  55 1528
#&gt; 11 113  128
#&gt; 12 137  129
#&gt; 13  40 1454
#&gt; 14 108  130
#&gt; 15  80 1383</div><div class='input'> 
 <span class='co'># Draw n samples that minimize the variance of y</span>
 <span class='op'>(</span> <span class='va'>min.sv</span> <span class='op'>&lt;-</span> <span class='fu'>optimized.sample.variance</span><span class='op'>(</span><span class='va'>meuse</span><span class='op'>$</span><span class='va'>zinc</span>, <span class='fl'>15</span>, 
               type<span class='op'>=</span><span class='st'>"minimized"</span><span class='op'>)</span> <span class='op'>)</span>
</div><div class='output co'>#&gt; <span class='message'></span>
#&gt; <span class='message'>minimized sample variance: 1795.17142857143</span></div><div class='output co'>#&gt;    idx   y
#&gt; 1  145 471
#&gt; 2  130 474
#&gt; 3   47 464
#&gt; 4  139 451
#&gt; 5  152 496
#&gt; 6   14 504
#&gt; 7   88 505
#&gt; 8   57 432
#&gt; 9   89 420
#&gt; 10 129 415
#&gt; 11   8 406
#&gt; 12 144 403
#&gt; 13  22 402
#&gt; 14  91 400
#&gt; 15  50 375</div><div class='input'> 
 <span class='co'># Plot results</span>
 <span class='fu'><a href='https://rdrr.io/pkg/raster/man/plot.html'>plot</a></span><span class='op'>(</span><span class='va'>meuse</span>, pch<span class='op'>=</span><span class='fl'>19</span>, col<span class='op'>=</span><span class='st'>"grey"</span><span class='op'>)</span>
</div><div class='input'>   <span class='fu'><a href='https://rdrr.io/pkg/raster/man/plot.html'>plot</a></span><span class='op'>(</span><span class='va'>meuse</span><span class='op'>[</span><span class='va'>max.sv</span><span class='op'>$</span><span class='va'>idx</span>,<span class='op'>]</span>, col<span class='op'>=</span><span class='st'>"red"</span>, add<span class='op'>=</span><span class='cn'>TRUE</span>, pch<span class='op'>=</span><span class='fl'>19</span><span class='op'>)</span>
</div><div class='input'>     <span class='fu'><a href='https://rdrr.io/pkg/raster/man/plot.html'>plot</a></span><span class='op'>(</span><span class='va'>meuse</span><span class='op'>[</span><span class='va'>min.sv</span><span class='op'>$</span><span class='va'>idx</span>,<span class='op'>]</span>, col<span class='op'>=</span><span class='st'>"blue"</span>, add<span class='op'>=</span><span class='cn'>TRUE</span>, pch<span class='op'>=</span><span class='fl'>19</span><span class='op'>)</span>
</div><div class='input'>     <span class='fu'><a href='https://rdrr.io/r/graphics/box.html'>box</a></span><span class='op'>(</span><span class='op'>)</span>
</div><div class='input'>     <span class='fu'><a href='https://rdrr.io/r/graphics/legend.html'>legend</a></span><span class='op'>(</span><span class='st'>"topleft"</span>, legend<span class='op'>=</span><span class='fu'><a href='https://rdrr.io/r/base/c.html'>c</a></span><span class='op'>(</span><span class='st'>"population"</span>,<span class='st'>"maximized variance"</span>, 
            <span class='st'>"minimized variance"</span><span class='op'>)</span>, col<span class='op'>=</span><span class='fu'><a href='https://rdrr.io/r/base/c.html'>c</a></span><span class='op'>(</span><span class='st'>"grey"</span>,<span class='st'>"red"</span>,<span class='st'>"blue"</span><span class='op'>)</span>,  
            pch<span class='op'>=</span><span class='fu'><a href='https://rdrr.io/r/base/c.html'>c</a></span><span class='op'>(</span><span class='fl'>19</span>,<span class='fl'>19</span>,<span class='fl'>19</span><span class='op'>)</span><span class='op'>)</span>
</div><div class='img'><img src='optimized.sample.variance-1.png' alt='' width='700' height='433' /></div><div class='input'>
<span class='co'># \donttest{</span>
 <span class='co'># Raster example (not memory safe) </span>
 <span class='kw'><a href='https://rdrr.io/r/base/library.html'>library</a></span><span class='op'>(</span><span class='va'><a href='https://rspatial.org/raster'>raster</a></span><span class='op'>)</span>
   <span class='va'>r</span> <span class='op'>&lt;-</span> <span class='fu'><a href='https://rdrr.io/pkg/raster/man/raster.html'>raster</a></span><span class='op'>(</span><span class='fu'><a href='https://rdrr.io/r/base/system.file.html'>system.file</a></span><span class='op'>(</span><span class='st'>"external/test.grd"</span>, package<span class='op'>=</span><span class='st'>"raster"</span><span class='op'>)</span><span class='op'>)</span>
  
<span class='co'># Calculate optimal sample variance and coerce to SpatialPointDataFrame </span>
<span class='co'>#   using xyFromCell</span>
     <span class='op'>(</span> <span class='va'>min.sv</span> <span class='op'>&lt;-</span> <span class='fu'>optimized.sample.variance</span><span class='op'>(</span><span class='fu'><a href='https://rdrr.io/pkg/raster/man/getValues.html'>getValues</a></span><span class='op'>(</span><span class='va'>r</span><span class='op'>)</span>, <span class='va'>n</span>, type<span class='op'>=</span><span class='st'>"minimized"</span><span class='op'>)</span> <span class='op'>)</span>
</div><div class='output co'>#&gt; <span class='message'></span>
#&gt; <span class='message'>minimized sample variance: 0.781294194342835</span></div><div class='output co'>#&gt;     idx        y
#&gt; 1  4517 425.4702
#&gt; 2  4592 425.2701
#&gt; 3  2063 424.9967
#&gt; 4  7080 424.8377
#&gt; 5  7734 425.8081
#&gt; 6  7929 426.0826
#&gt; 7  7295 426.3960
#&gt; 8  8580 426.3975
#&gt; 9  3019 426.5510
#&gt; 10 4287 426.5703
#&gt; 11 2222 426.8780
#&gt; 12 6660 426.8993
#&gt; 13 6931 427.4153
#&gt; 14 7448 427.5166
#&gt; 15 7159 427.5213</div><div class='input'>      <span class='va'>min.sv</span> <span class='op'>&lt;-</span> <span class='fu'>sp</span><span class='fu'>::</span><span class='fu'><a href='https://rdrr.io/pkg/sp/man/SpatialPoints.html'>SpatialPointsDataFrame</a></span><span class='op'>(</span><span class='fu'><a href='https://rdrr.io/pkg/raster/man/xyFromCell.html'>xyFromCell</a></span><span class='op'>(</span><span class='va'>r</span>, <span class='va'>min.sv</span><span class='op'>[</span>,<span class='st'>"idx"</span><span class='op'>]</span>, 
                                           spatial<span class='op'>=</span><span class='cn'>TRUE</span><span class='op'>)</span>, data<span class='op'>=</span><span class='va'>min.sv</span><span class='op'>)</span> 
     <span class='op'>(</span> <span class='va'>max.sv</span> <span class='op'>&lt;-</span> <span class='fu'>optimized.sample.variance</span><span class='op'>(</span><span class='fu'><a href='https://rdrr.io/pkg/raster/man/getValues.html'>getValues</a></span><span class='op'>(</span><span class='va'>r</span><span class='op'>)</span>, <span class='va'>n</span><span class='op'>)</span> <span class='op'>)</span>
</div><div class='output co'>#&gt; <span class='message'></span>
#&gt; <span class='message'>maximized sample variance: 517250.482316504</span></div><div class='output co'>#&gt;     idx         y
#&gt; 1  3639 1736.0580
#&gt; 2  5888  138.7071
#&gt; 3  5968  148.7244
#&gt; 4  3719 1648.3469
#&gt; 5  5251  152.5585
#&gt; 6  7483 1488.6211
#&gt; 7  5331  152.7412
#&gt; 8  3562 1483.9067
#&gt; 9  5885  152.9692
#&gt; 10 7563 1481.8656
#&gt; 11 6287  157.3041
#&gt; 12 7484 1468.7628
#&gt; 13 5887  159.4636
#&gt; 14 7564 1463.7230
#&gt; 15 6352  160.1675</div><div class='input'>      <span class='va'>max.sv</span> <span class='op'>&lt;-</span> <span class='fu'>sp</span><span class='fu'>::</span><span class='fu'><a href='https://rdrr.io/pkg/sp/man/SpatialPoints.html'>SpatialPointsDataFrame</a></span><span class='op'>(</span><span class='fu'><a href='https://rdrr.io/pkg/raster/man/xyFromCell.html'>xyFromCell</a></span><span class='op'>(</span><span class='va'>r</span>, <span class='va'>max.sv</span><span class='op'>[</span>,<span class='st'>"idx"</span><span class='op'>]</span>, 
                                           spatial<span class='op'>=</span><span class='cn'>TRUE</span><span class='op'>)</span>, data<span class='op'>=</span><span class='va'>max.sv</span><span class='op'>)</span>

 <span class='fu'><a href='https://rdrr.io/pkg/raster/man/plot.html'>plot</a></span><span class='op'>(</span><span class='va'>r</span><span class='op'>)</span>
</div><div class='input'>   <span class='fu'><a href='https://rdrr.io/pkg/raster/man/plot.html'>plot</a></span><span class='op'>(</span><span class='va'>max.sv</span>, col<span class='op'>=</span><span class='st'>"blue"</span>, add<span class='op'>=</span><span class='cn'>TRUE</span>, pch<span class='op'>=</span><span class='fl'>19</span><span class='op'>)</span>
</div><div class='input'>   <span class='fu'><a href='https://rdrr.io/pkg/raster/man/plot.html'>plot</a></span><span class='op'>(</span><span class='va'>min.sv</span>, col<span class='op'>=</span><span class='st'>"red"</span>, add<span class='op'>=</span><span class='cn'>TRUE</span>, pch<span class='op'>=</span><span class='fl'>19</span><span class='op'>)</span>
</div><div class='input'>   <span class='fu'><a href='https://rdrr.io/r/graphics/box.html'>box</a></span><span class='op'>(</span><span class='op'>)</span>
</div><div class='input'> <span class='fu'><a href='https://rdrr.io/r/graphics/legend.html'>legend</a></span><span class='op'>(</span><span class='st'>"topleft"</span>, legend<span class='op'>=</span><span class='fu'><a href='https://rdrr.io/r/base/c.html'>c</a></span><span class='op'>(</span><span class='st'>"maximized variance"</span>, <span class='st'>"minimized variance"</span><span class='op'>)</span>,
        col<span class='op'>=</span><span class='fu'><a href='https://rdrr.io/r/base/c.html'>c</a></span><span class='op'>(</span><span class='st'>"red"</span>,<span class='st'>"blue"</span><span class='op'>)</span>, pch<span class='op'>=</span><span class='fu'><a href='https://rdrr.io/r/base/c.html'>c</a></span><span class='op'>(</span><span class='fl'>19</span>,<span class='fl'>19</span><span class='op'>)</span><span class='op'>)</span>
</div><div class='img'><img src='optimized.sample.variance-2.png' alt='' width='700' height='433' /></div><div class='input'> <span class='co'># }  </span>
 
</div></pre>
  </div>
  <div class="col-md-3 hidden-xs hidden-sm" id="pkgdown-sidebar">
    <nav id="toc" data-toggle="toc" class="sticky-top">
      <h2 data-toc-skip>Contents</h2>
    </nav>
  </div>
</div>


      <footer>
      <div class="copyright">
  <p>Developed by Jeffrey S. Evans.</p>
</div>

<div class="pkgdown">
  <p>Site built with <a href="https://pkgdown.r-lib.org/">pkgdown</a> 1.6.1.</p>
</div>

      </footer>
   </div>

  


  </body>
</html>


