Error W/ a Tibble Data.Frame For Forecasting Regressions

Hello,

I'm trying to work on a seasonality forecasting project. In my example data below, I have 3 variables (A,B,C) beginning January 2016 to May 2020. I turn the dataset into a tibble data frame, I detrend the data using first differences and then use the TSLM function to regress the detrended values on a group of monthly dummies.

My problem is that I want to plot the forecasted values against the actual values, but I run into this error: Error: Can't proceed with the key of multiple variables. This error is throwing me off because two days ago I was able to execute my code without any troubles, but today I get the error. Any guidance on this error is much appreciated

My code w/ data:

library(Fable)
library(dplyr)
library(Fabletools)
library(Forecast)
library(timeseries)
`df<-data.frame(
A=c(27.9938, 28.0004,28.03154, 28.04374, 28.08446, 28.12364, 28.15138, 28.15892, 28.15968, 28.07314, 28.04036, 28.08632, 27.98332, 27.96234, 27.97982, 28.0106, 28.02084, 28.06482, 28.0602, 28.05052, 28.05138, 27.99212, 27.96588, 27.97364, 27.90562, 27.87026, 27.88142, 27.89572, 27.9344, 27.99952, 28.02304, 28.01158, 28.0443, 28.04298, 28.00724, 28.01248, 27.95378, 27.95702, 27.95918, 27.98758, 28.0348, 28.05396, 28.07982, 28.07998, 28.06708, 28.08386, 28.04272, 28.03444, 27.98222, 27.96958, 28.0002),
B=c(27.92668, 27.9936, 28.02364, 28.04648, 28.04492, 28.05294, 28.0191, 28.00362, 27.96778, 27.96118, 27.8963, 27.93334, 27.9451, 27.94296, 28.00206, 28.05464, 28.04646, 28.07908, 28.02212, 27.94902, 27.93564, 27.87986, 27.87676, 27.93026, 27.91306, 27.96704, 27.95872, 28.0064, 27.98354, 28.00128, 27.94422, 27.89578, 27.9424, 27.89796, 27.90042, 27.91856, 27.9423, 28.00996, 28.03854, 28.10654, 28.1248, 28.24094, 28.1972, 28.1662, 28.18706, 28.19138, 28.1637, 28.14834, 28.17378, 28.187, 28.22642),
C=c(27.7977, 27.94492, 27.89732, 27.7361, 27.9497, 27.80912, 27.8867, 27.8085, 27.96406, 27.96548, 27.99574, 27.93068, 27.95904, 27.96142, 28.17876, 28.16956, 28.14324, 28.0869, 28.17692, 28.15626, 27.83046, 28.02534, 28.10704, 28.2005, 27.981, 28.46818, 28.32172, 28.23436, 27.93638, 27.87202, 28.04238, 27.76178, 27.14638, 27.42408, 27.65992, 28.0452, 27.97926, 28.18418, 28.24784, 27.87126, 27.5862, 27.60312, 27.92416, 28.05302, 27.70676, 27.87256, 27.54304, 27.72288, 27.6785, 27.47676, 27.36466))

df <- df %>%
mutate(Month = yearmonth(seq(as.Date("2016-01-01"), by = "1 month", length = NROW(df))))%>% > pivot_longer(-Month, names_to = "Series", values_to = "value") %>%
as_tsibble(index = Month, key = Series)

df<-df%>%group_by(Series)%>%mutate(det=c(NA,diff(value)))

df<-df%>%as_tsibble(index = Month, key = Series)

train<-df%>%dplyr::filter(Month<=as.Date('2019-03-01'))

seas<-function(data, det){
seasonality=data%>%model(TSLM(det~season()))
}
x<-seas(data = train, det = det)

forag<-x%>%forecast(h=12,bias_adjust=TRUE)%>%filter(Series=="A")%>%autoplot(df)`

Remember that R is case sensitive, so library(Fable) does not work for the fable package. In the mutate pipeline you have %>%>. You do not need the forecast or timeseries packages, just the tsibble and fable from the tidyverts. It looks alright but I have a few suggested changes:

library(dplyr)      
library(tsibble)
library(fable)

df<-data.frame(
  A=c(27.9938, 28.0004,28.03154, 28.04374, 28.08446, 28.12364, 28.15138, 28.15892, 28.15968, 28.07314, 28.04036, 28.08632, 27.98332, 27.96234, 27.97982, 28.0106, 28.02084, 28.06482, 28.0602, 28.05052, 28.05138, 27.99212, 27.96588, 27.97364, 27.90562, 27.87026, 27.88142, 27.89572, 27.9344, 27.99952, 28.02304, 28.01158, 28.0443, 28.04298, 28.00724, 28.01248, 27.95378, 27.95702, 27.95918, 27.98758, 28.0348, 28.05396, 28.07982, 28.07998, 28.06708, 28.08386, 28.04272, 28.03444, 27.98222, 27.96958, 28.0002),
  B=c(27.92668, 27.9936, 28.02364, 28.04648, 28.04492, 28.05294, 28.0191, 28.00362, 27.96778, 27.96118, 27.8963, 27.93334, 27.9451, 27.94296, 28.00206, 28.05464, 28.04646, 28.07908, 28.02212, 27.94902, 27.93564, 27.87986, 27.87676, 27.93026, 27.91306, 27.96704, 27.95872, 28.0064, 27.98354, 28.00128, 27.94422, 27.89578, 27.9424, 27.89796, 27.90042, 27.91856, 27.9423, 28.00996, 28.03854, 28.10654, 28.1248, 28.24094, 28.1972, 28.1662, 28.18706, 28.19138, 28.1637, 28.14834, 28.17378, 28.187, 28.22642),
  C=c(27.7977, 27.94492, 27.89732, 27.7361, 27.9497, 27.80912, 27.8867, 27.8085, 27.96406, 27.96548, 27.99574, 27.93068, 27.95904, 27.96142, 28.17876, 28.16956, 28.14324, 28.0869, 28.17692, 28.15626, 27.83046, 28.02534, 28.10704, 28.2005, 27.981, 28.46818, 28.32172, 28.23436, 27.93638, 27.87202, 28.04238, 27.76178, 27.14638, 27.42408, 27.65992, 28.0452, 27.97926, 28.18418, 28.24784, 27.87126, 27.5862, 27.60312, 27.92416, 28.05302, 27.70676, 27.87256, 27.54304, 27.72288, 27.6785, 27.47676, 27.36466))

tsb <- df %>% 
  ts(start = c(2016,1), frequency = 12) %>%   # easier to convert to ts then to tsibble
  as_tsibble() %>%  
  rename(Month = index, Series = key) %>% 
  group_by(Series) %>% 
  mutate(det = difference(value)) %>%         # use difference() from the tsibble package
  ungroup()

train <- tsb %>% filter_index(~ "2019-03")    # use filter_index() from the tsibble package

x <- train %>% model(TSLM(det ~ season()))  

x %>% forecast(h=12) %>%      # bias_adjust deprecated for forecast(), see point_forecast
  filter(Series == "A") %>% 
  autoplot(tsb) 

Created on 2020-06-20 by the reprex package (v0.3.0)

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.