Search code examples
rnonlinear-optimizationnlsnonlinear-functionslog-likelihood

Non-linear least squares in R: Error in eval(predvars, data, env) : object not found


I am trying to fit a gnls function in R and throws me an error that says: Error in eval(predvars, data, env) : object A not found Not sure where I am going wrong.

set.seed(111)
y.size <- rnorm(100,4,1)
p <- rnorm(100,5,1)

df <- data.frame(p, y.size)

# fit generalised nonlinear least squares
require(nlme)
mgnls <- gnls(y.size ~  ((A *((p*K/Ka)-1))-1)* log(p), 
              start = list(A = c(-1,-10), 
                           K = c(800,3000), 
                           Ka = c(35000,45000)),
              data = df)
plot(mgnls) # more homogenous

For anyone needing more info: I'm trying to follow along this method


Solution

  • I see there are 2 issues. First I don't understand the convention list(A = c(-1,-10), K = c(800,3000),Ka = c(35000,4500)). Generally only 1 value is used to initialize the starting value.

    Second, your equation defines K/Ka with both values as adjustable parameters. This will cause errors since there are an infinite number of values for K and Ka which will evaluate to the same value. It is better to set one value to a constant or define a new value equal to the ratio.

    set.seed(111)
    y.size <- rnorm(100,4,1)
    p <- rnorm(100,5,1)
    
    df <- data.frame(p, y.size)
    
    # fit generalised nonlinear least squares
    require(nlme)
    mgnls <- gnls(y.size ~  ((A *((p*K_Ka)-1))-1)* log(p), 
                  start = list(A = -5,   K_Ka = 0.5),
                   data=df)
    plot(mgnls) # more homogenous