Fit a Mean forecasting model using tsibble, fable in R

432 views Asked by At

(Using Orange dataset from library(Ecdat) for reproducibility.)

I am trying to fit a mean forecasting model in R using tsibble, fable package in R. The code below is pretty simple, however I get the error Error in NCOL(x) : object 'value' not found when I try to run the last model part (even though value is a column name in o_ts), not sure why would that be. I am following RJH tutorials from here (https://robjhyndman.com/hyndsight/fable/).

I would also appreciate any help whether arima & mean forecasting model are same, if not what is the function that I should be using instead of Arima.

library(Ecdat)
library(tsibble)
library(feasts)
library(tidyverse)
library(fable)

o<- Orange 

o_ts <- o %>% as_tsibble()

o_ts %>%
  filter(key=="priceoj") %>% 
  model(
    arima=arima(value))
2

There are 2 answers

3
Paul On

arima is from the stats package. I believe you want ARIMA from fable.

o_ts %>%
  filter(key == "priceoj") %>% 
  model(
    arima = ARIMA(value)
  )
#> # A mable: 1 x 2
#> # Key:     key [1]
#>   key                         arima
#>   <chr>                     <model>
#> 1 priceoj <ARIMA(1,1,0)(0,0,1)[12]>
0
Steffen Jensen On

If you by mean forecasting model are referring to taking the mean of the last X observation (Moving Average), then you should be using MEAN.
While ARIMA does refer to Moving Average (Auto Regressive Integrated Moving Average), however this refers to a weighted moving average of the forecast errors - you can read more here: 9.4 Moving average models in Forecasting: Principles and Practice

o <- Orange 

o_ts <- o %>% as_tsibble()

o_ts %>%
  filter(key == "priceoj") %>% 
  model(mean = MEAN(value))

If you want to specify the amount of observations to take the mean of, then you need to add the special ~window(size = X). Otherwise all observations are used.

o_ts %>%
  filter(key == "priceoj") %>% 
  model(mean = MEAN(value ~ window(size = 3)))