[英]How to set up start date in time series in with hourly data
我有一個時間序列,它是愛爾蘭的每小時電力負荷。 這是數據框:
> 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
我想創建一個具有每日季節性和開始日期 2018-01-01 00:00:00 的時間序列對象,其中包含一年的數據 - 但是,我無法正確定位日期軸。 我嘗試了以下方法:
my_ts = ts(load2[,4], frequency = 24, start= c(as.numeric(as.Date("2018-01-01")),1))
這似乎有效:
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
但是時間序列的時間軸是一個數字(即自 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
同樣,我不能使用任何操縱日期的預測包函數。
所以問題是:如何將這個時間序列的時間軸轉換為 Date 對象? (仍在使用預測包)。 非常感謝!
ts
類和預測包不適用於每小時數據。 我建議你使用較新的tsibble
類,對其中有一個autoplot
在節日包裝和預測功能,傳說中的包的功能。 這是一個使用半小時電力需求數據的示例。
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)
由reprex 包(v0.3.0) 於 2020 年 9 月 21 日創建
有關處理這些對象的包的信息,請參閱http://tidyverts.org 。
有關預測模型和相關語法的教科書介紹,請參閱http://OTexts.com/fpp3
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.