Global temperature anomalies (1880–1992) show two main features:
A standard quantile regression would fit a smooth curve that ignores this cooling period if it is forced to be non-decreasing everywhere. This demo illustrates how mixed constraints capture both features.
if (!exists("degree")) { degree <- 3 }
years <- 1880:1992
temperature <- c(
-0.32, -0.32, -0.40, -0.39, -0.65, -0.43, -0.40, -0.52, -0.30, -0.12,
-0.40, -0.42, -0.39, -0.45, -0.35, -0.36, -0.19, -0.14, -0.37, -0.22,
0.00, -0.08, -0.24, -0.36, -0.49, -0.27, -0.19, -0.43, -0.29, -0.30,
-0.29, -0.29, -0.28, -0.23, -0.04, -0.02, -0.24, -0.42, -0.35, -0.16,
-0.17, -0.09, -0.13, -0.16, -0.14, -0.14, 0.10, -0.03, 0.03, -0.18,
-0.06, 0.04, 0.02, -0.13, 0.03, -0.06, 0.02, 0.13, 0.13, -0.03,
0.15, 0.12, 0.10, 0.04, 0.11, -0.04, 0.01, 0.13, -0.01, -0.06,
-0.14, -0.02, 0.04, 0.14, -0.07, -0.06, -0.17, 0.10, 0.10, 0.05,
-0.01, 0.08, 0.02, 0.02, -0.26, -0.16, -0.09, -0.02, -0.12, 0.03,
0.04, -0.11, -0.07, 0.19, -0.07, -0.05, -0.22, 0.16, 0.09, 0.14,
0.28, 0.39, 0.07, 0.29, 0.11, 0.11, 0.16, 0.32, 0.35, 0.25,
0.47, 0.41, 0.13
)
cat(sprintf("Years: %d to %d (%d observations)\n",
min(years), max(years), length(years)))
#> Years: 1880 to 1992 (113 observations)
cat(sprintf("Temperature range: [%.2f, %.2f] C\n\n",
min(temperature), max(temperature)))
#> Temperature range: [-0.65, 0.47] C
# Normalize years to [0, 1] for numerical stability
xtab <- (years - min(years)) / (max(years) - min(years))
ytab <- temperature
# Define knots at specific years
target_knots <- c(1917, 1936, 1945, 1970)
knots_years <- sort(c(min(years), target_knots, max(years)))
knots <- (knots_years - min(years)) / (max(years) - min(years))
cat("Knots at years:", knots_years, "\n\n")
#> Knots at years: 1880 1917 1936 1945 1970 1992
# Evaluation grid
x_eval <- seq(0, 1, length.out = 300)
years_eval <- min(years) + x_eval * (max(years) - min(years))The demo fits four configurations:
cat("=== 1. Unconstrained median regression ===\n")
#> === 1. Unconstrained median regression ===
fit_uncon <- quantile_spline(degree = degree, xtab, ytab, knots, tau = 0.5,
monot = 0, convcons = 0)
y_uncon <- spline_eval(fit_uncon, x_eval)
cat("\n=== 2. Full increasing monotonicity (everywhere) ===\n")
#>
#> === 2. Full increasing monotonicity (everywhere) ===
fit_full_inc <- quantile_spline(degree = degree, xtab, ytab, knots, tau = 0.5,
monot = 1, convcons = 0)
y_full_inc <- spline_eval(fit_full_inc, x_eval)
cat("\n=== 3. Mixed constraints ===\n")
#>
#> === 3. Mixed constraints ===
cat(" Decreasing between 1945 and 1970\n")
#> Decreasing between 1945 and 1970
cat(" Increasing elsewhere\n")
#> Increasing elsewhere
monot_mixed <- c(1, 1, 0, -1, 1)
cat(" Constraint vector:", monot_mixed, "\n")
#> Constraint vector: 1 1 0 -1 1
fit_mixed <- quantile_spline(degree = degree, xtab, ytab, knots, tau = 0.5,
monot = monot_mixed, convcons = 0)
y_mixed <- spline_eval(fit_mixed, x_eval)
cat("\n=== 4. Multiple quantiles (0.1, 0.5, 0.9) with mixed constraints ===\n")
#>
#> === 4. Multiple quantiles (0.1, 0.5, 0.9) with mixed constraints ===
tau_multi <- c(0.1, 0.5, 0.9)
fits_multi <- list()
for (i in seq_along(tau_multi)) {
fits_multi[[i]] <- quantile_spline(degree = degree, xtab, ytab, knots,
tau = tau_multi[i],
monot = monot_mixed, convcons = 0)
cat(sprintf(" tau = %.1f done\n", tau_multi[i]))
}
#> tau = 0.1 done
#> tau = 0.5 done
#> tau = 0.9 donepar(mfrow = c(1, 2), mar = c(4, 4, 3, 1))
# Plot 1: Unconstrained fit
plot(years, temperature, pch = 1, col = "black",
xlab = "Year", ylab = "Temperature anomaly (C)",
main = "1. Unconstrained median (tau = 0.5)")
lines(years_eval, y_uncon, col = "red", lwd = 2)
abline(v = knots_years, col = "blue", lty = 2, lwd = 0.5)
abline(v = c(1945, 1970), col = "orange", lty = 3, lwd = 1.5)
abline(h = 0, col = "black", lty = 3, lwd = 0.5)
grid()
# Plot 2: Full increasing constraint
plot(years, temperature, pch = 1, col = "black",
xlab = "Year", ylab = "Temperature anomaly (C)",
main = "2. Full increasing constraint")
lines(years_eval, y_full_inc, col = "blue", lwd = 2)
abline(v = knots_years, col = "blue", lty = 2, lwd = 0.5)
abline(v = c(1945, 1970), col = "orange", lty = 3, lwd = 1.5)
abline(h = 0, col = "black", lty = 3, lwd = 0.5)
text(1950, -0.1, "Forced increasing\nbut data shows cooling",
col = "blue", cex = 0.7)
grid()Left: unconstrained median regression. Right: full increasing constraint, which ignores the cooling period.
par(mfrow = c(1, 2), mar = c(4, 4, 3, 1))
# Plot 3: Mixed constraints
plot(years, temperature, pch = 1, col = "black",
xlab = "Year", ylab = "Temperature anomaly (C)",
main = "3. Mixed constraints")
lines(years_eval, y_mixed, col = "darkgreen", lwd = 2)
abline(v = knots_years, col = "blue", lty = 2, lwd = 0.5)
abline(v = c(1945, 1970), col = "orange", lty = 2, lwd = 2)
abline(h = 0, col = "black", lty = 3, lwd = 0.5)
rect(1945, -0.8, 1970, 0.6, col = rgb(1, 0.5, 0, 0.1), border = NA)
text(1957, 0.5, "Decreasing\nconstraint", col = "orange", cex = 0.8)
text(1900, 0.4, "Increasing", col = "darkgreen", cex = 0.7)
text(1985, 0.4, "Increasing", col = "darkgreen", cex = 0.7)
grid()
# Plot 4: Multiple quantiles with mixed constraints
colors <- c("orange", "darkgreen", "darkred")
plot(years, temperature, pch = 1, col = "black",
xlab = "Year", ylab = "Temperature anomaly (C)",
main = "4. Quantile regression with mixed constraints")
for (i in seq_along(tau_multi)) {
y_fit <- spline_eval(fits_multi[[i]], x_eval)
lines(years_eval, y_fit, col = colors[i], lwd = 2,
lty = ifelse(tau_multi[i] == 0.5, 1, 2))
}
abline(v = knots_years, col = "blue", lty = 2, lwd = 0.5)
abline(v = c(1945, 1970), col = "orange", lty = 2, lwd = 2)
abline(h = 0, col = "black", lty = 3, lwd = 0.5)
rect(1945, -0.8, 1970, 0.6, col = rgb(1, 0.5, 0, 0.1), border = NA)
legend("topleft",
legend = c("tau = 0.1", "tau = 0.5", "tau = 0.9"),
col = colors, lty = c(2, 1, 2), lwd = 2, cex = 0.8)
grid()Left: mixed constraints (decreasing 1945-1970, increasing elsewhere). Right: multiple quantiles with mixed constraints.
compute_derivative <- function(y, x) diff(y) / diff(x)
deriv_mixed <- compute_derivative(y_mixed, years_eval)
deriv_full <- compute_derivative(y_full_inc, years_eval)
idx_1945 <- which.min(abs(years_eval - 1945))
idx_1970 <- which.min(abs(years_eval - 1970))
cat("=== Derivative analysis ===\n")
#> === Derivative analysis ===
cat(sprintf("Full increasing: min derivative = %.4f (should be >= 0)\n",
min(deriv_full)))
#> Full increasing: min derivative = 0.0000 (should be >= 0)
cat(sprintf("Mixed constraints: min derivative = %.4f\n",
min(deriv_mixed)))
#> Mixed constraints: min derivative = -0.0037
cat(sprintf(" derivative between 1945-1970: range [%.4f, %.4f]\n",
min(deriv_mixed[idx_1945:idx_1970]),
max(deriv_mixed[idx_1945:idx_1970])))
#> derivative between 1945-1970: range [-0.0036, 0.0000]The temperature data show:
The full increasing constraint ignores this cooling period: the fitted curve is forced to be non-decreasing everywhere, which is inconsistent with the data.
The mixed constraints (decreasing only between 1945 and 1970, increasing elsewhere) better reflect the actual pattern. The multiple quantiles reveal how the distribution of temperature anomalies changes over time.
This demonstrates that flexible constraint patterns are more realistic than global monotonicity.