A pre-loaded example dataset in R

Main page: https://www.rdocumentation.org/packages/datasets/versions/3.6.2/topics/Nile

Nile
## Time Series:
## Start = 1871 
## End = 1970 
## Frequency = 1 
##   [1] 1120 1160  963 1210 1160 1160  813 1230 1370 1140  995  935 1110  994 1020
##  [16]  960 1180  799  958 1140 1100 1210 1150 1250 1260 1220 1030 1100  774  840
##  [31]  874  694  940  833  701  916  692 1020 1050  969  831  726  456  824  702
##  [46] 1120 1100  832  764  821  768  845  864  862  698  845  744  796 1040  759
##  [61]  781  865  845  944  984  897  822 1010  771  676  649  846  812  742  801
##  [76] 1040  860  874  848  890  744  749  838 1050  918  986  797  923  975  815
##  [91] 1020  906  901 1170  912  746  919  718  714  740
require(stats)
require(graphics)
par(mfrow = c(2, 2))
plot(Nile)
acf(Nile)
pacf(Nile)
ar(Nile) # selects order 2
## 
## Call:
## ar(x = Nile)
## 
## Coefficients:
##      1       2  
## 0.4081  0.1812  
## 
## Order selected 2  sigma^2 estimated as  21247
cpgram(ar(Nile)$resid)

par(mfrow = c(1, 1))
arima(Nile, c(2, 0, 0))
## 
## Call:
## arima(x = Nile, order = c(2, 0, 0))
## 
## Coefficients:
##          ar1     ar2  intercept
##       0.4096  0.1987   919.8397
## s.e.  0.0974  0.0990    35.6410
## 
## sigma^2 estimated as 20291:  log likelihood = -637.98,  aic = 1283.96
## Now consider missing values, following Durbin & Koopman
NileNA <- Nile
NileNA[c(21:40, 61:80)] <- NA
arima(NileNA, c(2, 0, 0))
## 
## Call:
## arima(x = NileNA, order = c(2, 0, 0))
## 
## Coefficients:
##          ar1     ar2  intercept
##       0.3622  0.1678   918.3103
## s.e.  0.1273  0.1323    39.5037
## 
## sigma^2 estimated as 23676:  log likelihood = -387.7,  aic = 783.41
plot(NileNA)
pred <-
   predict(arima(window(NileNA, 1871, 1890), c(2, 0, 0)), n.ahead = 20)
lines(pred$pred, lty = 3, col = "red")
lines(pred$pred + 2*pred$se, lty = 2, col = "blue")
lines(pred$pred - 2*pred$se, lty = 2, col = "blue")
pred <-
   predict(arima(window(NileNA, 1871, 1930), c(2, 0, 0)), n.ahead = 20)
lines(pred$pred, lty = 3, col = "red")
lines(pred$pred + 2*pred$se, lty = 2, col = "blue")
lines(pred$pred - 2*pred$se, lty = 2, col = "blue")

## Structural time series models
par(mfrow = c(3, 1))
plot(Nile)
## local level model
(fit <- StructTS(Nile, type = "level"))
## 
## Call:
## StructTS(x = Nile, type = "level")
## 
## Variances:
##   level  epsilon  
##    1469    15099
lines(fitted(fit), lty = 2)              # contemporaneous smoothing
lines(tsSmooth(fit), lty = 2, col = 4)   # fixed-interval smoothing
plot(residuals(fit)); abline(h = 0, lty = 3)
## local trend model
(fit2 <- StructTS(Nile, type = "trend")) ## constant trend fitted
## 
## Call:
## StructTS(x = Nile, type = "trend")
## 
## Variances:
##   level    slope  epsilon  
##    1427        0    15047
pred <- predict(fit, n.ahead = 30)
## with 50% confidence interval
ts.plot(Nile, pred$pred,
        pred$pred + 0.67*pred$se, pred$pred -0.67*pred$se)

## Now consider missing values
plot(NileNA)
(fit3 <- StructTS(NileNA, type = "level"))
## 
## Call:
## StructTS(x = NileNA, type = "level")
## 
## Variances:
##   level  epsilon  
##   685.8  17899.8
lines(fitted(fit3), lty = 2)
lines(tsSmooth(fit3), lty = 3)
plot(residuals(fit3)); abline(h = 0, lty = 3)