sanbalo
sanbalo

Reputation: 1

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. 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 (i.e 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:

> 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? (still using the forecast package). Many thanks!

Upvotes: 0

Views: 863

Answers (1)

Rob Hyndman
Rob Hyndman

Reputation: 31810

The ts class and the forecast package do not work well with hourly data. 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. 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)

For information about the packages that handle these objects, see http://tidyverts.org.

For a textbook introduction to forecasting models and associated syntax, see http://OTexts.com/fpp3

Upvotes: 1

Related Questions