'Plot initial data and predicted data (aima model) in R

I have a dataframe and for example df[[i]] object:

c(0.115357, 0.081623, 0.064095, 0.037976, 0.034594, 0.072012, 0.062988,
0.029926,0.016034, 0.068849, 0.045474, 0.014287, 0.042347, 
0.012183, 0.007037, 0.010355, 0.035283, 0.006473, 0.003692, 0.002738, 
0.003707, 0.002289, 0.001643, 0.001023, 0.000878, 6e-04, 0.000851, 
0.000645, 0.000968, 0.000856, 0.000637, 0.00052, 0.000611, 0.000397, 
0.000193, 1e-04, 7.5e-05, 7.2e-05, 7.4e-05, 4e-05, 4e-05)

I try to plot data the same: enter image description here

For my dataframe train data is:

dfL_F[[28]][1:25]

and predict data:

forecast1 <- predict(arimaModel_1, 16)

There is my code:

arimaModel_1 <- arima(dfL_F[[28]][1:25], order = c(1,1,2), method = "CSS")
forecast1 <- predict(arimaModel_1, 16)
ts.plot(as.ts(dfL_F[[28]][1:25]),forecast1)

And I get the error:

ts.plot(as.ts(dfL_F[[28]][1:25]),forecast1)
Error in .cbind.ts(list(...), .makeNamesTs(...), dframe = dframe, union = TRUE) : 
  non-time series not of the correct length

How to plot different order ARIMA and intial data for my case?

I'm sorry, but this post does not help solve my problems Predict and plot after fitting arima() model in R



Solution 1:[1]

The forecast1 is a list of two elements

> str(forecast1)
List of 2
 $ pred: Time-Series [1:16] from 26 to 41: -2.62e-05 1.53e-04 2.36e-04 2.75e-04 2.93e-04 ...
 $ se  : Time-Series [1:16] from 26 to 41: 0.0167 0.0168 0.0171 0.018 0.0191 ...

It returns a list because the usage predict.Arima says

predict(object, n.ahead = 1, newxreg = NULL, se.fit = TRUE, ...)

where

se.fit - Logical: should standard errors of prediction be returned?

Thus, by default it returns the standard error of prediction and if we use the full list, it returns the error

ts.plot(as.ts(dfL_F[[28]][1:25]), forecast1)
Error in .cbind.ts(list(...), .makeNamesTs(...), dframe = dframe, union = TRUE) : 
  non-time series not of the correct length

We need to extract the pred

ts.plot(as.ts(dfL_F[[28]][1:25]), forecast1$pred)

-output

enter image description here

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1