[英]hourly time series in R. How ts(… start) works?
Trying to create a time series per hour in R.试图在 R 中每小时创建一个时间序列。
I've a data frame collecting the amount of vehicules per hour, it looks as:我有一个收集每小时车辆数量的数据框,它看起来像:
> 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
Looking at the answer at starting a daily time series in R that clearly explains how to create a ts by day I've converted this df to a time series as:查看在R中开始每日时间序列的答案,该答案清楚地解释了如何按天创建 ts 我已将此 df 转换为时间序列:
ts2Start <- df$interval[1]
ts2End <- df$interval[nrow(df)]
indexPerHour <- seq(ts2Start, ts2End, by = 'hour')
Since we have 365 days in a year and 24h per day, I created the ts as:由于我们一年有 365 天,每天有 24 小时,因此我将 ts 创建为:
> df.ts <- ts(df$total, start = c(2017, as.numeric(format(indexPerHour[1], '%j'))),
+ frequency=24*365)
where在哪里
as.numeric(format(indexPerHour[1], '%j')))
returns 282返回 282
In order to validate what I'm doing I checked if the date obtained from the index is the same as the first row in my data frame为了验证我在做什么,我检查了从索引获得的日期是否与我的数据框中的第一行相同
head(date_decimal(index(df.ts)),1)
but while my first date/time should be: "2017-10-09 00:00:00 " I'm getting: "2017-01-12 16:59:59 UTC"但是虽然我的第一个日期/时间应该是:“2017-10-09 00:00:00”,但我得到的是:“2017-01-12 16:59:59 UTC”
It looks as the first index in the df.ts
series has started at ~ 282/24看起来
df.ts
系列中的第一个索引已经开始于 ~ 282/24
I do not understand what I'm doing wrong.我不明白我做错了什么。 How the start parameter works in
ts()
? start 参数如何在
ts()
?
I also checked the post: How to Create a R TimeSeries for Hourly data我还检查了帖子: 如何为每小时数据创建 R TimeSeries
where it is suggested to use xts
package.建议使用
xts
包。
The issue is that I'm just learning from a book where tslm()
is used and xts
object does not seem to be supported.问题是我只是从使用
tslm()
并且似乎不支持xts
对象的书中学习。
Can I use ts()
to create hourly time series ?我可以使用
ts()
创建每小时时间序列吗?
You should use an xts library instead.您应该改用 xts 库。 For example:
例如:
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.