## ----setup, include=FALSE-----------------------------------------------------
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>",
  message = FALSE,
  warning = FALSE,
  fig.width = 7,
  fig.height = 4.5,
  fig.alt = "ggplot2 chart showing multivariable disease-favorable weather conditions."
)

## ----packages, message=FALSE, warning=FALSE-----------------------------------
library(windcut)
library(dplyr)
library(ggplot2)
library(tidyr)
library(cowplot)

## -----------------------------------------------------------------------------
data(window_pane_demo_data)

weather <- window_pane_demo_data$weather
assessments <- window_pane_demo_data$assessments

knitr::kable(head(weather))

## -----------------------------------------------------------------------------
example_site <- assessments$site_id[1]
example_reference <- assessments %>%
  filter(site_id == example_site) %>%
  pull(assessment_time)

condition_weather <- weather %>%
  filter(site_id == example_site) %>%
  filter(time >= example_reference - 35 * 86400, time <= example_reference) %>%
  mutate(
    favorable = daily_mean_temp >= 18 &
      daily_mean_temp <= 26 &
      daily_mean_rh >= 85,
    wet_and_warm = daily_sum_leaf_wetness > 0 &
      daily_mean_temp >= 18 &
      daily_mean_temp <= 26
  )

ggplot(condition_weather, aes(time, daily_mean_temp)) +
  geom_vline(
    xintercept = example_reference,
    linetype = "dashed",
    color = "#20262e",
    linewidth = 0.7
  ) +
  geom_ribbon(aes(ymin = 18, ymax = 26), fill = "#6ea87d", alpha = 0.12) +
  geom_line(color = "#20262e", linewidth = 0.8) +
  geom_point(
    data = condition_weather %>% filter(favorable),
    color = "#c47f2c",
    size = 2.6
  ) +
  labs(
    title = "A multivariable condition is a biological rule applied row by row",
    subtitle = "Orange points satisfy temperature and humidity requirements",
    x = NULL,
    y = "Daily mean temperature (deg C)"
  ) +
  theme_half_open()

## -----------------------------------------------------------------------------
condition_timeline <- condition_weather %>%
  select(time, favorable, wet_and_warm) %>%
  pivot_longer(
    cols = c(favorable, wet_and_warm),
    names_to = "condition",
    values_to = "present"
  )

ggplot(condition_timeline, aes(time, condition, fill = present)) +
  geom_tile(color = "white", linewidth = 0.5, height = 0.8) +
  scale_fill_manual(values = c("FALSE" = "#e8ecef", "TRUE" = "#2b6c4f")) +
  labs(
    title = "Condition timelines reveal duration and interruption",
    x = NULL,
    y = NULL,
    fill = "Condition met"
  ) +
  theme_half_open() +
  theme(panel.grid = element_blank())

## -----------------------------------------------------------------------------
summary_statistics <- list(
  daily_mean_temp = list(
    mean = "mean",
    days_18_26 = count_between(18, 26)
  ),
  daily_mean_rh = list(
    mean = "mean",
    humid_days = humid_hours(85)
  ),
  daily_sum_rain = list(
    total = "sum"
  ),
  daily_sum_leaf_wetness = list(
    wet_days = wet_days(0)
  ),
  .conditions = list(
    favorable_days = count_when(
      daily_mean_temp >= 18 & daily_mean_temp <= 26 & daily_mean_rh >= 85
    ),
    favorable_spell = max_consecutive_when(
      daily_sum_leaf_wetness > 0 & daily_mean_temp >= 18 & daily_mean_temp <= 26
    ),
    warm_rain_total = sum_when(
      daily_sum_rain,
      daily_mean_temp >= 18 & daily_mean_temp <= 28
    )
  )
)

## -----------------------------------------------------------------------------
windows <- make_windows(
  min_offset = -35,
  max_offset = 0,
  width = 7,
  slide_by = 7,
  reference_col = "assessment_time"
)

features <- window_pane(
  weather = weather,
  assessments = assessments,
  windows = windows,
  reference_col = "assessment_time",
  id_col = "site_id",
  response_col = "disease_intensity",
  unit = "days",
  weather_cols = c(
    "daily_mean_temp",
    "daily_mean_rh",
    "daily_sum_rain",
    "daily_sum_leaf_wetness"
  ),
  statistics = summary_statistics
)

features %>%
  select(site_id, disease_intensity, contains("favorable"), contains("warm_rain")) %>%
  slice_head(n = 6) %>%
  knitr::kable()

## -----------------------------------------------------------------------------
condition_examples <- features |>
  select(site_id, contains("favorable"), contains("warm_rain")) |>
  slice_head(n = 6)

knitr::kable(condition_examples)

## -----------------------------------------------------------------------------
condition_features <- features |>
  filter(site_id == example_site) |>
  select(contains("favorable_days"), contains("favorable_spell"), contains("warm_rain_total")) |>
  pivot_longer(
    cols = everything(),
    names_to = "feature",
    values_to = "value"
  ) |>
  mutate(
    window = sub(".*_window_", "window_", feature),
    summary_class = case_when(
      grepl("favorable_days", feature) ~ "Count of favorable days",
      grepl("favorable_spell", feature) ~ "Longest favorable spell",
      grepl("warm_rain_total", feature) ~ "Rain during warm days",
      TRUE ~ "Condition summary"
    ),
    window = factor(window, levels = unique(window))
  )

ggplot(condition_features, aes(window, value, fill = summary_class)) +
  geom_col(show.legend = FALSE) +
  facet_wrap(~ summary_class, scales = "free_y", ncol = 1) +
  labs(
    title = "Condition functions summarize different biological quantities",
    subtitle = "Example values for one site across 7-day candidate windows",
    x = "Candidate window",
    y = "Summary value"
  ) +
  theme_half_open() +
  theme(axis.text.x = element_text(angle = 35, hjust = 1))

