## ----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 weather data preparation or window-pane feature generation."
)

## ----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

weather |>
  slice_head(n = 6) |>
  knitr::kable()

assessments |>
  slice_head(n = 6) |>
  knitr::kable()

## -----------------------------------------------------------------------------
data_structure <- data.frame(
  table = c("weather", "assessments"),
  rows = c(nrow(weather), nrow(assessments)),
  unique_sites = c(n_distinct(weather$site_id), n_distinct(assessments$site_id))
)

knitr::kable(data_structure)

## -----------------------------------------------------------------------------
weather_check <- weather |>
  summarise(
    first_date = min(time),
    last_date = max(time),
    sites = n_distinct(site_id),
    missing_temp = sum(is.na(daily_mean_temp)),
    missing_rh = sum(is.na(daily_mean_rh))
  )

knitr::kable(weather_check)

## -----------------------------------------------------------------------------
hourly_weather <- window_pane_demo_data$weather_hourly

daily_from_hourly <- aggregate_weather_daily(
  weather = hourly_weather,
  id_col = "site_id",
  time_col = "time",
  weather_cols = c("temp", "rh", "rain", "leaf_wetness"),
  statistics = list(
    temp = c("mean", "min", "max"),
    rh = "mean",
    rain = list(sum = "sum"),
    leaf_wetness = list(sum = "sum")
  )
)

daily_from_hourly |>
  slice_head(n = 6) |>
  knitr::kable()

## -----------------------------------------------------------------------------
assessments |>
  select(site_id, planting_time, assessment_time, disease_intensity) |>
  slice_head(n = 8) |>
  knitr::kable()

## -----------------------------------------------------------------------------
windows <- make_windows(
  min_offset = -21,
  max_offset = 0,
  width = 7,
  slide_by = 1,
  reference_col = "assessment_time"
)

windows |>
  slice_head(n = 8) |>
  knitr::kable()

## -----------------------------------------------------------------------------
plot_window_pane(
  windows,
  max_windows = 12,
  color_by = "none",
  title = "Candidate windows before disease assessment",
  subtitle = "The same relative-time grid is applied to each site-specific assessment date",
  xlab = "Time relative to assessment date (days)",
  ylab = NULL
) +
  theme(axis.text.y = element_text(size = 8))

## -----------------------------------------------------------------------------
daily_weather_cols <- c(
  "daily_mean_temp",
  "daily_mean_rh",
  "daily_sum_rain",
  "daily_sum_leaf_wetness"
)

## -----------------------------------------------------------------------------
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(90)
  ),
  daily_sum_rain = list(
    total = "sum",
    rainy_days = rainy_days(0)
  ),
  daily_sum_leaf_wetness = list(
    wet_days = wet_days(0)
  )
)

## -----------------------------------------------------------------------------
first_site <- assessments$site_id[1]
first_reference <- assessments$assessment_time[1]

single_site_scan <- scan_windows(
  weather = weather |> filter(site_id == first_site),
  reference_time = first_reference,
  windows = windows,
  weather_cols = daily_weather_cols,
  statistics = summary_statistics
)

single_site_scan |>
  select(label, daily_mean_temp_mean, daily_mean_temp_days_18_26,
         daily_mean_rh_humid_days, daily_sum_rain_total) |>
  slice_head(n = 8) |>
  knitr::kable()

## -----------------------------------------------------------------------------
plot_weather <- weather |>
  filter(site_id == first_site) |>
  filter(time >= first_reference - 21 * 86400, time <= first_reference)

ggplot(plot_weather, aes(time, daily_mean_temp)) +
  geom_vline(
    xintercept = first_reference,
    linetype = "dashed",
    color = "#20262e",
    linewidth = 0.7
  ) +
  geom_line(color = "#2b6c4f", linewidth = 0.8) +
  labs(
    title = "Weather history before the reference date",
    subtitle = "The dashed line is the disease assessment date for one site",
    x = NULL,
    y = "Daily mean temperature (deg C)"
  ) +
  theme_half_open()

## -----------------------------------------------------------------------------
features <- window_pane(
  weather = weather,
  assessments = assessments,
  windows = windows,
  id_col = "site_id",
  reference_col = "assessment_time",
  response_col = "disease_intensity",
  weather_cols = daily_weather_cols,
  statistics = summary_statistics
)

feature_overview <- data.frame(
  rows = nrow(features),
  columns = ncol(features)
)

knitr::kable(feature_overview)

features |>
  select(1:10) |>
  slice_head(n = 6) |>
  knitr::kable()

## -----------------------------------------------------------------------------
screened <- screen_window_features(
  data = features,
  response_col = "disease_intensity",
  method = "spearman"
)

screened |>
  slice_head(n = 8) |>
  knitr::kable()

## -----------------------------------------------------------------------------
top_screened <- screened |>
  mutate(abs_estimate = abs(estimate)) |>
  arrange(desc(abs_estimate)) |>
  slice_head(n = 8) |>
  mutate(feature = factor(feature, levels = rev(feature)))

ggplot(top_screened, aes(abs_estimate, feature)) +
  geom_col(fill = "#6ea87d") +
  labs(
    title = "Top screened windows",
    x = "Absolute Spearman correlation",
    y = NULL
  ) +
  theme_half_open()

