Nile                package:datasets                R Documentation

_F_l_o_w _o_f _t_h_e _R_i_v_e_r _N_i_l_e

_D_e_s_c_r_i_p_t_i_o_n:

     Measurements of the annual flow of the river Nile at Ashwan
     1871-1970.

_U_s_a_g_e:

     Nile

_F_o_r_m_a_t:

     A time series of length 100.

_S_o_u_r_c_e:

     Durbin, J. and Koopman, S. J. (2001) _Time Series Analysis by
     State Space Methods._  Oxford University Press. <URL:
     http://www.ssfpack.com/dkbook/>

_R_e_f_e_r_e_n_c_e_s:

     Balke, N. S. (1993) Detecting level shifts in time series.
     _Journal of Business and Economic Statistics_ *11*, 81-92.

     Cobb, G. W. (1978) The problem of the Nile: conditional solution
     to a change-point problem.  _Biometrika_ *65*, 243-51.

_E_x_a_m_p_l_e_s:

     require(stats)
     par(mfrow = c(2,2))
     plot(Nile)
     acf(Nile)
     pacf(Nile)
     ar(Nile) # selects order 2
     cpgram(ar(Nile)$resid)
     par(mfrow = c(1,1))
     arima(Nile, c(2, 0, 0))

     ## Now consider missing values, following Durbin & Koopman
     NileNA <- Nile
     NileNA[c(21:40, 61:80)] <- NA
     arima(NileNA, c(2, 0, 0))
     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"))
     lines(fitted(fit), lty = 2)              # contempareneous 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
     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"))
     lines(fitted(fit3), lty = 2)
     lines(tsSmooth(fit3), lty = 3)
     plot(residuals(fit3)); abline(h = 0, lty = 3)

