2012-08-25 24 views
0

我正在使用fitdistrplus包的函數fitdist。 對於某些發行版,我收到錯誤消息。我不介意,我只是希望程序不要停下來,併爲變量賦值並繼續。忽略錯誤並將值分配給列表

基礎上的幫助,我有我的代碼如下所示:

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 

但得到這個錯誤:

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 

編輯 改變了我= 0到i = 1(我的錯誤!) 包括params

+5

你可能想看看'tryCatch' – Dason

+1

我使用基於'tryCatch',節省的結果和隨後的一個列表中的任何警告和錯誤的函數處理;請參閱http://stackoverflow.com/q/4948361/210673 – Aaron

+2

從「i = 1」開始。另外,爲了清晰起見編輯並改進問題,對於未來的讀者,請不要改變問題本身。也就是說,現在還不清楚Blue Magister爲什麼會建議'tryCatch',因爲這是你的問題。有時候人們會在EDIT或FOLLOWUP部分回答信息。 – Aaron

回答

3

由於達誠意見,tryCatch應該有所幫助:

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]] 
      )