## Significant FES windows reveal more than a global rejection decision.
local({
  set.seed(12345)
  n <- 480
  unit_x <- runif(n)
  angle <- runif(n, 0, 2 * pi)
  patterns <- list(
    "Monotone increase" = list(
      x = unit_x,
      y = unit_x + rnorm(n, sd = 0.08)
    ),
    "U-shaped" = list(
      x = unit_x,
      y = (unit_x - 0.5)^2 + rnorm(n, sd = 0.025)
    ),
    "Circular" = list(
      x = cos(angle) + rnorm(n, sd = 0.06),
      y = sin(angle) + rnorm(n, sd = 0.06)
    ),
    "Alternating bands" = list(
      x = unit_x,
      y = (floor(unit_x * 4) %% 2) * 0.5 + runif(n, 0, 0.5)
    )
  )
  structures <- lapply(
    patterns,
    function(data) {
      fes(
        data$x,
        data$y,
        K1 = 4,
        K2 = 4,
        M = 4,
        return.details = TRUE
      )
    }
  )

  summary <- data.frame(
    pattern = names(structures),
    p_value = vapply(structures, function(fit) fit$p.value[["Sidak"]],
                     numeric(1L)),
    positive_windows = vapply(
      structures,
      function(fit) sum(fit$significant.windows$direction == "positive"),
      integer(1L)
    ),
    negative_windows = vapply(
      structures,
      function(fit) sum(fit$significant.windows$direction == "negative"),
      integer(1L)
    )
  )
  print(summary)

  old_parameters <- graphics::par(no.readonly = TRUE)
  old_parameters$new <- FALSE
  on.exit(graphics::par(old_parameters))
  graphics::par(mfrow = c(2, 2), mar = c(3.5, 3.5, 3.3, 0.8),
                mgp = c(2, 0.6, 0))
  for (name in names(structures)) {
    plot(structures[[name]], max.windows = 8, main = name)
  }
})
