I'm struggling to regularize a time series using the tsibble package. The documentation indicates that this can be done using index_by()
+ summarise()
, but I'm clearly missing some details. Here's what I've tried:
library(tidyverse)
library(lubridate)
library(tsibble)
# example data set
date <- ymd(c("1976-05-18", "1976-05-19", "1976-05-24", "1976-06-01"))
fish <- c(203, 282, 301, 89)
volume <- c(210749, 287555, 378965, 308935)
n <- c(5, 7, 10, 8)
tbl <- tibble(date, fish, volume, n)
tsbl <- tsibble(tbl, index = date, regular = FALSE)
# regularize the tsibble (ie time series)
tsbl %>%
index_by(date, unit = "day") %>% # unit value "day" is intuitive but incorrect?
mutate(week = isoweek(date)) %>% # add (numeric) week column
summarise(date = date,
fish = sum(fish),
volume = sum(volume),
n = sum(n),
cpue = fish/volume) # calculate catch per unit effort
TIA!