kalman.ar: Time-Varying Autoregressive Model by the Kalman Filter

View source: R/kalman.ar.R

kalman.arR Documentation

Time-Varying Autoregressive Model by the Kalman Filter

Description

Fits an autoregressive model whose coefficients are allowed to change through time, using the Kalman filter and a fixed-interval smoother. The result goes to kalman.spec for an evolutive spectrum: a spectrum for every year.

Usage

kalman.ar(y, yrs = NULL, order, prior = c("rw1", "rw2"), lambda = NULL,
          variance = c("constant", "varying"), nu = NULL)

## S3 method for class 'kalman.ar'
print(x, digits = 4, ...)

Arguments

y

a numeric vector, one value per year, or a chronology (a data.frame such as that from chron), in which case the column std is used. NA values before and after the series are dropped. See ‘Details’ for NA values inside it.

yrs

numeric. The years of y, in steps of one. Taken from the row names if y is a chronology, otherwise seq_along(y).

order

the order of the autoregression, a whole number. There is no default. See ‘Details’.

prior

how the coefficients move from year to year. "rw1": each coefficient is a random walk. "rw2": the slope of each coefficient is a random walk, which gives smoother paths.

lambda

the variance of the yearly change in the coefficients ("rw1") or in their slopes ("rw2"), as a ratio to the innovation variance of the series scaled to unit variance. If NULL it is estimated by maximum likelihood.

variance

"constant": one innovation variance for the whole series. "varying": the innovation variance changes smoothly through time. See ‘Details’.

nu

with variance = "varying", the variance of the yearly change in the logarithm of the innovation variance. If NULL it is estimated.

x

an object of class "kalman.ar".

digits

number of significant digits to print.

...

not used.

Details

The model for a series with its mean removed is

y_t = a_{1,t} y_{t-1} + \ldots + a_{p,t} y_{t-p} + e_t

where e_t is white noise with variance \sigma^2. This is the ordinary AR(p) model except that the coefficients carry a time index. They are treated as the unobserved state of a state-space model and estimated for each year from the whole series (Kitagawa and Gersch 1985).

One number, lambda, sets how freely the coefficients move. At zero under "rw1" they do not move and the model is the ordinary one. At zero under "rw2" they change along straight lines. As lambda grows the coefficients follow the series more closely, and eventually follow its noise.

Choosing order and lambda is the user’s job. Maximum likelihood for lambda is a sound starting point, but it answers a narrow question: whether one-step predictions improve when the coefficients move. It can return zero, as it does for the sunspot numbers in the examples. A larger value set by hand will always show a spectrum that changes; a series with fixed coefficients shows the same when fitted that way, so such a picture is not by itself evidence of change. Ed Cook, who suggested this method for dplR, reports that trying several orders served him better than a criterion such as AIC, and that orders well above what AIC would pick can be needed to resolve a cycle (30 for the roughly 11-year sunspot cycle). If a cycle of known period is the target, the lags should span a few cycles of it, and order should stay well below the number of years: for the 289 years of sunspot numbers, order 30 spans nearly three cycles, while order 60 leaves fewer than five years for each coefficient and fills the spectrum with peaks that the series cannot support. A picture that depends on the setting should be reported with the setting. profile on a fit shows the likelihood over a range of lambda.

The log-likelihood is that of the one-step prediction errors, with the coefficients given a vague starting distribution and the first prediction errors, as many as there are elements in the state (order for "rw1", twice that for "rw2"), left out. It is given for the fitted model and for the ordinary AR model with fixed coefficients over the same years, so the two can be compared. AIC counts \sigma^2 and, if they were estimated, lambda and nu. Log-likelihoods from different orders or different priors are over different years and cannot be compared.

By default the innovation variance is constant. A series whose year-to-year scatter changes, as a chronology’s can where sample depth is low, is then at risk of having that change read as a change in the coefficients. With variance = "varying" the innovation variance of year t is \sigma^2 r_t, where the logarithm of r_t is a random walk with step variance nu and averages zero. It is estimated from the squared one-step prediction errors, whose logarithms are the log variance plus noise of known variance (Harvey et al. 1994); the filter is then run again with the new variances, and the two steps repeat until the variances settle. nu is chosen by a quasi-likelihood, and when that gives zero the variance is constant and a message says so. The path is smooth: an abrupt or short-lived change in variance is spread out and understated.

