title: Singular gradient error during bootstrapped nls fit to bad data
body: <p>I have a dataset containing an independent variable and a set of dependent variables. I'd like to fit a function to each set of independent variables, using a bootstrapped nonlinear least squares procedure. In some cases, the independent variables are 'good quality', i.e. fit the function reasonably well. In other cases, they're noisy.</p>
  
  <p>In all cases, I can use <code>nls()</code> to get an estimate of the parameters. However, when the data are noisy, a bootstrap throws the error <code>Error in nls(...) : singular gradient</code>.  I can understand why <code>nls</code> fitting to noisy data would fail, e.g. by failing to converge after too many iterations, but I don't understand why it is a singular gradient error, and why I only get it resampled datasets of poor quality.</p>
  
  <p>Code:</p>
  
  <pre><code>require(ggplot2)
  require(plyr)
  require(boot)
  
  # Data are in long form: columns are 'enzyme', 'x', and 'y'
  enz &lt;- read.table("http://dl.dropbox.com/s/ts3ruh91kpr47sj/SE.txt", header=TRUE)
  
  # Nonlinear formula to fit to data
  mmFormula &lt;- formula(y ~ (x*Vmax) / (x + Km))
  </code></pre>
  
  <p><code>nls</code> is perfectly capable of fitting the data (even if in some cases, like <code>a</code>, I doubt the model fits the data. </p>
  
  <pre><code># Use nls to fit mmFormula to the data - this works well enough
  fitDf &lt;- ddply(enz, .(enzyme), function(x) coefficients(nls(mmFormula, x, start=list(Km=100, Vmax=0.5))))
  
  # Create points to plot for the simulated fits
  xGrid &lt;- 0:200
  simFits &lt;- dlply(fitDf, .(enzyme), function(x) data.frame(x=xGrid, y=(xGrid * x$Vmax)/(xGrid + x$Km)))
  simFits &lt;- ldply(simFits, identity) 
  
  ggplot() + geom_point(data=enz, aes(x=x, y=y)) + geom_line(data=simFits, aes(x=x, y=y)) + 
    facet_wrap(~enzyme, scales="free_y") + aes(ymin=0)
  </code></pre>
  
  <p><img src="https://i.stack.imgur.com/TkH0n.png" alt="NLS fits of mmFormula to data"></p>
  
  <p>Bootstrapping works fine for the good quality data:</p>
  
  <pre><code># Function to pass to bootstrap; returns coefficients of nls fit to formula
  nlsCoef &lt;- function(df, i) {
    KmGuess &lt;- median(df$x)
    VmaxGuess &lt;- max(df$y)
    dfSamp &lt;- df[i,]
    nlsCoef &lt;- coefficients(nls(mmFormula, dfSamp, start=list(Km=100, Vmax=0.5)))
  }
  
  eBoot &lt;- boot(subset(enz, enzyme=="e"), nlsCoef, R=1000) #No error
  </code></pre>
  
  <p>But not for the poor quality data</p>
  
  <pre><code>dBoot &lt;- boot(subset(enz, enzyme=="d"), nlsCoef, R=10)
  &gt; Error in nls(mmFormula, dfSamp, start = list(Km = KmGuess, Vmax = VmaxGuess)) : 
     singular gradient
  </code></pre>
  
  <p>What is causing this error? And what should I do about it, given that I'd like to use <code>plyr</code> to simultaneously perform lots of bootstrap simulations?</p>