title: Plot a smooth and extrapolated curve using an nls model with several fitted parameters
body: <p>I feel that I am close to finding the answer for my problem, but somehow I just cannot manage to do it. I have used nls function to fit 3 parameters using a rather complicated function describing fertilization success of eggs (y-axis) in a range of sperm concentrations (x-axis) (Styan's model <a href="http://www.jstor.org/stable/10.1086/286168" rel="nofollow noreferrer">[1]</a>, <a href="http://onlinelibrary.wiley.com/doi/10.1111/j.1558-5646.2008.00521.x/abstract" rel="nofollow noreferrer">[2]</a>). Fitting the parameters works fine, but I cannot manage to plot a smoothed extrapolated curve using <code>predict</code> function (see at the end of this post). I guess it is because I have used a value that was not fitted on x-axis. My question is how to plot a smoothed and extrapolated curve based on a model fitted with <code>nls</code> function
  using non-fitted parameter on x-axis?</p>
  
  <p>Here is an example:</p>
  
  <pre><code>library(ggplot2)
  
  data.nls &lt;- structure(list(S0 = c(0.23298, 2.32984, 23.2984, 232.98399, 2329.83993, 
  23298.39926), fert = c(0.111111111111111, 0.386792452830189, 
  0.158415841584158, 0.898648648648649, 0.616, 0.186440677966102
  ), speed = c(0.035161615379406, 0.035161615379406, 0.035161615379406, 
  0.035161615379406, 0.035161615379406, 0.035161615379406), E0 = c(6.86219803476946, 
  6.86219803476946, 6.86219803476946, 6.86219803476946, 6.86219803476946, 
  7.05624476582978), tau = c(1800, 1800, 1800, 1800, 1800, 1800
  ), B0 = c(0.000102758645352932, 0.000102758645352932, 0.000102758645352932, 
  0.000102758645352932, 0.000102758645352932, 0.000102758645352932
  )), .Names = c("S0", "fert", "speed", "E0", "tau", "B0"), row.names = c(NA, 
  6L), class = "data.frame")
  
  ## Model S
  
  modelS &lt;- function(Fe, tb, Be) with (data.nls,{
  x &lt;- Fe*(S0/E0)*(1-exp(-B0*E0*tau))
  b &lt;- Fe*(S0/E0)*(1-exp(-B0*E0*tb))
  x*exp(-x)+Be*(1-exp(-x)-(x*exp(-x)))*exp(-b)})
  
  ## Define starting values
  
  start &lt;- list(Fe = 0.2, tb = 0.1, Be = 0.1)
  
  ## Fit the model using nls
  
  modelS.fitted &lt;- nls(formula = fert ~ modelS(Fe, tb, Be), data = data.nls, start = start, 
  control=nls.control(warnOnly=TRUE,minFactor=1e-5),trace = T, lower = c(0,0,0), 
  upper = c(1, Inf, 1), algorithm = "port")
  
  ## Combine model parameters
  
  model.data &lt;- cbind(data.nls, data.frame(pred = predict(modelS.fitted)))
  
  ## Plot
  
  ggplot(model.data) +
  geom_point(aes(x = S0, y = fert), size = 2) +
  geom_line(aes(x = S0, y = pred), lwd = 1.3) +
  scale_x_log10()
  </code></pre>
  
  <p><img src="https://i.stack.imgur.com/P7wjV.png" alt="enter image description here"></p>
  
  <p>I have tried following joran's example <a href="https://stackoverflow.com/questions/9918807/how-get-plot-from-nls-in-r">here</a>, but it has no effect, maybe because I did not fit <code>S0</code>:</p>
  
  <pre><code>r &lt;- range(model.data$S0)
  S0.ext &lt;- seq(r[1],r[2],length.out = 200)
  predict(modelS.fitted, newdata = list(S0 = S0.ext))
  # [1] 0.002871585 0.028289057 0.244399948 0.806316161 0.705116868 0.147974213
  </code></pre>