[英]How to set up start date in time series in with hourly data
I have a time series that is hourly electricity load in Ireland.我有一个时间序列,它是爱尔兰的每小时电力负荷。 This is the data frame:
这是数据框:
> head(load2)
# A tibble: 6 x 4
datetime fecha hora load_MWH
<dttm> <date> <dbl> <dbl>
1 2018-01-01 00:00:00 2018-01-01 0 7594
2 2018-01-01 01:00:00 2018-01-01 1 7091
3 2018-01-01 02:00:00 2018-01-01 2 6652
4 2018-01-01 03:00:00 2018-01-01 3 6308
5 2018-01-01 04:00:00 2018-01-01 4 5972
6 2018-01-01 05:00:00 2018-01-01 5 5810
I want to create a Time Series object with daily seasonality and start date 2018-01-01 00:00:00 with one year worth of data - however, I am unable to get right the date axis.我想创建一个具有每日季节性和开始日期 2018-01-01 00:00:00 的时间序列对象,其中包含一年的数据 - 但是,我无法正确定位日期轴。 I tried the following:
我尝试了以下方法:
my_ts = ts(load2[,4], frequency = 24, start= c(as.numeric(as.Date("2018-01-01")),1))
which seems to work:这似乎有效:
head(my_ts)
Time Series:
Start = c(17532, 1)
End = c(17532, 6)
Frequency = 24
load_MWH
[1,] 7594
[2,] 7091
[3,] 6652
[4,] 6308
[5,] 5972
[6,] 5810
But the time axis of the time series is a number (ie number of seconds since origin in 1970-01-01) but not a date format and therefore I can not make any operation with dates and autoplot shows the number but not the month / year:但是时间序列的时间轴是一个数字(即自 1970 年 1 月 1 日以来的秒数)但不是日期格式,因此我无法对日期进行任何操作,自动绘图显示数字但不显示月份/年:
> autoplot(energyireland2018_ts2[,1]) + scale_x_date(date_labels = "%b/%d")
Scale for 'x' is already present. Adding another scale for 'x', which will replace the
existing scale.
Error: Invalid input: date_trans works with objects of class Date only
Similarly, I cannot use any of the forecast package functions that manipulate dates.同样,我不能使用任何操纵日期的预测包函数。
So the question is: how can I convert the time axis of this time series into a Date object?所以问题是:如何将这个时间序列的时间轴转换为 Date 对象? (still using the forecast package).
(仍在使用预测包)。 Many thanks!
非常感谢!
The ts
class and the forecast package do not work well with hourly data. ts
类和预测包不适用于每小时数据。 I suggest you use the newer tsibble
class, for which there is an autoplot
function in the feasts package and forecasting functions in the fable package.我建议你使用较新的
tsibble
类,对其中有一个autoplot
在节日包装和预测功能,传说中的包的功能。 Here is an example using half-hourly electricity demand data.这是一个使用半小时电力需求数据的示例。
library(feasts)
library(tsibbledata)
vic_elec
#> # A tsibble: 52,608 x 5 [30m] <Australia/Melbourne>
#> Time Demand Temperature Date Holiday
#> <dttm> <dbl> <dbl> <date> <lgl>
#> 1 2012-01-01 00:00:00 4383. 21.4 2012-01-01 TRUE
#> 2 2012-01-01 00:30:00 4263. 21.0 2012-01-01 TRUE
#> 3 2012-01-01 01:00:00 4049. 20.7 2012-01-01 TRUE
#> 4 2012-01-01 01:30:00 3878. 20.6 2012-01-01 TRUE
#> 5 2012-01-01 02:00:00 4036. 20.4 2012-01-01 TRUE
#> 6 2012-01-01 02:30:00 3866. 20.2 2012-01-01 TRUE
#> 7 2012-01-01 03:00:00 3694. 20.1 2012-01-01 TRUE
#> 8 2012-01-01 03:30:00 3562. 19.6 2012-01-01 TRUE
#> 9 2012-01-01 04:00:00 3433. 19.1 2012-01-01 TRUE
#> 10 2012-01-01 04:30:00 3359. 19.0 2012-01-01 TRUE
#> # … with 52,598 more rows
autoplot(vic_elec, Demand)
Created on 2020-09-21 by the reprex package (v0.3.0)由reprex 包(v0.3.0) 于 2020 年 9 月 21 日创建
For information about the packages that handle these objects, see http://tidyverts.org .有关处理这些对象的包的信息,请参阅http://tidyverts.org 。
For a textbook introduction to forecasting models and associated syntax, see http://OTexts.com/fpp3有关预测模型和相关语法的教科书介绍,请参阅http://OTexts.com/fpp3
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.