I'm trying to fit a logistic function to some data points. Each data "set" has 6 points that I'm trying to fit a seperate logistic function to.

Here is some sample code:

x = c(60, 80, 100, 140, 160, 180)
y = c(24.0688, 26.3774, 25.1653, 15.7559, 12.4160, 15.5849)

df = data.frame(x=x, y=y)

nls(y ~ SSlogis(x, 25, 110, 100), df)

But I get this error:

Error in nlsModel(formula, mf, start, wts) : 
singular gradient matrix at initial parameter estimates

I'm not sure how I should be setting the Asym, xmid, scal parameters. I tried doing a call to nls with my own parameterized formulation of the logistic function but I get the same error. I thought it was the small number of data points, but I tried combining some of the data and I get the same error.

So my questions are:

  1. Is it possible to fit a logistic to this few points?
  2. Is the nls function the right way to go, or should I be using a different approach?
  3. How do I set the initial Asym, xmid, and scal parameters?



1 Answer 1


You are not using the SSlogis function correctly: it needs some parameters, and it will calculate the starting values by itself (that's why it is SS, i.e. self-start):

> nls(y ~ SSlogis(x, Asym, xmid, scal), df)
Nonlinear regression model
  model:  y ~ SSlogis(x, Asym, xmid, scal) 
   data:  df 
  Asym   xmid   scal 
 31.75 155.05 -60.64 
 residual sum-of-squares: 35.24

Number of iterations to convergence: 7 
Achieved convergence tolerance: 8.703e-06 
  • $\begingroup$ Thank you for that! How does that work if Asym, xmid and scal havent been defined? Also, it fixed it for that case, but for some other (more messy) data it fails. For example, x = c(118,130,142, 62,74,86), and y=c(19.7662,16.6663,14.1713,17.5192,13.4772,16.7067) $\endgroup$
    – reisner
    Commented Jun 11, 2012 at 18:48
  • 1
    $\begingroup$ This syntax defines Asym, xmid and scal as unknowns to be estimated. When the data don't follow a logistic curve, the initial value heuristic (which you can see in the code of SSlogis) can fail. There is not much automatic processing that you can do in this case. $\endgroup$
    – Aniko
    Commented Jun 11, 2012 at 19:19
  • $\begingroup$ Thank you. Is there anything I can do for that other data set? $\endgroup$
    – reisner
    Commented Jun 11, 2012 at 19:23
  • 1
    $\begingroup$ Yes, stop trying to fit a square peg into a round hole. Step back and rethink why do you want to fit separate logistic curves to sets of 6 data-points that don't seem to follow a logistic relationship. $\endgroup$
    – Aniko
    Commented Jun 11, 2012 at 20:51
  • $\begingroup$ Well, it's more like a square peg into a muddy square hole. Measurements are not always perfect :) - Anyways, thanks for your help. $\endgroup$
    – reisner
    Commented Jun 11, 2012 at 22:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.