首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >For循环中的排序模型

For循环中的排序模型
EN

Stack Overflow用户
提问于 2016-11-24 22:01:12
回答 1查看 83关注 0票数 0

假设我在R中有下面的"for“循环来生成滚动预测,并从一组四种ARMA模型中重构模型。我构建的代码是基于上一篇关于这个主题的文章(参见下面的链接:https://stats.stackexchange.com/questions/208985/selecting-arima-order-using-rolling-forecast)

代码语言:javascript
运行
复制
h <- 1
train <- window(USDlogreturns, end=1162)
test <- window(USDlogreturns, start=1163)
n <- length(test) - h + 1
fit1 <- Arima(train, order=c(0,0,0), include.mean=TRUE, method="ML")
fit2 <- Arima(train, order=c(0,0,1), include.mean=TRUE, method="ML")
fit3 <- Arima(train, order=c(1,0,0), include.mean=TRUE, method="ML")
fit4 <- Arima(train, order=c(1,0,1), include.mean=TRUE, method="ML")
fc1 <- ts(numeric(n), start=1163+1, freq=1)
fc2 <- ts(numeric(n), start=1163+1, freq=1)
fc3 <- ts(numeric(n), start=1163+1, freq=1)
fc4 <- ts(numeric(n), start=1163+1, freq=1)
for(i in 1:n)
{  
x <- window(USDlogreturns, end=1162 + i)
refit1 <- Arima(x, model=fit1, include.mean=TRUE, method="ML")
refit2 <- Arima(x, model=fit2, include.mean=TRUE, method="ML")
refit3 <- Arima(x, model=fit3, include.mean=TRUE, method="ML")
refit4 <- Arima(x, model=fit4, include.mean=TRUE, method="ML")
fc1[i] <- forecast(refit1, h=h)$mean[h]
fc2[i] <- forecast(refit2, h=h)$mean[h]
fc3[i] <- forecast(refit3, h=h)$mean[h]
fc4[i] <- forecast(refit4, h=h)$mean[h]
}
result.fc<-cbind(fc1, fc2, fc3, fc4)

以下代码计算各种预测精度度量(有关这些度量的说明,请参阅此链接:http://127.0.0.1:15135/library/forecast/html/accuracy.html)。

代码语言:javascript
运行
复制
accuracy(fc1, test)[,1:5]
accuracy(fc2, test)[,1:5]
accuracy(fc3, test)[,1:5]
accuracy(fc4, test)[,1:5]

我的问题是:

我如何告诉循环按照五个不同的矩阵中的五个预测精度度量对四个估计模型进行排序?

谢谢你的帮助。

EN

Stack Overflow用户

回答已采纳

发布于 2016-11-25 14:28:54

我的解决方案,但我不知道我是否正确理解,你想得到什么结果。

代码语言:javascript
运行
复制
# empty vectors
acc_fc1=c()
acc_fc2=c()
acc_fc3=c()
acc_fc4=c()

h <- 1
train <- window(USDlogreturns, end=1162)
test <- window(USDlogreturns, start=1163)
n <- length(test) - h + 1
fit1 <- Arima(train, order=c(0,0,0), include.mean=TRUE, method="ML")
fit2 <- Arima(train, order=c(0,0,1), include.mean=TRUE, method="ML")
fit3 <- Arima(train, order=c(1,0,0), include.mean=TRUE, method="ML")
fit4 <- Arima(train, order=c(1,0,1), include.mean=TRUE, method="ML")
fc1 <- ts(numeric(n), start=1163+1, freq=1)
fc2 <- ts(numeric(n), start=1163+1, freq=1)
fc3 <- ts(numeric(n), start=1163+1, freq=1)
fc4 <- ts(numeric(n), start=1163+1, freq=1)
for(i in 1:n)
{  
  x <- window(USDlogreturns, end=1162 + i)
  refit1 <- Arima(x, model=fit1, include.mean=TRUE, method="ML")
  refit2 <- Arima(x, model=fit2, include.mean=TRUE, method="ML")
  refit3 <- Arima(x, model=fit3, include.mean=TRUE, method="ML")
  refit4 <- Arima(x, model=fit4, include.mean=TRUE, method="ML")
  fc1[i] <- forecast(refit1, h=h)$mean[h]
  fc2[i] <- forecast(refit2, h=h)$mean[h]
  fc3[i] <- forecast(refit3, h=h)$mean[h]
  fc4[i] <- forecast(refit4, h=h)$mean[h]

  acc_fc1=rbind(acc_fc1, accuracy(fc1, test)[,1:5])
  acc_fc2=rbind(acc_fc2, accuracy(fc2, test)[,1:5])
  acc_fc3=rbind(acc_fc3, accuracy(fc3, test)[,1:5])
  acc_fc4=rbind(acc_fc4, accuracy(fc4, test)[,1:5])
}
result.fc<-cbind(fc1, fc2, fc3, fc4)

# 5 matrices with accuracy measures
result.acc1<-cbind(acc_fc1[,1], acc_fc2[,1], acc_fc3[,1], acc_fc4[,1])
result.acc2<-cbind(acc_fc1[,2], acc_fc2[,2], acc_fc3[,2], acc_fc4[,2])
result.acc3<-cbind(acc_fc1[,3], acc_fc2[,3], acc_fc3[,3], acc_fc4[,3])
result.acc4<-cbind(acc_fc1[,4], acc_fc2[,4], acc_fc3[,4], acc_fc4[,4])
result.acc5<-cbind(acc_fc1[,5], acc_fc2[,5], acc_fc3[,5], acc_fc4[,5])

# if you want to know which model is the best
t(apply(result.acc1, 1, order))
t(apply(result.acc2, 1, order))
t(apply(result.acc3, 1, order))
t(apply(result.acc4, 1, order))
t(apply(result.acc5, 1, order))
票数 0
EN
查看全部 1 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40795032

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档