0

I am trying to fit a non-linear model using nls function. The model has a conditioning and works by groups therefore I try to implement a loop which covers over 300 combinations of starting values. I am using tryCatch but to my surprise the loop crashes, I guess because of the starting values or lower and upper bounds.

tryCatch(

for(r in 1:nrow(st4)){

halfLE3[[r]] <- nls(

  inty ~ I(time < (position/velocity) + dinitial) * Inty_S0 +
    (time >= (position/velocity) + dinitial) *
    I(Intyf[probe] + (Inty_S0[probe] - Intyf) * 
        (exp(-Decay * (time - (position/velocity) + dinitial)))),
  
  data = Data4,

  algorithm = "port",

  control = list(warnOnly = TRUE),
  
  start = list(Decay=st4[r,3], dinitial=st4[r,2],
               Intyf=rep(st4[r,4], length(levels(Data4$probe))), 
               Inty_S0=rep(st4[r,5], length(levels(Data4$probe))),
               velocity=st4[r,1]),
  
  lower = list(Decay= .1, dinitial=0.1, velocity=10, Intyf=0.1, inty_S0=.5),
  upper = list(Decay= 1, dinitial=10, velocity=8000, Intyf=1, inty_S0=1.5)
)

}

,error = function(e) {e}

)

st4 is a dataframe with 300 combinations of starting values. probe refers to group.

Do you have any idea why the loop crashes even using the tryCatch? Do you have any idea what I can improve? I used the nls.control changing the maxiter and other parameters but works same as the control I use here above.

I would be very grateful for any suggestion.

Loub
  • 101
  • 1

1 Answers1

0

I fixed the issue. The tryCatch should be located inside the for loop adding a bracket.

for(r in 1:nrow(st4)){

tryCatch({ nls()......

},error = function(e) {e}

) }

Dharman
  • 30,962
  • 25
  • 85
  • 135
Loub
  • 101
  • 1