e_derivative_finite_difference: Derivatives of time series using finite difference...

e_derivative_finite_differenceR Documentation

Derivatives of time series using finite difference coefficients

Description

To approximate a derivative to an arbitrary order of accuracy, it is possible to use the finite difference. A finite difference can be central, forward or backward. https://en.wikipedia.org/wiki/Finite_difference_coefficient Applies highest possible accuracy given the location in the vector; end points have lower accuracy and use the 1-width forward/backward method.

Usage

e_derivative_finite_difference(x, sw_derivative = 1)

Arguments

x

numeric vector

sw_derivative

order of the derivative, 1 = slope, up to 6

Value

dx derivative

Examples

# ragged empirical time series
dat <-
  tibble::tibble(
    x  = datasets::lh |> as.numeric()
  , dx = e_derivative_finite_difference(x)
  , i  = 1:length(x)
  )
dat |> print(n = 10)
# reshape to plot
dat_long <-
  dat |>
  tidyr::pivot_longer(
    cols = c(x, dx)
  ) |>
  dplyr::mutate(
    name = name |> factor(levels = c("x", "dx"))
  )
library(ggplot2)
p <- ggplot(dat_long, aes(x = i, y = value))
p <- p + theme_bw()
p <- p + geom_hline(aes(yintercept = 0), colour = "black"
                   , linetype = "solid", linewidth = 0.2, alpha = 0.3)
p <- p + geom_line()
p <- p + facet_grid(name ~ ., scales = "free_y", drop = TRUE)
p <- p + scale_x_continuous(breaks = seq(0, max(dat$i), by = 2))
print(p)

# smooth sin function
dat <-
  tibble::tibble(
    x   = seq(0, 4 * pi, by = pi / 180)
  , x_sin = sin(x)
  , x_cos = cos(x)
  , dx_sin = e_derivative_finite_difference(x_sin) / (pi / 180)
  , i  = 1:length(x)
  )
dat |> print(n = 10)
# reshape to plot
dat_long <-
  dat |>
  tidyr::pivot_longer(
    cols = c(x_sin, x_cos, dx_sin)
  ) |>
  dplyr::mutate(
    name = name |> factor(levels = c("x_sin", "x_cos", "dx_sin"))
  )
library(ggplot2)
p <- ggplot(dat_long, aes(x = x, y = value, color = name, linetype = name))
p <- p + theme_bw()
p <- p + geom_hline(aes(yintercept = 0), colour = "black"
                   , linetype = "solid", linewidth = 0.2, alpha = 0.3)
p <- p + geom_line(size = 2, alpha = 1/2)
#p <- p + facet_grid(name ~ ., scales = "free_y", drop = TRUE)
p <- p + scale_x_continuous(breaks = seq(0, 4 * pi, by = pi/2))
print(p)

erikerhardt/erikmisc documentation built on April 17, 2025, 10:48 a.m.