tidyverts / fabletools

General fable features useful for extension packages
http://fabletools.tidyverts.org/
89 stars 31 forks source link

Augment() fails #399

Closed USMortality closed 3 months ago

USMortality commented 3 months ago

Not really sure why the agument function fails here:

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

y <- c(878.6, 866.4, 864.9, 1017.8, 1029.5, 961.5, 896.2)
h <- 2

z <- length(y) - h

df_bl <- tibble(x = seq.int(1, length(y)), y = y) |> as_tsibble(index = x)
# Split data into training and test
df_train <- df_bl |> filter(x <= z)
df_test <- df_bl |> filter(x > z)

df_train |>
  as_tsibble(index = x) |>
  model(lm = TSLM(y ~ trend())) |>
  augment()
Error in `mutate()`:
ℹ In argument: `dplyr::across(...)`.
Caused by error in `across()`:
! Can't compute column `lm`.
Caused by error in `mutate()`:
ℹ In argument: `.fitted = fitted(x, ...)[[".fitted"]]`.
Caused by error in `object$fitted`:
! $ operator is invalid for atomic vectors
Run `rlang::last_trace()` to see where the error occurred.

Just plotting works:

df_train |>
  as_tsibble(index = x) |>
  model(lm = TSLM(y ~ trend())) |>
  forecast(h = h) |>
  autoplot(bind_rows(df_train, df_test))
USMortality commented 3 months ago

Looks like this may have todo with the column naming. Replacing x/y with something more arbitrary works. Probably an internation collision.

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

y <- c(878.6, 866.4, 864.9, 1017.8, 1029.5, 961.5, 896.2)
h <- 2

z <- length(y) - h

df_bl <- tibble(year = seq.int(1, length(y)), asr = y) |> as_tsibble(index = year)
# Split data into training and test
df_train <- df_bl |> filter(year <= z)
df_test <- df_bl |> filter(year > z)

df_train |>
  as_tsibble(index = year) |>
  model(lm = TSLM(asr ~ trend())) |>
  augment()