Using tryCatch to avoid erros in a double loop

33 views Asked by At

I have to write a double loop in order to check the right parameters for 'i' and 'j', so I thought I could use tryCatch() to keep the program running when parameters that do not fit a distribution are met.

I've written the following code:

tryCatch({
for (i in -2:2) {
  for (j in 0:6) {
    parms<-JohnsonFit(c(0, 1, i, j), moment="use")
    sJohnson(parms)
    datos <- rJohnson(1000, parms)
    plot(density(datos))
    }
  }
})

But I get this error:

Error in JohnsonFit(c(0, 1, i, j), moment = "use") : 

Moment ratio in error

Any ideas? Thanks in advance.

1

There are 1 answers

2
Stéphane Laurent On BEST ANSWER

Try:

for (i in -2:2) {
  for (j in 0:6) {
    try({
      parms <- JohnsonFit(c(0, 1, i, j), moment="use")
      sJohnson(parms)
      datos <- rJohnson(1000, parms)
      plot(density(datos))
    })
  }
}