Search code examples
rtime-seriesforecast

I need to make my forecast function to output a vector in R


I write the below r code to forecast to a future time. I initially divide the data set into train and test set so I can cross-validate the model used. I then write to forecast the train set to the length of test set.

# construct a time series data
n <- 50
phi <- 0.5
set.seed(1)
wn <- rnorm(n, mean=0, sd=1)
ar1 <- sqrt((wn[1])^2/(1-phi^2))
for(i in 2:n){
  ar1[i] <- ar1[i - 1] * phi + wn[i]
}
library(forecast) # load `forecast` package

train <- head(ar1, round(length(ar1) * 0.6)) # Divide the time series into `train` set
h <- length(ar1) - length(train)
test <- tail(ar1, h) # Divide the time series into `test` set
model <- auto.arima(train) # obtain optimual model
future <- forecast(test, model=model,h=h) # forecast the `test` set into 20 times ahead
future

What I have

The object future is printed out as:

#     Point Forecast   Lo 80    Hi 80     Lo 95    Hi 95
#21   4.431517e-01 -0.7445769 1.630880 -1.373322 2.259626
#22   1.942684e-01 -1.1025747 1.491112 -1.789082 2.177619
#23   8.516321e-02 -1.2316138 1.401940 -1.928673 2.099000
#24   3.733377e-02 -1.2832396 1.357907 -1.982309 2.056976
#25   1.636634e-02 -1.3049353 1.337668 -2.004390 2.037123
#26   7.174662e-03 -1.3142669 1.328616 -2.013796 2.028145
#27   3.145222e-03 -1.3183232 1.324614 -2.017866 2.024157
#28   1.378799e-03 -1.3200948 1.322852 -2.019641 2.022398
#29   6.044368e-04 -1.3208702 1.322079 -2.020417 2.021625
#30   2.649724e-04 -1.3212098 1.321740 -2.020756 2.021286
#31   1.161584e-04 -1.3213587 1.321591 -2.020905 2.021138
#32   5.092140e-05 -1.3214239 1.321526 -2.020970 2.021072
#33   2.232288e-05 -1.3214525 1.321497 -2.020999 2.021044
#34   9.785882e-06 -1.3214651 1.321485 -2.021012 2.021031
#35   4.289926e-06 -1.3214706 1.321479 -2.021017 2.021026
#36   1.880614e-06 -1.3214730 1.321477 -2.021020 2.021023
#37   8.244217e-07 -1.3214740 1.321476 -2.021021 2.021022
#38   3.614092e-07 -1.3214745 1.321475 -2.021021 2.021022
#39   1.584342e-07 -1.3214747 1.321475 -2.021021 2.021022
#40   6.945425e-08 -1.3214748 1.321475 -2.021021 2.021021

What I want

I want the object future to print out as a vector of a point estimate alone without any interval.

#4.431517,  1.942684, ...,6.945425

and should be a class of vector


Solution

  • If we check the structure of 'future', it is a list of elements and the mean is a time series i.e.

    str(future)
    #List of 10
    #$ method   : chr "ARIMA(1,0,0) with zero mean"
    #$ model    :List of 18
    # ..
    # $ mean     : Time-Series [1:20] from 21 to 40: 0.4432 0.1943 0.0852 0.0373 0.0164 ...
    

    The list element can be extracted by the usual extraction methods i.e. either with $ or [[, then convert the element which is a time series to vector. The ts is also a vector with some attributes

    We can convert to numeric, with just concatenation

    c(future$mean)
    #[1] 0.44315168953402 0.19426842584397 0.08516321199086 0.03733376973171 0.01636634328130 0.00717466235867
    #[7] 0.00314522181749 0.00137879941755 0.00060443680737 0.00026497244592 0.00011615837461 0.00005092140032
    #[13] 0.00002232287615 0.00000978588170 0.00000428992573 0.00000188061365 0.00000082442167 0.00000036140920
    #[19] 0.00000015843423 0.00000006945425
    

    Or by changing the class

    class(future$mean) <- 'numeric'