View source: R/pred_functions.R
| gen_marginal_samples | R Documentation |
gen_marginal_samples() generates model predictions over a grid of values for one or two specified covariates,
while filling in the remaining covariates either by drawing from the training data (if fixed_x is not provided)
or by using a fixed values for the remaining covariates (if fixed_x is provided). The result is a set of conditional
predictions that can be used to visualize the marginal effect of the selected covariates under varying input configurations.
gen_marginal_samples(
mod,
to_eval,
nsamp = 200,
fixed_x,
n_eval_points,
eval_range,
display_progress = TRUE
)
mod |
A |
to_eval |
A character vector specifying the names of the covariates to evaluate. Can be one or two variables. |
nsamp |
Positive integer specifying the number of posterior samples to generate. Default is 200. |
fixed_x |
optional data frame specifying a fixed covariate configuration. If provided, this configuration is used for all nonswept covariates. If omitted, covariates are sampled from the training data. |
n_eval_points |
Positive integer specifying the number of evaluation points along each axis. If missing, defaults to 100 for 1D and 30 for 2D evaluations. |
eval_range |
optional numeric vector (1D) or list of two numeric vectors (2D) specifying the range over which to evaluate
the covariates in |
display_progress |
logical value indicating whether to display progress bars and messages during training. The default is |
This function generates conditional predictive surfaces by evaluating the fitted model across a grid of values for one or two selected covariates.
For each of the nsamp draws, the remaining covariates are either held fixed (if fixed_x is provided) or filled in by sampling a single row from the training data.
The selected covariates in to_eval are then varied across a regular grid defined by n_eval_points and eval_range, and model predictions are computed using calc_pred_moments.
The resulting samples represent conditional predictions across different covariate contexts, and can be used to visualize marginal effects, interaction surfaces, or predictive uncertainty.
Note that computational and memory requirements increase rapidly with grid size. In particular, for two-dimensional evaluations, the
kernel matrix scales quadratically with the number of evaluation points per axis. Large values of n_eval_points may lead to high
memory usage during prediction, especially when using a GPU. If memory constraints arise, consider reducing n_eval_points.
A list containing posterior predictive summaries over the evaluation grid:
mean_pred: A matrix (1D case) or array (2D case) of predicted means for each evaluation point and posterior sample.
grid: The evaluation grid used to generate predictions. A numeric vector (1D) or a named list of two vectors (grid1, grid2) for 2D evaluations.
if (torch::torch_is_installed()) {
# Simulate data
set.seed(123)
torch::torch_manual_seed(123)
n <- 100
x <- matrix(runif(n * 2), n, 2)
y <- sin(2 * pi * x[, 1]) + rnorm(n, sd = 0.1)
data <- data.frame(y = y, x1 = x[, 1], x2 = x[, 2])
# Fit GPR model
res <- shrinkGPR(y ~ x1 + x2, data = data)
# Generate posterior samples
samps <- gen_posterior_samples(res, nsamp = 1000)
# Plot the posterior samples
boxplot(samps$thetas)
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.