## The documentation and README example, expanded into a visual demo.
local({
  set.seed(12345)
  x <- runif(200)
  y <- x + rnorm(200, sd = 0.15)
  result <- fes(x, y, K1 = 4, K2 = 4)
  print(result)

  old_parameters <- graphics::par(no.readonly = TRUE)
  old_parameters$new <- FALSE
  on.exit(graphics::par(old_parameters))
  graphics::layout(matrix(c(1, 2), nrow = 1L), widths = c(1.25, 1))

  graphics::par(mar = c(4, 4, 3.2, 1), mgp = c(2.2, 0.7, 0))
  graphics::plot(
    x,
    y,
    pch = 21,
    cex = 0.85,
    col = grDevices::adjustcolor("#17365D", alpha.f = 0.55),
    bg = grDevices::adjustcolor("#2C7FB8", alpha.f = 0.42),
    xlab = "X",
    ylab = "Y",
    main = "Noisy linear dependence",
    bty = "n",
    las = 1,
    panel.first = graphics::grid(col = "#E8E8E8", lty = 1)
  )
  smooth <- stats::lowess(x, y, f = 0.45)
  graphics::lines(smooth, col = "#D95F02", lwd = 2.5)
  graphics::mtext(
    sprintf("Primary FES p = %s", format.pval(result[["Sidak"]], digits = 3)),
    side = 3,
    line = 0.25,
    cex = 0.8,
    col = "#4D4D4D"
  )

  graphics::par(mar = c(4, 4, 3.2, 1), mgp = c(2.2, 0.7, 0))
  evidence <- -log10(pmax(result, .Machine$double.xmin))
  positions <- graphics::barplot(
    evidence,
    col = grDevices::hcl.colors(length(evidence), "Viridis"),
    border = NA,
    ylab = expression(-log[10](italic(p))),
    main = "Global evidence",
    las = 1,
    ylim = c(0, max(evidence, -log10(0.05)) * 1.12)
  )
  graphics::abline(h = -log10(0.05), col = "#D7191C", lty = 2, lwd = 1.5)
  graphics::text(positions, evidence, labels = format(round(evidence, 1)),
                 pos = 3, cex = 0.75)

  ## Show the adjusted multiscale windows on a common color scale.
  invisible(fes(
    x,
    y,
    K1 = 4,
    K2 = 4,
    M = 3,
    plot.it = TRUE,
    plot.sidak = TRUE,
    plot.scale = TRUE
  ))
})
