## ----include = FALSE----------------------------------------------------------
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)

## ----setup--------------------------------------------------------------------
library(SporeLag)

## -----------------------------------------------------------------------------
str(pollen_demo)

## -----------------------------------------------------------------------------
table(pollen_demo$site)

## -----------------------------------------------------------------------------
tapply(is.na(pollen_demo$count), pollen_demo$site, sum)

## -----------------------------------------------------------------------------
north <- pollen_demo[pollen_demo$site == "North", ]
north[north$date >= as.Date("2024-03-15") & north$date <= as.Date("2024-03-25"), ]

## ----error = TRUE-------------------------------------------------------------
try({
apply_lag(pollen_demo, value = "count", lags = 1, date = "date", by = "site")
})

## -----------------------------------------------------------------------------
tryCatch(
  apply_lag(pollen_demo, value = "count", lags = 1, date = "date", by = "site"),
  sporelag_error_gaps = function(e) "gapped grid detected"
)

## -----------------------------------------------------------------------------
grid <- complete_daily_grid(pollen_demo, date = "date", by = "site")
table(grid$site)
sum(is.na(grid$count))

## -----------------------------------------------------------------------------
new_year <- data.frame(date = as.Date(c("2024-12-29", "2024-12-30", "2025-01-01")))
assign_iso_week(new_year, date = "date")

## -----------------------------------------------------------------------------
grid <- grid |>
  assign_iso_week(date = "date") |>
  assign_season(date = "date")

table(grid$season, grid$site)

## -----------------------------------------------------------------------------
custom <- assign_season(
  grid[, c("site", "date")],
  date = "date",
  definition = "custom",
  breaks = c(Dormant = "11-01", Tree = "02-15", Grass = "05-01", Weed = "08-01")
)
table(custom$season, custom$site)

## -----------------------------------------------------------------------------
grid <- impute_weekly_mean(grid, value = "count", by = "site")

grid[grid$site == "North" &
       grid$date >= as.Date("2024-03-16") & grid$date <= as.Date("2024-03-25"),
     c("date", "iso_week", "count", "count_imputed", "count_imputed_flag")]

## -----------------------------------------------------------------------------
strict <- impute_weekly_mean(
  grid[, c("site", "date", "count", "iso_week", "iso_year")],
  value = "count", by = "site", min_obs = 4
)
sum(strict$count_imputed_flag)
sum(is.na(strict$count_imputed))

## -----------------------------------------------------------------------------
tapply(grid$count_imputed_flag, grid$site, mean)

## -----------------------------------------------------------------------------
grid <- build_moving_average(
  grid, value = "count_imputed", window = c(3, 7), date = "date", by = "site"
)

## -----------------------------------------------------------------------------
raw_ma <- build_moving_average(grid[, c("site", "date", "count")],
                               value = "count", window = 7,
                               date = "date", by = "site")
sum(is.na(raw_ma$count_ma7))

raw_ma5 <- build_moving_average(grid[, c("site", "date", "count")],
                                value = "count", window = 7, min_obs = 5,
                                date = "date", by = "site")
sum(is.na(raw_ma5$count_ma7))

## -----------------------------------------------------------------------------
grid <- apply_lag(
  grid, value = "count_imputed", lags = 0:3, date = "date", by = "site"
)

## -----------------------------------------------------------------------------
south <- grid[grid$site == "South", ]
head(south[, c("site", "date", "count_imputed",
               "count_imputed_lag1", "count_imputed_lag3")], 4)

## -----------------------------------------------------------------------------
model_ready <- pollen_demo |>
  complete_daily_grid(date = "date", by = "site") |>
  assign_iso_week(date = "date") |>
  assign_season(date = "date") |>
  impute_weekly_mean(value = "count", by = "site") |>
  build_moving_average(value = "count_imputed", window = c(3, 7),
                       date = "date", by = "site") |>
  apply_lag(value = "count_imputed", lags = 0:3,
            date = "date", by = "site")

dim(model_ready)
names(model_ready)

