Created
November 19, 2018 20:50
-
-
Save kawasaki2013/42c2d2c7aa1b76c9e8f17c92f63f8e18 to your computer and use it in GitHub Desktop.
Flow of the River Nile
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ``` | |
| require(stats); require(graphics) | |
| 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) # 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 | |
| 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) | |
| ``` |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment