neuralGAM objectR/predict.NeuralGAM.R
predict.NeuralGAM.RdGenerate predictions from a fitted neuralGAM model. Supported types:
type = "link" (default): linear predictor on the link scale.
type = "response": predictions on the response scale.
type = "terms": per-term contributions to the linear predictor (no intercept).
Uncertainty estimation via MC Dropout (epistemic only)
If se.fit = TRUE, standard errors (SE) of the fitted mean are returned
(mgcv-style via Monte Carlo Dropout).
For type = "response", SEs are mapped to the response scale by the delta method:
\(se_\mu = |d\mu/d\eta| \cdot se_\eta\).
interval = "confidence" returns CI bands derived from SEs; prediction intervals are not supported.
For type = "terms", interval="confidence" returns per-term CI matrices (and se.fit when requested).
Details
Epistemic SEs (CIs) are obtained via Monte Carlo Dropout. When type != "terms"
and SEs/CIs are requested in the presence of smooth terms, uncertainty is aggregated
jointly to capture cross-term covariance in a single MC pass set. Otherwise,
per-term variances are used (parametric variances are obtained from stats::predict(..., se.fit=TRUE)).
For type="terms", epistemic SEs and CI matrices are returned when requested.
PIs are not defined on the link scale and are not supported.
A fitted neuralGAM object.
Optional data.frame/list of covariates at which to predict.
If omitted, the training data cached in the object are used.
One of c("link","response","terms"). Default "link".
If type = "terms", character vector of term names to include.
If NULL, all terms are returned. Intercept is not included (as in mgcv).
Logical; if TRUE, return SEs of the fitted mean (epistemic). Default FALSE.
For type="terms", returns a matrix of per-term SEs when available.
One of c("none","confidence") (default "none"). For type="terms",
setting interval="confidence" returns per-term CI matrices.
Coverage level for confidence intervals (e.g., 0.95). Default 0.95.
Integer; number of MC-dropout forward passes when computing epistemic uncertainty.
Integer (0/1). Default 1.
Other options (passed on to internal predictors).
type="terms":
interval="none": matrix of per-term contributions; if se.fit=TRUE, a list with $fit, $se.fit.
interval="confidence": a list with matrices $fit, $se.fit, $lwr, $upr.
type="link" or type="response":
interval="none": vector (or list with $fit, $se.fit if se.fit=TRUE).
interval="confidence": data.frame with fit, lwr, upr.
# \dontrun{
library(neuralGAM)
dat <- sim_neuralGAM_data()
train <- dat$train
test <- dat$test
ngam0 <- neuralGAM(
y ~ s(x1) + x2 + s(x3),
data = train, family = "gaussian",
num_units = 128, uncertainty_method = "epistemic"
)
#> [1] "Initializing neuralGAM..."
#> [1] "BACKFITTING Iteration 1 - Current Err = 0.0256878026356043 BF Threshold = 0.001 Converged = FALSE"
#> [1] "BACKFITTING Iteration 2 - Current Err = 0.00191428881847602 BF Threshold = 0.001 Converged = FALSE"
#> [1] "BACKFITTING Iteration 3 - Current Err = 0.00227093850530566 BF Threshold = 0.001 Converged = FALSE"
#> [1] "BACKFITTING Iteration 4 - Current Err = 0.00297710805512887 BF Threshold = 0.001 Converged = FALSE"
#> [1] "BACKFITTING Iteration 5 - Current Err = 0.00226034835167828 BF Threshold = 0.001 Converged = FALSE"
#> [1] "BACKFITTING Iteration 6 - Current Err = 0.00122839683827546 BF Threshold = 0.001 Converged = FALSE"
#> [1] "BACKFITTING Iteration 7 - Current Err = 0.00032701749332347 BF Threshold = 0.001 Converged = TRUE"
#> [1] "Computing CI/PI using uncertainty_method = epistemic at alpha = 0.05"
link_ci <- predict(ngam0, type = "link", interval = "confidence",
level = 0.95, forward_passes = 10)
resp_ci <- predict(ngam0, type = "response", interval = "confidence",
level = 0.95, forward_passes = 10)
trm_se <- predict(ngam0, type = "terms",
se.fit = TRUE, forward_passes = 10)
# }