سؤال

I am using the forecast package in R and this creates a forecast object.

I am wanting to convert the forecast into a vector so that I can use 7bits wrapper and use R in MQL4 code.

Example forecast code:

> 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

I would like to be able to somehow store those Forecast, lo 80, hi 80 etc. In a vector so I can pull them out of R and into MQL4 for use in an indicator.

I tried:

> 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

However if I try to pull out forecast I get:

> test1$Forecast
NULL

If I run head the structure appears as:

> 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

Any help would be appreciated. It is keeping me from moving ahead with my tinkering haha.

Thanks in advance.

هل كانت مفيدة؟

المحلول

Function forecast() produces list. With function str() you can check structure of this object and with function names() see the names of each element in this list.

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]
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top