title: Polynomial regression in R - with extra constraints on the curve
body: <p>I know how to do a basic polynomial regression in R. However, I can only use <code>nls</code> or <code>lm</code> to fit a line that minimizes error with the points. </p>
  
  <p>This works most of the time, but sometimes when there are measurement gaps in the data, the model becomes very counter-intuitive. Is there a way to add extra constraints?</p>
  
  <p><strong>Reproducible Example:</strong></p>
  
  <p>I want to fit a model to the following made up data (similar to my real data):</p>
  
  <pre><code>x &lt;- c(0, 6, 21, 41, 49, 63, 166)
  y &lt;- c(3.3, 4.2, 4.4, 3.6, 4.1, 6.7, 9.8)
  df &lt;- data.frame(x, y)
  </code></pre>
  
  <p>First, let's plot it.</p>
  
  <pre><code>library(ggplot2)
  points &lt;- ggplot(df, aes(x,y)) + geom_point(size=4, col='red')
  points
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/6cMtM.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/6cMtM.png" alt="Made up points"></a></p>
  
  <p>It looks like if we connected these points with a line, it would change direction 3 times, so let's try fitting a quartic to it.</p>
  
  <pre><code>lm &lt;- lm(formula = y ~ x + I(x^2) + I(x^3) + I(x^4))
  quartic &lt;- function(x)  lm$coefficients[5]*x^4 + lm$coefficients[4]*x^3 + lm$coefficients[3]*x^2 + lm$coefficients[2]*x + lm$coefficients[1]
  
  points + stat_function(fun=quartic)
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/iqckO.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/iqckO.png" alt="Non-intuitive Model"></a></p>
  
  <p>Looks like the model fits the points pretty well... except, because our data had a large gap between 63 and 166, there is a huge spike there which has no reason to be in the model. (For my actual data I know that there is no huge peak there)</p>
  
  <p>So the question in this case is: </p>
  
  <ul>
  <li><strong>How can I set that local maximum to be on (166, 9.8)?</strong> </li>
  </ul>
  
  <p>If that's not possible, then another way to do it would be:</p>
  
  <ul>
  <li><strong>How can I limit the y-values predicted by the line from becoming larger than y=9.8</strong>.</li>
  </ul>
  
  <p>Or perhaps there's a better model to be using? (Other than doing it piece-wise). My purpose is to compare features of models between graphs.</p>