Learn R Programming

EmpiricalDynamics (version 0.1.2)

compute_derivative: Compute Derivative of a Time Series

Description

Main dispatcher function for numerical differentiation. Supports multiple methods appropriate for different data characteristics.

Usage

compute_derivative(
  Z,
  t = NULL,
  method = c("tvr", "savgol", "spline", "finite_diff", "spectral"),
  ...
)

Value

Numeric vector of estimated derivatives with diagnostic attributes.

Arguments

Z

Numeric vector of observations.

t

Numeric vector of time points (NULL assumes dt=1).

method

Differentiation method: "tvr", "savgol", "spline", "finite_diff", "fd", or "spectral".

...

Additional arguments passed to the specific method.

Details

Available methods:

  • tvr: Total Variation Regularized differentiation (recommended for economic data with trends and shocks).

  • savgol: Savitzky-Golay filter (moderate noise, preserves peaks).

  • spline: Smoothing spline (high noise, prioritizes trend).

  • finite_diff or fd: Centered finite differences (low noise).

  • spectral: FFT-based (periodic data only).

See Also

compute_derivative_tvr, suggest_differentiation_method

Examples

Run this code
t <- seq(0, 10, by = 0.1)
Z <- sin(t) + rnorm(length(t), sd = 0.1)

# Finite differences (fast, no dependencies)
dZ_fd <- compute_derivative(Z, t, method = "finite_diff")

# Access the derivative vector for plotting
plot(t, dZ_fd$derivative, type = "l", main = "Derivative Comparison")
lines(t, cos(t), col = "red", lty = 2) # True derivative

# \donttest{
# TVR (requires CVXR)
if (requireNamespace("CVXR", quietly = TRUE)) {
  dZ_tvr <- compute_derivative(Z, t, method = "tvr")
}
# }

Run the code above in your browser using DataLab