[R] optimization in for loop

David Winsemius dwinsemius at comcast.net
Wed Jun 29 21:12:33 CEST 2011


On Jun 29, 2011, at 2:31 PM, siriustar wrote:

> Hi, dear R help
> I am trying to use optim inside a for loop:
>
> ##For example. a: intial guess.  b: result.  f: function to be  
> minimized
> for (i in 1:10) {
>  b[i] <- optim(a[i], f)}
>
> However, some intial values cause error in optim function (e.g. "  
> system is
> computationally singular..."). Then the for loop stops and won't try  
> the
> following initial guesses.

If you want to work around errors, you need to catch them and provide  
alternative:

?try


>
> What can I do if I want the for loop procedure to finish all the  
> initial
> guesses? I know this may be a stupid question.. but I am not familar  
> with R
> optim. So thankyou very much for any help..
>
> Sirius
>
> --
> View this message in context: http://r.789695.n4.nabble.com/optimization-in-for-loop-tp3633638p3633638.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

David Winsemius, MD
West Hartford, CT



More information about the R-help mailing list