Probability-integral-transform calibration check for h-step forecasts
Source:R/forecast_pit.R
forecast_pit.RdFor a fit with at least one dynamic component, evaluates how well the
h-step posterior-predictive distribution covers the actually
observed outcomes at the corresponding period(s). A well-calibrated
forecast produces approximately Uniform(0, 1) PIT values.
Usage
forecast_pit(
fit,
y_future,
h = NULL,
n_draws = NULL,
newdata = NULL,
newdesign = NULL,
newexposure = NULL
)Arguments
- fit
A fitted
lameorlame_robustobject trained on the firstT - hperiods (the function does not refit; the user is responsible for the train/forecast split).- y_future
A list of length
hof held-out observed matrices, one per forecast period. The same shape as the fitted actor grid. Named periods are aligned to shared actors; unshared actors and missing outcomes are excluded. Unipartite self cells are excluded and symmetric networks score each unordered dyad once. Bipartite grids retain cells whose row and column positions coincide.- h
The forecast horizon (length of
y_future). Inferred fromlength(y_future)when not supplied.- n_draws
Number of posterior draws to use for the forecast. Default uses all stored draws.
- newdata
Future dyadic covariate arrays passed to the forecaster. Required for robust fits that contain dyadic covariates, as in
predict.lame_robust().- newdesign
Full prepared future designs, including nodal roles. Supported only by ordinary lame fits.
- newexposure
Future Poisson exposures, one per horizon. Defaults to the last fitted exposure. Zero exposure defines a degenerate zero-count law.
Value
A list of class forecast_pit with components
pit (numeric vector), ks_stat (KS statistic against
Uniform), ks_p (NA; a dependent-network null reference is
unavailable), cover_95 (fraction of
PIT values in [0.025, 0.975]), matched draw_index, predictive
cdf_lower/cdf_upper, and scored actor/period cells.
Network cells need not be independent. The KS statistic, histogram and coverage
are descriptive. No global model-adequacy p-value is reported without a
calibrated joint network reference. ks_inference records this restriction.
Details
Workflow: fit on periods 1:(T - h), forecast h steps
ahead, score against the held-out T - h + 1 : T observations.
Returns a numeric vector of PIT values (one per observed dyad in the
held-out periods), plus a one-row summary with the Kolmogorov-Smirnov
statistic against Uniform(0, 1) and a fraction-of-observed-dyads
coverage diagnostic.
Families. For family = "normal", the
PIT averages the Gaussian CDF over matched predictor and residual-variance
draws. For
discrete families ("binary", "poisson", "ordinal")
the randomised PIT of Czado, Gneiting & Held (2009) is used. Poisson forecasts
integrate the Gaussian observation effect and future exposure. Ordinal scoring
requires saved explicit posterior cutpoints; data-induced joint rank simulation
does not define a fixed future threshold law.
Robust fits from lame_robust() use their genuine future latent states,
matched persistent pair precisions and Gaussian or transient Student residual CDF.
Fitted temporary observation precisions are never reused for future outcomes.
Plotting. The companion plot() method on the returned
object renders a histogram with a uniform reference line. Use
ggplot2::ggplot(pit$pit) + geom_histogram() for custom plots.
References
Czado, C., Gneiting, T., & Held, L. (2009). Predictive model assessment for count data. Biometrics, 65(4), 1254-1261.
Examples
# \donttest{
data(YX_bin_list)
Y_train <- lapply(YX_bin_list$Y, function(y) 1 * (y > 0))[1:3]
Y_test <- lapply(YX_bin_list$Y, function(y) 1 * (y > 0))[4]
X_train <- YX_bin_list$X[1:3]
# chains = 1 keeps the example short; the default runs two chains and compares them
fit <- lame(Y_train, Xdyad = X_train,
family = "binary", R = 0,
dynamic_ab = TRUE,
nscan = 60, burn = 15, odens = 5, verbose = FALSE, chains = 1)
pit <- forecast_pit(fit, y_future = Y_test)
pit$ks_stat
#> [1] 0.1847729
plot(pit)
# }