diff_vec() applies a Differencing Transformation.
diff_inv_vec() inverts the differencing transformation.
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)
A numeric vector to be differenced or inverted.
Which lag (how far back) to be included in the differencing calculation.
The number of differences to perform.
If log differences should be calculated. Note that difference inversion of a log-difference is approximate.
Only used in the
Whether or not to report the initial values used to invert the difference as a message.
This function is
NA padded by default so it works well with
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
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
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
diff_vec(difference = 1, log = TRUE) %>% exp() is approximately 1 + rate of change.
A numeric vector
Advanced Differencing and Modeling:
step_diff() - Recipe for
tk_augment_differences() - Adds many differences to a
Additional Vector Functions:
Box Cox Transformation:
Rolling Window Transformation:
Loess Smoothing Transformation:
Missing Value Imputation for Time Series:
library(dplyr) library(timetk) # --- USAGE ---- diff_vec(1:10, lag = 2, difference = 2) %>% diff_inv_vec(lag = 2, difference = 2, initial_values = 1:4) # --- VECTOR ---- # Get Change 1:10 %>% diff_vec() # Get Acceleration 1:10 %>% diff_vec(difference = 2) # Get approximate rate of change 1:10 %>% diff_vec(log = TRUE) %>% exp() - 1 # --- 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 ) )
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.