Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

In R package 'timetk', how to understand the parameter 'difference' in function 'diff_vec'?

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading