0

I always get the error:

Error in levels(x) : argument "object" is missing, with no default

when trying to fit:

fit60 <- nls(Predicted.Bromide~c+a*(1-exp(-b*n)),start=C(a=10,b=0.02,c=0),data=min60)

Or with the columns extracted to x=min60min and y=area:

fit60 <- nls(area~C+A*(1-exp(-B*min60min)),start=C(A=0,B=0,C=0))

Dataset:

dput(min60)

structure(list(Predicted.Bromide = c(4721.05, 16030.5, 17251.1, 
20353.1, 22997.8, 24657.3, 26896.4, 26529.1, 25282.7, 26546.2, 
27265.3, 27491.1, 27678.4, 30140.1, 28285.4, 28079.6, 29111.3, 
28781.1, 28723.7, 27959.3, 29430.7, 30273.6, 28618.7, 29716, 
29761.5, 30276.7, 29642.6, 31369.1, 33416.5, 31204.2, 32652.1, 
31013.3, 32591.6, 33436.7, 32240.2, 32557.2, 31548.4, 33701.9, 
32376.5, 34323.4, 34430.5, 33255.7, 33988.1, 32779.9, 34013.3, 
35157.1, 34905.4, 32918.9, 34915.6, 35001.5, 33874.1, 34954.4, 
34495.8, 34679.4, 33409.8, 34318.7), n = c(1, 2, 3, 4, 5, 6, 
7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 
23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 
39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 
55, 56)), .Names = c("Predicted.Bromide", "n"), row.names = c(NA, 
56L), na.action = structure(57L, .Names = "57", class = "omit"), class = "data.frame")

What can be the problem? In my opinion everything is correct in the code.

ToTo
  • 1
  • 2
  • Since you are new to stackoverflow: If there is an answer that solved your problem, it is good practice to accept this answer by clicking on the small check next to this answer which then turns green. That not only appreciates the effort this person put into solving your problem but also shows other users that might have a similar problem how it could be fixed. – Cleb Dec 05 '15 at 12:30

4 Answers4

1

there is a typo in fit60 <- nls(Predicted.Bromide~c+a*(1-exp(-b*n)),start=C(a=10,b=0.02,c=0),data=min60) it should be a small c. Then it is just to find the right starting values. I am not sure if the equation you want fit is correct. Shouldn't it be a*(1 - exp(-b*n)) Terazaki (1915)/Schumacher

Mateusz1981
  • 1,817
  • 17
  • 33
1

Here's a possible solution:

min60 <- structure(list(Predicted.Bromide = c(4721.05, 16030.5, 17251.1, 
20353.1, 22997.8, 24657.3, 26896.4, 26529.1, 25282.7, 26546.2, 
27265.3, 27491.1, 27678.4, 30140.1, 28285.4, 28079.6, 29111.3, 
28781.1, 28723.7, 27959.3, 29430.7, 30273.6, 28618.7, 29716, 
29761.5, 30276.7, 29642.6, 31369.1, 33416.5, 31204.2, 32652.1, 
31013.3, 32591.6, 33436.7, 32240.2, 32557.2, 31548.4, 33701.9, 
32376.5, 34323.4, 34430.5, 33255.7, 33988.1, 32779.9, 34013.3, 
35157.1, 34905.4, 32918.9, 34915.6, 35001.5, 33874.1, 34954.4, 
34495.8, 34679.4, 33409.8, 34318.7), n = c(1, 2, 3, 4, 5, 6, 
7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 
23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 
39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 
55, 56)), .Names = c("Predicted.Bromide", "n"), row.names = c(NA, 
56L), na.action = structure(57L, .Names = "57", class = "omit"), class = "data.frame")

head(min60)

plot(Predicted.Bromide ~ n, min60)

FUN <- function(n, a, b, c){
  Predicted.Bromide <- c+a*(1-exp(-b*n))
  Predicted.Bromide
}

plot(min60$n, FUN(min60$n,1e5,0.1,1000), t="l")
points(Predicted.Bromide ~ n, min60)

starting.values <- list(a=1e5,b=0.1,c=1000)
#lower.values <- list(a=1,b=0,c=0)
#upper.values <- list(a=1e7,b=3,c=10000)

fit60 <- nls(Predicted.Bromide ~ FUN(n,a,b,c), 
  data=min60,
  start = starting.values,
  #lower = lower.values,
  #upper = upper.values,
  algorithm = "port"
)
fit60

plot(Predicted.Bromide ~ n, min60)
lines(predict(fit60, min60))

enter image description here

Marc in the box
  • 11,769
  • 4
  • 47
  • 97
  • fit60<-nls(area~A*(1-exp(-B*min60min))+C,start=c(A=1e5,B=0.1,C=1000)) also worked with this one – ToTo Oct 01 '15 at 08:28
  • Yep - that's even simpler. The upper and lower limits can also be defined, but I commented them out in this example. – Marc in the box Oct 01 '15 at 08:31
1

As @Mateusz1981 pointed out, the error occurs due to a typo. If you fix this typo, you will end up with another error message ("singular gradient"). That means that you have to adjust your starting values. The following works fine for me:

fit60 <- nls(Predicted.Bromide~c+a*(1-exp(-b*n)),start=c(a=8000,b=0.2,c=4000),data=min60)
plot(Predicted.Bromide ~ n, min60)
lines(predict(fit60, min60), col='red')

The plot then looks as follows: enter image description here

Cleb
  • 25,102
  • 20
  • 116
  • 151
1

Use the "plinear" algorithm for this problem and you won't need starting values for the linear parameters. In the solution below .lin1 and .lin2 are a and c:

fit60 <- nls(Predicted.Bromide ~ cbind(1, 1-exp(-b*n)), data = min60,
  start = list(b = 0.02), algorithm = "plinear")

giving:

> fit60
Nonlinear regression model
  model: Predicted.Bromide ~ cbind(1, 1 - exp(-b * n))
   data: min60
        b     .lin1     .lin2 
1.231e-01 9.265e+03 2.384e+04 
 residual sum-of-squares: 1.97e+08

Number of iterations to convergence: 23 
Achieved convergence tolerance: 5.93e-06
G. Grothendieck
  • 254,981
  • 17
  • 203
  • 341