Title: | Locally Stationary Time Series |
---|---|
Description: | A set of functions that allow stationary analysis and locally stationary time series analysis. |
Authors: | Ricardo Olea [aut, cph], Wilfredo Palma [aut, cph], Pilar Rubio [aut], Mauricio Vargas [aut, cre] |
Maintainer: | Mauricio Vargas <[email protected]> |
License: | Apache License (>= 2) |
Version: | 2.1 |
Built: | 2024-11-08 04:12:38 UTC |
Source: | https://github.com/pachadotdev/lsts |
Plots the contour plot of the smoothing periodogram of a time series, by blocks or windows.
block.smooth.periodogram( y, x = NULL, N = NULL, S = NULL, p = 0.25, spar.freq = 0, spar.time = 0 )
block.smooth.periodogram( y, x = NULL, N = NULL, S = NULL, p = 0.25, spar.freq = 0, spar.time = 0 )
y |
(type: numeric) data vector |
x |
(type: numeric) optional vector, if |
N |
(type: numeric) value corresponding to the length of the window to
compute periodogram.
If |
S |
(type: numeric) value corresponding to the lag with which will be taking the blocks or windows to calculate the periodogram. |
p |
(type: numeric) value used if it is desired that |
spar.freq |
(type: numeric) smoothing parameter, typically (but not
necessarily) in |
spar.time |
(type: numeric) smoothing parameter, typically (but not
necessarily) in |
The number of windows of the function is ,
where
trunc
truncates de entered value and n is
the length of the vector y
. All windows are of the same length
N
, if this value isn't entered by user then is computed as
(Dahlhaus).
LSTS_spb
computes the periodogram in each of the
M windows and then smoothes it two times with
smooth.spline
function; the first time using
spar.freq
parameter and the second time with spar.time
. These
windows overlap between them.
A ggplot object.
For more information on theoretical foundations and estimation methods see Dahlhaus R, others (1997). “Fitting time series models to nonstationary processes.” The annals of Statistics, 25(1), 1–37. Dahlhaus R, Giraitis L (1998). “On the optimal segment length for parameter estimates for locally stationary time series.” Journal of Time Series Analysis, 19(6), 629–655.
block.smooth.periodogram(malleco)
block.smooth.periodogram(malleco)
Plots the p-values Ljung-Box test.
Box.Ljung.Test(z, lag = NULL, main = NULL)
Box.Ljung.Test(z, lag = NULL, main = NULL)
z |
(type: numeric) data vector |
lag |
(type: numeric) the number of periods for the autocorrelation |
main |
(type: character) a title for the returned plot |
The Ljung-Box test is used to check if exists autocorrelation in a time series. The statistic is
with n the
number of observations and the autocorrelation
coefficient in the sample when the lag is j.
LSTS_lbtp
computes and returns the p-values graph with lag j.
A ggplot object.
For more information on theoretical foundations and estimation methods see Brockwell PJ, Davis RA, Calder MV (2002). Introduction to time series and forecasting, volume 2. Springer. Ljung GM, Box GE (1978). “On a measure of lack of fit in time series models.” Biometrika, 65(2), 297–303.
Box.Ljung.Test(malleco, lag = 5)
Box.Ljung.Test(malleco, lag = 5)
Numerical aproximation of the Hessian of a function.
hessian(f, x0, ...)
hessian(f, x0, ...)
f |
(type: numeric) name of function that defines log likelihood (or negative of it). |
x0 |
(type: numeric) scalar or vector of parameters that give the point at which you want the hessian estimated (usually will be the mle). |
... |
Additional arguments to be passed to the function. |
Computes the numerical approximation of the Hessian of f
, evaluated at
x0
.
Usually needs to pass additional parameters (e.g. data). N.B. this uses no
numerical sophistication.
An matrix of 2nd derivatives, where
is the length of
x0
.
# Variance of the maximum likelihood estimator for mu parameter in # gaussian data loglik <- function(series, x, sd = 1) { -sum(log(dnorm(series, mean = x, sd = sd))) } sqrt(c(var(malleco) / length(malleco), diag(solve(hessian( f = loglik, x = mean(malleco), series = malleco, sd = sd(malleco) )))))
# Variance of the maximum likelihood estimator for mu parameter in # gaussian data loglik <- function(series, x, sd = 1) { -sum(log(dnorm(series, mean = x, sd = sd))) } sqrt(c(var(malleco) / length(malleco), diag(solve(hessian( f = loglik, x = mean(malleco), series = malleco, sd = sd(malleco) )))))
This function run the state-space equations for expansion infinite of moving average in processes LS-ARMA or LS-ARFIMA.
LS.kalman( series, start, order = c(p = 0, q = 0), ar.order = NULL, ma.order = NULL, sd.order = NULL, d.order = NULL, include.d = FALSE, m = NULL )
LS.kalman( series, start, order = c(p = 0, q = 0), ar.order = NULL, ma.order = NULL, sd.order = NULL, d.order = NULL, include.d = FALSE, m = NULL )
series |
(type: numeric) univariate time series. |
start |
(type: numeric) numeric vector, initial values for parameters to run the model. |
order |
(type: numeric) vector corresponding to |
ar.order |
(type: numeric) AR polimonial order. |
ma.order |
(type: numeric) MA polimonial order. |
sd.order |
(type: numeric) polinomial order noise scale factor. |
d.order |
(type: numeric) |
include.d |
(type: numeric) logical argument for |
m |
(type: numeric) truncation order of the MA infinity process. By
default |
The model fit is done using the Whittle likelihood, while the generation of
innovations is through Kalman Filter.
Details about ar.order, ma.order, sd.order
and d.order
can be
viewed in LS.whittle
.
A list with:
residuals |
standard residuals. |
fitted_values |
model fitted values. |
delta |
variance prediction error. |
For more information on theoretical foundations and estimation methods see Brockwell PJ, Davis RA, Calder MV (2002). Introduction to time series and forecasting, volume 2. Springer. Palma W (2007). Long-memory time series: theory and methods, volume 662. John Wiley \& Sons. Palma W, Olea R, Ferreira G (2013). “Estimation and forecasting of locally stationary processes.” Journal of Forecasting, 32(1), 86–96.
fit_kalman <- LS.kalman(malleco, start(malleco))
fit_kalman <- LS.kalman(malleco, start(malleco))
Produces a summary of the results to Whittle
estimator to Locally Stationary Time Series (LS.whittle
function).
LS.summary(object)
LS.summary(object)
object |
(type: list) the output of |
Calls the output from LS.whittle
and computes the standard
error and p-values to provide a detailed summary.
A list with the following components:
summary |
a resume table with estimate, std. error, z-value and p-value of the model. |
aic |
AIC of the model. |
npar |
number of parameters in the model. |
fit_whittle <- LS.whittle( series = malleco, start = c(1, 1, 1, 1), order = c(p = 1, q = 0), ar.order = 1, sd.order = 1, N = 180, n.ahead = 10 ) LS.summary(fit_whittle)
fit_whittle <- LS.whittle( series = malleco, start = c(1, 1, 1, 1), order = c(p = 1, q = 0), ar.order = 1, sd.order = 1, N = 180, n.ahead = 10 ) LS.summary(fit_whittle)
This function computes Whittle estimator to LS-ARMA and LS-ARFIMA models.
LS.whittle( series, start, order = c(p = 0, q = 0), ar.order = NULL, ma.order = NULL, sd.order = NULL, d.order = NULL, include.d = FALSE, N = NULL, S = NULL, include.taper = TRUE, control = list(), lower = -Inf, upper = Inf, m = NULL, n.ahead = 0 )
LS.whittle( series, start, order = c(p = 0, q = 0), ar.order = NULL, ma.order = NULL, sd.order = NULL, d.order = NULL, include.d = FALSE, N = NULL, S = NULL, include.taper = TRUE, control = list(), lower = -Inf, upper = Inf, m = NULL, n.ahead = 0 )
series |
(type: numeric) univariate time series. |
start |
(type: numeric) numeric vector, initial values for parameters to run the model. |
order |
(type: numeric) vector corresponding to |
ar.order |
(type: numeric) AR polimonial order. |
ma.order |
(type: numeric) MA polimonial order. |
sd.order |
(type: numeric) polinomial order noise scale factor. |
d.order |
(type: numeric) |
include.d |
(type: numeric) logical argument for |
N |
(type: numeric) value corresponding to the length of the window to
compute periodogram. If |
S |
(type: numeric) value corresponding to the lag with which will go taking the blocks or windows. |
include.taper |
(type: logical) logical argument that by default is
|
control |
(type: list) A list of control parameters. More details in
|
lower |
(type: numeric) lower bound, replicated to be as long as
|
upper |
(type: numeric) upper bound, replicated to be as long as
|
m |
(type: numeric) truncation order of the MA infinity process, by
default |
n.ahead |
(type: numeric) The number of steps ahead for which prediction is required. By default is zero. |
This function estimates the parameters in models: LS-ARMA
and LS-ARFIMA
with infinite moving average expansion
for , where for
,
is an autoregressive polynomial,
is a
moving average polynomial,
is a long-memory parameter,
is a noise scale factor and
is a
Gaussian white noise sequence with zero mean and unit variance. This class
of models extends the well-known ARMA and ARFIMA process, which is obtained
when the components
,
,
and
do not depend on
.
The evolution of these models can be specified in terms of a general class
of functions. For example, let
,
, be
a basis for a space of smoothly varying functions and let
be the time-varying long-memory parameter in model
LS-ARFIMA. Then we could write
in terms of the basis
as follows
for unknown values of
and
.
In this situation, estimating
involves determining
and
estimating the coefficients
.
LS.whittle
optimizes LS.whittle.loglik
as objective
function using nlminb
function, for both LS-ARMA
(include.d=FALSE
) and LS-ARFIMA (include.d=TRUE
) models.
Also computes Kalman filter with LS.kalman
and this values
are given in var.coef
in the output.
A list with the following components:
coef |
The best set of parameters found. |
var.coef |
covariance matrix approximated for maximum likelihood
estimator |
loglik |
log-likelihood of |
aic |
Akaike'S ‘An Information Criterion’, for one fitted model LS-ARMA
or LS-ARFIMA. The formula is |
series |
original time serie. |
residuals |
standard residuals. |
fitted.values |
model fitted values. |
pred |
predictions of the model. |
se |
the estimated standard errors. |
model |
A list representing the fitted model. |
# Analysis by blocks of phi and sigma parameters N <- 200 S <- 100 M <- trunc((length(malleco) - N) / S + 1) table <- c() for (j in 1:M) { x <- malleco[(1 + S * (j - 1)):(N + S * (j - 1))] table <- rbind(table, nlminb( start = c(0.65, 0.15), N = N, objective = LS.whittle.loglik, series = x, order = c(p = 1, q = 0) )$par) } u <- (N / 2 + S * (1:M - 1)) / length(malleco) table <- as.data.frame(cbind(u, table)) colnames(table) <- c("u", "phi", "sigma") # Start parameters phi <- smooth.spline(table$phi, spar = 1, tol = 0.01)$y fit.1 <- nls(phi ~ a0 + a1 * u, start = list(a0 = 0.65, a1 = 0.00)) sigma <- smooth.spline(table$sigma, spar = 1)$y fit.2 <- nls(sigma ~ b0 + b1 * u, start = list(b0 = 0.65, b1 = 0.00)) fit_whittle <- LS.whittle( series = malleco, start = c(coef(fit.1), coef(fit.2)), order = c(p = 1, q = 0), ar.order = 1, sd.order = 1, N = 180, n.ahead = 10 )
# Analysis by blocks of phi and sigma parameters N <- 200 S <- 100 M <- trunc((length(malleco) - N) / S + 1) table <- c() for (j in 1:M) { x <- malleco[(1 + S * (j - 1)):(N + S * (j - 1))] table <- rbind(table, nlminb( start = c(0.65, 0.15), N = N, objective = LS.whittle.loglik, series = x, order = c(p = 1, q = 0) )$par) } u <- (N / 2 + S * (1:M - 1)) / length(malleco) table <- as.data.frame(cbind(u, table)) colnames(table) <- c("u", "phi", "sigma") # Start parameters phi <- smooth.spline(table$phi, spar = 1, tol = 0.01)$y fit.1 <- nls(phi ~ a0 + a1 * u, start = list(a0 = 0.65, a1 = 0.00)) sigma <- smooth.spline(table$sigma, spar = 1)$y fit.2 <- nls(sigma ~ b0 + b1 * u, start = list(b0 = 0.65, b1 = 0.00)) fit_whittle <- LS.whittle( series = malleco, start = c(coef(fit.1), coef(fit.2)), order = c(p = 1, q = 0), ar.order = 1, sd.order = 1, N = 180, n.ahead = 10 )
This function computes Whittle estimator for LS-ARMA and LS-ARFIMA models, in data with mean zero. If mean is not zero, then it is subtracted to data.
LS.whittle.loglik( x, series, order = c(p = 0, q = 0), ar.order = NULL, ma.order = NULL, sd.order = NULL, d.order = NULL, include.d = FALSE, N = NULL, S = NULL, include.taper = TRUE )
LS.whittle.loglik( x, series, order = c(p = 0, q = 0), ar.order = NULL, ma.order = NULL, sd.order = NULL, d.order = NULL, include.d = FALSE, N = NULL, S = NULL, include.taper = TRUE )
x |
(type: numeric) parameter vector. |
series |
(type: numeric) univariate time series. |
order |
(type: numeric) vector corresponding to |
ar.order |
(type: numeric) AR polimonial order. |
ma.order |
(type: numeric) MA polimonial order. |
sd.order |
(type: numeric) polinomial order noise scale factor. |
d.order |
(type: numeric) |
include.d |
(type: numeric) logical argument for |
N |
(type: numeric) value corresponding to the length of the window to
compute periodogram. If |
S |
(type: numeric) value corresponding to the lag with which will go taking the blocks or windows. |
include.taper |
(type: logical) logical argument that by default is
|
The estimation of the time-varying parameters can be carried out by means of the Whittle log-likelihood function proposed by Dahlhaus (1997),
where is the number of blocks,
the length of the series per
block,
,
is the shift from block to block,
,
,
and
the Fourier frequencies in the block
(
,
).
For more information on theoretical foundations and estimation methods see Brockwell PJ, Davis RA, Calder MV (2002). Introduction to time series and forecasting, volume 2. Springer. Palma W, Olea R, others (2010). “An efficient estimator for locally stationary Gaussian long-memory processes.” The Annals of Statistics, 38(5), 2958–2997.
This function calculates log-likelihood with known ,
through
LS.whittle.loglik
function.
LS.whittle.loglik.sd( x, series, order = c(p = 0, q = 0), ar.order = NULL, ma.order = NULL, sd.order = NULL, d.order = NULL, include.d = FALSE, N = NULL, S = NULL, include.taper = TRUE, theta.par = numeric() )
LS.whittle.loglik.sd( x, series, order = c(p = 0, q = 0), ar.order = NULL, ma.order = NULL, sd.order = NULL, d.order = NULL, include.d = FALSE, N = NULL, S = NULL, include.taper = TRUE, theta.par = numeric() )
x |
(type: numeric) parameter vector. |
series |
(type: numeric) univariate time series. |
order |
(type: numeric) vector corresponding to |
ar.order |
(type: numeric) AR polimonial order. |
ma.order |
(type: numeric) MA polimonial order. |
sd.order |
(type: numeric) polinomial order noise scale factor. |
d.order |
(type: numeric) |
include.d |
(type: numeric) logical argument for |
N |
(type: numeric) value corresponding to the length of the window to
compute periodogram. If |
S |
(type: numeric) value corresponding to the lag with which will go taking the blocks or windows. |
include.taper |
(type: logical) logical argument that by default is
|
theta.par |
(type: numeric) vector with the known parameters of the model. |
This function computes LS.whittle.loglik
with x
as
x = c(theta.par, x)
.
Calculate the log-likelihood with known, through
LS.whittle.loglik
function.
LS.whittle.loglik.theta( x, series, order = c(p = 0, q = 0), ar.order = NULL, ma.order = NULL, sd.order = NULL, d.order = NULL, include.d = FALSE, N = NULL, S = NULL, include.taper = TRUE, sd.par = 1 )
LS.whittle.loglik.theta( x, series, order = c(p = 0, q = 0), ar.order = NULL, ma.order = NULL, sd.order = NULL, d.order = NULL, include.d = FALSE, N = NULL, S = NULL, include.taper = TRUE, sd.par = 1 )
x |
(type: numeric) parameter vector. |
series |
(type: numeric) univariate time series. |
order |
(type: numeric) vector corresponding to |
ar.order |
(type: numeric) AR polimonial order. |
ma.order |
(type: numeric) MA polimonial order. |
sd.order |
(type: numeric) polinomial order noise scale factor. |
d.order |
(type: numeric) |
include.d |
(type: numeric) logical argument for |
N |
(type: numeric) value corresponding to the length of the window to
compute periodogram. If |
S |
(type: numeric) value corresponding to the lag with which will go taking the blocks or windows. |
include.taper |
(type: logical) logical argument that by default is
|
sd.par |
(type: numeric) value corresponding to known variance. |
This function computes LS.whittle.loglik
with x
as
x = c(x, sd.par)
.
A ts object containing average annual ring width measured in milimiters for different Araucaria Araucana trees in the Malleco Region (Chile). The years of observation in this data cover the period 1242-1975.
A time series object with 734 elements
National Oceanic and Atmospheric Administration (NOAA)
This function computes the periodogram from a stationary time serie. Returns the periodogram, its graph and the Fourier frequency.
periodogram(y, plot = TRUE, include.taper = FALSE)
periodogram(y, plot = TRUE, include.taper = FALSE)
y |
(type: numeric) data vector |
plot |
(type: logical) logical argument which allows to plot the periodogram. Defaults to TRUE. |
include.taper |
(type: logical) logical argument which by default is
|
The tapered periodogram it is given by
with ,
and
are Fourier frequencies defined as
, with
.
The data taper used is the cosine bell function,
. If the series has missing data,
these are replaced by the average of the data and
it is corrected by
$n-N$, where
is the amount of missing values of serie. The plot of
the periodogram is
periodogram
values vs. .
A list with with the periodogram and the lambda values.
For more information on theoretical foundations and estimation methods see Brockwell PJ, Davis RA, Calder MV (2002). Introduction to time series and forecasting, volume 2. Springer. Dahlhaus R, others (1997). “Fitting time series models to nonstationary processes.” The annals of Statistics, 25(1), 1–37.
fft
, Mod
,
smooth.spline
.
# AR(1) simulated set.seed(1776) ts.sim <- arima.sim(n = 1000, model = list(order = c(1, 0, 0), ar = 0.7)) per <- periodogram(ts.sim) per$plot
# AR(1) simulated set.seed(1776) ts.sim <- arima.sim(n = 1000, model = list(order = c(1, 0, 0), ar = 0.7)) per <- periodogram(ts.sim) per$plot
This function returns the smoothing periodogram of a stationary time serie, its plot and its Fourier frequency.
smooth.periodogram(y, plot = TRUE, spar = 0)
smooth.periodogram(y, plot = TRUE, spar = 0)
y |
(type: numeric) data vector. |
plot |
(type: logical) logical argument which allows to plot the periodogram. Defaults to TRUE. |
spar |
(type: numeric) smoothing parameter, typically (but not
necessarily) in |
smooth.periodogram
computes the periodogram from y
vector and
then smooth it with smoothing spline method, which basically
approximates a curve using a cubic spline (see more details in
smooth.spline
). is the Fourier frequency
obtained through
periodogram
.
It must have caution with the minimum length of y
, because
smooth.spline
requires the entered vector has at least length 4 and
the length of y
does not equal to the length of the data of the
periodogram that smooth.spline
receives.
If it presents problems with tol (tolerance), see
smooth.spline
.
A list with with the smooth periodogram and the lambda values
# AR(1) simulated require(ggplot2) set.seed(1776) ts.sim <- arima.sim(n = 1000, model = list(order = c(1, 0, 0), ar = 0.7)) per <- periodogram(ts.sim) aux <- smooth.periodogram(ts.sim, plot = FALSE, spar = .7) sm_p <- data.frame(x = aux$lambda, y = aux$smooth.periodogram) sp_d <- data.frame( x = aux$lambda, y = spectral.density(ar = 0.7, lambda = aux$lambda) ) g <- per$plot g + geom_line(data = sm_p, aes(x, y), color = "#ff7f0e") + geom_line(data = sp_d, aes(x, y), color = "#d31244")
# AR(1) simulated require(ggplot2) set.seed(1776) ts.sim <- arima.sim(n = 1000, model = list(order = c(1, 0, 0), ar = 0.7)) per <- periodogram(ts.sim) aux <- smooth.periodogram(ts.sim, plot = FALSE, spar = .7) sm_p <- data.frame(x = aux$lambda, y = aux$smooth.periodogram) sp_d <- data.frame( x = aux$lambda, y = spectral.density(ar = 0.7, lambda = aux$lambda) ) g <- per$plot g + geom_line(data = sm_p, aes(x, y), color = "#ff7f0e") + geom_line(data = sp_d, aes(x, y), color = "#d31244")
Returns theoretical spectral density evaluated in ARMA and ARFIMA processes.
spectral.density(ar = numeric(), ma = numeric(), d = 0, sd = 1, lambda = NULL)
spectral.density(ar = numeric(), ma = numeric(), d = 0, sd = 1, lambda = NULL)
ar |
(type: numeric) AR vector. If the time serie doesn't have AR term then omit it. For more details see the examples. |
ma |
(type: numeric) MA vector. If the time serie doesn't have MA term then omit it. For more details see the examples. |
d |
(type: numeric) Long-memory parameter. If d is zero, then the process is ARMA(p,q). |
sd |
(type: numeric) Noise scale factor, by default is 1. |
lambda |
(type: numeric) |
The spectral density of an ARFIMA(p,d,q) processes is
With and
.
is the
Mod
of .
LSTS_sd
returns the
values corresponding to . When
d
is zero, the spectral
density corresponds to an ARMA(p,q).
An unnamed vector of numeric class.
For more information on theoretical foundations and estimation methods see Brockwell PJ, Davis RA, Calder MV (2002). Introduction to time series and forecasting, volume 2. Springer. Palma W (2007). Long-memory time series: theory and methods, volume 662. John Wiley \& Sons.
# Spectral Density AR(1) require(ggplot2) f <- spectral.density(ar = 0.5, lambda = malleco) ggplot(data.frame(x = malleco, y = f)) + geom_line(aes(x = as.numeric(x), y = as.numeric(y))) + labs(x = "Frequency", y = "Spectral Density") + theme_minimal()
# Spectral Density AR(1) require(ggplot2) f <- spectral.density(ar = 0.5, lambda = malleco) ggplot(data.frame(x = malleco, y = f)) + geom_line(aes(x = as.numeric(x), y = as.numeric(y))) + labs(x = "Frequency", y = "Spectral Density") + theme_minimal()
Plot time-series diagnostics.
ts.diag(x, lag = 10, band = qnorm(0.975)/sqrt(length(x)))
ts.diag(x, lag = 10, band = qnorm(0.975)/sqrt(length(x)))
x |
(type: numeric) residuals of the fitted time series model. |
lag |
(type: numeric) maximum lag at which to calculate the acf and Ljung-Box test. By default set to 10. |
band |
(type: numeric) absolute value for bandwidth in the the ACF plot. By default set to 'qnorm(0.975)/sqrt(n)' which approximates to 0.07 for malleco data (n = 734) |
This function plot the residuals, the autocorrelation function of the
residuals (ACF) and the p-values of the Ljung-Box Test for all lags up to
lag
.
A ggplot object.
ts.diag(malleco)
ts.diag(malleco)