## ----setup, include=FALSE-----------------------------------------------------
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>",
  message = FALSE,
  warning = FALSE,
  fig.width = 7,
  fig.height = 4.8,
  dev.args = list(bg = "white"),
  fig.alt = "ggplot2 chart from the functional data analysis workflow."
)

## ----packages, message=FALSE, warning=FALSE-----------------------------------
library(windcut)
library(dplyr)
library(ggplot2)
library(cowplot)

## -----------------------------------------------------------------------------
data(fda_demo_data)

weather_daily <- fda_demo_data$weather_daily
assessments <- fda_demo_data$assessments
variable_specs <- fda_demo_data$variable_specs

knitr::kable(head(weather_daily))
knitr::kable(head(assessments))
knitr::kable(variable_specs)

## -----------------------------------------------------------------------------
subject_data <- weather_daily %>%
  left_join(assessments %>% select(site_id, wm_class), by = "site_id")

knitr::kable(
  subject_data %>%
    select(site_id, dap, daily_mean_rh, daily_sum_rain, daily_mean_leaf_wetness, wm_class) %>%
    slice_head(n = 6)
)

## -----------------------------------------------------------------------------
class_counts <- assessments %>%
  count(wm_class, name = "n_sites")

knitr::kable(class_counts)

## -----------------------------------------------------------------------------
fda_fit <- run_fda_analysis(
  data = subject_data,
  id_col = "site_id",
  time_col = "dap",
  group_col = "wm_class",
  value_cols = variable_specs$variable,
  value_labels = variable_specs,
  smooth_method = "spline",
  smooth_args = list(spar = 0.65),
  alpha = c(0.05, 0.01),
  n_permutations = 200,
  min_interval_length = 5
)

## -----------------------------------------------------------------------------
knitr::kable(fda_fit$interval_summary)

## -----------------------------------------------------------------------------
plot_fda_means(
  fda_fit,
  value_col = "daily_mean_rh",
  xlab = "Days after planting",
  ylab = "Mean relative humidity (%)",
  legend_title = "White mold class",
  palette = c("wm-" = "#2b6c4f", "wm+" = "#c47f2c")
)

## -----------------------------------------------------------------------------
plot_fda_difference(
  fda_fit,
  value_col = "daily_mean_rh",
  xlab = "Days after planting",
  ylab = "wm+ minus wm-"
)

## -----------------------------------------------------------------------------
plot_fda_p_values(
  fda_fit,
  value_col = "daily_mean_rh",
  alpha = c(0.05, 0.01),
  xlab = "Days after planting"
)

## -----------------------------------------------------------------------------
plot_fda_intervals(
  fda_fit,
  title = "Retained FDA intervals by variable",
  xlab = "Days relative to planting"
)

## -----------------------------------------------------------------------------
rh_model <- fit_fda_group_model(
  fda_fit,
  value_col = "daily_mean_rh",
  bs.yindex = list(bs = "ps", k = 20, m = c(2, 1)),
  bs.int = list(bs = "ps", k = 20, m = c(2, 1))
)

## -----------------------------------------------------------------------------
plot_functional_on_scalar(
  rh_model,
  title = "Function-on-scalar coefficient function for relative humidity",
  xlab = "Days after planting",
  ylab = "Coefficient function"
)

## -----------------------------------------------------------------------------
fda_features <- extract_fda_features(
  fda_fit,
  alpha = 0.05,
  prefix = "fda",
  statistics = c("mean", "max")
)

knitr::kable(head(fda_features))

## -----------------------------------------------------------------------------
fda_features_by_variable <- extract_fda_features(
  fda_fit,
  alpha = 0.05,
  prefix = "fda",
  statistics = list(
    daily_mean_temp = c("mean", "min", "max"),
    daily_mean_rh = c("mean", "min"),
    daily_sum_rain = list(total = "sum", max = "max"),
    daily_mean_leaf_wetness = list(mean = "mean", total = "sum"),
    .default = "mean"
  )
)

knitr::kable(head(fda_features_by_variable))

## -----------------------------------------------------------------------------
fda_features_biological <- extract_fda_features(
  fda_fit,
  alpha = 0.05,
  prefix = "fda",
  statistics = list(
    daily_mean_rh = list(mean = "mean", humid_days = humid_hours(85)),
    daily_mean_temp = list(mean = "mean", days_18_26 = count_between(18, 26)),
    daily_sum_rain = list(total = "sum", rainy_days = rainy_days(0))
  )
)

knitr::kable(head(fda_features_biological))

## -----------------------------------------------------------------------------
feature_name <- setdiff(names(fda_features), "site_id")[1]

feature_plot_data <- fda_features %>%
  left_join(assessments %>% select(site_id, wm_class), by = "site_id") %>%
  transmute(site_id, wm_class, value = .data[[feature_name]])

ggplot(feature_plot_data, aes(wm_class, value, fill = wm_class, color = wm_class)) +
  geom_violin(width = 0.85, alpha = 0.22, linewidth = 0.4, show.legend = FALSE) +
  geom_boxplot(width = 0.18, alpha = 0.6, outlier.shape = NA, linewidth = 0.5, show.legend = FALSE) +
  geom_jitter(width = 0.08, alpha = 0.55, size = 1.7, show.legend = FALSE) +
  scale_fill_manual(values = c("wm-" = "#2b6c4f", "wm+" = "#c47f2c")) +
  scale_color_manual(values = c("wm-" = "#2b6c4f", "wm+" = "#c47f2c")) +
  labs(
    title = "Interval-derived FDA feature by binary disease class",
    x = "White mold class",
    y = feature_name
  ) +
  cowplot::theme_half_open()

