Split conformal intervals from a pre-fit MetaHunt pipeline
Source:R/conformal.R
conformal_from_fit.RdLower-level entry point that builds split conformal intervals for new
target covariates using an already-fitted d-fSPA basis decomposition,
an already-fitted weight model, and a user-supplied calibration set.
Use this when you have independently tuned K or want to reuse a
pipeline fit; otherwise the high-level split_conformal() is usually
more convenient.
Usage
conformal_from_fit(
dfspa_fit,
weight_model,
F_cal,
W_cal,
W_new,
alpha = 0.05,
wrapper = NULL,
grid_weights = NULL
)Arguments
- dfspa_fit
A
"dfspa"object fromdfspa().- weight_model
A
"metahunt_weight_model"object fromfit_weight_model(). Must satisfyweight_model$K == nrow(dfspa_fit$bases).- F_cal
An
n_cal-by-G_gridnumeric matrix of observed study-level function evaluations for the calibration set. Calibration studies must be independent of bothdfspa_fitandweight_modelfor valid coverage.- W_cal
A matrix or data frame of study-level covariates for the calibration set, with the same columns used to fit
weight_model.- W_new
A matrix or data frame of study-level covariates for new target studies.
- alpha
Miscoverage level; default
0.05.- wrapper
Optional reduction function (see
apply_wrapper()). IfNULL, intervals are pointwise at every grid point.- grid_weights
Optional length-
G_gridnon-negative numeric vector used for the default wrapper and for weighted inner products.
Value
An object of class "metahunt_conformal"; see split_conformal()
for a description of its fields.
See also
split_conformal() for the high-level version that splits and
fits internally, cross_conformal() for the K-fold variant.
Examples
set.seed(1)
G <- 30; m <- 80
x <- seq(0, 1, length.out = G)
basis <- rbind(sin(pi * x), cos(pi * x), x)
W <- data.frame(w1 = rnorm(m))
eta <- cbind(0.8 * W$w1, -0.4 * W$w1, rep(0, m))
pi_true <- exp(eta) / rowSums(exp(eta))
F_hat <- pi_true %*% basis + matrix(rnorm(m * G, sd = 0.05), m, G)
# user-controlled split and fit
tr <- 1:50; cal <- 51:70; new <- 71:80
fit <- dfspa(F_hat[tr, ], K = 3)
pih <- project_to_simplex(F_hat[tr, ], fit$bases)
wm <- fit_weight_model(pih, W[tr, , drop = FALSE])
res <- conformal_from_fit(
fit, wm,
F_cal = F_hat[cal, ], W_cal = W[cal, , drop = FALSE],
W_new = W[new, , drop = FALSE],
wrapper = mean
)
res
#> MetaHunt conformal prediction
#> method: split
#> alpha: 0.05
#> n calibration: 20
#> mode: scalar (via wrapper)
#> n targets: 10
#> quantile: 0.1492