Error using the nls function

I'm having a little problem using the nls function. Could you help me understand and solve the problem below? Note that I can generate for df1 database, but not for df2 database. How to solve?

Executable code below:

df1<-structure(list(Category = c("ABC", "ABC", "ABC"), Days=c(42,43,44),  Numbers = c(456.589136904762, 456.589136904762, 456.589136904762)), class= "data.frame", row.names = c(NA, -3L))
mod1 <- nls(Numbers ~ b1*Days^2+b2,start = list(b1 = 0,b2 = 0),data = df1, algorithm = "port")

> mod1
Nonlinear regression model
  model: Numbers ~ b1 * Days^2 + b2
   data: df1
       b1        b2 
1.513e-08 4.566e+02 
 residual sum-of-squares: 1.422e-10
Algorithm "port", convergence message: X-convergence (3)

df2<-structure(list(Category = c("ABC", "ABC", "ABC"), Days=c(42,43,44),  Numbers = c(456.594054487179, 456.589136904762, 456.589136904762)), class= "data.frame", row.names = c(NA, -3L))

mod2 <- nls(Numbers ~ b1*Days^2+b2,start = list(b1 = 0,b2 = 0),data = df2, algorithm = "port")
Error in nls(Numbers ~ b1 * Days^2 + b2, start = list(b1 = 0, b2 = 0),  : 
Convergence failure: false convergence (8)

This is likely a problem of numerical accuracy. Note that the proper result for b1 in df1 is zero. For df2 it's not quite zero.

Also, in case it helps, these are both linear regressions so you can use lm() if you wish.

Thanks @startz for reply!

I was thinking about a probability solving this, and I thought about rounding the values to 2 decimal places, like this: df2$Numbers = round(df2$Numbers, 2) . So, the values in the Numbers column were equal to df2, then mod2 works. Do you think this can be a solution??

That might work. To be honest, I'm not quite sure what you're after.

You can use the nls option trace=TRUE to show you what its looking at as it works.
I was playing around and I found b1=0, b2 = 400 to run without convergence failure.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.