Wednesday, April 10, 2024

Interpolating Values in a Tsibble

 I don't know why the documentation I ran into wasn't just a lot easier, but there's a few steps you need to take.  We'll use the gafa_stock data from the tsibbledata package.


google_stock <- gafa_stock |>
  filter(Symbol == "GOOG", year(Date) >= 2015) %>%
  update_tsibble(regular = TRUE) %>%
  fill_gaps() %>%
  mutate(Close = na_interpolation(Close),
         Volume = na_interpolation(Volume))

The enabling part of this is to make sure your tsibble knows the periods are regular.  At first it does not:

> google_stock
# A tsibble: 1,006 x 4 [!]
# Key:       Symbol [1]

 In the case above fill_gaps() won't have anything to do because the periodicity is not known:  [!], which is easily fixed by update_tsibble( ...., regular = TRUE):

 

> google_stock
# A tsibble: 1,006 x 4 [1D]
# Key:       Symbol [1]

 You can also use scan_gaps() to learn what fill_gaps() is going to do. If you get no rows you know fill_gaps() won't work either.

Once that is done there are two steps required:

  1. fill_gaps() adds missing dates, leaving non-key values as NA
  2. na_interpolation() fills in the NA values of a column. 

Now that you know the tricks to making this work you can pick any number of na_* methods to fill in the missing values.