Haris
Haris

Reputation: 27

R code: "Error in parse unexpected end of input" when using non-linear solver

I am using the non-linear solver (nls) in R, but cannot run my model due to a parser error that I have no idea how to debug. Could someone please offer some advice on how to fix this?

Code:

Bass.nls <- nls( Zt[which(!is.na(Zt))] ~ M * ( ((P+Q)^2 / P) * exp(-(P+Q) * days) ) / (1+(Q/P)*exp(-(P+Q)*days))^2, start = list(M=Z[tInt], P=0.03, Q=0.38), lower = list(Y[tInt], 0,0), upper = list(2e10, 1,1), algorithm = "port", trace = TRUE)

Error:

Error in parse(text = x, keep.source = FALSE) : :2:0: unexpected end of input 1: ~

Upvotes: 0

Views: 20299

Answers (2)

Caped_Crusader
Caped_Crusader

Reputation: 33

The error

Unexpected end of input

occurs because of a missing paranthesis. You might have forgotten to finish the call to the function with a closing paranthesis - ")".

Look at the section 6.4 of this link for a better explanation.


You should write the script as described in Johannes' answer as it gets easy for you to debug the mistake then.

Upvotes: 2

Johannes Ranke
Johannes Ranke

Reputation: 469

My advice would be to write a script that contains the call in a more readable form, like

Bass.f <- function(days, M, P, Q) {
  M * (((P + Q)^2 / P) * exp(-(P + Q) * days)) / 
    (1 + (Q / P) * exp(-(P + Q) * days))^2
}

Then you can call the function to see if it is written correctly, like

Bass.f(1, 100, 0.03, 0.38)

and then try with the call

Bass.nls <- nls( Zt[which(!is.na(Zt))] ~ Bass.f(days, M, P, Q),
  start = list(M = Z[tInt], P = 0.03, Q = 0.38),
  lower = list(Y[tInt], 0, 0),
  upper = list(2e10, 1, 1),
  algorithm = "port", trace = TRUE)

Does this still give the same error? If yes, it would be useful to see the data you are using, i.e. the output of

dput(Zt)

Z[tInt]

days

Upvotes: 0

Related Questions