<!-- 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>Local Polynomial Regression — poly.regression • 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="Local Polynomial Regression — poly.regression" />
<meta property="og:description" content="Calculates a Local Polynomial Regression for smoothing
or imputation of missing data.
This is a wrapper function for loess that simplifies data smoothing and imputation
of missing values. The function allows for smoothing a vector, based on an index
(derived automatically) or covariates. If the impute option is TRUE NA values are
imputed, otherwise the returned vector will still have NA's present. If impute and
na.only are both TRUE the vector is returned, without being smoothed but with imputed
NA values filled in. The loess weight function is defined using the tri-cube weight
function w(x) = (1-|x|^3)^3 where; x is the distance of a data point from the point
the curve being fitted." />




<!-- 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>Local Polynomial Regression</h1>
    <small class="dont-index">Source: <a href='https://github.com/jeffreyevans/spatialEco/blob/master/R/poly.regression.R'><code>R/poly.regression.R</code></a></small>
    <div class="hidden name"><code>poly.regression.Rd</code></div>
    </div>

    <div class="ref-description">
    <p>Calculates a Local Polynomial Regression for smoothing
or imputation of missing data.</p>
<p>This is a wrapper function for loess that simplifies data smoothing and imputation
of missing values. The function allows for smoothing a vector, based on an index
(derived automatically) or covariates. If the impute option is TRUE NA values are
imputed, otherwise the returned vector will still have NA's present. If impute and
na.only are both TRUE the vector is returned, without being smoothed but with imputed
NA values filled in. The loess weight function is defined using the tri-cube weight
function w(x) = (1-|x|^3)^3 where; x is the distance of a data point from the point
the curve being fitted.</p>
    </div>

    <pre class="usage"><span class='fu'>poly.regression</span><span class='op'>(</span>
  <span class='va'>y</span>,
  x <span class='op'>=</span> <span class='cn'>NULL</span>,
  s <span class='op'>=</span> <span class='fl'>0.75</span>,
  impute <span class='op'>=</span> <span class='cn'>FALSE</span>,
  na.only <span class='op'>=</span> <span class='cn'>FALSE</span>,
  ci <span class='op'>=</span> <span class='cn'>FALSE</span>,
  <span class='va'>...</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>y</th>
      <td><p>Vector to smooth or impute NA values</p></td>
    </tr>
    <tr>
      <th>x</th>
      <td><p>Optional x covariate data (must match dimensions of y)</p></td>
    </tr>
    <tr>
      <th>s</th>
      <td><p>Smoothing parameter (larger equates to more smoothing)</p></td>
    </tr>
    <tr>
      <th>impute</th>
      <td><p>(FALSE/TRUE) Should NA values be inputed</p></td>
    </tr>
    <tr>
      <th>na.only</th>
      <td><p>(FALSE/TRUE) Should only NA values be change in y</p></td>
    </tr>
    <tr>
      <th>ci</th>
      <td><p>(FALSE/TRUE) Should confidence intervals be returned</p></td>
    </tr>
    <tr>
      <th>...</th>
      <td><p>Additional arguments passed to loess</p></td>
    </tr>
    </table>

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

    <p>If ci = FALSE, a vector of smoothed values,
otherwise a list object with:</p><ul>
<li><p>loess - A vector, same length of y, representing the smoothed or
inputed data</p></li>
<li><p>lower.ci - Lower confidence interval</p></li>
<li><p>upper.ci - Upper confidence interval</p></li>
</ul>

    <h2 class="hasAnchor" id="see-also"><a class="anchor" href="#see-also"></a>See also</h2>

    <div class='dont-index'><p><code><a href='https://rdrr.io/r/stats/loess.html'>loess</a></code> for loess ... model options</p></div>
    <h2 class="hasAnchor" id="author"><a class="anchor" href="#author"></a>Author</h2>

    <p>Jeffrey S. Evans  <a href='mailto:jeffrey_evans@tnc.org'>jeffrey_evans@tnc.org</a></p>

    <h2 class="hasAnchor" id="examples"><a class="anchor" href="#examples"></a>Examples</h2>
    <pre class="examples"><div class='input'> <span class='va'>x</span> <span class='op'>&lt;-</span> <span class='fu'><a href='https://rdrr.io/r/base/seq.html'>seq</a></span><span class='op'>(</span><span class='op'>-</span><span class='fl'>20</span>, <span class='fl'>20</span>, <span class='fl'>0.1</span><span class='op'>)</span>
 <span class='va'>y</span> <span class='op'>&lt;-</span> <span class='fu'><a href='https://rdrr.io/r/base/Trig.html'>sin</a></span><span class='op'>(</span><span class='va'>x</span><span class='op'>)</span><span class='op'>/</span><span class='va'>x</span> <span class='op'>+</span> <span class='fu'><a href='https://rdrr.io/r/stats/Normal.html'>rnorm</a></span><span class='op'>(</span><span class='fu'><a href='https://rdrr.io/r/base/length.html'>length</a></span><span class='op'>(</span><span class='va'>x</span><span class='op'>)</span>, sd<span class='op'>=</span><span class='fl'>0.03</span><span class='op'>)</span>
 <span class='va'>p</span> <span class='op'>&lt;-</span> <span class='fu'><a href='https://rdrr.io/r/base/which.html'>which</a></span><span class='op'>(</span><span class='va'>y</span> <span class='op'>==</span> <span class='st'>"NaN"</span><span class='op'>)</span>
 <span class='va'>y</span> <span class='op'>&lt;-</span> <span class='va'>y</span><span class='op'>[</span><span class='op'>-</span><span class='va'>p</span><span class='op'>]</span>
 <span class='va'>r</span> <span class='op'>&lt;-</span> <span class='fu'>poly.regression</span><span class='op'>(</span><span class='va'>y</span>, ci<span class='op'>=</span><span class='cn'>TRUE</span>, s<span class='op'>=</span><span class='fl'>0.30</span><span class='op'>)</span>
 
 <span class='fu'><a href='https://r-spatial.github.io/sf/reference/plot.html'>plot</a></span><span class='op'>(</span><span class='va'>y</span>,type<span class='op'>=</span><span class='st'>"l"</span>, lwd<span class='op'>=</span><span class='fl'>0.5</span>, main<span class='op'>=</span><span class='st'>"s = 0.10"</span><span class='op'>)</span>
</div><div class='input'>   <span class='va'>y.polygon</span> <span class='op'>&lt;-</span> <span class='fu'><a href='https://rdrr.io/r/base/c.html'>c</a></span><span class='op'>(</span><span class='op'>(</span><span class='va'>r</span><span class='op'>$</span><span class='va'>lower.ci</span><span class='op'>)</span><span class='op'>[</span><span class='fl'>1</span><span class='op'>:</span><span class='fu'><a href='https://rdrr.io/r/base/length.html'>length</a></span><span class='op'>(</span><span class='va'>y</span><span class='op'>)</span><span class='op'>]</span>, <span class='op'>(</span><span class='va'>r</span><span class='op'>$</span><span class='va'>upper.ci</span><span class='op'>)</span><span class='op'>[</span><span class='fu'><a href='https://rdrr.io/r/base/rev.html'>rev</a></span><span class='op'>(</span><span class='fl'>1</span><span class='op'>:</span><span class='fu'><a href='https://rdrr.io/r/base/length.html'>length</a></span><span class='op'>(</span><span class='va'>y</span><span class='op'>)</span><span class='op'>)</span><span class='op'>]</span><span class='op'>)</span>
   <span class='va'>x.polygon</span> <span class='op'>&lt;-</span> <span class='fu'><a href='https://rdrr.io/r/base/c.html'>c</a></span><span class='op'>(</span><span class='fl'>1</span><span class='op'>:</span><span class='fu'><a href='https://rdrr.io/r/base/length.html'>length</a></span><span class='op'>(</span><span class='va'>y</span><span class='op'>)</span>, <span class='fu'><a href='https://rdrr.io/r/base/rev.html'>rev</a></span><span class='op'>(</span><span class='fl'>1</span><span class='op'>:</span><span class='fu'><a href='https://rdrr.io/r/base/length.html'>length</a></span><span class='op'>(</span><span class='va'>y</span><span class='op'>)</span><span class='op'>)</span><span class='op'>)</span>
   <span class='fu'><a href='https://rdrr.io/r/graphics/polygon.html'>polygon</a></span><span class='op'>(</span><span class='va'>x.polygon</span>, <span class='va'>y.polygon</span>, col<span class='op'>=</span><span class='st'>"#00009933"</span>, border<span class='op'>=</span><span class='cn'>NA</span><span class='op'>)</span> 
</div><div class='input'>      <span class='fu'><a href='https://rdrr.io/r/graphics/lines.html'>lines</a></span><span class='op'>(</span><span class='va'>r</span><span class='op'>$</span><span class='va'>loess</span>, lwd<span class='op'>=</span><span class='fl'>1.5</span>, col<span class='op'>=</span><span class='st'>"red"</span><span class='op'>)</span>
</div><div class='img'><img src='poly.regression-1.png' alt='' width='700' height='433' /></div><div class='input'> 
 <span class='co'># Impute NA values, replacing only NA's</span>
 <span class='va'>y.na</span> <span class='op'>&lt;-</span> <span class='va'>y</span>
 <span class='va'>y.na</span><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'>100</span>,<span class='fl'>200</span>,<span class='fl'>300</span><span class='op'>)</span><span class='op'>]</span> <span class='op'>&lt;-</span> <span class='cn'>NA</span> 
 <span class='va'>p.y</span> <span class='op'>&lt;-</span> <span class='fu'>poly.regression</span><span class='op'>(</span><span class='va'>y.na</span>, s<span class='op'>=</span><span class='fl'>0.10</span>, impute <span class='op'>=</span> <span class='cn'>TRUE</span>, na.only <span class='op'>=</span> <span class='cn'>TRUE</span><span class='op'>)</span>
 <span class='va'>y</span> <span class='op'>-</span> <span class='va'>p.y</span>
</div><div class='output co'>#&gt;   [1]  0.00000000  0.00000000  0.00000000  0.00000000  0.00000000  0.00000000
#&gt;   [7]  0.00000000  0.00000000  0.00000000  0.00000000  0.00000000  0.00000000
#&gt;  [13]  0.00000000  0.00000000  0.00000000  0.00000000  0.00000000  0.00000000
#&gt;  [19]  0.00000000  0.00000000  0.00000000  0.00000000  0.00000000  0.00000000
#&gt;  [25]  0.00000000  0.00000000  0.00000000  0.00000000  0.00000000  0.00000000
#&gt;  [31]  0.00000000  0.00000000  0.00000000  0.00000000  0.00000000  0.00000000
#&gt;  [37]  0.00000000  0.00000000  0.00000000  0.00000000  0.00000000  0.00000000
#&gt;  [43]  0.00000000  0.00000000  0.00000000  0.00000000  0.00000000  0.00000000
#&gt;  [49]  0.00000000  0.00000000  0.00000000  0.00000000  0.00000000  0.00000000
#&gt;  [55]  0.00000000  0.00000000  0.00000000  0.00000000  0.00000000  0.00000000
#&gt;  [61]  0.00000000  0.00000000  0.00000000  0.00000000  0.00000000  0.00000000
#&gt;  [67]  0.00000000  0.00000000  0.00000000  0.00000000  0.00000000  0.00000000
#&gt;  [73]  0.00000000  0.00000000  0.00000000  0.00000000  0.00000000  0.00000000
#&gt;  [79]  0.00000000  0.00000000  0.00000000  0.00000000  0.00000000  0.00000000
#&gt;  [85]  0.00000000  0.00000000  0.00000000  0.00000000  0.00000000  0.00000000
#&gt;  [91]  0.00000000  0.00000000  0.00000000  0.00000000  0.00000000  0.00000000
#&gt;  [97]  0.00000000  0.00000000  0.00000000  0.02910029  0.00000000  0.00000000
#&gt; [103]  0.00000000  0.00000000  0.00000000  0.00000000  0.00000000  0.00000000
#&gt; [109]  0.00000000  0.00000000  0.00000000  0.00000000  0.00000000  0.00000000
#&gt; [115]  0.00000000  0.00000000  0.00000000  0.00000000  0.00000000  0.00000000
#&gt; [121]  0.00000000  0.00000000  0.00000000  0.00000000  0.00000000  0.00000000
#&gt; [127]  0.00000000  0.00000000  0.00000000  0.00000000  0.00000000  0.00000000
#&gt; [133]  0.00000000  0.00000000  0.00000000  0.00000000  0.00000000  0.00000000
#&gt; [139]  0.00000000  0.00000000  0.00000000  0.00000000  0.00000000  0.00000000
#&gt; [145]  0.00000000  0.00000000  0.00000000  0.00000000  0.00000000  0.00000000
#&gt; [151]  0.00000000  0.00000000  0.00000000  0.00000000  0.00000000  0.00000000
#&gt; [157]  0.00000000  0.00000000  0.00000000  0.00000000  0.00000000  0.00000000
#&gt; [163]  0.00000000  0.00000000  0.00000000  0.00000000  0.00000000  0.00000000
#&gt; [169]  0.00000000  0.00000000  0.00000000  0.00000000  0.00000000  0.00000000
#&gt; [175]  0.00000000  0.00000000  0.00000000  0.00000000  0.00000000  0.00000000
#&gt; [181]  0.00000000  0.00000000  0.00000000  0.00000000  0.00000000  0.00000000
#&gt; [187]  0.00000000  0.00000000  0.00000000  0.00000000  0.00000000  0.00000000
#&gt; [193]  0.00000000  0.00000000  0.00000000  0.00000000  0.00000000  0.00000000
#&gt; [199]  0.00000000  0.02868291  0.00000000  0.00000000  0.00000000  0.00000000
#&gt; [205]  0.00000000  0.00000000  0.00000000  0.00000000  0.00000000  0.00000000
#&gt; [211]  0.00000000  0.00000000  0.00000000  0.00000000  0.00000000  0.00000000
#&gt; [217]  0.00000000  0.00000000  0.00000000  0.00000000  0.00000000  0.00000000
#&gt; [223]  0.00000000  0.00000000  0.00000000  0.00000000  0.00000000  0.00000000
#&gt; [229]  0.00000000  0.00000000  0.00000000  0.00000000  0.00000000  0.00000000
#&gt; [235]  0.00000000  0.00000000  0.00000000  0.00000000  0.00000000  0.00000000
#&gt; [241]  0.00000000  0.00000000  0.00000000  0.00000000  0.00000000  0.00000000
#&gt; [247]  0.00000000  0.00000000  0.00000000  0.00000000  0.00000000  0.00000000
#&gt; [253]  0.00000000  0.00000000  0.00000000  0.00000000  0.00000000  0.00000000
#&gt; [259]  0.00000000  0.00000000  0.00000000  0.00000000  0.00000000  0.00000000
#&gt; [265]  0.00000000  0.00000000  0.00000000  0.00000000  0.00000000  0.00000000
#&gt; [271]  0.00000000  0.00000000  0.00000000  0.00000000  0.00000000  0.00000000
#&gt; [277]  0.00000000  0.00000000  0.00000000  0.00000000  0.00000000  0.00000000
#&gt; [283]  0.00000000  0.00000000  0.00000000  0.00000000  0.00000000  0.00000000
#&gt; [289]  0.00000000  0.00000000  0.00000000  0.00000000  0.00000000  0.00000000
#&gt; [295]  0.00000000  0.00000000  0.00000000  0.00000000  0.00000000 -0.02111197
#&gt; [301]  0.00000000  0.00000000  0.00000000  0.00000000  0.00000000  0.00000000
#&gt; [307]  0.00000000  0.00000000  0.00000000  0.00000000  0.00000000  0.00000000
#&gt; [313]  0.00000000  0.00000000  0.00000000  0.00000000  0.00000000  0.00000000
#&gt; [319]  0.00000000  0.00000000  0.00000000  0.00000000  0.00000000  0.00000000
#&gt; [325]  0.00000000  0.00000000  0.00000000  0.00000000  0.00000000  0.00000000
#&gt; [331]  0.00000000  0.00000000  0.00000000  0.00000000  0.00000000  0.00000000
#&gt; [337]  0.00000000  0.00000000  0.00000000  0.00000000  0.00000000  0.00000000
#&gt; [343]  0.00000000  0.00000000  0.00000000  0.00000000  0.00000000  0.00000000
#&gt; [349]  0.00000000  0.00000000  0.00000000  0.00000000  0.00000000  0.00000000
#&gt; [355]  0.00000000  0.00000000  0.00000000  0.00000000  0.00000000  0.00000000
#&gt; [361]  0.00000000  0.00000000  0.00000000  0.00000000  0.00000000  0.00000000
#&gt; [367]  0.00000000  0.00000000  0.00000000  0.00000000  0.00000000  0.00000000
#&gt; [373]  0.00000000  0.00000000  0.00000000  0.00000000  0.00000000  0.00000000
#&gt; [379]  0.00000000  0.00000000  0.00000000  0.00000000  0.00000000  0.00000000
#&gt; [385]  0.00000000  0.00000000  0.00000000  0.00000000  0.00000000  0.00000000
#&gt; [391]  0.00000000  0.00000000  0.00000000  0.00000000  0.00000000  0.00000000
#&gt; [397]  0.00000000  0.00000000  0.00000000  0.00000000</div><div class='input'> 
 <span class='fu'><a href='https://r-spatial.github.io/sf/reference/plot.html'>plot</a></span><span class='op'>(</span><span class='va'>p.y</span>,type<span class='op'>=</span><span class='st'>"l"</span>, lwd<span class='op'>=</span><span class='fl'>1.5</span>, col<span class='op'>=</span><span class='st'>"blue"</span>, main<span class='op'>=</span><span class='st'>"s = 0.10"</span><span class='op'>)</span>
</div><div class='input'>   <span class='fu'><a href='https://rdrr.io/r/graphics/lines.html'>lines</a></span><span class='op'>(</span><span class='va'>y</span>, lwd<span class='op'>=</span><span class='fl'>1.5</span>, col<span class='op'>=</span><span class='st'>"red"</span><span class='op'>)</span>
</div><div class='img'><img src='poly.regression-2.png' alt='' width='700' height='433' /></div><div class='input'>
</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>


