Skip to contents

diff_vec() applies a Differencing Transformation. diff_inv_vec() inverts the differencing transformation.

Usage

diff_vec(
  x,
  lag = 1,
  difference = 1,
  log = FALSE,
  initial_values = NULL,
  silent = FALSE
)

diff_inv_vec(x, lag = 1, difference = 1, log = FALSE, initial_values = NULL)

Arguments

x

A numeric vector to be differenced or inverted.

lag

Which lag (how far back) to be included in the differencing calculation.

difference

The number of differences to perform.

  • 1 Difference is equivalent to measuring period change.

  • 2 Differences is equivalent to measuring period acceleration.

log

If log differences should be calculated. Note that difference inversion of a log-difference is approximate.

initial_values

Only used in the diff_vec_inv() operation. A numeric vector of the initial values, which are used to invert differences. This vector is the original values that are the length of the NA missing differences.

silent

Whether or not to report the initial values used to invert the difference as a message.

Value

A numeric vector

Details

Benefits:

This function is NA padded by default so it works well with dplyr::mutate() operations.

Difference Calculation

Single differencing, diff_vec(x_t) is equivalent to: x_t - x_t1, where the subscript _t1 indicates the first lag. This transformation can be interpereted as change.

Double Differencing Calculation

Double differencing, diff_vec(x_t, difference = 2) is equivalent to: (x_t - x_t1) - (x_t - x_t1)_t1, where the subscript _t1 indicates the first lag. This transformation can be interpereted as acceleration.

Log Difference Calculation

Log differencing, diff_vec(x_t, log = TRUE) is equivalent to: log(x_t) - log(x_t1) = log(x_t / x_t1), where x_t is the series and x_t1 is the first lag.

The 1st difference diff_vec(difference = 1, log = TRUE) has an interesting property where diff_vec(difference = 1, log = TRUE) %>% exp() is approximately 1 + rate of change.

See also

Advanced Differencing and Modeling:

Additional Vector Functions:

Examples

library(dplyr)

# --- USAGE ----

diff_vec(1:10, lag = 2, difference = 2) %>%
    diff_inv_vec(lag = 2, difference = 2, initial_values = 1:4)
#> diff_vec(): Initial values: 1, 2, 3, 4
#>  [1]  1  2  3  4  5  6  7  8  9 10

# --- VECTOR ----

# Get Change
1:10 %>% diff_vec()
#> diff_vec(): Initial values: 1
#>  [1] NA  1  1  1  1  1  1  1  1  1

# Get Acceleration
1:10 %>% diff_vec(difference = 2)
#> diff_vec(): Initial values: 1, 2
#>  [1] NA NA  0  0  0  0  0  0  0  0

# Get approximate rate of change
1:10 %>% diff_vec(log = TRUE) %>% exp() - 1
#> diff_vec(): Initial values: 1
#>  [1]        NA 1.0000000 0.5000000 0.3333333 0.2500000 0.2000000 0.1666667
#>  [8] 0.1428571 0.1250000 0.1111111


# --- MUTATE ----

m4_daily %>%
    group_by(id) %>%
    mutate(difference = diff_vec(value, lag = 1)) %>%
    mutate(
        difference_inv = diff_inv_vec(
            difference,
            lag = 1,
            # Add initial value to calculate the inverse difference
            initial_values = value[1]
        )
    )
#> diff_vec(): Initial values: 2076.2
#> diff_vec(): Initial values: 1821.9
#> diff_vec(): Initial values: 9109.38
#> diff_vec(): Initial values: 5647.3
#> # A tibble: 9,743 × 5
#> # Groups:   id [4]
#>    id    date       value difference difference_inv
#>    <fct> <date>     <dbl>      <dbl>          <dbl>
#>  1 D10   2014-07-03 2076.     NA              2076.
#>  2 D10   2014-07-04 2073.     -2.80           2073.
#>  3 D10   2014-07-05 2049.    -24.7            2049.
#>  4 D10   2014-07-06 2049.      0.200          2049.
#>  5 D10   2014-07-07 2006.    -42.5            2006.
#>  6 D10   2014-07-08 2018.     11.2            2018.
#>  7 D10   2014-07-09 2019.      1.5            2019.
#>  8 D10   2014-07-10 2007.    -11.7            2007.
#>  9 D10   2014-07-11 2010       2.60           2010 
#> 10 D10   2014-07-12 2002.     -8.5            2002.
#> # ℹ 9,733 more rows