整理一步预测

时间:2016-08-19 08:27:45

标签: r forecasting purrr

我有大约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。这很容易using for-loops但我认为我会尝试这样做的整洁方式。所以我想创建一个结束日期作为第一列的data_frame和一个data_frame,其中包含从df到第二个结束日期的所有数据,然后我可以使用{{ 1}}和朋友们。如何创建此嵌套的data_frame?

1 个答案:

答案 0 :(得分: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