使用循环为订单的所有组合创建模型
Use Loop to create Models for all combinations of order
我正在尝试使用循环为 p = 15 到 1、d=2 到 0、q = 15 到 1 的所有组合创建模型,然后使用滚动预测来评估这些模型,然后选择说出具有最低 mape 的那个 (Accuracy(fc$mean,test)[5])。
我不太擅长循环,所以非常感谢任何提示。我在下面有一个有效的 "before code" 示例,"after code" 显示了我想如何修改它。
代码之前:
library("fpp")
h <- 5
tsTrain <- window(hsales,end=1989.99)
tsTest <- window(hsales,start=1990)
n <- length(tsTest) - h + 1
ModFit <- Arima(tsTrain, order=c(15,2,15)
fc <- ts(numeric(n), start=1990+(h-1)/12, freq=12)
for(i in 1:n)
{
x <- window(hsales, end=1989.99 + (i-1)/12)
reModFit <- Arima(x, model=ModFit)
fc[i] <- forecast(reModFit, h=h)$mean[h]
}
代码后:
library("fpp")
h <- 5
tsTrain <- window(hsales,end=1989.99)
tsTest <- window(hsales,start=1990)
n <- length(tsTest) - h + 1
##Create models for all combinations of p 15 to 0, d 2 to 0, q 15 to 0
ModFit1 <- Arima(tsTrain, order=c(15,2,15)
ModFit2 <- Arima(tsTrain, order=c(9,2,15)
ModFit3 <- Arima(tsTrain, order=c(8,2,15)
.
.
.
ModFit15 <- Arima(tsTrain, order=c(0,2,15)
fc1 <- ts(numeric(n), start=1990+(h-1)/12, freq=12)
fc2 <- ts(numeric(n), start=1990+(h-1)/12, freq=12)
fc3 <- ts(numeric(n), start=1990+(h-1)/12, freq=12)
.
.
.
fc15 <- ts(numeric(n), start=1990+(h-1)/12, freq=12)
for(i in 1:n)
{
x <- window(hsales, end=1989.99 + (i-1)/12)
reModFit1 <- Arima(x, model=ModFit1)
reModFit2 <- Arima(x, model=ModFit2)
reModFit3 <- Arima(x, model=ModFit3)
.
.
.
reModFit15 <- Arima(x, model=ModFit15)
fc1[i] <- forecast(reModFit1, h=h)$mean[h]
fc2[i] <- forecast(reModFit2, h=h)$mean[h]
fc3[i] <- forecast(reModFit3, h=h)$mean[h]
.
.
.
fc15[i] <- forecast(reModFit15, h=h)$mean[h]
}
##Calculating mape for forecasts
Accuracy(fc1$mean,tsTest)[,5]
Accuracy(fc2$mean,tsTest)[,5]
Accuracy(fc3$mean,tsTest)[,5]
.
.
.
Accuracy(fc15$mean,tsTest)[,5]
##Return the order of the Arima model that has the lowest mape
ModFit5 ARIMA(10,2,15) ##This is just an example, I don't know if this really the one with the lowest mape.
更新:
pvar<-1:15
dvar<-1:2
qvar<-1:15
OrderGrid<-expand.grid(pvar,dvar,qvar)
ModFit <-function(a,b,c,dat){
m=Arima(tsTrain, order=c(a,b,c))
return(m)
}
Fits<-apply(OrderGrid,1,ModFit)
错误:
Arima(tsTrain, order = c(a, b, c)) 错误:
缺少参数 "c",没有默认值
更新:
ModFit <- function(x, dat){
m=Arima(dat, order=c(x[[1]], x[[2]], x[[3]]),method="ML")
return(m)
}
Fits <- plyr::alply(OrderGrid, 1, ModFit, dat = tsTrain)
使用 expand.grid
和 alply
作为 apply
将简化为 arima 对象矩阵,这将是一个痛苦
pvar<-1:15
dvar<-1:2
qvar<-1:15
OrderGrid<-expand.grid(pvar,dvar,qvar)
ModFit <- function(x, dat){
m=Arima(dat, order=c(x[[1]], x[[2]], x[[3]]))
return(m)
}
Fits <- plyr::alply(OrderGrid, 1, ModFit, dat = tsTrain)
Fits
将是一个列表。
我正在尝试使用循环为 p = 15 到 1、d=2 到 0、q = 15 到 1 的所有组合创建模型,然后使用滚动预测来评估这些模型,然后选择说出具有最低 mape 的那个 (Accuracy(fc$mean,test)[5])。
我不太擅长循环,所以非常感谢任何提示。我在下面有一个有效的 "before code" 示例,"after code" 显示了我想如何修改它。
代码之前:
library("fpp")
h <- 5
tsTrain <- window(hsales,end=1989.99)
tsTest <- window(hsales,start=1990)
n <- length(tsTest) - h + 1
ModFit <- Arima(tsTrain, order=c(15,2,15)
fc <- ts(numeric(n), start=1990+(h-1)/12, freq=12)
for(i in 1:n)
{
x <- window(hsales, end=1989.99 + (i-1)/12)
reModFit <- Arima(x, model=ModFit)
fc[i] <- forecast(reModFit, h=h)$mean[h]
}
代码后:
library("fpp")
h <- 5
tsTrain <- window(hsales,end=1989.99)
tsTest <- window(hsales,start=1990)
n <- length(tsTest) - h + 1
##Create models for all combinations of p 15 to 0, d 2 to 0, q 15 to 0
ModFit1 <- Arima(tsTrain, order=c(15,2,15)
ModFit2 <- Arima(tsTrain, order=c(9,2,15)
ModFit3 <- Arima(tsTrain, order=c(8,2,15)
.
.
.
ModFit15 <- Arima(tsTrain, order=c(0,2,15)
fc1 <- ts(numeric(n), start=1990+(h-1)/12, freq=12)
fc2 <- ts(numeric(n), start=1990+(h-1)/12, freq=12)
fc3 <- ts(numeric(n), start=1990+(h-1)/12, freq=12)
.
.
.
fc15 <- ts(numeric(n), start=1990+(h-1)/12, freq=12)
for(i in 1:n)
{
x <- window(hsales, end=1989.99 + (i-1)/12)
reModFit1 <- Arima(x, model=ModFit1)
reModFit2 <- Arima(x, model=ModFit2)
reModFit3 <- Arima(x, model=ModFit3)
.
.
.
reModFit15 <- Arima(x, model=ModFit15)
fc1[i] <- forecast(reModFit1, h=h)$mean[h]
fc2[i] <- forecast(reModFit2, h=h)$mean[h]
fc3[i] <- forecast(reModFit3, h=h)$mean[h]
.
.
.
fc15[i] <- forecast(reModFit15, h=h)$mean[h]
}
##Calculating mape for forecasts
Accuracy(fc1$mean,tsTest)[,5]
Accuracy(fc2$mean,tsTest)[,5]
Accuracy(fc3$mean,tsTest)[,5]
.
.
.
Accuracy(fc15$mean,tsTest)[,5]
##Return the order of the Arima model that has the lowest mape
ModFit5 ARIMA(10,2,15) ##This is just an example, I don't know if this really the one with the lowest mape.
更新:
pvar<-1:15
dvar<-1:2
qvar<-1:15
OrderGrid<-expand.grid(pvar,dvar,qvar)
ModFit <-function(a,b,c,dat){
m=Arima(tsTrain, order=c(a,b,c))
return(m)
}
Fits<-apply(OrderGrid,1,ModFit)
错误:
Arima(tsTrain, order = c(a, b, c)) 错误: 缺少参数 "c",没有默认值
更新:
ModFit <- function(x, dat){
m=Arima(dat, order=c(x[[1]], x[[2]], x[[3]]),method="ML")
return(m)
}
Fits <- plyr::alply(OrderGrid, 1, ModFit, dat = tsTrain)
使用 expand.grid
和 alply
作为 apply
将简化为 arima 对象矩阵,这将是一个痛苦
pvar<-1:15
dvar<-1:2
qvar<-1:15
OrderGrid<-expand.grid(pvar,dvar,qvar)
ModFit <- function(x, dat){
m=Arima(dat, order=c(x[[1]], x[[2]], x[[3]]))
return(m)
}
Fits <- plyr::alply(OrderGrid, 1, ModFit, dat = tsTrain)
Fits
将是一个列表。