## FES detects relationships that are not well summarized by correlation.
local({
  set.seed(12345)
  n <- 300
  independent_x <- runif(n)
  angle <- runif(n, 0, 2 * pi)
  patterns <- list(
    Independent = list(x = independent_x, y = runif(n)),
    Linear = list(x = independent_x,
                  y = independent_x + rnorm(n, sd = 0.14)),
    Sinusoidal = list(x = independent_x,
                      y = sin(4 * pi * independent_x) + rnorm(n, sd = 0.28)),
    Circular = list(x = cos(angle) + rnorm(n, sd = 0.08),
                    y = sin(angle) + rnorm(n, sd = 0.08))
  )
  p_values <- vapply(
    patterns,
    function(data) fes(data$x, data$y, K1 = 3, K2 = 3)[["Sidak"]],
    numeric(1L)
  )
  print(p_values)

  old_parameters <- graphics::par(no.readonly = TRUE)
  old_parameters$new <- FALSE
  on.exit(graphics::par(old_parameters))
  graphics::layout(
    matrix(c(1, 2, 5, 3, 4, 5), nrow = 2L, byrow = TRUE),
    widths = c(1, 1, 1.15)
  )
  point_color <- grDevices::adjustcolor("#2166AC", alpha.f = 0.52)

  for (name in names(patterns)) {
    data <- patterns[[name]]
    graphics::par(mar = c(3.4, 3.4, 3.2, 0.8), mgp = c(1.9, 0.55, 0))
    graphics::plot(
      data$x,
      data$y,
      pch = 16,
      cex = 0.62,
      col = point_color,
      xlab = "X",
      ylab = "Y",
      main = name,
      bty = "n",
      las = 1,
      panel.first = graphics::grid(col = "#ECECEC", lty = 1)
    )
    graphics::mtext(
      sprintf("FES p = %s", format.pval(p_values[[name]], digits = 2)),
      side = 3,
      line = 0.25,
      cex = 0.72,
      col = "#4D4D4D"
    )
  }

  graphics::par(mar = c(4, 7, 3.2, 1), mgp = c(2.2, 0.7, 0))
  evidence <- -log10(pmax(p_values, .Machine$double.xmin))
  positions <- graphics::barplot(
    evidence,
    names.arg = names(evidence),
    horiz = TRUE,
    col = grDevices::hcl.colors(length(evidence), "Viridis"),
    border = NA,
    xlab = expression(-log[10](italic(p))),
    main = "FES evidence",
    las = 1,
    xlim = c(0, max(evidence, -log10(0.05)) * 1.15)
  )
  graphics::abline(v = -log10(0.05), col = "#D7191C", lty = 2, lwd = 1.5)
  graphics::text(evidence, positions, labels = format(round(evidence, 1)),
                 pos = 4, cex = 0.72)
})
