Search code examples
rfable-rtsibble

Multiple time series in a long format tsibble


I want to run time series models to forecast one step ahead using fable package. As far I understand, I need to have my data in tsibble format. Here is what I am trying to do,

  • Generate three ids
  • Time stamps for those three ids
  • Three random series
  • Join it all as a tsibble
  • One month ahead forecast

So I want to make a tsibble first. To do that I am trying to create by using the following lines,

ts <- tsibble(
  ids = c(rep(43, 20), rep(33, 20), rep(11, 20)),
  timest = rep(yearmonth("2010 Jan") + 0:19, each = 3),
  data = rnorm(60, mean = 10, sd = 5),
  key = ids
)

Using this tsibble I want to run the following models,

fit <- ts %>%
  model(
    arima = ARIMA(data)
  )
fit

fc <- fit %>%
  forecast(h = "1 month")
fc

However, I am having problem in creating tsibble. I know that I cannot have duplicates but pointing key = ids should solve the problem. Anybody can help me to find the mistake I am doing?


Solution

  • You are almost there. Instead of timest = rep(yearmonth("2010 Jan") + 0:19, each = 3) you need timest = rep(yearmonth("2010 Jan") + 0:19, times = 3) The times were not aligned with the id's. each replicates the "2010 jan" 3 times in a row, instead of the whole input repeated 3 times. See the details with the help of rep ?rep

    library(tsibble)
    
    ts <- tsibble(
      ids = c(rep(43, 20), rep(33, 20), rep(11, 20)),
      timest = rep(yearmonth("2010 Jan") + 0:19, times = 3),
      data = rnorm(60, mean = 10, sd = 5),
      key = ids,
      index = timest
    )