Decompose xts hourly time series
Asked Answered
I

2

4

I want to decompose hourly time series with decompose, ets, or stl or whatever function. Here is an example code and its output:

require(xts)
require(forecast)
time_index1 <- seq(from = as.POSIXct("2012-05-15 07:00"),
                   to = as.POSIXct("2012-05-17 18:00"), by="hour")
head(time_index1 <- format(time_index1, format="%Y-%m-%d %H:%M:%S",
                           tz="UTC", usetz=TRUE)
# [1] "2012-05-15 05:00:00 UTC" "2012-05-15 06:00:00 UTC"
# [3] "2012-05-15 07:00:00 UTC" "2012-05-15 08:00:00 UTC"
# [5] "2012-05-15 09:00:00 UTC" "2012-05-15 10:00:00 UTC"
head(time_index <- as.POSIXct(time_index1))
# [1] "2012-05-15 05:00:00 CEST" "2012-05-15 06:00:00 CEST"
# [3] "2012-05-15 07:00:00 CEST" "2012-05-15 08:00:00 CEST"
# [5] "2012-05-15 09:00:00 CEST" "2012-05-15 10:00:00 CEST"

Why does the timezone for time_index change back to CEST?

set.seed(1)
value <- rnorm(n = length(time_index1))
eventdata1 <- xts(value, order.by = time_index)
tzone(eventdata1)
# [1] ""
head(index(eventdata1))
# [1] "2012-05-15 05:00:00 CEST" "2012-05-15 06:00:00 CEST"
# [3] "2012-05-15 07:00:00 CEST" "2012-05-15 08:00:00 CEST"
# [5] "2012-05-15 09:00:00 CEST" "2012-05-15 10:00:00 CEST"
ets(eventdata1)
# ETS(A,N,N) 
# 
# Call:
#  ets(y = eventdata1) 
# 
#   Smoothing parameters:
#     alpha = 1e-04 
# 
#   Initial states:
#     l = 0.1077 
# 
#   sigma:  0.8481
# 
#      AIC     AICc      BIC 
# 229.8835 230.0940 234.0722
decompose(eventdata1)
# Error in decompose(eventdata1) : 
#   time series has no or less than 2 periods
stl(eventdata1)
# Error in stl(eventdata1) : 
#   series is not periodic or has less than two periods

When I call tzone or indexTZ there is no timezone but the index clearly show that the times are defined with a timezone.

Also, why does only ets work? Can it be used to decompose a time series?

Idelia answered 23/7, 2015 at 9:54 Comment(0)
S
5

Why does the timezone for time_index change back to CEST?

Because you didn't specify tz= in your call to as.POSIXct. It will only pick up the timezone from the string if it's specified by offset from UTC (e.g. -0800). See ?strptime.

R> head(time_index <- as.POSIXct(time_index1, "UTC"))
[1] "2012-05-15 12:00:00 UTC" "2012-05-15 13:00:00 UTC"
[3] "2012-05-15 14:00:00 UTC" "2012-05-15 15:00:00 UTC"
[5] "2012-05-15 16:00:00 UTC" "2012-05-15 17:00:00 UTC"

When I call tzone or indexTZ there is no timezone but the index clearly show that the times are defined with a timezone.

All POSIXct objects have a timezone. A timezone of "" simply means R wasn't able to determine a specific timezone, so it is using the timezone specified by your operating system. See ?timezone.

Only the ets function works because your xts object doesn't have a properly defined frequency attribute. This is a known limitation of xts objects, and I have plans to address them over the next several months. You can work around the current issues by explicitly specifying the frequency attribute after calling the xts constructor.

R> set.seed(1)
R> value <- rnorm(n = length(time_index1))
R> eventdata1 <- xts(value, order.by = time_index)
R> attr(eventdata1, 'frequency') <- 24  # set frequency attribute
R> decompose(as.ts(eventdata1))  # decompose expects a 'ts' object
Sheryl answered 23/7, 2015 at 11:43 Comment(3)
Thanks for the answer Joshua :). Small question though. Is the frequency=24 a value to tell to the program that it is hourly values? when I plot the decomposition I get two things that I don't understand. The range of time values is from 1.0 to 3.5 -> what is this? and the trend and random curves have a smaller time range. Could you explain what is happening here please?Idelia
@SamyGeronymos: I don't understand the decomposition procedure, so I can't help with that part. That also means I chose to set frequency = 24 somewhat arbitrarily. You might try setting it to 1/24... but, again, I'm not an expert here.Sheryl
@JoshuaUlrich, see explanation of the frequency attribute here: robjhyndman.com/hyndsight/seasonal-periodsMandimandible
M
3

You can use tbats to decompose hourly data:

require(forecast)
set.seed(1)
time_index1 <- seq(from = as.POSIXct("2012-05-15 07:00"),
               to = as.POSIXct("2012-05-17 18:00"), by="hour")
value <- rnorm(n = length(time_index1))
eventdata1 <- msts(value, seasonal.periods = c(24) )
seasonaldecomp <- tbats(eventdata1)
plot(seasonaldecomp)

Additionally, using msts instead of xts allows you to specify multiple seasons/cycles, fore instance hourly as well as daily: c(24, 24*7)

Mandimandible answered 2/3, 2017 at 18:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.