time series interval and forecasting in R

I have a dataset from 27-Oct-2015 to 24-Feb-2016, with time interval of 1 minute. Data zz as below:

Details of zz data can be found in this link: https://drive.google.com/file/d/1IzoIpyiedh_33UJ5SbdrwJTj6cEGSqtZ/view?usp=sharing

     ,dataid,meter_value
2015-10-27 19:50:00,1103,183138
2015-10-27 19:51:00,1103,183138
2015-10-27 19:52:00,1103,183138
:
2016-02-24 00:38:00,1103,197015.985697259
2016-02-24 00:39:00,1103,197015.992848629
2016-02-24 00:40:00,1103,197016

I created timeseries from 2015-10-27 19:50:00 to 2015-12-31 23:59:00 with frequency interval=10080, to forecast the meter_value from 2016-01-01 00:00:00 to 2016-02-24 00:40:00, and compare with my real data. My timeseries of R code as below:

I tried using different frequency value such as 60, 1440, 525960, but I still get same result as frequency value=10080

mytsTT <- ts(zz$meter_value, start = c(2015,10,27,19,50), end = c(2015,12,31,23,59), frequency = 10080)

    str(mytsTT)
#Time-Series [1:3] from 2015 to 2015: 183138 183138 183138
# - attr(*, "index")= POSIXct[1:3], format: "2015-10-27 19:50:00" ...
# - attr(*, "frequency")= num 0.0167

I updated mytsTT according to nwerth's code, but my forecasting points are weird.

I was trying to forecasting meter_value from "2016-01-01 00:00:00 to 2016-02-24 00:40:00" and compare with real data but my forecasting points are not correct.

When i convert two forecasting points, I got this output which is not correct. I expected to get forecasting points "2016-01-01 00:00:00 to 2016-02-24 00:40:00" but what i got for forecasting points are from 93851 to 94850

> as.POSIXct.Date(94850)
[1] "2229-09-10 08:00:00 +08"
> as.POSIXct.Date(93851)
[1] "2226-12-16 08:00:00 +08"
start_time <- as.POSIXct("2015-10-27 19:50")
end_time   <- as.POSIXct("2015-12-31 23:59")
mytsTT2 <- ts(zoo(
  zz$meter_value,
  order.by = seq.POSIXt(start_time, end_time, by = "mins"),
  frequency = 10080
))
str(mytsTT2)
# Time-Series [1:93850] from 1 to 93850: 183138 183138 183138 183138 183138 ...
# - attr(*, "index")= POSIXct[1:93850], format: "2015-10-27 19:50:00" ...
# - attr(*, "frequency")= num 10080

Forecasting R-code:

autoplot(forecast(mytsTT2,1000))
write.csv(forecast(mytsTT2,1000),"D1103-mytsTT2-1000fc.csv",quote=F)

Original data and forecasting data

First, ts only accepts one- or two-length vectors for start and end. From the docs:

start the time of the first observation. Either a single number or a vector of two integers, which specify a natural time unit and a (1-based) number of samples into the time unit. See the examples for the use of the second form.
end the time of the last observation, specified in the same way as start .

Second, I'm having trouble getting ts to work nicely for this. So I'll show how to make it with the zoo package:

library(zoo)
start_time <- as.POSIXct("2015-10-27 19:50")
end_time   <- as.POSIXct("2015-12-31 23:59")
mytsTT <- zoo(
  zz[["meter_value"]],
  order.by = seq.POSIXt(start_time, end_time, by = "mins"),
  frequency = 10080
)
str(mytsTT)
# ‘zooreg’ series from 2015-10-27 19:50:00 to 2015-12-31 23:59:00
#   Data: num [1:93910] 1 1 1 1 1 1 1 1 1 1 ...
#   Index:  POSIXct[1:93910], format: "2015-10-27 19:50:00" "2015-10-27 19:51:00" ...
#   Frequency: 10080 
3 Likes

Hi Newrth,
Thanks for your code. As I got error when I run your code, so I amended abit so that I can do forecasting, but my forecasting output is not correct. When I convert the first forecast point
93851 to date and time, it shows weird date and time.

> as.POSIXct.Date(93851)
[1] "2226-12-16 08:00:00 +08"

I supposed to get forecast point from 2016-01-01 00:00 to 2016-02-24 00:40:00.
How should I convert those forecast points?
How should I change my X-axis to date & time?

Could you help me how should I get correct forecasting code?

My R-code as below:

start_time <- as.POSIXct("2015-10-27 19:50")
end_time   <- as.POSIXct("2015-12-31 23:59")
mytsTT2 <- ts(zoo(
  zz$meter_value,
  order.by = seq.POSIXt(start_time, end_time, by = "mins"),
  frequency = 10080
))
# Time-Series [1:93850] from 1 to 93850: 183138 183138 183138 183138 183138 ...
# - attr(*, "index")= POSIXct[1:93850], format: "2015-10-27 19:50:00" ...
# - attr(*, "frequency")= num 10080

My forecasting R-code as below:

forecast(mytsTT2,1000)     
# output of forecast(mytsTT2) using ETS as below:

autoplot(forecast(mytsTT2,1000))
image

I got error when I run this line "zz[["meter_value"]],

Error in zz[["meter_value"]] : subscript out of bounds

how should I fix it? or is there any alternate solution?

The problem comes from this chunk:

mytsTT2 <- ts(zoo(
  zz$meter_value,
  order.by = seq.POSIXt(start_time, end_time, by = "mins"),
  frequency = 10080
))

The call to ts() is only given the zoo series, which it takes as the data argument. But it's not given a start or end, so it uses the default: values from 1 to the length of the series. Those aren't times, they're indices. The ts class doesn't actually hold datetime values.

You could use these indices to determine the time. I'm not having much luck myself, probably because handling time can be tricky (in this case, daylight savings time).