繁体   English   中英

R 中的每小时时间序列。 ts(... start) 是如何工作的?

[英]hourly time series in R. How ts(… start) works?

试图在 R 中每小时创建一个时间序列。

我有一个收集每小时车辆数量的数据框,它看起来像:

> head(df)
# A tibble: 6 x 8
      interval             cars  vans trucks total `mean speed` `% occupation`  hour
      <dttm>              <int> <int>  <int> <int>        <dbl>          <dbl> <int>
    1 2017-10-09 00:00:00     7     0      0     7         7.37             1.     0
    2 2017-10-09 01:00:00    24     0      0    24        16.1              3.     1
    3 2017-10-09 02:00:00    27     0      0    27        18.1              2.     2
    4 2017-10-09 03:00:00    47     3      0    50        31.5              3.     3
    5 2017-10-09 04:00:00   122     1      5   128        48.0             16.     4
    6 2017-10-09 05:00:00   353     6      2   361        66.3             20.     5

> tail(df,1)
    # A tibble: 1 x 8
      interval             cars  vans trucks total `mean speed` `% occupation`  hour
      <dttm>              <int> <int>  <int> <int>        <dbl>          <dbl> <int>
    1 2018-03-15 20:00:00    48     0      2    50         31.5             5.    20

查看在R开始每日时间序列的答案,该答案清楚地解释了如何按天创建 ts 我已将此 df 转换为时间序列:

ts2Start <- df$interval[1]
ts2End <- df$interval[nrow(df)]
indexPerHour <- seq(ts2Start, ts2End, by = 'hour')

由于我们一年有 365 天,每天有 24 小时,因此我将 ts 创建为:

> df.ts <- ts(df$total, start = c(2017, as.numeric(format(indexPerHour[1], '%j'))),
+                                frequency=24*365)

在哪里

as.numeric(format(indexPerHour[1], '%j')))

返回 282

为了验证我在做什么,我检查了从索引获得的日期是否与我的数据框中的第一行相同

head(date_decimal(index(df.ts)),1)

但是虽然我的第一个日期/时间应该是:“2017-10-09 00:00:00”,但我得到的是:“2017-01-12 16:59:59 UTC”

看起来df.ts系列中的第一个索引已经开始于 ~ 282/24

我不明白我做错了什么。 start 参数如何在ts()

我还检查了帖子: 如何为每小时数据创建 R TimeSeries

建议使用xts包。

问题是我只是从使用tslm()并且似乎不支持xts对象的书中学习。

我可以使用ts()创建每小时时间序列吗?

您应该改用 xts 库。 例如:

time_index <- seq(from = as.POSIXct("2016-01-01 00:00:00"), 
                  to = as.POSIXct("2018-10-01 00:00:00"), by = "hour")

traff = xts(df, order.by = time_index)```

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM