设为首页 加入收藏

TOP

时间序列分析工具箱——timetk(三)
2019-09-03 02:41:23 】 浏览:449
Tags:时间序列 分析 工具箱 timetk
951 ## wday.lbl^6 2.276e+01 8.200e+01 0.278 0.782319 ## mday NA NA NA NA ## qday -1.362e+02 2.418e+02 -0.563 0.575326 ## yday -2.356e+02 1.416e+02 -1.664 0.101627 ## mweek -1.670e+02 1.477e+02 -1.131 0.262923 ## week -1.764e+02 1.890e+02 -0.933 0.354618 ## week.iso 2.315e+02 1.256e+02 1.842 0.070613 . ## week2 -1.235e+02 1.547e+02 -0.798 0.428283 ## week3 NA NA NA NA ## week4 NA NA NA NA ## mday7 NA NA NA NA ## --- ## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1 ## ## Residual standard error: 260.4 on 57 degrees of freedom ## Multiple R-squared: 0.9798, Adjusted R-squared: 0.9706 ## F-statistic: 106.4 on 26 and 57 DF, p-value: < 2.2e-16

STEP 3:构建未来(新)数据

使用 tk_index() 来扩展索引。

# Retrieves the timestamp information
beer_sales_idx <- beer_sales_tbl %>%
    tk_index()

tail(beer_sales_idx)
## [1] "2016-07-01" "2016-08-01" "2016-09-01" "2016-10-01" "2016-11-01"
## [6] "2016-12-01"

通过 tk_make_future_timeseries 函数从现有索引构造未来索引。函数会在内部检查索引的周期性,并返回正确的序列。我们在 whole vignette on how to make future time series 介绍了该主题更详尽的内容。

# Make future index
future_idx <- beer_sales_idx %>%
    tk_make_future_timeseries(
        n_future = 12)

future_idx
##  [1] "2017-01-01" "2017-02-01" "2017-03-01" "2017-04-01" "2017-05-01"
##  [6] "2017-06-01" "2017-07-01" "2017-08-01" "2017-09-01" "2017-10-01"
## [11] "2017-11-01" "2017-12-01"

tk_get_timeseries_signature() 将未来索引转换成时间序列签名数据框。

new_data_tbl <- future_idx %>%
    tk_get_timeseries_signature()

new_data_tbl
## # A tibble: 12 x 29
##         index  index.num    diff  year year.iso  half quarter month
##        <date>      <int>   <int> <int>    <int> <int>   <int> <int>
##  1 2017-01-01 1483228800      NA  2017     2016     1       1     1
##  2 2017-02-01 1485907200 2678400  2017     2017     1       1     2
##  3 2017-03-01 1488326400 2419200  2017     2017     1       1     3
##  4 2017-04-01 1491004800 2678400  2017     2017     1       2     4
##  5 2017-05-01 1493596800 2592000  2017     2017     1       2     5
##  6 2017-06-01 1496275200 2678400  2017     2017     1       2     6
##  7 2017-07-01 1498867200 2592000  2017     2017     2       3     7
##  8 2017-08-01 1501545600 2678400  2017     2017     2       3     8
##  9 2017-09-01 1504224000 2678400  2017     2017     2       3     9
## 10 2017-10-01 1506816000 2592000  2017     2017     2       4    10
## 11 2017-11-01 1509494400 2678400  2017     2017     2       4    11
## 12 2017-12-01 1512086400 2592000  2017     2017     2       4    12
## # ... with 21 more variables: month.xts <int>, month.lbl <ord>,
## #   day <int>, hour <int>, minute <int>, second <int>, hour12 <int>,
## #   am.pm <int>, wday <int>, wday.xts <int>, wday.lbl <ord>,
## #   mday <int>, qday <int>, yday <int>, mweek <int>, week <int>,
## #   week.iso <int>, week2 <int>, week3 <int>, week4 <int>,
## #   mday7 <int>

STEP 4:预测新数据

predict() 应用于回归模型。注意,和之前使用 lm() 函数时一样,去掉 indexdiff 列。

# Make predictions
pred <- predict(
    fit_lm,
    newdata = select(
        new_data_tbl, -c(index, diff)))

predictions_tbl <- tibble(
    date  = future_idx,
    value = pred)

predictions_tbl
## # A tibble: 12 x 2
##          date     value
##        <date>     <dbl>
##  1 2017-01-01  9509.122
##  2 2017-02-01 10909.189
##  3 2017-03-01 12281.835
##  4 2017-04-01 11378.678
##  5 2017-05-01 13080.710
##  6 2017-06-01 13583.471
##  7 2017-07-0
首页 上一页 1 2 3 4 5 下一页 尾页 3/5/5
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇data.table包使用应该注意的一些.. 下一篇R语言S3类的理解与构建

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目