title: R Nonlinear Least Squares (nls) Model Fitting
body: <p>I'm trying to fit the information from the G function of my data to the following mathematical mode:  y = A / ((1 + (B^2)*(x^2))^((C+1)/2)) .  The shape of this graph can be seen here:</p>
  
  <p><a href="http://www.wolframalpha.com/input/?i=y%20=%201/%20%28%281%20%2b%20%282%5E2%29%2a%28x%5E2%29%29%5E%28%282%2b1%29/2%29%29" rel="nofollow">http://www.wolframalpha.com/input/?i=y+%3D+1%2F+%28%281+%2B+%282%5E2%29*%28x%5E2%29%29%5E%28%282%2B1%29%2F2%29%29</a></p>
  
  <p>Here's a basic example of what I've been doing:</p>
  
  <pre><code>data(simdat)
  
  library(spatstat)
  
  simdat.Gest &lt;- Gest(simdat) #Gest is a function within spatstat (explained below)
  
  Gvalues &lt;- simdat.Gest$rs
  
  Rvalues &lt;- simdat.Gest$r
  
  GvsR_dataframe &lt;- data.frame(R = Rvalues, G = rev(Gvalues))
  
  themodel &lt;- nls(rev(Gvalues) ~ (1 / (1 + (B^2)*(R^2))^((C+1)/2)), data = GvsR_dataframe, start = list(B=0.1, C=0.1), trace = FALSE)
  </code></pre>
  
  <p>"Gest" is a function found within the 'spatstat' library. It is the G function, or the nearest-neighbour function, which displays the distance between particles on the independent axis, versus the probability of finding a nearest neighbour particle on the dependent axis. Thus, it begins at y=0 and hits a saturation point at y=1.</p>
  
  <p>If you plot simdat.Gest, you'll notice that the curve is 's' shaped, meaning that it starts at y = 0 and ends up at y = 1.  For this reason, I reveresed the vector Gvalues, which are the dependent variables.  Thus, the information is in the correct orientation to be fitted the above model.  </p>
  
  <p>You may also notice that I've automatically set A = 1.  This is because G(r) always saturates at 1, so I didn't bother keeping it in the formula.</p>
  
  <p>My problem is that I keep getting errors.  For the above example, I get this error:</p>
  
  <pre><code>Error in nls(rev(Gvalues) ~ (1/(1 + (B^2) * (R^2))^((C + 1)/2)), data = GvsR_dataframe,  : 
    singular gradient
  </code></pre>
  
  <p>I've also been getting this error:</p>
  
  <pre><code>Error in nls(Gvalues1 ~ (1/(1 + (B^2) * (x^2))^((C + 1)/2)), data = G_r_dataframe,  : 
    step factor 0.000488281 reduced below 'minFactor' of 0.000976562
  </code></pre>
  
  <p>I haven't a clue as to where the first error is coming from.  The second, however, I believe was occurring because I did not pick suitable starting values for B and C.</p>
  
  <p>I was hoping that someone could help me figure out where the first error was coming from.  Also, what is the most effective way to pick starting values to avoid the second error?</p>
  
  <p>Thanks!</p>