In R package 'timetk', how to understand the parameter 'difference' in function 'diff_vec'?
library(timetk)
1:10 %>% diff_vec(lag=1)
#result: [1] NA 1 1 1 1 1 1 1 1 1
I try to change the valus of 'difference', but can't understand the result
#what's the usage of parameter 'difference'
1:10 %>% diff_vec(lag=1,difference = 1)
#result: [1] NA 1 1 1 1 1 1 1 1 1
1:10 %>% diff_vec(lag=1,difference = 2)
#result: [1] NA NA 0 0 0 0 0 0 0 0
1:10 %>% diff_vec(lag=1,difference = 3)
#result: [1] NA NA NA 0 0 0 0 0 0 0
CodePudding user response:
It might be easier to understand with (1:10)^2
. Argument difference
is perform difference(differential) between consecutive value multiple time.
(1:10)^2 %>% diff_vec(lag=1,difference = 1)
[1] NA 3 5 7 9 11 13 15 17 19
For difference = 2
means perform difference = 1
twice.
(1:10)^2 %>% diff_vec(lag=1,difference = 1) %>% diff_vec(lag=1,difference = 1)
[1] NA NA 2 2 2 2 2 2 2 2
(1:10)^2 %>% diff_vec(lag=1,difference = 2)
[1] NA NA 2 2 2 2 2 2 2 2
In the same way, difference = 3
means perform difference = 1
three times.
(1:10)^2 %>% diff_vec(lag=1,difference = 1) %>% diff_vec(lag=1,difference = 1) %>% diff_vec(lag=1,difference = 1)
[1] NA NA NA 0 0 0 0 0 0 0
(1:10)^2 %>% diff_vec(lag=1,difference = 3)
[1] NA NA NA 0 0 0 0 0 0 0