Skip to contents

Eearly warning signal (EWS) detection for time series data. Both rolling window and expanding window approaches are supported. Includes various methods for detrending the data before analysis. For visualizing the results, see plot.ews().

Usage

detect_warnings(
  data,
  method = "rolling",
  metrics = "all",
  window = 0.5,
  burnin = 0.1,
  demean = TRUE,
  detrend = "none",
  threshold = 2,
  consecutive = 2L,
  bandwidth,
  span,
  degree
)

Arguments

data

[ts, numeric()]
Univariate time series data.

method

[character(1): "rolling"]
Name of the analysis method. Either "rolling" or "expanding" for rolling window and expanding window, respectively.

metrics

[character(1): "all"]
Names of the EWS metrics to compute. The available options are:

  • "ar1": The autoregressive coefficient of an AR1 model.

  • "sd": Standard deviation.

  • "skew": Skewness.

  • "kurt": Kurtosis.

  • "cv": Coefficient of variation.

  • "rr": Return rate (1 - ar1).

  • "all": All of the above.

window

[numeric(1): 0.5]
Window size as a proportion of the total time series length.

burnin

[numeric(1): 0.1]
Burn-in period as a proportion of the total time series length.

demean

[logical(1): TRUE]
Should the time series be demeaned before analysis? If TRUE, the "ar1" metric will be based on an AR1 model where the mean of the observations is first subtracted. See stats::ar.ols() for details.

detrend

[character(1): "none"]
Name of the detrending method to apply to the time series data before computing the metrics. The available options are:

  • "gaussian": Estimates a smooth curve via kernel-based regression using stats::ksmooth() with a Gaussian kernel which is then subtracted from the time series.

  • "loess": Estimates a smooth curve via local polynomial regression using stats::loess() which is then subtracted from the time series.

  • "linear": Fits a linear regression model via stats::lm() and uses the residuals for computing the metrics.

  • "first-diff": Uses the differences between the time series and its first-order lagged values.

  • "none": Use the original time series data (no detrending).

threshold

[numeric(1): 2.0]
The z-score threshold value for the expanding window method.

consecutive

[integer(1): 2L]
The number of times the threshold has to be crossed consecutively to be counted as a detection.

bandwidth

See stats::ksmooth().

span

See stats::loess().

degree

See stats::loess().

Value

An object of class ews containing the EWS results as a tibble.

Examples

set.seed(123)
ts_data <- stats::arima.sim(list(order = c(1, 1, 0), ar = 0.6), n = 200)

# Rolling window (default)
ews_roll <- detect_warnings(ts_data)

# Expanding window
ews_exp <- detect_warnings(ts_data, method = "expanding")