作者GTBUG (贾斯伯张)
看板R_Language
标题[问题] 写loop选最适ARIMA model碰到的问题
时间Tue Apr 9 14:08:48 2013
[问题类型]:
程式谘询
[软体熟悉度]:
入门(写过其他程式,只是对语法不熟悉)
[问题叙述]:
目前针对一笔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
1F:→ Wush978:错误发生的时候,你回圈的指标分别是多少呢? 04/09 16:06
2F:→ GTBUG:W大你好,所以R也可以在错误时做break,然後trace他的指标? 04/09 16:25
3F:→ GTBUG:在这方便我很菜,麻烦W大开导^^" 04/09 16:26
4F:→ GTBUG: 面 04/09 16:26
5F:→ andrew43:在debug时,可在每层loop里写上cat(i,j)等以显示其值 04/09 16:29
6F:→ andrew43:要手动追踪也可以, 但我不常用就忘了 XD 04/09 16:30
7F:推 lin15:手动就直接看i j的值就知道停在哪边了 04/09 16:38
8F:→ Wush978:可参考#1HP9v_dr来除错,或是直接用例外处理把回圈跑完 04/10 07:05