0

我有大约 100 天的半小时观察时间序列,如下所示:

> df
# A tibble: 4,704 x 3
    city            datetime orders
   <chr>              <time>  <dbl>
1   Wien 2016-05-12 00:00:00      1
2   Wien 2016-05-12 00:30:00      4
3   Wien 2016-05-12 01:00:00      2
4   Wien 2016-05-12 01:30:00      0
5   Wien 2016-05-12 02:00:00      5
6   Wien 2016-05-12 02:30:00      10
7   Wien 2016-05-12 03:00:00      11
8   Wien 2016-05-12 03:30:00      22
9   Wien 2016-05-12 04:00:00      4
10  Wien 2016-05-12 04:30:00      2
# ... with 4,694 more rows

我想对这个时间序列进行滚动预测——根据前 n 天的数据估计模型,然后预测第 n+1 天。使用 for 循环很容易,但我想我会尝试以整洁的方式来做这件事。所以我想创建一个data_frame结束日期作为第一列的数据帧和一个包含从 df 到第二个结束日期的所有数据的 data_frame,然后我可以迭代使用purrr::map()和朋友。如何创建这个嵌套的 data_frame?

4

1 回答 1

1
df <- read.table(text="city            datetime orders
Wien '2016-05-12 00:00:00'      1
Wien '2016-05-12 01:00:00'      2
Wien '2016-05-12 02:00:00'      5
Wien '2016-05-12 03:00:00'      11
Wien '2016-05-12 03:30:00'      22
Wien '2016-05-12 04:00:00'      4
Wien '2016-05-12 04:30:00'      2",header=T,stringsAsFactors=F)

df2 <- read.table(text="end
'2016-05-12 00:30:00'
'2016-05-12 01:30:00'
'2016-05-12 02:30:00'",header=T,stringsAsFactors=F)

df2 <- df2 %>% mutate(df = map(end,~df %>% dplyr::filter(datetime <.x)))
str(df2)
# 'data.frame': 3 obs. of  2 variables:
# $ end: chr  "2016-05-12 00:30:00" "2016-05-12 01:30:00" "2016-05-12 02:30:00"
# $ df :List of 3
# ..$ :'data.frame':    1 obs. of  3 variables:
#   .. ..$ city    : chr "Wien"
# .. ..$ datetime: chr "2016-05-12 00:00:00"
# .. ..$ orders  : int 1
# ..$ :'data.frame':    3 obs. of  3 variables:
#   .. ..$ city    : chr  "Wien" "Wien" "Wien"
# .. ..$ datetime: chr  "2016-05-12 00:00:00" "2016-05-12 00:30:00" "2016-05-12 01:00:00"
# .. ..$ orders  : int  1 4 2
# ..$ :'data.frame':    5 obs. of  3 variables:
#   .. ..$ city    : chr  "Wien" "Wien" "Wien" "Wien" ...
# .. ..$ datetime: chr  "2016-05-12 00:00:00" "2016-05-12 00:30:00" "2016-05-12 01:00:00" "2016-05-12 01:30:00" ...
# .. ..$ orders  : int  1 4 2 0 5
于 2017-10-27T00:19:09.927 回答