robjhyndman / vital

Tidy Analysis Tools for Mortality, Fertility, Migration and Population Data
https://pkg.robjhyndman.com/vital
27 stars 2 forks source link

augment.mdl_vtl_ts() jumbles rows when adding .fitted #39

Closed MilesMcBain closed 3 days ago

MilesMcBain commented 4 days ago

It's a similar pathology to #37, so I at least knew what to look for. We see:

library(vital)
#> Registered S3 method overwritten by 'tsibble':
#>   method               from 
#>   as_tibble.grouped_df dplyr
library(dplyr)
#> Warning: package 'dplyr' was built under R version 4.4.1
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union
library(ggplot2)
#> Warning: package 'ggplot2' was built under R version 4.4.1
functional_data_model <-
  aus_fertility |>
  smooth_fertility(.var = Fertility) |>
  model(functional_asfr = FDM(log(.smooth)))

functional_data_model |>
  augment() |>
  ggplot(aes(
    x = Age,
    y = .response,
    colour = Year,
    group = Year
  )) +
  geom_line()


functional_data_model |>
  augment() |>
  ggplot(aes(
    x = Age,
    y = .fitted,
    colour = Year,
    group = Year
  )) +
  geom_line()

Created on 2024-11-21 with reprex v2.1.1

The issue is introduced here: https://github.com/robjhyndman/vital/blob/313d7be839a3cbb3597ccd7ff71a96354da9d17b/R/augment.R#L19-L24

Since response(x) is arranged by Age then Year, and x$fit$fitted is Year then Age.

A join instead of mutate would fix it, but you have a bit of a difficulty in that this approach won't produce a valid Tsibble. This is because response(x) has no key - it should be Age but that is dropped over in model.vital(). So possibly this is a difficult case created by working with single gendered data?

Have a PR coming for you that box-steps around this, but I wonder should the Age key get poked back in after fitting in model.vital?

robjhyndman commented 3 days ago

Fixed in #43