title: How to calculate 95% prediction interval from nls
body: <p>Borrowing the example data from this <a href="https://stackoverflow.com/questions/33305620/plotting-nls-fits-with-overlapping-prediction-intervals-in-a-single-figure">question</a>, if I have the following data and I fit the following non linear model to it, how can I calculate the 95% <strong>prediction</strong> <strong>interval</strong> for my curve?</p>
  
  <pre><code>library(broom)
  library(tidyverse)
  
  x &lt;- seq(0, 4, 0.1)
  y1 &lt;- (x * 2 / (0.2 + x))
  y &lt;- y1 + rnorm(length(y1), 0, 0.2)
  
  d &lt;- data.frame(x, y)
  
  mymodel &lt;- nls(y ~ v * x / (k + x),
              start = list(v = 1.9, k = 0.19),
              data = d)
  
  mymodel_aug &lt;- augment(mymodel)
  
  ggplot(mymodel_aug, aes(x, y)) +
    geom_point() +
    geom_line(aes(y = .fitted), color = "red") +
    theme_minimal()
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/mVn55.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/mVn55.png" alt="enter image description here"></a></p>
  
  <p>As an example, I can easily calculate the prediction interval from a linear model like this:</p>
  
  <pre><code>## linear example
  
  d2 &lt;- d %&gt;%
    filter(x &gt; 1)
  
  mylinear &lt;- lm(y ~ x, data = d2)
  
  mypredictions &lt;-
    predict(mylinear, interval = "prediction", level = 0.95) %&gt;%
    as_tibble()
  
  d3 &lt;- bind_cols(d2, mypredictions)
  
  ggplot(d3, aes(x, y)) +
    geom_point() +
    geom_line(aes(y = fit)) +
    geom_ribbon(aes(ymin = lwr, ymax = upr), alpha = .15) +
    theme_minimal()
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/3mGKN.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/3mGKN.png" alt="enter image description here"></a></p>