Problem to fit ensemble models

Hi guys,
Actually I was following an article to simulate ensemble models with my data:
https://robjhyndman.com/seminars/nyrc2020/,
And I got an error :
Problem with 'summarise()' input 'n'.
x object of type 'closure' cannot be divided into subsets
i input 'n' is dist_mixture(n[1], n[2], weights = c(0.5, 0.5)).

My Code

ens_fit <- train %>%
model(
tslm = TSLM(n ~ trend() + season(period = 22)),
arima = ARIMA(n ~ trend() + season(period = 3), stepwise = TRUE)
) %>%
summarize(
n = dist_mixture(
n[1], n[2],
weights = c(0.5, 0.5))
)

Any advice will be appreciated,

Regards

Please provide a reprex, as described here: FAQ: What's a reproducible example (`reprex`) and how do I do one?

Sorry about my delayed answer, I was learning how to do it, follow reprex as requested:

library(forecast)
#> Warning: package 'forecast' was built under R version 3.6.2
#> Registered S3 method overwritten by 'quantmod':
#>   method            from
#>   as.zoo.data.frame zoo
library(tsibble)
#> Warning: package 'tsibble' was built under R version 3.6.2
library(fable)
#> Warning: package 'fable' was built under R version 3.6.2
#> Carregando pacotes exigidos: fabletools
#> Warning: package 'fabletools' was built under R version 3.6.2
#> 
#> Attaching package: 'fabletools'
#> The following objects are masked from 'package:forecast':
#> 
#>     accuracy, forecast
library(distributional)
#> Warning: package 'distributional' was built under R version 3.6.2


iniciativa <- tibble(
    data_planejada = sample(seq(as.Date("2020-01-01"), length=20, by="1 day"), size=20),
    n = sample(seq(100), size=20)
)


iniciativa <- iniciativa %>% as_tsibble()
#> Using `data_planejada` as index variable.

ens_fit <- iniciativa %>%
    model(
        tslm = TSLM(n ~ trend() + season(period = 22)),
        arima = ARIMA(n ~ trend() + season(period = 3), stepwise = TRUE)
    ) %>%
    summarize(
        n = dist_mixture(
            n[1], n[2],
            weights = c(0.5, 0.5))
    )
#> Error in summarize(., n = dist_mixture(n[1], n[2], weights = c(0.5, 0.5))): não foi possível encontrar a função "summarize"

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

Also, I would like to hear some advice about how to choose the best model given the metrics attached.
There is a standard about when some error is acceptable than other ?

Thanks for your help

  1. You don't need the forecast package here. It is not used and is designed to handle ts objects, not tsibble objects.
  2. The error says it can't find the function "summarize". That's because you haven't loaded the dplyr package.
  3. Even if you had loaded the dplyr package, you would get an error because you have missed the forecasting step.

Here is some code that works.

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

iniciativa <- tibble(
    data_planejada = seq(as.Date("2020-01-01"), length = 200, by = "1 day"),
    n = sample(seq(100), size = 200, replace = TRUE)
  ) %>%
  as_tsibble(index = data_planejada)

ens_fit <- iniciativa %>%
  model(
    tslm = TSLM(n ~ trend() + season(period = 22)),
    arima = ARIMA(n ~ trend() + season(period = 3), stepwise = TRUE)
  ) %>%
  forecast(h = 20) %>%
  summarize(
    n = dist_mixture(
      n[1], n[2],
      weights = c(0.5, 0.5)
    )
  )

ens_fit
#> # A fable: 20 x 2 [1D]
#>    data_planejada            n
#>    <date>               <dist>
#>  1 2020-07-19     mixture(n=2)
#>  2 2020-07-20     mixture(n=2)
#>  3 2020-07-21     mixture(n=2)
#>  4 2020-07-22     mixture(n=2)
#>  5 2020-07-23     mixture(n=2)
#>  6 2020-07-24     mixture(n=2)
#>  7 2020-07-25     mixture(n=2)
#>  8 2020-07-26     mixture(n=2)
#>  9 2020-07-27     mixture(n=2)
#> 10 2020-07-28     mixture(n=2)
#> 11 2020-07-29     mixture(n=2)
#> 12 2020-07-30     mixture(n=2)
#> 13 2020-07-31     mixture(n=2)
#> 14 2020-08-01     mixture(n=2)
#> 15 2020-08-02     mixture(n=2)
#> 16 2020-08-03     mixture(n=2)
#> 17 2020-08-04     mixture(n=2)
#> 18 2020-08-05     mixture(n=2)
#> 19 2020-08-06     mixture(n=2)
#> 20 2020-08-07     mixture(n=2)

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

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

If you have a query related to it or one of the replies, start a new topic and refer back with a link.