简体   繁体   English

如何循环获取最小 RMSE 值并在每列中使用“应用”进行预测

[英]How to loop for minimum RMSE values and use “apply” for forecasting in each column

Could anyone provides me guidance on how to use for loop to look up minimum RMSE value and use "apply function" for forecasting in each column?任何人都可以为我提供有关如何使用 for 循环查找最小 RMSE 值并使用“应用函数”在每列中进行预测的指导吗? Here is the dataset.这是数据集。

T 6753 6763 6803 6806 6777 6799 6809 6832 6838 6831 6838 6807 6782 6809 6785 6766 6788 6704 6656 7093 7091 7100 7074 7047 7063 7070 7068 7054 7056 7067 7040 7027 7032 7055 7058 7051 7074 7109 7103 7127 7121 7111 7123 7147 7119 7106 7103 7091 7097 7103 7086 7099 7094 7139 7186 7198 7248 7274 7319 7329 7384 7410 7479 T 6753 6763 6803 6806 6777 6799 6809 6832 6838 6831 6838 6807 6782 6809 6785 6766 6788 6704 6656 7093 7091 7100 7074 7047 7063 7070 7068 7054 7056 7067 7040 7027 7032 7055 7058 7051 7074 7109 7103 7127 7121 7111 7123 7147 7119 7106 7103 7091 7097 7103 7086 7099 7094 7139 7186 7198 7248 7274 7319 7329 7384 7410 7479

C 2307 2296 2297 2287 2273 2259 2246 2230 2215 2194 2175 2110 2098 2074 2070 2107 2117 2128 2106 1687 1674 1664 1638 1641 1672 1679 1677 1675 1681 1675 1665 1697 1694 1693 1693 1691 1703 1706 1700 1695 1698 1712 1688 1701 1693 1674 1690 1688 1710 1711 1692 1688 1700 1684 1755 1744 1764 1762 1753 1753 1768 1763 1788 C 2307 2296 2297 2287 2273 2259 2246 2230 2215 2194 2175 2110 2098 2074 2070 2107 2117 2128 2106 1687 1674 1664 1638 1641 1672 1679 1677 1675 1681 1675 1665 1697 1694 1693 1693 1691 1703 1706 1700 1695 1698 1712 1688 1701 1693 1674 1690 1688 1710 1711 1692 1688 1700 1684 1755 1744 1764 1762 1753 1753 1768 1763 1788

I have a total of 2 columns that I need to forecast in R.我总共需要在 R 中预测 2 列。 I plan to use "Holt" to forecast future values.我计划使用“Holt”来预测未来的价值。 One of the variables in Holt is to input beta. Holt 中的变量之一是输入 beta。 I don't know how to write a function to assign beta to each column.我不知道如何编写 function 来为每一列分配 beta。

Following is how I calculate RMSE value.以下是我计算 RMSE 值的方法。 It is easy if I only have one column.如果我只有一列,这很容易。 But now I have 2 or more columns.但现在我有 2 列或更多列。

beta<-seq(.05,.9,by=0.001)
RMSE<-NULL
for (i in seq_along(beta)){
  fit<-holt(cretrain, beta=beta[i],h = length(cretest))
  RMSE[i]<-accuracy(fit,cretest)[2,2]
}

beta.fit<-data_frame(beta,RMSE)
beta.min<-filter(beta.fit,RMSE==min(RMSE))

Here is how I use apply for forecasting.这是我如何使用 apply 进行预测。 Ideally, I want beta to base off the values that coming out of the fist chunk of codes.理想情况下,我希望 beta 以来自第一块代码的值为基础。

list<-apply(df,2,function(x)holt(ts(x,start = c(2015,1),end = c(2020,1),frequency = 12),h=2,beta=.5))

Thank you谢谢

We can loop over the columns with lapply , then loop over the 'beta' values, apply the holt , rbind the accuracy measures for each 'beta' ('beta.fit'), subset the rows where the 'RMSE' is min imum.我们可以使用lapply列,然后遍历“beta”值,应用holtrbind为每个“beta”(“beta.fit”)的accuracy度量,将“RMSE” min的行subset . This returns the subset for each column of the dataset这将返回数据集每一列的子集

beta <- seq(.05, .9,by=0.001)
out <- lapply(df, function(x) {
         beta.fit <- do.call(rbind, lapply(beta, function(b) {
             fit <- holt(x, beta = b, h = length(x))
             print(head(fit$mean))
               data.frame(beta = b, RMSE =  accuracy(fit, x)[2,2])
             }))
       subset(beta.fit, RMSE == min(RMSE))
     })

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM