1 min read

tibbletime 使用技巧

本文于2020-10-10更新。 如发现问题或者有建议,欢迎提交 Issue

1 求滚动\(\rho\)(Vaughan and Dancho 2018)

set.seed(123)
x <- rnorm(100)
set.seed(456)
y <- rnorm(100)
x %>% head()
y %>% head()
t <- 1:100
t %>% head()
test_roll_cor <- 
  data_frame(x = x, y = y, t = t)
rolling_cor <- rollify(.f = ~cor(.x,.y), window = 15)
test_roll_cor %>% 
  mutate(rolling_cor = rolling_cor(x,y))

2 as_tbl_time

weather_time <- nycflights13::weather %>% 
  select(origin, time_hour, temp, precip) %>% 
  as_tbl_time(index = time_hour)

这里使用nycflights13::weather的数据,定义time index为time_hour

weather_time %>% head

3 collapse_by

weather_time %>% 
  group_by(origin) %>% 
  collapse_by(period = "monthly")

tibbletime collapse_by Evaluation error: invalid 'tz' value. 但是会出现这个bug,已经向作者报错了, collapse_by error invalid timezone · Issue #51 · business-science/tibbletime

4 filter time(Wang 2018)

比原生的dplyr方便。 这里已经指定了time_hour为time index,因此直接给规则。

weather_time %>% 
  filter_time(~ "2013-01-02") %>% 
  arrange(desc(time_hour))

参考文献

Vaughan, Davis, and Matt Dancho. 2018. Tibbletime: Time Aware Tibbles. https://CRAN.R-project.org/package=tibbletime.

Wang, Earo. 2018. “Tsibble? Or Tibbletime?” 2018. https://blog.earo.me/2018/02/06/tsibble-or-tibbletime/.