1

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

Rspacer
  • 2,369
  • 1
  • 14
  • 40
  • First you have more parameters, You should only have 2 parameters in your equation, you have 3 parameters – Onyambu May 27 '22 at 00:24
  • I don't understand your question. Why can't I have 3 parameters? It's a non-linear polynomial – Rspacer May 27 '22 at 00:26
  • Because if you open the paranthesis, one parameter is just a multiplication of the others making it irrelevant eg if you have `y~A*B*C*x` IS not worth it, just have `y~A*x` – Onyambu May 27 '22 at 00:29
  • Also why not use `nls` why mult you use `gnls`? – Onyambu May 27 '22 at 00:31
  • 1
    I am not farmiliar with mle. Though its easy to write a quick function which shall return NA in the case whereby A is not in the bounds. for `K_Ka` you will have the bounds as min = 800/45000, max = 3000/3500 – Onyambu May 27 '22 at 00:47
  • 1
    Also note that the answer given already has the conditions satisfied – Onyambu May 27 '22 at 00:52
  • @onyambu Thanks I'll see if I can find a few examples to produce NA's when condition isn't met. Thank you for your valuable input – Rspacer May 27 '22 at 00:56

1 Answers1

2

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
Dave2e
  • 22,192
  • 18
  • 42
  • 50