rlinear-regressionforecastingfable

Why are forecast values slightly lower with `fable::TSLM()` than `stats::lm()`?


I am doing some work which involves modelling values over time, and in the interests of clarity I would like to use the fable package to do this. I want to create a linear model over time with a log transformation - however, I have found that the values generated by fable::TSLM() are in some cases significantly different from those generated by stats::lm(), which has been previously used in the model. It's possible that this issue is caused by my incorrect use of fable functions, however it may also be a bug in the package. The following reprex illustrates my issue:

library(tsibble)
library(fable)
library(dplyr)
library(tidyr) # Not essential
library(ggplot2) # Not essential

# Create a toy dataset
test_data <- tsibble(
  Month = yearmonth("2020 Jan") + 0:11,
  
  # Month_Number will be used to fit a `stats` style model
  Month_Number = 1:12,
  Value = c(100, 95, 91, 75, 89, 85, 82, 75, 62, 57, 58, 50),
  index = Month
)

# Create a `fable` style model
fable_model <- test_data %>% 
  fabletools::model(m = TSLM(log(Value) ~ trend()))

# Generate modelled values using `fable`
modelled_values <- fable_model %>% 
  augment() %>% 
  mutate(Type = "Modelled") %>% 
  rename(Fable_Model = .fitted, Actual_Value = Value) %>% 
  select(-.resid) %>% 
  as_tsibble()

# generate forecasted values using `fable`
future_values <- fable_model %>% 
  forecast(h = 12, point_forecast = list(Fable_Model = mean)) %>% 
  mutate(Type = "Forecast") %>% 
  as_tsibble() %>% 
  select(-Value)

# Generate a `stats` style model
exp_model <- lm(log(Value) ~ Month_Number, data = test_data)

# Bind the modelled and forecast `fable` values together
all_values <- bind_rows(modelled_values, future_values) %>%
  
  # Mutate a column of `stats` predicted values
  mutate(Stats_Model = exp(predict(exp_model, newdata = tibble(Month_Number = 1:24))))

# Check out the mean difference in predictions - these are negligible for modelled values but are
# much more significant for forecasted values. 
all_values %>% 
  as_tibble() %>% 
  group_by(Type) %>% 
  summarise(Mean_Difference = mean(abs(Fable_Model - Stats_Model)), .groups = "drop")
#> # A tibble: 2 x 2
#>   Type     Mean_Difference
#>   <chr>              <dbl>
#> 1 Forecast        2.91e- 1
#> 2 Modelled        3.79e-14

# Can also visualise the differences with this code:
all_values %>% 
  pivot_longer(c(Actual_Value, Fable_Model, Stats_Model), names_to = "Series", values_to = "Value") %>% 
  ggplot(aes(x = as_date(Month), y = Value, colour = Series)) +
  geom_line()

Created on 2020-12-10 by the reprex package (v0.3.0)

Solution

  • As stated on link there is a little correction with transformed data in the package fable to produce means instead of medians.

    I think it comes from this, as you use a log transformation that modifies the residual law.

    Note that if you use point_forecast = list(Fable_Model = median) both models give the same results.

    So I guess fable is right