The innovation variance is the size of what cannot be predicted one year ahead. It is not the variance of the series, and it does not respond to a slow, large swing in a strongly autocorrelated series, which needs only a run of ordinary innovations of the same sign. Such events show in a wavelet analysis (morlet) and not here.

The variance path is estimated from the prediction errors and then treated as known. The log-likelihood of a fit with variance = "varying" is therefore somewhat too high when set against a fit with constant variance. The comparison with fixed coefficients inside one fit is not affected: both use the same path.

A residual chronology has had its autocorrelation removed and is seldom what is wanted here.

A year enters the fit only if it and the order years before it all have values. One NA inside the series therefore takes order + 1 years out of the fit; a message gives the count. Coefficients are still returned for those years, carried across from the years on either side.

Nothing forces the coefficients of a year to be those of a stationary process. The number of years in which they are not is printed, and stationary in the result identifies them. A spectrum is still computed for such years but its peaks should not be trusted. Many such years suggest that lambda or order is too high.

Memory use grows with the number of years times the square of the length of the state.

Value

An object of class "kalman.ar", a list with elements:

coef

matrix of smoothed coefficients, one row per year and one column per lag. The first order years of the series have no row.

se

matrix of their standard errors.

yrs

the years of the rows of coef.

order, prior

as given.

lambda

the value used.

lambda.estimated

TRUE if it was estimated.

sigma2

innovation variance, in the units of y squared. With variance = "varying", its geometric mean over the years.

sigma2.t

innovation variance of each year, one per row of coef.

variance

as given.

nu, nu.estimated

the value used and whether it was estimated. nu is NULL with constant variance.

loglik, aic

log-likelihood and AIC.

fixed

a list with coef, sigma2, loglik and aic for the AR model with fixed coefficients, under the same variance path.

stationary

logical, one per row of coef.

n, n.used, n.lik, n.skipped

years in the series, years that entered the fit, years in the likelihood, and years left out because of missing values.

y, y.yrs, mean

the series, its years and its mean.

call

the call.

Note

This function is experimental and may change in future releases: arguments, defaults and the object returned. How to choose order and lambda is not settled.

Written from the published method at the suggestion of Ed Cook. It is not a port of his programs and will not reproduce their numbers.

Author(s)

Andy Bunn

References

Harvey, A., Ruiz, E. and Shephard, N. (1994) Multivariate stochastic variance models. Review of Economic Studies, 61(2), 247–264.

Kitagawa, G. and Gersch, W. (1985) A smoothness priors time-varying AR coefficient modeling of nonstationary covariance time series. IEEE Transactions on Automatic Control, 30(1), 48–56.

Kitagawa, G. and Gersch, W. (1996) Smoothness Priors Analysis of Time Series. Lecture Notes in Statistics 116. Springer.

See Also

kalman.spec, profile.kalman.ar, morlet, redfit, ar

Examples

## Annual sunspot numbers. Maximum likelihood finds no gain from
## letting the coefficients move:
ss <- datasets::sunspot.year
fit <- kalman.ar(as.numeric(ss), yrs = as.numeric(time(ss)), order = 30)
fit

## Setting lambda by hand lets them move
fit2 <- kalman.ar(as.numeric(ss), yrs = as.numeric(time(ss)), order = 30,
             lambda = 1e-3)
fit2
matplot(fit2$yrs, fit2$coef[, 1:3], type = "l", lty = 1,
        xlab = "Year", ylab = "Coefficient")

## A chronology
data(ca533)
ca533.rwi <- detrend(rwl = ca533, method = "Spline")
ca533.crn <- chron(ca533.rwi)
kalman.ar(ca533.crn, order = 10)

## The same, letting the innovation variance change
fit3 <- kalman.ar(ca533.crn, order = 10, variance = "varying")
fit3
plot(fit3$yrs, sqrt(fit3$sigma2.t), type = "l", xlab = "Year",
     ylab = "Innovation sd")

dplR documentation built on Dec. 6, 2026, 1:07 a.m.