-1

I would like to do a non-linear regression with a logit function like brian s. cheng, based on Fox & Weisberg.

I wanted to go the most pain-free way and went with nls, but got the error

Error in lm.fit(x, y, offset = offset, singular.ok = singular.ok, ...) : 
NA/NaN/Inf in foreign function call (arg 1)

I checked that I don't have any NaN, Inf or similar and found that I have Singular gradient error. However I don't use zero-residue artificial data (as one should not). As it has something to do with the internals of nls I turned to nlsLM, but the error remains. What can I do?

My data is https://pastebin.com/iTXQcBzB

My code is

nlsLM(y ~ SSlogis(x, Asym, xmid, scal), mydata) % Error in lm.fit
nlsLM(y ~ theta1/(1 + exp(-(theta2 + theta3 * x))), mydata) % Singular gradient

The data provided is actually just a part and my complete code is more like

mydata %>%
  group_by(groupNr) %>%
  do(regmodel = nls(.$y ~ SSlogis(.$x, Asym, xmid, scal), ., 
     start = c(Asym = max(.$y), xmid = mean(.$x), scal = 1)))
Community
  • 1
  • 1
Make42
  • 12,236
  • 24
  • 79
  • 155

1 Answers1

0

You need better starting values:

nls(y ~ SSlogis(x, Asym, xmid, scal), mydata, 
    start = c(Asym = max(mydata$y), xmid = mean(mydata$x), scal = 1))

giving:

Nonlinear regression model
  model: y ~ SSlogis(x, Asym, xmid, scal)
   data: mydata
     Asym      xmid      scal 
2.304e+04 5.519e+00 3.065e-01 
 residual sum-of-squares: 477139282

Number of iterations to convergence: 8 
Achieved convergence tolerance: 5.271e-06
G. Grothendieck
  • 254,981
  • 17
  • 203
  • 341
  • Seems like a step in the right direction, but now with my data (I have multiple datasets that are like this one and I need to do it for all of them) I get the error `Error in qr.default(.swts * attr(rhs, "gradient")) :NA/NaN/Inf in external Function call (arg 1)` I posted the code in the question to illustrate what I am doing. – Make42 Apr 11 '17 at 16:02
  • I created a pastebin where the error appears: https://pastebin.com/ZiVxJQMk – Make42 Apr 11 '17 at 16:14