The data is about “Age of Death” of 42 Successive Kings of England. In this case the age of death of 42 successive kings of England has been read into the variable ‘kings’.
Once you have read the time series data into R, the next step is to store the data in a time series object in R, so that you can use R’s many functions for analysing time series data. To store the data in a time series object, we use the ts() function in R. For example, to store the data in the variable ‘kings’ as a time series object in R, we type:
kings <-read.table("http://robjhyndman.com/tsdldata/misc/kings.dat",
header=TRUE, skip=3)
kingstimeseries <- ts(kings)
kingstimeseries
## Time Series:
## Start = 1
## End = 41
## Frequency = 1
## X60
## [1,] 43
## [2,] 67
## [3,] 50
## [4,] 56
## [5,] 42
## [6,] 50
## [7,] 65
## [8,] 68
## [9,] 43
## [10,] 65
## [11,] 34
## [12,] 47
## [13,] 34
## [14,] 49
## [15,] 41
## [16,] 13
## [17,] 35
## [18,] 53
## [19,] 56
## [20,] 16
## [21,] 43
## [22,] 69
## [23,] 59
## [24,] 48
## [25,] 59
## [26,] 86
## [27,] 55
## [28,] 68
## [29,] 51
## [30,] 33
## [31,] 49
## [32,] 67
## [33,] 77
## [34,] 81
## [35,] 67
## [36,] 71
## [37,] 81
## [38,] 68
## [39,] 70
## [40,] 77
## [41,] 56
Once you have read a time series into R, the next step is usually to make a plot of the time series data, which you can do with the plot.ts() function in R.
For example, to plot the time series of the age of death of 42 successive kings of England, we type:
We can see from the time plot that this time series could probably be
described using an additive model, since the random fluctuations in the
data are roughly constant in size over time.
Decomposing a time series means separating it into its constituent components, which are usually a trend component and an irregular component, and if it is a seasonal time series, a seasonal component. ## Decomposing Non-Seasonal Data A non-seasonal time series consists of a trend component and an irregular component. Decomposing the time series involves trying to separate the time series into these components, that is, estimating the the trend component and the irregular component.
To estimate the trend component of a non-seasonal time series that can be described using an additive model, it is common to use a smoothing method, such as calculating the simple moving average of the time series. The SMA() function in the “TTR” R package can be used to smooth time series data using a simple moving average. As discussed above, the time series of the age of death of 42 successive kings of England appears is non-seasonal, and can probably be described using an additive model, since the random fluctuations in the data are roughly constant in size over time. Thus, we can try to estimate the trend component of this time series by smoothing using a simple moving average. To smooth the time series using a simple moving average of order 3, and plot the smoothed time series data, we type:
kingstimeseriesSMA3 <- SMA(kingstimeseries,n=3)
plot.ts(kingstimeseriesSMA3)
There still appears to be quite a lot of random fluctuations in the time series smoothed using a simple moving average of order 3. Thus, to estimate the trend component more accurately, we might want to try smoothing the data with a simple moving average of a higher order. This takes a little bit of trial-and-error, to find the right amount of smoothing. For example, we can try using a simple moving average of order 8:
kingstimeseriesSMA8 <- SMA(kingstimeseries,n=8)
plot.ts(kingstimeseriesSMA8)
The data smoothed with a simple moving average of order 8 gives a
clearer picture of the trend component, and we can see that the age of
death of the English kings seems to have decreased from about 55 years
old to about 38 years old during the reign of the first 20 kings, and
then increased after that to about 73 years old by the end of the reign
of the 40th king in the time series.
ARIMA models are defined for stationary time series. Therefore, if you start off with a non-stationary time series, you will first need to ‘difference’ the time series until you obtain a stationary time series. If you have to difference the time series d times to obtain a stationary series, then you have an ARIMA(p,d,q) model, where d is the order of differencing used. The resulting time series of first differences (above) does not appear to be stationary in mean. Therefore, we can difference the time series twice, to see if that gives us a stationary time series.
kingtimeseriesdiff1 <- diff(kingstimeseries, differences=1)
plot.ts(kingtimeseriesdiff1)
## Selecting a Candidate ARIMA Model If your time series is stationary,
or if you have transformed it to a stationary time series by
differencing d times, the next step is to select the appropriate ARIMA
model, which means finding the values of most appropriate values of p
and q for an ARIMA(p,d,q) model. To do this, you usually need to examine
the correlogram and partial correlogram of the stationary time series.
To plot a correlogram and partial correlogram, we can use the “acf()”
and “pacf()” functions in R, respectively. To get the actual values of
the autocorrelations and partial autocorrelations, we set “plot=FALSE”
in the “acf()” and “pacf()” functions. ### Example of the Ages at Death
of the Kings of England For example, to plot the correlogram for lags
1-20 of the once differenced time series of the ages at death of the
kings of England, and to get the values of the autocorrelations, we
type:
acf(kingtimeseriesdiff1, lag.max=20)
acf(kingtimeseriesdiff1, lag.max=20, plot=FALSE)
##
## Autocorrelations of series 'kingtimeseriesdiff1', by lag
##
## 0 1 2 3 4 5 6 7 8 9 10
## 1.000 -0.338 -0.188 -0.043 0.213 -0.033 -0.166 0.101 0.032 -0.090 -0.113
## 11 12 13 14 15 16 17 18 19 20
## 0.228 -0.035 -0.198 0.122 0.078 0.018 -0.173 0.077 0.062 -0.060
We see from the correlogram that the autocorrelation at lag 1 (-0.360) exceeds the significance bounds, but all other autocorrelations between lags 1-20 do not exceed the significance bounds.
To plot the partial correlogram for lags 1-20 for the once differenced time series of the ages at death of the English kings, and get the values of the partial autocorrelations, we use the “pacf()” function, by typing:
pacf(kingtimeseriesdiff1, lag.max=20)
pacf(kingtimeseriesdiff1, lag.max=20, plot=FALSE)
##
## Partial autocorrelations of series 'kingtimeseriesdiff1', by lag
##
## 1 2 3 4 5 6 7 8 9 10 11
## -0.338 -0.341 -0.311 -0.009 0.004 -0.135 0.008 -0.020 -0.118 -0.213 0.034
## 12 13 14 15 16 17 18 19 20
## -0.022 -0.195 0.008 -0.018 0.034 -0.037 -0.038 -0.053 -0.073
The partial correlogram shows that the partial autocorrelations at lags 1, 2 and 3 exceed the significance bounds, are negative, and are slowly decreasing in magnitude with increasing lag (lag 1: -0.360, lag 2: -0.335, lag 3:-0.321). The partial autocorrelations tail off to zero after lag 3.
Since the correlogram is zero after lag 1, and the partial correlogram tails off to zero after lag 3, this means that the following ARMA (autoregressive moving average) models are possible for the time series of first differences: - an ARMA(3,0) model, that is, an autoregressive model of order p=3, since the partial autocorrelogram is zero after lag 3, and the autocorrelogram tails off to zero (although perhaps too abruptly for this model to be appropriate) - an ARMA(0,1) model, that is, a moving average model of order q=1, since the autocorrelogram is zero after lag 1 and the partial autocorrelogram tails off to zero - an ARMA(p,q) model, that is, a mixed model with p and q greater than 0, since the autocorrelogram and partial correlogram tail off to zero (although the correlogram probably tails off to zero too abruptly for this model to be appropriate) We use the principle of parsimony to decide which model is best: that is, we assume that the model with the fewest parameters is best. The ARMA(3,0) model has 3 parameters, the ARMA(0,1) model has 1 parameter, and the ARMA(p,q) model has at least 2 parameters. Therefore, the ARMA(0,1) model is taken as the best model. A MA (moving average) model is usually used to model a time series that shows short-term dependencies between successive observations. Intuitively, it makes good sense that a MA model can be used to describe the irregular component in the time series of ages at death of English kings, as we might expect the age at death of a particular English king to have some effect on the ages at death of the next king or two, but not much effect on the ages at death of kings that reign much longer after that. ### Example of the Ages at Death of the Kings of England For example, we discussed above that an ARIMA(0,1,1) model seems a plausible model for the ages at deaths of the kings of England. You can specify the values of p, d and q in the ARIMA model by using the “order” argument of the “arima()” function in R.
kingstimeseriesarima <- arima(kingstimeseries, order=c(0,1,1))
kingstimeseriesarima
##
## Call:
## arima(x = kingstimeseries, order = c(0, 1, 1))
##
## Coefficients:
## ma1
## -0.7213
## s.e. 0.1245
##
## sigma^2 estimated as 234.7: log likelihood = -166.29, aic = 336.58
Since an ARMA(0,1) model (with p=0, q=1) is taken to be the best candidate model for the time series of first differences of the ages at death of English kings, then the original time series of the ages of death can be modelled using an ARIMA(0,1,1) model (with p=0, d=1, q=1, where d is the order of differencing required). # Forecasting Using an ARIMA Model Once you have selected the best candidate ARIMA(p,d,q) model for your time series data, you can estimate the parameters of that ARIMA model, and use that as a predictive model for making forecasts for future values of your time series.
You can estimate the parameters of an ARIMA(p,d,q) model using the “arima()” function in R. We can then use the ARIMA model to make forecasts for future values of the time series, using the “forecast.Arima()” function in the “forecast” R package. For example, to forecast the ages at death of the next five English kings, we type:
kingstimeseriesforecasts <- forecast(kingstimeseriesarima, h=5)
kingstimeseriesforecasts
## Point Forecast Lo 80 Hi 80 Lo 95 Hi 95
## 42 67.74827 48.11473 87.38181 37.72136 97.77518
## 43 67.74827 47.36652 88.13002 36.57708 98.91946
## 44 67.74827 46.64483 88.85171 35.47334 100.02320
## 45 67.74827 45.94701 89.54953 34.40612 101.09041
## 46 67.74827 45.27085 90.22569 33.37202 102.12452
plot(kingstimeseriesforecasts)
The original time series for the English kings includes the ages at
death of 42 English kings. The forecast.Arima() function gives us a
forecast of the age of death of the next five English kings (kings
43-47), as well as 80% and 95% prediction intervals for those
predictions. The age of death of the 42nd English king was 56 years (the
last observed value in our time series), and the ARIMA model gives the
forecasted age at death of the next five kings as 67.8 years.
We can plot the observed ages of death for the first 42 kings, as well as the ages that would be predicted for these 42 kings and for the next 5 kings using our ARIMA(0,1,1) model, by typing.
As in the case of exponential smoothing models, it is a good idea to investigate whether the forecast errors of an ARIMA model are normally distributed with mean zero and constant variance, and whether the are correlations between successive forecast errors.
For example, we can make a correlogram of the forecast errors for our ARIMA(0,1,1) model for the ages at death of kings, and perform the Ljung-Box test for lags 1-20, by typing:
acf(kingstimeseriesforecasts$residuals, lag.max=20)
Box.test(kingstimeseriesforecasts$residuals, lag=20, type="Ljung-Box")
##
## Box-Ljung test
##
## data: kingstimeseriesforecasts$residuals
## X-squared = 11.387, df = 20, p-value = 0.9356
plot.ts(kingstimeseriesforecasts$residuals)
Since the correlogram shows that none of the sample autocorrelations for lags 1-20 exceed the significance bounds, and the p-value for the Ljung-Box test is 0.9, we can conclude that there is very little evidence for non-zero autocorrelations in the forecast errors at lags 1-20.
To investigate whether the forecast errors are normally distributed with mean zero and constant variance, we can make a time plot and histogram (with overlaid normal curve) of the forecast errors:
plotForecastErrors <- function(forecasterrors)
{
# make a histogram of the forecast errors:
mybinsize <- IQR(forecasterrors,na.rm=TRUE)/4
mysd <- sd(forecasterrors,na.rm=TRUE)
mymin <- min(forecasterrors,na.rm=TRUE) - mysd*5
mymax <- max(forecasterrors,na.rm=TRUE) + mysd*3
# generate normally distributed data with mean 0 and standard deviation mysd
mynorm <- rnorm(10000, mean=0, sd=mysd)
mymin2 <- min(mynorm)
mymax2 <- max(mynorm)
if (mymin2 < mymin) { mymin <- mymin2 }
if (mymax2 > mymax) { mymax <- mymax2 }
# make a red histogram of the forecast errors, with the normally distributed data overlaid:
mybins <- seq(mymin, mymax, mybinsize)
hist(forecasterrors, col="red", freq=FALSE, breaks=mybins)
# freq=FALSE ensures the area under the histogram = 1
# generate normally distributed data with mean 0 and standard deviation mysd
myhist <- hist(mynorm, plot=FALSE, breaks=mybins)
# plot the normal curve as a blue line on top of the histogram of forecast errors:
points(myhist$mids, myhist$density, type="l", col="blue", lwd=2)
}
plotForecastErrors(kingstimeseriesforecasts$residuals)
The time plot of the in-sample forecast errors shows that the variance
of the forecast errors seems to be roughly constant over time (though
perhaps there is slightly higher variance for the second half of the
time series). The histogram of the time series shows that the forecast
errors are roughly normally distributed and the mean seems to be close
to zero. Therefore, it is plausible that the forecast errors are
normally distributed with mean zero and constant variance.
Since successive forecast errors do not seem to be correlated, and the forecast errors seem to be normally distributed with mean zero and constant variance, the ARIMA(0,1,1) does seem to provide an adequate predictive model for the ages at death of English kings.