Re: [問題] 寫loop選最適ARIMA model碰到的問題

看板R_Language作者 (賈斯伯張)時間10年前 (2013/10/08 17:26), 編輯推噓0(000)
留言0則, 0人參與, 最新討論串2/2 (看更多)
最近為了改寫程式,開始嘗試W大先前建議的例外處理, 我把原文的code先包成一個function, ex: functionA<-function(i,j,k) { 原文code.... } 接著利用tryCatch()去執行functionA (這邊我不太了解,是在呼叫function時用tryCatch去包, 還是要先把tryCatch寫在function裡面,我這邊做的是前者) z <- tryCatch( { functionA(5,2,5) }, error = function(e) { message("Model not good: ", cat(p,d,q)) ;next }, finally { print("all done") } ) z 接著R就開始output依序的model output,直到某一model無法fitting,就中止 (所以所要的例外處理然後繼續loop 是失敗了...) error message: Encountered an error: Error in arima(tr_set$ED, order = c(p, d, q)): non-stationary AR part from CSS -->是有告知我中止的原因 錯誤在value[[3L]](cond) : 沒有可中斷/下一個的迴圈,跳到頂層 此外: 警告訊息: -->代表我例外處理放錯地方 所以next跳錯loop的位子? In arima(tr_set$ED, order = c(p, d, q)) : possible convergence problem: optim gave code = 1 -->again same error message as usual 以上問題請教 麻煩了 ※ 引述《GTBUG (賈斯伯張)》之銘言: : [問題類型]: : 程式諮詢 : [軟體熟悉度]: : 入門(寫過其他程式,只是對語法不熟悉) : [問題敘述]: : 目前針對一筆3年的daily資料作建模(365*3筆daily count data),已利用過傳統 : time-series建模方法,透過ACF, PACF等判斷去找model,也利用過auto.arima去找model : ,現在希望透過手寫一個loop去run各種model的組合,再由最小AIC來選出較適的模型 : 當我run後,卻吐回一個error message給我,然後終止loop而沒有任何結果 : : "錯誤在stats:::arima(x, order = order, seasonal = seasonal, fixed = : par[1:narma], : wrong length for 'fixed' " : : 如果不用loop, 而是一個一個MODEL慢慢try,就可以跑(如下) : ex. : fit<-arimax(x,order=c(3,1,3),seasonal=list(order=c(0,1,1),period=7),xreg=xreg) : fit2<-arimax(x,order=c(3,1,3),seasonal=list(order=c(0,1,2),period=7),xreg=xreg) : .. : .. : [程式範例]: : : count = 0 : for(i in 0:5) { : for(j in 0:2) { : for(k in 0:1) { : for(l in 0:1) { : for(m in 0:2) { : for(n in 0:1) { : for(o in 0:2) { : model = arimax(ed0710$ED, order=c(i,j,k), : seasonal=list(order=c(l,m,n), period=7*o), xreg=xreg1) : if (count == 0) { : aicmin = model$aic : bestmodel = model : } : if ((count != 0)&& (model$aic< aicmin)) { : aicmin = model$aic : bestmodel = model : diff1 = j : diff2 = m : seas = o*7 : } : count = count+1 : } : } : } : } : } : } : print(bestmodel) : print(count) : print(diff1) : print(diff2) : print(seas) : } : : [關鍵字]: time-series, ARIMA, loop -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 203.65.71.253 ※ 編輯: GTBUG 來自: 203.65.71.253 (10/08 17:27)
文章代碼(AID): #1IKy-zYI (R_Language)
文章代碼(AID): #1IKy-zYI (R_Language)