Solved – Fitting logistic to small number of points in R

gradient descentlogisticmodelingr

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?

Thanks!

Best Answer

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