| generate_posterior | R Documentation |
Draws from the posterior distribution of model coefficients, with optional dispersion sampling, posterior predictive draws, and propagation of uncertainty in estimated correlation parameters.
generate_posterior(
object,
new_sigmasq_tilde = object$sigmasq_tilde,
new_predictors = NULL,
theta_1 = 0,
theta_2 = 0,
posterior_predictive_draw = function(N, mean, sqrt_dispersion, ...) {
rnorm(N,
mean, sqrt_dispersion)
},
draw_dispersion = TRUE,
include_posterior_predictive = FALSE,
num_draws = 1,
enforce_qp_constraints = TRUE,
draw_correlation = FALSE,
correlation_param_mean = NULL,
correlation_param_vcov = NULL,
correlation_VhalfInv_fxn = NULL,
correlation_Vhalf_fxn = NULL,
correlation_param_vcov_scale = NULL,
include_warnings = TRUE,
...
)
object |
A fitted |
new_sigmasq_tilde |
Numeric; dispersion |
new_predictors |
Matrix; predictor matrix for posterior predictive sampling. Default uses in-sample predictors. |
theta_1 |
Numeric; shape increment for the inverse-gamma prior on
|
theta_2 |
Numeric; rate increment for the inverse-gamma prior. Default 0. |
posterior_predictive_draw |
Function; sampler for posterior predictive
realisations. Must accept |
draw_dispersion |
Logical; sample |
include_posterior_predictive |
Logical; generate posterior predictive
draws at |
num_draws |
Positive integer; number of draws. Default 1. |
enforce_qp_constraints |
Logical; if TRUE, enforce active QP inequality constraints during posterior sampling via the stored elliptical-slice constrained sampler. Default TRUE. |
draw_correlation |
Logical; propagate correlation parameter
uncertainty. Requires |
correlation_param_mean |
Numeric vector; mean of the approximate
normal posterior for correlation parameters on the unbounded
(working) scale. Default: |
correlation_param_vcov |
Matrix; variance-covariance for correlation
parameter draws. Default: inverse Hessian from BFGS
( |
correlation_VhalfInv_fxn |
Function; maps correlation parameter
vector to |
correlation_Vhalf_fxn |
Function or NULL; maps to
|
correlation_param_vcov_scale |
NULL or numeric; if supplied,
divides a user-supplied |
include_warnings |
Logical; emit warnings for degenerate draws, constraint violations, etc. Default TRUE. |
... |
Additional arguments forwarded to the GLM weight function,
dispersion function, and |
Uses a Laplace approximation centred at the MAP estimate for non-Gaussian responses.
Dispersion posterior.
When draw_dispersion = TRUE, \sigma^2 is drawn from
\sigma^2 \mid \mathbf{y} \sim
\mathrm{InvGamma}(\alpha_1, \alpha_2),
where
\alpha_1 = \theta_1 + \tfrac{1}{2}(N - s \cdot \mathrm{tr}(\mathbf{H})),
\quad
\alpha_2 = \theta_2 + \tfrac{1}{2}(N - s \cdot \mathrm{tr}(\mathbf{H}))
\tilde{\sigma}^2,
\mathbf{H} = \mathbf{XUGX}^\top is the hat matrix, s = 1
when unbias_dispersion = TRUE (else s = 0), and
\theta_1 = -1, \theta_2 = 0 recovers an improper uniform prior
on \sigma^2, while \theta_1 = 0, \theta_2 = 0 gives the
usual scale-invariant improper prior p(\sigma^2) \propto 1/\sigma^2.
Correlation parameter posterior.
When draw_correlation = TRUE and the fitted model contains an
estimated correlation structure, each draw first samples
\boldsymbol{\rho} from
\boldsymbol{\rho}^{(m)} \sim
\mathcal{N}(\hat{\boldsymbol{\rho}}_{\mathrm{REML}},
\mathbf{H}^{-1}_{\mathrm{BFGS}}),
rebuilds the posterior covariance under the drawn correlation structure (reusing all pre-computed design matrices, constraints, and penalty matrices) and then draws coefficients from the updated posterior. Knot placement, partitioning, coefficient re-estimation, and GCV tuning are skipped entirely. Draws producing non-positive-definite correlation matrices are rejected and redrawn (up to 50 attempts).
When draw_correlation = FALSE (default), correlation parameters
are fixed at their estimated values.
Inequality constraints.
Active QP inequalities can be enforced during posterior sampling via
elliptical slice sampling, producing draws from the corresponding
truncated multivariate normal posterior on the coefficient scale.
The public enforce_qp_constraints argument is forwarded to the
stored sampler for both the standard and correlation-aware posterior
paths.
When num_draws = 1, a named list:
List of length K+1; per-partition coefficient vectors on the original scale.
Drawn (or fixed) dispersion.
Posterior predictive vector (only when
include_posterior_predictive = TRUE).
Drawn correlation parameters
on the working scale (only when draw_correlation = TRUE).
When num_draws > 1, each element becomes a list of length
num_draws, and post_pred_draw (if requested) is an
N_{\mathrm{new}} \times M matrix, where M = \mathrm{num\_draws}.
lgspline,
generate_posterior_correlation,
wald_univariate
set.seed(1234)
n_blocks <- 100; block_size <- 5; N <- n_blocks * block_size
rho_true <- 0.3
t <- seq(-5, 5, length.out = N)
true_mean <- sin(t)
errors <- Reduce("rbind",
lapply(1:n_blocks, function(i) {
sigma <- diag(block_size) + rho_true *
(matrix(1, block_size, block_size) - diag(block_size))
matsqrt(sigma) %*% rnorm(block_size)
})
)
y <- true_mean + errors * 0.5
model_fit <- lgspline(t, y,
K = 3,
correlation_id = rep(1:n_blocks, each = block_size),
correlation_structure = "exchangeable",
include_warnings = FALSE
)
## Propagate correlation uncertainty across 50 draws
post <- generate_posterior(model_fit,
draw_correlation = TRUE, num_draws = 50,
include_warnings = FALSE
)
## Fixed correlation parameters for comparison
post_fixed <- generate_posterior(model_fit, num_draws = 50)
corr_draws <- unlist(post$post_draw_correlation_params)
rho_draws <- exp(-exp(corr_draws))
print(summary(rho_draws))
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.