4

我在 R 中使用预测包,这会创建一个预测对象。

我想将预测转换为向量,以便我可以使用 7 位包装器并在 MQL4 代码中使用 R。

示例预测代码:

> forecast(fit, h=5)
     Point Forecast    Lo 80    Hi 80    Lo 95    Hi 95
1057       1.605098 1.602110 1.608087 1.600528 1.609668
1058       1.605109 1.600891 1.609327 1.598658 1.611561
1059       1.604868 1.599723 1.610012 1.597000 1.612735
1060       1.604978 1.599037 1.610919 1.595892 1.614065
1061       1.605162 1.598511 1.611813 1.594990 1.615335

我希望能够以某种方式将这些预测、lo 80、hi 80 等存储在向量中,这样我就可以将它们从 R 中拉出并进入 MQL4 以用于指标。

我试过:

> test1 <- forecast(fit, h=5)
> test1
     Point Forecast    Lo 80    Hi 80    Lo 95    Hi 95
1057       1.605098 1.602110 1.608087 1.600528 1.609668
1058       1.605109 1.600891 1.609327 1.598658 1.611561
1059       1.604868 1.599723 1.610012 1.597000 1.612735
1060       1.604978 1.599037 1.610919 1.595892 1.614065
1061       1.605162 1.598511 1.611813 1.594990 1.615335

但是,如果我尝试提取预测,我会得到:

> test1$Forecast
NULL

如果我运行 head 结构显示为:

> head(test1)
$method
[1] "ARIMA(2,1,2)                   "

$model
Series: mt4test$close 
ARIMA(2,1,2)                    

Coefficients:
          ar1      ar2     ma1     ma2
      -0.5030  -0.9910  0.4993  0.9783
s.e.   0.0123   0.0089  0.0202  0.0140

sigma^2 estimated as 5.437e-06:  log likelihood=4897.31
AIC=-9784.61   AICc=-9784.55   BIC=-9759.81

$level
[1] 80 95

$mean
Time Series:
Start = 1057 
End = 1061 
Frequency = 1 
[1] 1.605098 1.605109 1.604868 1.604978 1.605162

$lower
          80%      95%
[1,] 1.602110 1.600528
[2,] 1.600891 1.598658
[3,] 1.599723 1.597000
[4,] 1.599037 1.595892
[5,] 1.598511 1.594990

$upper
          80%      95%
[1,] 1.608087 1.609668
[2,] 1.609327 1.611561
[3,] 1.610012 1.612735
[4,] 1.610919 1.614065
[5,] 1.611813 1.615335

任何帮助,将不胜感激。它使我无法继续进行修补哈哈。

提前致谢。

4

1 回答 1

3

函数forecast()产生列表。使用函数str(),您可以检查此对象的结构,并使用函数names()查看此列表中每个元素的名称。

library(forecast)
fit <- Arima(WWWusage,c(3,1,0))
test1<-forecast(fit)

names(test1)
[1] "method"    "model"     "level"     "mean"      "lower"     "upper"     "x"        
[8] "xname"     "fitted"    "residuals"

 #to extract forecast
test1$mean

Time Series:
Start = 101 
End = 110 
Frequency = 1 
 [1] 219.6608 219.2299 218.2766 217.3484 216.7633 216.3785 216.0062 215.6326 215.3175 215.0749

 #or as vector
as.vector(test1$mean)
 [1] 219.6608 219.2299 218.2766 217.3484 216.7633 216.3785 216.0062 215.6326 215.3175 215.0749

 #to extract upper interval
test1$upper

           80%      95%
 [1,] 223.5823 225.6582
 [2,] 228.5332 233.4581
 [3,] 232.7151 240.3585
 .... .... ....
[10,] 260.7719 284.9625

 #to extract lower interval
test1$lower

 #to extract only 95% upper interval
test1$upper[,2]
于 2012-12-14T17:25:19.450 回答