作者GTBUG (贾斯伯张)
看板R_Language
标题Re: [问题] 写loop选最适ARIMA model碰到的问题
时间Tue Oct 8 17:26:17 2013
最近为了改写程式,开始尝试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)