Title: | Time Series Analysis Using the Innovations Algorithm |
---|---|
Description: | Provides functions for modeling and forecasting time series data. Forecasting is based on the innovations algorithm. A description of the innovations algorithm can be found in the textbook "Introduction to Time Series and Forecasting" by Peter J. Brockwell and Richard A. Davis. <https://link.springer.com/book/10.1007/b97391>. |
Authors: | George Weigt |
Maintainer: | George Weigt <[email protected]> |
License: | FreeBSD |
Version: | 1.10 |
Built: | 2025-02-28 05:31:56 UTC |
Source: | https://github.com/cran/itsmr |
Provides functions for modeling and forecasting time series data. Forecasting is based on the innovations algorithm. A description of the innovations algorithm can be found in the textbook Introduction to Time Series and Forecasting by Peter J. Brockwell and Richard A. Davis.
Package: | itsmr |
Type: | Package |
Version: | 1.10 |
Date: | 2022-07-27 |
License: | FreeBSD |
LazyLoad: | yes |
URL: | https://georgeweigt.github.io/itsmr-refman.pdf |
George Weigt
Maintainer: George Weigt <[email protected]>
Brockwell, Peter J., and Richard A. Davis. Introduction to Time Series and Forecasting. 2nd ed. Springer, 2002.
plotc(wine) ## Define a suitable data model M = c("log","season",12,"trend",1) ## Obtain residuals and check for stationarity e = Resid(wine,M) test(e) ## Define a suitable ARMA model a = arma(e,p=1,q=1) ## Obtain residuals and check for white noise ee = Resid(wine,M,a) test(ee) ## Forecast future values forecast(wine,M,a)
plotc(wine) ## Define a suitable data model M = c("log","season",12,"trend",1) ## Obtain residuals and check for stationarity e = Resid(wine,M) test(e) ## Define a suitable ARMA model a = arma(e,p=1,q=1) ## Obtain residuals and check for white noise ee = Resid(wine,M,a) test(ee) ## Forecast future values forecast(wine,M,a)
Autocovariance of ARMA model
aacvf(a, h)
aacvf(a, h)
a |
ARMA model |
h |
Maximum lag |
The ARMA model is a list with the following components.
phi
|
Vector of AR coefficients (index number equals coefficient subscript) |
theta
|
Vector of MA coefficients (index number equals coefficient subscript) |
sigma2
|
White noise variance |
Returns a vector of length h+1
to accomodate lag 0 at index 1.
a = arma(Sunspots,2,0) aacvf(a,40)
a = arma(Sunspots,2,0) aacvf(a,40)
Autocovariance of data
acvf(x, h = 40)
acvf(x, h = 40)
x |
Time series data |
h |
Maximum lag |
Returns a vector of length h+1
to accomodate lag 0 at index 1.
acvf(Sunspots)
acvf(Sunspots)
Number of international airline passengers, 1949 to 1960
plotc(airpass)
plotc(airpass)
Compute AR infinity coefficients
ar.inf(a, n = 50)
ar.inf(a, n = 50)
a |
ARMA model |
n |
Order |
The ARMA model is a list with the following components.
phi
|
Vector of AR coefficients (index number equals coefficient subscript) |
theta
|
Vector of MA coefficients (index number equals coefficient subscript) |
sigma2
|
White noise variance |
Returns a vector of length n+1
to accomodate coefficient 0 at index 1.
a = yw(Sunspots,2) ar.inf(a)
a = yw(Sunspots,2) ar.inf(a)
Forecast using ARAR algorithm
arar(y, h = 10, opt = 2)
arar(y, h = 10, opt = 2)
y |
Time series data |
h |
Steps ahead |
opt |
Display option (0 silent, 1 tabulate, 2 plot and tabulate) |
Returns the following list invisibly.
pred |
Predicted values |
se |
Standard errors |
l |
Lower bounds (95% confidence interval) |
u |
Upper bounds |
arar(airpass)
arar(airpass)
Estimate ARMA model coefficients using maximum likelihood
arma(x, p = 0, q = 0)
arma(x, p = 0, q = 0)
x |
Time series data |
p |
AR order |
q |
MA order |
Calls the standard R function arima
to estimate AR and MA coefficients.
The innovations algorithm is used to estimate white noise variance.
Returns an ARMA model consisting of a list with the following components.
phi |
Vector of AR coefficients (index number equals coefficient subscript) |
theta |
Vector of MA coefficients (index number equals coefficient subscript) |
sigma2 |
White noise variance |
aicc |
Akaike information criterion corrected |
se.phi |
Standard errors for the AR coefficients |
se.theta |
Standard errors for the MA coefficients |
M = c("diff",1) e = Resid(dowj,M) a = arma(e,1,0) print(a)
M = c("diff",1) e = Resid(dowj,M) a = arma(e,1,0) print(a)
Find the best model from a range of possible ARMA models
autofit(x, p = 0:5, q = 0:5)
autofit(x, p = 0:5, q = 0:5)
x |
Time series data (typically residuals from |
p |
Range of AR orders |
q |
Range of MA orders |
Tries all combinations of p
and q
and returns the
model with the lowest AICC.
The arguments p
and q
should be small ranges as this function
can be slow otherwise.
The innovations algorithm is used to estimate white noise variance.
Returns an ARMA model consisting of a list with the following components.
phi |
Vector of AR coefficients (index number equals coefficient subscript) |
theta |
Vector of MA coefficients (index number equals coefficient subscript) |
sigma2 |
White noise variance |
aicc |
Akaike information criterion corrected |
se.phi |
Standard errors for the AR coefficients |
se.theta |
Standard errors for the MA coefficients |
M = c("diff",1) e = Resid(dowj,M) a = autofit(e) print(a)
M = c("diff",1) e = Resid(dowj,M) a = autofit(e) print(a)
Estimate AR coefficients using the Burg method
burg(x, p)
burg(x, p)
x |
Time series data (typically residuals from |
p |
AR order |
The innovations algorithm is used to estimate white noise variance.
Returns an ARMA model consisting of a list with the following components.
phi |
Vector of AR coefficients (index number equals coefficient subscript) |
theta |
0 |
sigma2 |
White noise variance |
aicc |
Akaike information criterion corrected |
se.phi |
Standard errors for the AR coefficients |
se.theta |
0 |
M = c("diff",1) e = Resid(dowj,M) a = burg(e,1) print(a)
M = c("diff",1) e = Resid(dowj,M) a = burg(e,1) print(a)
Check for causality and invertibility
check(a)
check(a)
a |
ARMA model |
The ARMA model is a list with the following components.
phi
|
Vector of AR coefficients (index number equals coefficient subscript) |
theta
|
Vector of MA coefficients (index number equals coefficient subscript) |
sigma2
|
White noise variance |
None
a = specify(ar=c(0,0,.99)) check(a)
a = specify(ar=c(0,0,.99)) check(a)
USA accidental deaths, 1973 to 1978
plotc(deaths)
plotc(deaths)
Dow Jones utilities index, August 28 to December 18, 1972
plotc(dowj)
plotc(dowj)
Forecast future values
forecast(x, M, a, h = 10, opt = 2, alpha = 0.05)
forecast(x, M, a, h = 10, opt = 2, alpha = 0.05)
x |
Time series data |
M |
Data model |
a |
ARMA model |
h |
Steps ahead |
opt |
Display option (0 silent, 1 tabulate, 2 plot and tabulate) |
alpha |
Level of significance |
The data model can be NULL
for none.
Otherwise M
is a vector of function names and arguments.
Example:
M = c("log","season",12,"trend",1)
The above model takes the log of the data, then subtracts a seasonal component of period 12, then subtracts a linear trend component.
These are the available functions:
diff
|
Difference the data. Has a single argument, the lag. |
hr
|
Subtract harmonic components. Has one or more arguments, each specifying the number of observations per harmonic. |
log
|
Take the log of the data, has no arguments. |
season
|
Subtract a seasonal component. Has a single argument, the number of observations per season. |
trend
|
Subtract a trend component. Has a single argument, the order of the trend (1 linear, 2 quadratic, etc.) |
At the end of the model there is an implicit subtraction of the mean operation. Hence the resulting time series always has zero mean.
All of the functions are inverted before the forecast results are displayed.
Returns the following list invisibly.
pred |
Predicted values |
se |
Standard errors (not returned for data models with log) |
l |
Lower bounds (95% confidence interval) |
u |
Upper bounds |
M = c("log","season",12,"trend",1) e = Resid(wine,M) a = arma(e,1,1) forecast(wine,M,a)
M = c("log","season",12,"trend",1) e = Resid(wine,M) a = arma(e,1,1) forecast(wine,M,a)
Estimate ARMA coefficients using the Hannan-Rissanen algorithm
hannan(x, p, q)
hannan(x, p, q)
x |
Time series data (typically residuals from |
p |
AR order |
q |
MA order ( |
The innovations algorithm is used to estimate white noise variance.
Returns an ARMA model consisting of a list with the following components.
phi |
Vector of AR coefficients (index number equals coefficient subscript) |
theta |
Vector of MA coefficients (index number equals coefficient subscript) |
sigma2 |
White noise variance |
aicc |
Akaike information criterion corrected |
se.phi |
Standard errors for the AR coefficients |
se.theta |
Standard errors for the MA coefficients |
M = c("diff",12) e = Resid(deaths,M) a = hannan(e,1,1) print(a)
M = c("diff",12) e = Resid(deaths,M) a = hannan(e,1,1) print(a)
Estimate harmonic components
hr(x, d)
hr(x, d)
x |
Time series data |
d |
Vector of harmonic periods |
Returns a vector the same length as x
.
Subtract from x
to obtain residuals.
y = hr(deaths,c(12,6)) plotc(deaths,y)
y = hr(deaths,c(12,6)) plotc(deaths,y)
Estimate MA coefficients using the innovations algorithm
ia(x, q, m = 17)
ia(x, q, m = 17)
x |
Time series data (typically residuals from |
q |
MA order |
m |
Recursion level |
Normally m
should be set to the default value.
The innovations algorithm is used to estimate white noise variance.
Returns an ARMA model consisting of a list with the following components.
phi |
0 |
theta |
Vector of MA coefficients (index number equals coefficient subscript) |
sigma2 |
White noise variance |
aicc |
Akaike information criterion corrected |
se.phi |
0 |
se.theta |
Standard errors for the MA coefficients |
M = c("diff",1) e = Resid(dowj,M) a = ia(e,1) print(a)
M = c("diff",1) e = Resid(dowj,M) a = ia(e,1) print(a)
Level of Lake Huron, 1875 to 1972
plotc(lake)
plotc(lake)
Compute MA infinity coefficients
ma.inf(a, n = 50)
ma.inf(a, n = 50)
a |
ARMA model |
n |
Order |
The ARMA model is a list with the following components.
phi
|
Vector of AR coefficients (index number equals coefficient subscript) |
theta
|
Vector of MA coefficients (index number equals coefficient subscript) |
sigma2
|
White noise variance |
Returns a vector of length n+1
to accomodate coefficient 0 at index 1.
M = c("diff",12) e = Resid(deaths,M) a = arma(e,1,1) ma.inf(a,10)
M = c("diff",12) e = Resid(deaths,M) a = arma(e,1,1) ma.inf(a,10)
Plot a periodogram
periodogram(x, q = 0, opt = 2)
periodogram(x, q = 0, opt = 2)
x |
Time series data |
q |
MA filter order |
opt |
Plot option (0 silent, 1 periodogram only, 2 periodogram and filter) |
The filter q
can be a vector in which case the overall filter is the
composition of MA filters of the designated orders.
The periodogram vector divided by 2pi is returned invisibly.
periodogram(Sunspots,c(1,1,1,1))
periodogram(Sunspots,c(1,1,1,1))
Plot data and/or model ACF and PACF
plota(u, v = NULL, h = 40)
plota(u, v = NULL, h = 40)
u , v
|
Data and/or ARMA model in either order |
h |
Maximum lag |
None
plota(Sunspots) a = yw(Sunspots,2) plota(Sunspots,a)
plota(Sunspots) a = yw(Sunspots,2) plota(Sunspots,a)
Plot one or two time series
plotc(y1, y2 = NULL)
plotc(y1, y2 = NULL)
y1 |
Data vector (plotted in blue with knots) |
y2 |
Data vector (plotted in red, no knots) |
None
plotc(uspop) y = trend(uspop,2) plotc(uspop,y)
plotc(uspop) y = trend(uspop,2) plotc(uspop,y)
Plot spectrum of data or ARMA model
plots(u)
plots(u)
u |
Data vector or an ARMA model |
None
a = specify(ar=c(0,0,.99)) plots(a)
a = specify(ar=c(0,0,.99)) plots(a)
Compute residuals
Resid(x, M = NULL, a = NULL)
Resid(x, M = NULL, a = NULL)
x |
Time series data |
M |
Data model |
a |
ARMA model |
The data model can be NULL
for none.
Otherwise M
is a vector of function names and arguments.
Example:
M = c("log","season",12,"trend",1)
The above model takes the log of the data, then subtracts a seasonal component of period 12, then subtracts a linear trend component.
These are the available functions:
diff
|
Difference the data. Has a single argument, the lag. |
hr
|
Subtract harmonic components. Has one or more arguments, each specifying the number of observations per harmonic. |
log
|
Take the log of the data, has no arguments. |
season
|
Subtract a seasonal component. Has a single argument, the number of observations per season. |
trend
|
Subtract a trend component. Has a single argument, the order of the trend (1 linear, 2 quadratic, etc.) |
At the end of the model there is an implicit subtraction of the mean operation. Hence the resulting time series always has zero mean.
Returns a vector of residuals the same length as x
.
M = c("log","season",12,"trend",1) e = Resid(wine,M) a = arma(e,1,1) ee = Resid(wine,M,a)
M = c("log","season",12,"trend",1) e = Resid(wine,M) a = arma(e,1,1) ee = Resid(wine,M,a)
Estimate seasonal component
season(x, d)
season(x, d)
x |
Time series data |
d |
Number of observations per season |
Returns a vector the same length as x
.
Subtract from x
to obtain residuals.
y = season(deaths,12) plotc(deaths,y)
y = season(deaths,12) plotc(deaths,y)
Run a self test
selftest()
selftest()
This function is a useful check if the code is modified.
None
selftest()
selftest()
Generate synthetic observations
sim(a, n = 100)
sim(a, n = 100)
a |
ARMA model |
n |
Number of synthetic observations required |
The ARMA model is a list with the following components.
phi
|
Vector of AR coefficients (index number equals coefficient subscript) |
theta
|
Vector of MA coefficients (index number equals coefficient subscript) |
sigma2
|
White noise variance |
Returns a vector of n
synthetic observations.
a = specify(ar=c(0,0,.99)) x = sim(a,60) plotc(x)
a = specify(ar=c(0,0,.99)) x = sim(a,60) plotc(x)
Apply an exponential filter
smooth.exp(x, alpha)
smooth.exp(x, alpha)
x |
Time series data |
alpha |
Smoothness setting, 0-1 |
Zero is maximum smoothness.
Returns a vector of smoothed data the same length as x
.
y = smooth.exp(strikes,.4) plotc(strikes,y)
y = smooth.exp(strikes,.4) plotc(strikes,y)
Apply a low pass filter
smooth.fft(x, f)
smooth.fft(x, f)
x |
Time series data |
f |
Cut-off frequency, 0-1 |
The cut-off frequency is specified as a fraction.
For example, c=.25
passes the lowest 25% of the spectrum.
Returns a vector the same length as x
.
y = smooth.fft(deaths,.1) plotc(deaths,y)
y = smooth.fft(deaths,.1) plotc(deaths,y)
Apply a moving average filter
smooth.ma(x, q)
smooth.ma(x, q)
x |
Time series data |
q |
Filter order |
The averaging function uses 2q+1
values.
Returns a vector the same length as x
.
y = smooth.ma(strikes,2) plotc(strikes,y)
y = smooth.ma(strikes,2) plotc(strikes,y)
Apply a spectral filter
smooth.rank(x, k)
smooth.rank(x, k)
x |
Time series data |
k |
Number of frequencies |
Passes the mean and the k
frequencies with the highest amplitude.
The remainder of the spectrum is filtered out.
Returns a vector the same length as x
.
y = smooth.rank(deaths,2) plotc(deaths,y)
y = smooth.rank(deaths,2) plotc(deaths,y)
Specify an ARMA model
specify(ar = 0, ma = 0, sigma2 = 1)
specify(ar = 0, ma = 0, sigma2 = 1)
ar |
Vector of AR coefficients (index number equals coefficient subscript) |
ma |
Vector of MA coefficients (index number equals coefficient subscript) |
sigma2 |
White noise variance |
Returns an ARMA model consisting of a list with the following components.
phi |
Vector of AR coefficients (index number equals coefficient subscript) |
theta |
Vector of MA coefficients (index number equals coefficient subscript) |
sigma2 |
White noise variance |
specify(ar=c(0,0,.99))
specify(ar=c(0,0,.99))
USA union strikes, 1951-1980
plotc(strikes)
plotc(strikes)
Number of sunspots, 1770 to 1869
plotc(Sunspots)
plotc(Sunspots)
Test residuals for stationarity and randomness
test(e)
test(e)
e |
Time series data (typically residuals from |
Plots ACF, PACF, residuals, and QQ. Displays results for Ljung-Box, McLeod-Li, turning point, difference-sign, and rank tests. The plots can be used to check for stationarity and the other tests check for white noise.
None
M = c("log","season",12,"trend",1) e = Resid(wine,M) test(e) ## Is e stationary? a = arma(e,1,1) ee = Resid(wine,M,a) test(ee) ## Is ee white noise?
M = c("log","season",12,"trend",1) e = Resid(wine,M) test(e) ## Is e stationary? a = arma(e,1,1) ee = Resid(wine,M,a) test(ee) ## Is ee white noise?
Estimate trend component
trend(x, p)
trend(x, p)
x |
Time series data |
p |
Polynomial order (1 linear, 2 quadratic, etc.) |
Returns a vector the same length as x
.
Subtract from x
to obtain residuals.
The returned vector is the least squares fit of a polynomial to the data.
y = trend(uspop,2) plotc(uspop,y)
y = trend(uspop,2) plotc(uspop,y)
Australian red wine sales, January 1980 to October 1991
plotc(wine)
plotc(wine)
Estimate AR coefficients using the Yule-Walker method
yw(x, p)
yw(x, p)
x |
Time series data (typically residuals from |
p |
AR order |
The innovations algorithm is used to estimate white noise variance.
Returns an ARMA model consisting of a list with the following components.
phi |
Vector of AR coefficients (index number equals coefficient subscript) |
theta |
0 |
sigma2 |
White noise variance |
aicc |
Akaike information criterion corrected |
se.phi |
Standard errors for the AR coefficients |
se.theta |
0 |
M = c("diff",1) e = Resid(dowj,M) a = yw(e,1)
M = c("diff",1) e = Resid(dowj,M) a = yw(e,1)