TS Forecasting code

machine_learning

#1

Hi Experts.

Please help me in understanding the following code

fit <- arima(AirPassengers, order=c(1,0,0), list(order=c(2,1,0), period=12))

fore <- predict(fit, n.ahead=24)

error bounds at 95% confidence level

U <- fore$pred + 2*fore$se

L <- fore$pred – 2*fore$se

Regards,
tony


#2

Hi @tillutony,

Could you share the first five rows of dataset you are working on? (I presume its “AirPassengers”)? Also could you give some more context as to why this code is written?


#3

Hi Jal Faizy,

Answers inline

Could you share the first five rows of dataset you are working on? (I presume its “AirPassengers”)?

yes .Air passengers dataset.

Also could you give some more context as to why this code is written?

was trying to explore forecasting using arima model on the dataset. the same code was used to build the model in one of the blog so was unable to follow why it has been used so put to the Experts team to get their inputs.

fit <- arima(AirPassengers, order=c(1,0,0), list(order=c(2,1,0), period=12))
fit

Call:
arima(x = AirPassengers, order = c(1, 0, 0), seasonal = list(order = c(2, 1,
0), period = 12))

Coefficients:
ar1 sar1 sar2
0.9458 -0.1333 0.0821
s.e. 0.0284 0.1035 0.1078

sigma^2 estimated as 143.1: log likelihood = -516.18, aic = 1040.37

Predict

fore <- predict(fit, n.ahead=24)
fore
$pred
Jan Feb Mar Apr May Jun Jul Aug
1961 445.0772 418.6286 451.3255 485.0739 496.9859 555.4025 641.1830 627.2158
1962 463.4606 435.4701 463.6918 501.9637 511.8873 571.0617 657.1925 640.0611
Sep Oct Nov Dec
1961 528.6446 478.3612 410.0384 452.4290
1962 540.7620 491.0499 419.6633 461.3783

$se
Jan Feb Mar Apr May Jun Jul Aug
1961 11.96267 16.46600 19.63824 22.09347 24.07871 25.72521 27.11359 28.29798
1962 35.68346 38.94721 41.65083 43.92872 45.87078 47.54098 48.98693 50.24524
Sep Oct Nov Dec
1961 29.31703 30.19955 30.96776 31.63920
1962 51.34481 52.30891 53.15659 53.90364

U <- fore$pred + 2*fore$se
U
Jan Feb Mar Apr May Jun Jul Aug
1961 469.0025 451.5606 490.6020 529.2609 545.1433 606.8530 695.4102 683.8117
1962 534.8275 513.3645 546.9934 589.8211 603.6288 666.1437 755.1663 740.5516
Sep Oct Nov Dec
1961 587.2786 538.7603 471.9739 515.7074
1962 643.4516 595.6677 525.9765 569.1856

ts.plot(AirPassengers, fore$pred, U, L, col=c(1,2,4,4), lty = c(1,1,2,2))

legend(“topleft”, c(“Actual”, “Forecast”, “Error Bounds (95% Confidence)”), col=c(1,2,4), lty=c(1,1,2))

Appreciate if you can help me on the above.

Regards,
Tony