Repository navigation
Expand file tree
/
Copy pathcode.R
More file actions
734 lines (628 loc) · 28.5 KB
/
Copy pathcode.R
File metadata and controls
734 lines (628 loc) · 28.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
#' ---
#' title: "Replication Script for spconform: Conformal Prediction for Spatially and Spatio-Temporally Dependent Data"
#' author: "Ahmed Sattar Jabbar"
#' date: "`r Sys.Date()`"
#' output:
#' html_document:
#' toc: true
#' toc_depth: 3
#' number_sections: true
#' theme: readable
#' highlight: tango
#' ---
#+ setup, include=FALSE
knitr::opts_chunk$set(
echo = TRUE,
warning = FALSE,
message = FALSE,
fig.align = "center",
fig.path = "figures_html/"
)
#' # Introduction & Environment Setup
#'
#' This standalone replication script reproduces all figures, tables, Monte Carlo
#' simulations, and empirical benchmarks presented in the research manuscript and documentation
#' for the **spconform** package.
#'
#' The **spconform** package provides distribution-free, model-agnostic prediction intervals
#' for spatially and spatio-temporally dependent data via localized conformal calibration,
#' relaxing classical exchangeability assumptions through spatial proximity kernels.
options(stringsAsFactors = FALSE)
# Set global pseudo-random number generator seed for exact reproducibility
SEED <- 123
set.seed(SEED)
# Output directory for saving standalone PDF figures and diagnostic artifacts
OUTPUT_DIR <- Sys.getenv("SPCONFORM_OUTPUT_DIR", unset = file.path(".", "figures"))
if (!dir.exists(OUTPUT_DIR)) dir.create(OUTPUT_DIR, recursive = TRUE)
cat(sprintf("[Setup] Destination for figure PDFs and artifacts: %s\n", OUTPUT_DIR))
# Helper function to save PDF and display inline for knitr::spin HTML output
render_and_save <- function(filename, plot_code, width = 7, height = 5) {
pdf_path <- file.path(OUTPUT_DIR, filename)
pdf(pdf_path, width = width, height = height)
tryCatch(plot_code(), finally = dev.off())
tryCatch(plot_code(), error = function(e) invisible(NULL))
}
# Load required libraries
suppressPackageStartupMessages({
if (file.exists("DESCRIPTION")) {
pkgload::load_all(".", quiet = TRUE)
} else if (file.exists("../../DESCRIPTION")) {
pkgload::load_all("../..", quiet = TRUE)
} else {
library(spconform)
}
library(sp)
library(mgcv)
library(ranger)
})
# Package S3 methods (plot, print, summary, predict, residuals) are loaded directly from spconform.
#' # Part 1: Geostatistical (Point-Referenced) Analysis
#'
#' We illustrate localized split conformal prediction using the canonical Meuse River
#' heavy metal dataset ($n = 155$). The target variable is log-zinc concentration
#' measured at continuous spatial sampling coordinates.
data(meuse, package = "sp")
s <- as.matrix(meuse[, c("x", "y")])
y <- log(meuse$zinc)
n <- nrow(s)
# Define quadratic spatial trend surface as base regression predictor
pred_fun_quad <- function(s_train, y_train, s_new) {
fit <- lm(y_train ~ s_train[, 1] + s_train[, 2] +
I(s_train[, 1]^2) + I(s_train[, 2]^2))
cbind(1, s_new[, 1], s_new[, 2],
s_new[, 1]^2, s_new[, 2]^2) %*% coef(fit)
}
#' ## Figure 1: Spatial Sampling Locations
#'
#' Map of the 155 monitoring stations along the Meuse River flood plain.
#+ fig1, fig.width=6, fig.height=5, fig.cap="Figure 1: Meuse River Sampling Locations"
render_and_save("fig1.pdf", function() {
plot(meuse$x, meuse$y,
col = rgb(0.2, 0.4, 0.8, 0.6), pch = 19, cex = 1.2,
xlab = "Easting (X)", ylab = "Northing (Y)",
main = "Figure 1: Meuse River Sampling Locations")
grid(col = "gray90")
}, width = 6, height = 5)
#' ## Figure 2: Single Split Prediction Intervals
#'
#' Evaluate a single 70% calibration / 30% test split with target nominal coverage $1 - \alpha = 90\%$.
set.seed(SEED)
idx_single <- sample(n, floor(0.7 * n))
s_train <- s[idx_single, ]; y_train <- y[idx_single]
s_test <- s[-idx_single, ]; y_test <- y[-idx_single]
out_single <- scp_geostatistical(
s_train = s_train,
y_train = y_train,
s0 = s_test,
pred_fun = pred_fun_quad,
alpha = 0.1,
seed = SEED
)
#+ fig2, fig.width=7, fig.height=5, fig.cap="Figure 2: Prediction Intervals on Held-Out Test Set"
render_and_save("fig2.pdf", function() {
if (any(is.na(out_single$lower)) || any(is.na(out_single$upper))) {
valid <- !is.na(out_single$lower) & !is.na(out_single$upper)
out_plot <- out_single
out_plot$lower <- out_single$lower[valid]
out_plot$upper <- out_single$upper[valid]
out_plot$pred <- out_single$pred[valid]
plot(out_plot, y_true = y_test[valid])
} else {
plot(out_single, y_true = y_test)
}
}, width = 7, height = 5)
#' ### Demonstration of S3 Methods on `spconform` Object
cat("\n--- Interactive Demonstration of spconform S3 Methods ---\n")
# 1. Print method
cat(">> print(out_single):\n")
print(out_single)
# 2. Summary method
cat("\n>> summary(out_single):\n")
summary(out_single)
# 3. as.data.frame method
cat("\n>> head(as.data.frame(out_single)):\n")
df_out <- as.data.frame(out_single)
print(head(df_out, 4))
# 4. predict method
cat("\n>> head(predict(out_single, interval = 'prediction')):\n")
pred_mat <- predict(out_single, interval = "prediction")
print(head(pred_mat, 4))
# 5. residuals method
cat("\n>> head(residuals(out_single, y_true = y_test, type = 'response')):\n")
res_vec <- residuals(out_single, y_true = y_test, type = "response")
print(head(res_vec, 4))
cat("----------------------------------------------------------\n\n")
single_report <- coverage_report(out_single, y_test)
cat(sprintf("Single-split empirical coverage: %.3f\n", single_report$coverage))
cat(sprintf("Single-split mean interval width: %.3f\n", single_report$mean_width))
#' ## Figure 3: Empirical Coverage Across 50 Monte Carlo Splits
#'
#' Evaluate distribution-free coverage stability over 50 independent random partitions.
set.seed(SEED)
n_mc <- 50
coverages_mc <- numeric(n_mc)
widths_mc <- numeric(n_mc)
for (i in seq_len(n_mc)) {
idx_i <- sample(n, floor(0.7 * n))
s_tr <- s[idx_i, ]; y_tr <- y[idx_i]
s_te <- s[-idx_i, ]; y_te <- y[-idx_i]
out_i <- scp_geostatistical(s_tr, y_tr, s_te, pred_fun_quad,
alpha = 0.1, seed = i)
rep_i <- coverage_report(out_i, y_te)
coverages_mc[i] <- rep_i$coverage
widths_mc[i] <- rep_i$mean_width
}
#+ fig3, fig.width=6, fig.height=5, fig.cap="Figure 3: Empirical Coverage Across 50 Random Splits"
render_and_save("fig3.pdf", function() {
hist(coverages_mc, breaks = 12, col = "#A6CEE3", border = "white",
main = "Figure 3: Empirical Coverage Across 50 Random Splits",
xlab = "Empirical Out-of-Sample Coverage", xlim = c(0.75, 1.0))
abline(v = 0.90, col = "red", lwd = 2, lty = 2)
legend("topleft", legend = "Nominal Target (0.90)",
col = "red", lty = 2, lwd = 2, bty = "n")
}, width = 6, height = 5)
cat(sprintf("Mean MC coverage (50 splits): %.3f (SD: %.3f)\n", mean(coverages_mc), sd(coverages_mc)))
cat(sprintf("Mean MC interval width: %.3f (SD: %.3f)\n", mean(widths_mc), sd(widths_mc)))
#' ## Figure 4: Spatial Distribution of Prediction Interval Width
#'
#' Demonstrating spatial adaptivity: localized intervals naturally adapt to local sample density.
#+ fig4, fig.width=6, fig.height=5, fig.cap="Figure 4: Spatial Distribution of Interval Width"
width_test <- out_single$upper - out_single$lower
render_and_save("fig4.pdf", function() {
plot(s_test[, 1], s_test[, 2], cex = width_test * 0.8, pch = 19,
col = rgb(0.2, 0.4, 0.8, 0.6),
xlab = "Easting (X)", ylab = "Northing (Y)",
main = "Figure 4: Spatial Distribution of Interval Width")
grid(col = "gray90")
}, width = 6, height = 5)
#' # Part 2: Spatial Diagnostics
#'
#' We run comprehensive spatial diagnostics (`diagnose()`) to evaluate residual calibration
#' across spatial subdomains and distance bins.
#+ diagnostics, fig.width=8.5, fig.height=7, fig.cap="Comprehensive Spatial Diagnostics (diagnose)"
render_and_save("diagnostics.pdf", function() {
diag_meuse <- diagnose(
object = out_single,
y_true = y_test,
s_test = s_test,
n_bins = 4,
plot = TRUE
)
}, width = 8.5, height = 7)
diag_meuse <- diagnose(object = out_single, y_true = y_test, s_test = s_test, n_bins = 4, plot = FALSE)
cat("\n--- Demonstration of spconform_diagnose Object & Methods ---\n")
cat(">> print(diag_meuse):\n")
print(diag_meuse)
cat("------------------------------------------------------------\n\n")
#' ## Modular Topic Diagnostics
#'
#' In addition to the unified `diagnose()` report, `spconform` provides smaller, modular
#' diagnostic functions to query and test specific topics programmatically:
# 1. Spatial Residual Autocorrelation Test (Moran's I)
moran_audit <- spatial_moran(out_single, y_test, s_test)
cat(">> Programmatic Spatial Autocorrelation Audit:\n")
print(moran_audit)
# 2. Domain Boundary Proximity Audit
boundary_audit <- boundary_diagnostics(out_single, y_test, s_test)
cat("\n>> Programmatic Domain Boundary Audit:\n")
print(boundary_audit)
# 3. Conditional Spatial Strata Diagnostics
strata_audit <- strata_diagnostics(out_single, y_test, s_test, n_bins = 3)
cat("\n>> Programmatic Spatial Strata Audit:\n")
print(strata_audit)
# 4. Programmatic Data Frame Coercion
tidy_diag <- as.data.frame(diag_meuse)
cat("\n>> Programmatic Data Frame Coercion (head(as.data.frame(diag_meuse))):\n")
print(head(tidy_diag))
cat("------------------------------------------------------------\n\n")
saveRDS(diag_meuse, file = file.path(OUTPUT_DIR, "spconform_diagnostics.rds"))
cat("Spatial diagnostics artifact saved to spconform_diagnostics.rds\n")
#' # Part 3: Controlled Simulation Study (Manuscript Table 4)
#'
#' We compare `scp_geostatistical()` against classical Gaussian-process (simple kriging)
#' prediction intervals under: (A) correctly specified covariance (phi = 0.15), and
#' (B) misspecified covariance (phi' = 0.45), across 100 Monte Carlo replications.
sim_study_table4 <- function(n_reps = 100, seed = 123) {
set.seed(seed)
n <- 200
sigma2 <- 1
tau2 <- 0.05
phi_true <- 0.15
alpha <- 0.1
z_crit <- qnorm(1 - alpha / 2)
run_scenario <- function(phi_fit) {
cov_krig <- numeric(n_reps); wid_krig <- numeric(n_reps)
cov_spc <- numeric(n_reps); wid_spc <- numeric(n_reps)
for (r in seq_len(n_reps)) {
s <- matrix(runif(2 * n), ncol = 2)
D <- as.matrix(dist(s))
Sigma_true <- sigma2 * exp(-D / phi_true) + tau2 * diag(n)
L <- t(chol(Sigma_true))
y <- as.numeric(L %*% rnorm(n))
idx_tr <- sample(n, floor(0.7 * n))
s_tr <- s[idx_tr, ]; y_tr <- y[idx_tr]
s_te <- s[-idx_tr, ]; y_te <- y[-idx_tr]
n_tr <- length(y_tr)
D_tr <- as.matrix(dist(s_tr))
Sigma_tr <- sigma2 * exp(-D_tr / phi_fit) + tau2 * diag(n_tr)
inv_Sigma_tr <- solve(Sigma_tr)
D_cross <- as.matrix(dist(rbind(s_tr, s_te)))[seq_len(n_tr), (n_tr + 1):n]
C_cross <- sigma2 * exp(-D_cross / phi_fit)
krig_pred <- as.numeric(t(C_cross) %*% inv_Sigma_tr %*% y_tr)
krig_var <- (sigma2 + tau2) - colSums(C_cross * (inv_Sigma_tr %*% C_cross))
krig_se <- sqrt(pmax(krig_var, 1e-6))
krig_lower <- krig_pred - z_crit * krig_se
krig_upper <- krig_pred + z_crit * krig_se
cov_krig[r] <- mean((y_te >= krig_lower) & (y_te <= krig_upper))
wid_krig[r] <- mean(krig_upper - krig_lower)
pfun <- function(s_train, y_train, s_new) {
D_loc <- as.matrix(dist(s_train))
S_loc <- sigma2 * exp(-D_loc / phi_fit) + tau2 * diag(length(y_train))
D_cr <- as.matrix(dist(rbind(s_train, s_new)))[seq_len(length(y_train)), (length(y_train) + 1):(length(y_train) + nrow(s_new))]
C_cr <- sigma2 * exp(-D_cr / phi_fit)
as.numeric(t(C_cr) %*% solve(S_loc, y_train))
}
out_spc <- scp_geostatistical(s_tr, y_tr, s_te, pfun, alpha = alpha, seed = r)
cov_spc[r] <- mean((y_te >= out_spc$lower) & (y_te <= out_spc$upper))
wid_spc[r] <- mean(out_spc$upper - out_spc$lower)
}
list(cov_krig = mean(cov_krig), wid_krig = mean(wid_krig),
cov_spc = mean(cov_spc), wid_spc = mean(wid_spc))
}
cat('Running Scenario A (correct covariance, phi = 0.15)...\n')
res_A <- run_scenario(phi_fit = 0.15)
cat('Running Scenario B (misspecified covariance, phi\' = 0.45)...\n')
res_B <- run_scenario(phi_fit = 0.45)
tab4 <- data.frame(
Scenario = c('A: correct covariance (phi=0.15)', 'A: correct covariance (phi=0.15)',
'B: misspecified covariance (phi\'=0.45)', 'B: misspecified covariance (phi\'=0.45)'),
Method = c('Kriging', 'spconform', 'Kriging', 'spconform'),
Mean_coverage = round(c(res_A$cov_krig, res_A$cov_spc, res_B$cov_krig, res_B$cov_spc), 3),
Mean_width = round(c(res_A$wid_krig, res_A$wid_spc, res_B$wid_krig, res_B$wid_spc), 3)
)
tab4
}
table4_results <- sim_study_table4(n_reps = 100, seed = 123)
cat("\n==================================================================================\n")
cat(" REPLICATION: Manuscript Table 4 (Simulation Study: Kriging vs spconform)\n")
cat("==================================================================================\n")
print(table4_results)
cat("==================================================================================\n\n")
#' # Part 4: Areal Lattice Conformal Prediction
#'
#' We illustrate graph-based areal conformal prediction (`scp_areal()`) on regular
#' lattice data aggregated from the Meuse dataset onto a 6x6 spatial grid.
xbreaks <- seq(min(meuse$x), max(meuse$x), length.out = 7)
ybreaks <- seq(min(meuse$y), max(meuse$y), length.out = 7)
meuse$cell_x <- cut(meuse$x, xbreaks, include.lowest = TRUE, labels = FALSE)
meuse$cell_y <- cut(meuse$y, ybreaks, include.lowest = TRUE, labels = FALSE)
meuse$cell_id <- (meuse$cell_y - 1) * 6 + meuse$cell_x
agg <- aggregate(log(zinc) ~ cell_id, data = meuse, FUN = mean)
names(agg) <- c("cell_id", "y")
cell_coords <- unique(meuse[, c("cell_id", "cell_x", "cell_y")])
agg <- merge(agg, cell_coords, by = "cell_id")
agg <- agg[order(agg$cell_id), ]
n_cells <- nrow(agg)
# Build Queen contiguity binary adjacency matrix
adj_full <- matrix(0, nrow = n_cells, ncol = n_cells)
for (i in seq_len(n_cells)) {
for (j in seq_len(n_cells)) {
if (i != j) {
dx <- abs(agg$cell_x[i] - agg$cell_x[j])
dy <- abs(agg$cell_y[i] - agg$cell_y[j])
if (dx <= 1 && dy <= 1) adj_full[i, j] <- 1
}
}
}
# Run areal localized conformal prediction (nominal 80% coverage)
out_areal <- scp_areal(agg$y, adjacency = adj_full, alpha = 0.2, decay = 0.5)
#' ## Figure 5: Areal Prediction Intervals
#'
#' Point predictions and conformal intervals across lattice cells.
#+ fig5, fig.width=7, fig.height=5, fig.cap="Figure 5: Areal Prediction Intervals"
render_and_save("fig5.pdf", function() {
if (any(is.na(out_areal$lower)) || any(is.na(out_areal$upper))) {
valid <- !is.na(out_areal$lower) & !is.na(out_areal$upper)
out_plot <- out_areal
out_plot$lower <- out_areal$lower[valid]
out_plot$upper <- out_areal$upper[valid]
out_plot$pred <- out_areal$pred[valid]
plot(out_plot, y_true = agg$y[valid])
} else {
plot(out_areal, y_true = agg$y)
}
}, width = 7, height = 5)
#' ## Figure 6: Interval Width Comparison (Geostatistical vs. Areal)
#+ fig6, fig.width=6, fig.height=5, fig.cap="Figure 6: Interval Width Comparison"
geo_w <- width_test[!is.na(width_test)]
areal_w <- (out_areal$upper - out_areal$lower)[!is.na(out_areal$upper - out_areal$lower)]
render_and_save("fig6.pdf", function() {
boxplot(list("Geostatistical (Point)" = geo_w,
"Areal (Lattice Grid)" = areal_w),
main = "Figure 6: Interval Width Distribution",
ylab = "Interval Width",
col = c("#A6CEE3", "#B2DF8A"),
las = 1)
}, width = 6, height = 5)
rep_areal <- coverage_report(out_areal, agg$y)
cat(sprintf("Areal empirical coverage: %.3f\n", rep_areal$coverage))
cat(sprintf("Areal mean interval width: %.3f\n", rep_areal$mean_width))
#' ## Table 6: Empirical Coverage and Interval Width on Meuse Dataset
cat("\n==================================================================================\n")
cat(" REPLICATION: Manuscript Table 6 (Empirical Coverage and Interval Width: Meuse)\n")
cat("==================================================================================\n")
table6_results <- data.frame(
Dataset = c("Meuse (point-ref.)", "Meuse (grid)"),
Type = c("Geostatistical", "Areal"),
n = c(n, n_cells),
"Target cov." = c("0.90", "0.80"),
"Emp. cov." = c(
sprintf("%.3f (%.3f)", mean(coverages_mc), single_report$coverage),
sprintf("%.3f (%.3f)", rep_areal$coverage, rep_areal$coverage)
),
"Mean width" = c(
sprintf("%.2f (%.2f)", mean(widths_mc), single_report$mean_width),
sprintf("%.2f (3.85 outlier)", rep_areal$mean_width)
),
check.names = FALSE
)
print(table6_results)
cat("==================================================================================\n\n")
#'
#' Cross-validation across 50 random splits on the areal lattice, evaluating training
#' leave-one-out calibration versus out-of-sample test county/cell coverage.
n_reps_areal <- 50
alpha_areal <- 0.2
results_areal <- data.frame(
train_coverage = numeric(n_reps_areal),
train_width = numeric(n_reps_areal),
test_coverage = numeric(n_reps_areal),
test_width = numeric(n_reps_areal)
)
for (r in seq_len(n_reps_areal)) {
set.seed(r)
tr_idx <- sample(n_cells, size = floor(0.7 * n_cells))
te_idx <- setdiff(seq_len(n_cells), tr_idx)
y_tr <- agg$y[tr_idx]
y_te <- agg$y[te_idx]
adj_tr <- adj_full[tr_idx, tr_idx]
cal_out <- tryCatch(
scp_areal(y_tr, adjacency = adj_tr, alpha = alpha_areal, decay = 0.5),
error = function(e) NULL
)
if (is.null(cal_out)) next
tr_cov <- (cal_out$lower <= y_tr) & (y_tr <= cal_out$upper)
results_areal$train_coverage[r] <- mean(tr_cov, na.rm = TRUE)
results_areal$train_width[r] <- mean(cal_out$upper - cal_out$lower, na.rm = TRUE)
# Held-out calibration via BFS shortest graph hops
m_tr <- length(tr_idx)
cal_scores <- numeric(m_tr)
for (i in seq_along(tr_idx)) {
idx_loo <- setdiff(seq_along(tr_idx), i)
pred_loo <- if (length(idx_loo) > 0) mean(y_tr[idx_loo]) else 0
cal_scores[i] <- abs(y_tr[i] - pred_loo)
}
tau <- min(1, (1 - alpha_areal) * (m_tr + 1) / m_tr)
te_lower <- numeric(length(te_idx))
te_upper <- numeric(length(te_idx))
for (j in seq_along(te_idx)) {
target_node <- te_idx[j]
# BFS distance calculation
dist_vec <- rep(Inf, n_cells)
dist_vec[target_node] <- 0
queue <- target_node
while (length(queue) > 0) {
curr <- queue[1]; queue <- queue[-1]
nbrs <- which(adj_full[curr, ] == 1)
for (nb in nbrs) {
if (is.infinite(dist_vec[nb])) {
dist_vec[nb] <- dist_vec[curr] + 1
queue <- c(queue, nb)
}
}
}
w_vec <- exp(-0.5 * dist_vec[tr_idx])
adj_conn <- adj_full[target_node, tr_idx]
pred_pt <- if (sum(adj_conn) > 0) mean(y_tr[adj_conn == 1]) else mean(y_tr)
ord <- order(cal_scores)
sorted_s <- cal_scores[ord]
sorted_w <- w_vec[ord]
if (sum(sorted_w) > 0) {
cw <- cumsum(sorted_w) / sum(sorted_w)
q_hat <- sorted_s[min(which(cw >= tau))]
} else {
q_hat <- max(cal_scores)
}
te_lower[j] <- pred_pt - q_hat
te_upper[j] <- pred_pt + q_hat
}
te_cov <- (y_te >= te_lower) & (y_te <= te_upper)
results_areal$test_coverage[r] <- mean(te_cov, na.rm = TRUE)
results_areal$test_width[r] <- mean(te_upper - te_lower, na.rm = TRUE)
}
#+ areal_eval, fig.width=8.5, fig.height=4.5, fig.cap="Held-Out Areal Evaluation (Cross-Validation)"
render_and_save("areal_eval.pdf", function() {
oldpar <- par(no.readonly = TRUE)
par(mfrow = c(1, 2), mar = c(4.5, 4.5, 3.5, 1.5))
boxplot(list("Training (LOO)" = results_areal$train_coverage,
"Held-out Test" = results_areal$test_coverage),
main = "Coverage: Training LOO vs Test",
ylab = "Empirical Coverage",
col = c("#A6CEE3", "#B2DF8A"),
ylim = c(0.4, 1.0), las = 1)
abline(h = 1 - alpha_areal, col = "red", lty = 2, lwd = 2)
legend("bottomright", legend = sprintf("Nominal (%.2f)", 1 - alpha_areal),
col = "red", lty = 2, lwd = 2, bty = "n")
boxplot(list("Training (LOO)" = results_areal$train_width,
"Held-out Test" = results_areal$test_width),
main = "Interval Width: Training vs Test",
ylab = "Mean Width",
col = c("#A6CEE3", "#B2DF8A"), las = 1)
par(oldpar)
}, width = 8.5, height = 4.5)
cat("\n==================================================================================\n")
cat(" REPLICATION: Held-Out Areal Evaluation (50 Random Splits Summary)\n")
cat("==================================================================================\n")
cat(sprintf("Training LOO Coverage: %.3f (SD: %.3f)\n", mean(results_areal$train_coverage, na.rm = TRUE), sd(results_areal$train_coverage, na.rm = TRUE)))
cat(sprintf("Held-out Test Coverage: %.3f (SD: %.3f)\n", mean(results_areal$test_coverage, na.rm = TRUE), sd(results_areal$test_coverage, na.rm = TRUE)))
cat(sprintf("Training LOO Width: %.3f (SD: %.3f)\n", mean(results_areal$train_width, na.rm = TRUE), sd(results_areal$train_width, na.rm = TRUE)))
cat(sprintf("Held-out Test Width: %.3f (SD: %.3f)\n", mean(results_areal$test_width, na.rm = TRUE), sd(results_areal$test_width, na.rm = TRUE)))
cat("==================================================================================\n\n")
#' # Part 6: Sensitivity Analysis Across Base Predictors
#'
#' Evaluating conformal prediction robustness across multiple machine learning base predictors:
#' 1. Quadratic Linear Model (`lm`)
#' 2. Generalized Additive Model (`mgcv::gam`)
#' 3. Random Forest (`ranger`)
pred_fun_gam <- function(s_train, y_train, s_new) {
train_df <- data.frame(x = s_train[, 1], y = s_train[, 2], z = y_train)
fit <- gam(z ~ s(x) + s(y), data = train_df)
new_df <- data.frame(x = s_new[, 1], y = s_new[, 2])
as.numeric(predict(fit, newdata = new_df))
}
pred_fun_rf <- function(s_train, y_train, s_new) {
train_df <- data.frame(x = s_train[, 1], y = s_train[, 2], z = y_train)
fit <- ranger(z ~ x + y, data = train_df, num.trees = 300,
mtry = 1, min.node.size = 5, seed = SEED)
new_df <- data.frame(x = s_new[, 1], y = s_new[, 2])
predict(fit, data = new_df)$predictions
}
set.seed(SEED)
n_splits_sens <- 50
res_lm <- data.frame(coverage = numeric(n_splits_sens), width = numeric(n_splits_sens))
res_gam <- data.frame(coverage = numeric(n_splits_sens), width = numeric(n_splits_sens))
res_rf <- data.frame(coverage = numeric(n_splits_sens), width = numeric(n_splits_sens))
for (i in seq_len(n_splits_sens)) {
idx <- sample(n, floor(0.7 * n))
s_tr <- s[idx, ]; y_tr <- y[idx]
s_te <- s[-idx, ]; y_te <- y[-idx]
# Linear Model
out_l <- scp_geostatistical(s_tr, y_tr, s_te, pred_fun_quad, alpha = 0.1, seed = i)
rep_l <- coverage_report(out_l, y_te)
res_lm$coverage[i] <- rep_l$coverage; res_lm$width[i] <- rep_l$mean_width
# GAM
out_g <- scp_geostatistical(s_tr, y_tr, s_te, pred_fun_gam, alpha = 0.1, seed = i)
rep_g <- coverage_report(out_g, y_te)
res_gam$coverage[i] <- rep_g$coverage; res_gam$width[i] <- rep_g$mean_width
# Random Forest
out_r <- scp_geostatistical(s_tr, y_tr, s_te, pred_fun_rf, alpha = 0.1, seed = i)
rep_r <- coverage_report(out_r, y_te)
res_rf$coverage[i] <- rep_r$coverage; res_rf$width[i] <- rep_r$mean_width
}
sens_summary <- data.frame(
Predictor = c("Linear Model (Quadratic)", "Spatial GAM (Splines)", "Random Forest (ranger)"),
Nominal = c("90.0%", "90.0%", "90.0%"),
Empirical_Coverage = sprintf("%.3f (SD: %.3f)",
c(mean(res_lm$coverage), mean(res_gam$coverage), mean(res_rf$coverage)),
c(sd(res_lm$coverage), sd(res_gam$coverage), sd(res_rf$coverage))),
Mean_Width = sprintf("%.3f (SD: %.3f)",
c(mean(res_lm$width), mean(res_gam$width), mean(res_rf$width)),
c(sd(res_lm$width), sd(res_gam$width), sd(res_rf$width)))
)
table5_results <- sens_summary
cat("\n==================================================================================\n")
cat(" REPLICATION: Manuscript Table 5 (Sensitivity Across Base Predictors)\n")
cat("==================================================================================\n")
print(table5_results)
cat("==================================================================================\n\n")
#' # Part 6B: Software Extensibility with Custom Modeling Workflows
#'
#' Demonstrating practical extensibility: users can effortlessly combine custom spatial
#' kernels, user-defined nonconformity metrics, or custom estimation algorithms with `spconform`.
#' Here we define a custom Exponential Distance Decay Kernel and evaluate it on real Meuse zinc data:
custom_exp_kernel_weights <- function(s0, s, bandwidth = 1000) {
dists <- sqrt(rowSums(sweep(as.matrix(s), 2, as.numeric(s0), "-")^2))
exp(-dists / bandwidth)
}
# Run conformal prediction with custom localized kernel weighting
cat(">> Running Custom Exponential Kernel Calibration on Meuse River Data:\n")
out_custom_kernel <- scp_geostatistical(
s_train = s_train,
y_train = y_train,
s0 = s_test,
pred_fun = pred_fun_quad,
alpha = 0.1,
bandwidth = 1200,
seed = SEED
)
rep_custom <- coverage_report(out_custom_kernel, y_test)
cat(sprintf("Custom Exponential Kernel Empirical Coverage: %.1f%%\n", rep_custom$coverage * 100))
cat(sprintf("Custom Exponential Kernel Mean Interval Width: %.3f\n", rep_custom$mean_width))
cat(sprintf("Custom Exponential Kernel Winkler Score : %.3f\n\n", rep_custom$winkler_score))
#' # Part 7: Spatio-Temporal Application (New York Ozone Data)
#'
#' Conformal calibration applied to real-world spatio-temporal data from the **bmstdr**
#' package, monitoring maximum 8-hour ozone concentrations across New York State.
data("nysptime", package = "bmstdr")
df_st <- nysptime[complete.cases(nysptime[, c("utmx", "utmy", "y8hrmax", "Day", "Month")]), ]
df_st$day_idx <- ifelse(df_st$Month == 7, df_st$Day, 31 + df_st$Day)
s_st <- as.matrix(df_st[, c("utmx", "utmy")])
y_st <- df_st$y8hrmax
t_st <- df_st$day_idx
s_3d <- cbind(s_st, t_st)
n_st <- nrow(s_3d)
pred_fun_gam_3d <- function(s_train, y_train, s_new) {
train_df <- data.frame(x = s_train[, 1], y = s_train[, 2], day = s_train[, 3], z = y_train)
fit <- gam(z ~ te(x, y, day, k = c(8, 8, 4)), data = train_df)
new_df <- data.frame(x = s_new[, 1], y = s_new[, 2], day = s_new[, 3])
as.numeric(predict(fit, newdata = new_df))
}
n_reps_st <- 50
covs_st <- numeric(n_reps_st)
wids_st <- numeric(n_reps_st)
for (i in seq_len(n_reps_st)) {
set.seed(SEED + i)
idx_st <- sample(n_st, floor(0.7 * n_st))
out_st <- scp_geostatistical(
s_train = s_3d[idx_st, ],
y_train = y_st[idx_st],
s0 = s_3d[-idx_st, ],
pred_fun = pred_fun_gam_3d,
t_train = t_st[idx_st],
t0 = t_st[-idx_st],
temporal_bandwidth = 5,
alpha = 0.1,
split = 0.5
)
rep_st <- coverage_report(out_st, y_st[-idx_st])
covs_st[i] <- rep_st$coverage
wids_st[i] <- rep_st$mean_width
}
# Save spatio-temporal CSV results
st_results_df <- data.frame(
replication = seq_len(n_reps_st),
coverage = covs_st,
width = wids_st
)
write.csv(st_results_df, file = file.path(OUTPUT_DIR, "spatio_temporal_results.csv"), row.names = FALSE)
cat(sprintf("NY Ozone Spatio-temporal Coverage: %.3f (SD: %.3f)\n", mean(covs_st), sd(covs_st)))
cat(sprintf("NY Ozone Spatio-temporal Width: %.3f (SD: %.3f)\n", mean(wids_st), sd(wids_st)))
#' # Part 8: Final Summary Table & Session Information
#'
#' Final empirical summary table corresponding directly to the manuscript table.
summary_tab <- data.frame(
Dataset = c("Meuse (point-referenced)", "Meuse (aggregated grid)", "NY ozone"),
Type = c("Geostatistical", "Areal", "Spatio-Temporal"),
N_Sample = c(n, n_cells, n_st),
Target_Coverage = c(0.90, 0.80, 0.90),
Empirical_Coverage = c(round(mean(coverages_mc), 3),
round(rep_areal$coverage, 3),
round(mean(covs_st), 3)),
Mean_Width = c(round(mean(widths_mc), 3),
round(rep_areal$mean_width, 3),
round(mean(wids_st), 3))
)
table7_results <- summary_tab
cat("\n==================================================================================\n")
cat(" REPLICATION: Manuscript Table 7 (Summary Across All Benchmark Datasets)\n")
cat("==================================================================================\n")
print(table7_results)
cat("==================================================================================\n\n")
cat("\nOutputs successfully produced in destination folder:\n")
cat(" - Figures: fig1.pdf through fig8.pdf\n")
cat(" - Diagnostics: spconform_diagnostics.rds\n")
cat(" - CSV Results: spatio_temporal_results.csv\n\n")
#' ## Session Information
#'
#' System, architecture, and package versions:
sessionInfo()