1

在R包'timetk'中,如何理解函数'diff_vec'中的参数'difference'?

library(timetk)
1:10 %>% diff_vec(lag=1)
#result: [1] NA  1  1  1  1  1  1  1  1  1

我试图改变“差异”的价值,但无法理解结果

#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
4

1 回答 1

2

用 可能更容易理解(1:10)^2。参数difference是多次执行连续值之间的差异(差分)。

(1:10)^2 %>% diff_vec(lag=1,difference = 1)
[1] NA  3  5  7  9 11 13 15 17 19

Fordifference = 2意味着执行difference = 1两次。

(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

同理,difference = 3表示执行difference = 1三遍。

(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
于 2021-11-22T07:14:58.300 回答