tidyverts / tidyverts/fabletools

Mutate() issue - Hierarchical Forecasting

Open
#306 6 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
R
Stars
98
Forks
37
PR merge metrics
No merged PRs in 30d

Description

Hi, I am reposting this issue on GitHub, with a more complete example, as I suspect it might not be related to the data being used or code mistakes.

I am trying to perform Hierarchical Forecasting on a dataset that is fundamentally structured in the same way as the tourism tsibble referenced in Forecasting: Principles and Practice, but with more hierarchical levels. However, after the structural aggregation, a mutate() error shows up.
The data doesn't contain any missing values.

Following, you will find a reprex of the code, containing a minimal version of the data used that is able to reproduce the error.

Thanks in advance.

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

t_london <- tibble::tribble(
  ~Month,             ~Value.type,   ~LSOA11CD,             ~LSOA11NM,     ~WD19CD,      ~WD19NM,    ~LAD19CD,         ~LAD19NM,           ~CTYNM, ~RGN19NM, ~CNTY21NM, ~NTN21NM, ~Count,
  "2010 Dec", "Value-Type-1         ", "E01000001", "City of London 001A", "E05009288", "Aldersgate", "E09000001", "City of London", "City Of London", "London", "England",     "UK",     2L,
  "2011 Jan", "Value-Type-1         ", "E01000001", "City of London 001A", "E05009288", "Aldersgate", "E09000001", "City of London", "City Of London", "London", "England",     "UK",     2L,
  "2011 Feb", "Value-Type-1         ", "E01000001", "City of London 001A", "E05009288", "Aldersgate", "E09000001", "City of London", "City Of London", "London", "England",     "UK",     3L,
  "2011 Mar", "Value-Type-1         ", "E01000001", "City of London 001A", "E05009288", "Aldersgate", "E09000001", "City of London", "City Of London", "London", "England",     "UK",     2L,
  "2011 Apr", "Value-Type-1         ", "E01000001", "City of London 001A", "E05009288", "Aldersgate", "E09000001", "City of London", "City Of London", "London", "England",     "UK",     0L,
  "2011 May", "Value-Type-1         ", "E01000001", "City of London 001A", "E05009288", "Aldersgate", "E09000001", "City of London", "City Of London", "London", "England",     "UK",     2L,
  "2011 Jun", "Value-Type-1         ", "E01000001", "City of London 001A", "E05009288", "Aldersgate", "E09000001", "City of London", "City Of London", "London", "England",     "UK",     4L,
  "2011 Jul", "Value-Type-1         ", "E01000001", "City of London 001A", "E05009288", "Aldersgate", "E09000001", "City of London", "City Of London", "London", "England",     "UK",     3L,
  "2011 Aug", "Value-Type-1         ", "E01000001", "City of London 001A", "E05009288", "Aldersgate", "E09000001", "City of London", "City Of London", "London", "England",     "UK",     2L,
  "2011 Sep", "Value-Type-1         ", "E01000001", "City of London 001A", "E05009288", "Aldersgate", "E09000001", "City of London", "City Of London", "London", "England",     "UK",     0L,
  "2011 Oct", "Value-Type-1         ", "E01000001", "City of London 001A", "E05009288", "Aldersgate", "E09000001", "City of London", "City Of London", "London", "England",     "UK",     1L,
  "2011 Nov", "Value-Type-1         ", "E01000001", "City of London 001A", "E05009288", "Aldersgate", "E09000001", "City of London", "City Of London", "London", "England",     "UK",     1L,
  "2011 Dec", "Value-Type-1         ", "E01000001", "City of London 001A", "E05009288", "Aldersgate", "E09000001", "City of London", "City Of London", "London", "England",     "UK",     6L
)

t_london <- t_london  %>%
mutate(Month = yearmonth(Month)) %>%
  as_tsibble(key = c(LSOA11CD, Value.type), index=Month)

london_full <- t_london %>% aggregate_key((NTN21NM/ CNTY21NM / RGN19NM / CTYNM / LAD19NM / WD19NM /LSOA11NM) * Value.type, Total = sum(Count))

fit <- london_full %>%
  model(base = ARIMA(Total)) %>%
  reconcile(
    bu = bottom_up(base),
    ols = min_trace(base, method = "ols"),
    mint = min_trace(base, method = "mint_shrink"),
  )
#> Warning in max(which(abs(ma) > 1e-08)): no non-missing arguments to max;
#> returning -Inf

#> Warning: 16 errors (1 unique) encountered for base
#> [16] argument must be coercible to non-negative integer

fc <- fit %>%
  forecast(h = 5)
#> Warning: Problem with `mutate()` input `mint`.
#> ℹ diag(.) had 0 or NA entries; non-finite result is doubtful
#> ℹ Input `mint` is `(function (object, ...) ...`.
#> Warning: Problem with `mutate()` input `mint`.
#> ℹ diag(.) had 0 or NA entries; non-finite result is doubtful
#> ℹ Input `mint` is `(function (object, ...) ...`.
#> Error: Problem with `mutate()` input `mint`.
#> x infinite or missing values in 'x'
#> ℹ Input `mint` is `(function (object, ...) ...`.

Created on 2021-02-10 by the reprex package (v0.3.0)

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the supplied R reprex, especially aggregate_key(), reconcile(), and forecast(h = 5), and reproduce the mutate() failure and preceding ARIMA warnings. Trace the mint reconciliation path and add a regression test for this hierarchical structure; done means forecasting completes without the reported non-finite-value error.

Written by the indexing model from the issue text.

Assessment

Tech stack
r
Domain
data
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.