0

I'm using the function fitdist of fitdistrplus package. For some distributions I get an error. I don't mind it I just want the program not to stop, and assign a value to a variable and proceed.

Based on the help i've got my code looks like:

i=1
for (data in results)
  for (dist in distributions)
    resultst[[i]] <- tryCatch(
      fitdist( data, dist,method="mle",
           start=list(mean=mapply("[[", results[i], 1),
                      sd=mapply("[[", results[i], 2)),
           fix.arg=list(a=minv,b=maxv)),
           error = function(e) results[[i]])
       i=i+1

But get this error:

Error in resultst[[i]] <- tryCatch(fitdist(data, dist, method = "mle",  :
more elements supplied than there are to replace

SOLUTION:

i=1
for (data in results)
  for (dist in distributions)
  params <- tryCatch(
  fitdist( data, dist,method="mle",
           start=list(mapply("[[", results[i], 1),
                      mapply("[[", results[i], 2)),
           fix.arg=list(a=minv,b=maxv)),
           error = function(e) {
             fitdist( data, substring(dist,2),method="mle",
                      start=list(mapply("[[", results[i], 1),
                                 mapply("[[", results[i], 2)))
           })
resultst[i]<-params
resultst2[[i]]<-params
i=i+1

EDIT changed i=0 to i=1 (my mistake!) include params

jpcgandre
  • 1,487
  • 5
  • 31
  • 55
  • 5
    You might want to look into `tryCatch` – Dason Aug 25 '12 at 22:03
  • 1
    I use a function based on `tryCatch` that saves the result and any warnings and errors in a list for subsequent processing; see http://stackoverflow.com/q/4948361/210673 – Aaron left Stack Overflow Aug 26 '12 at 19:47
  • 2
    Start with `i=1`. Also, while editing for clarity and to improve the question is encouraged, for future readers, please don't change the question itself. That is, it's now unclear why Blue Magister would suggest `tryCatch`, because it's in your question. Sometimes people add information in response to an answer in an EDIT or FOLLOWUP section. – Aaron left Stack Overflow Aug 26 '12 at 19:50

1 Answers1

3

As Dason comments, tryCatch should help:

List2[[i]] <- tryCatch(
                    fitdist( data, dist,method="mle",
                          start=list(mean=x,
                                     sd=y,
                          fix.arg=list(a=minv,b=maxv)),
                    error = function(e) List1[[i]] 
              )
Blue Magister
  • 13,044
  • 5
  • 38
  • 56