| rlmb | R Documentation |
rlmb is used to generate iid samples from Bayesian Linear Models with multivariate normal priors.
The model is specified by providing a data vector, a design matrix, and a pfamily (determining the
prior distribution).
rlmb(
n = 1,
y,
x,
pfamily,
offset = rep(0, nobs),
weights = NULL,
Gridtype = 2,
n_envopt = NULL,
use_parallel = TRUE,
use_opencl = FALSE,
verbose = FALSE,
progbar = FALSE
)
## S3 method for class 'rlmb'
print(x, digits = max(3, getOption("digits") - 3), ...)
n |
number of draws to generate. If |
y |
a vector of observations of length |
x |
for |
pfamily |
a description of the prior distribution and associated constants to be used in the model. This
should be a pfamily function (see |
offset |
this can be used to specify an a priori known component to be
included in the linear predictor during fitting. This should be |
weights |
an optional vector of weights to be used in the fitting
process. Should be |
Gridtype |
an optional argument specifying the method used to determine the number of tangent points used to construct the enveloping function. |
n_envopt |
Effective sample size passed to EnvelopeOpt for grid
construction. Defaults to match |
use_parallel |
Logical. Whether to use parallel processing during simulation. |
use_opencl |
Logical. Whether to use OpenCL acceleration during Envelope construction. |
verbose |
Logical. Whether to print progress messages. |
progbar |
Logical. Whether to display a progress base during simulation. |
digits |
the number of significant digits to use when printing. |
... |
For |
The function rlmb is a minimalistic Bayesian simulation engine for Gaussian linear models.
It bypasses classical model fitting and formula parsing, operating directly on numeric inputs such as
the design matrix, response vector, and prior specification via the pfamily argument.
Internally, rlmb generates independent draws from the posterior distribution using multivariate
normal simulation when conjugate priors are specified.
The modeling framework follows \insertCiteWilkinsonRogers1973glmbayes, and the prior structure builds on the S system \insertCiteChambers1992glmbayes, Zellner's g-prior \insertCitezellner1986gpriorglmbayes, and the conjugate prior formulation of Raiffa and Schlaifer \insertCiteRaiffa1961glmbayes.
Prior specification is handled via the pfamily argument, which defines the prior mean,
covariance, and dispersion. The design of the pfamily family of functions was created by Kjell Nygren
and is modeled on how glm uses family to specify the likelihood. A helper function,
Prior_Setup, assists users in choosing prior parameters. It ships with sensible defaults but
also allows full customization. Available priors include the dNormal, dNormalGamma and
dIndependent_Normal_Gamma priors. The last of these allows for more flexible prior structures
including independent priors on variance components.
Posterior draws are generated using standard simulation procedures for conjugate priors \insertCiteRaiffa1961glmbayes.
For non-conjugate setups, the function uses envelope-based accept-reject sampling via the
likelihood-subgradient method \insertCiteNygren2006glmbayes. The Gridtype parameter controls
how many tangent points are used to construct the envelope-trading off tightness against computational cost-
and the iters component reports the number of candidate samples generated before acceptance.
The output includes posterior samples, prior specifications, dispersion estimates, and envelope diagnostics.
While rlmb does not return a full model object or support generic methods like predict or
summary, it is designed for efficient posterior simulation in Gaussian models where full model
reconstruction is unnecessary.
The rlmb function called from within lmb.
It is intended for simulation-heavy workflows such as Gibbs sampling or posterior
predictive checks where minimal overhead is preferred.
rlmb returns a object of class "rlmb". The function summary
(i.e., summary.rglmb) can be used to obtain or print a summary of the results.
The generic accessor functions coefficients, fitted.values,
residuals, and extractAIC can be used to extract
various useful features of the value returned by rlmb.
An object of class "rlmb" is a list containing at least the following components:
coefficients |
a matrix of dimension |
coef.mode |
a vector of |
dispersion |
Either a constant provided as part of the call, or a vector of length |
Prior |
A list with the priors specified for the model in question. Items in the list may vary based on the type of prior |
prior.weights |
a vector of weights specified or implied by the model |
y |
a vector with the dependent variable |
x |
a matrix with the implied design matrix for the model |
famfunc |
Family functions used during estimation process |
iters |
an |
Envelope |
the envelope that was used during sampling |
Objects of class "rlmb" are normally of class c("rlmb","rglmb","glmb","glm","lm"),
meaning they inherit from rglmb, glmb, glm, and lm. Well-designed
methods for these classes will be applied when appropriate, allowing "rlmb" objects to
benefit from existing infrastructure while supporting specialized behavior for restricted linear
model priors.
The classical modeling functions lm and glm.
lmb, glmb, rglmb
for related interfaces;
EnvelopeBuild, EnvelopeOrchestrator for envelope stages
used in non-conjugate Gaussian sampling.
pfamily for documentation of pfamily functions used to specify priors.
Prior_Setup, Prior_Check for functions used to initialize and to check priors,
Further reading: \insertCiteNygren2006glmbayes; \insertCiteglmbayesSimmethods,glmbayesChapterA08,glmbayesIndNormGammaVignetteglmbayes.
summary.glmb, predict.glmb, simulate.glmb,
extractAIC.glmb, dummy.coef.glmb and methods(class="glmb") for methods
inherited from class glmb and the methods and generic functions for classes glm and
lm from which class lmb also inherits.
glmbayes Modeling Functions
glmb(),
lmb(),
rglmb()
## Main Example based on Dobson Plant Weight Data
## Use demo(Ex_07_Schools) for a longer/more complex model
## Annette Dobson (1990) "An Introduction to Generalized Linear Models".
## Page 9: Plant Weight Data.
ctl <- c(4.17, 5.58, 5.18, 6.11, 4.50, 4.61, 5.17, 4.53, 5.33, 5.14)
trt <- c(4.81, 4.17, 4.41, 3.59, 5.87, 3.83, 6.03, 4.89, 4.32, 4.69)
group <- gl(2, 10, 20, labels = c("Ctl", "Trt"))
weight <- c(ctl, trt)
ps <- Prior_Setup(weight ~ group)
x <- ps$x
mu <- ps$mu
V <- ps$Sigma
y <- ps$y
shape <- ps$shape
rate <- ps$rate
rate_dg <- if (!is.null(ps$rate_gamma)) ps$rate_gamma else rate
## Two-Block Gibbs sampler for Plant Weight regression model
set.seed(180)
## Note: iteration counts reduced for CRAN checks; increase for production use
n_burnin <- 200
n_samples <- 200
## Initilize dispersion to ML estimate
dispersion2 <- ps$dispersion
## Run burn-in iterations
for (i in 1:n_burnin) {
## Update block for regression coefficients
out1 <- rlmb( n = 1, y = y, x = x,
pfamily = dNormal(mu = mu, Sigma = V, dispersion = dispersion2) )
## Update block for dispersion
out2 <- rlmb(n = 1, y = y, x = x,
pfamily = dGamma(shape = shape, rate = rate_dg, beta = out1$coefficients[1, ]))
dispersion2 <- out2$dispersion
}
## Create Objects to store outputs
beta_out <- matrix(0, nrow = n_samples, ncol = 2)
disp_out <- rep(0, n_samples)
for (i in 1:n_samples) {
## Update block for regression coefficients
out1 <- rlmb( n = 1, y = y, x = x,
pfamily = dNormal(mu = mu, Sigma = V, dispersion = dispersion2) )
## Update block for dispersion
out2 <- rlmb(n = 1, y = y, x = x,
pfamily = dGamma(shape = shape, rate = rate_dg, beta = out1$coefficients[1, ]))
dispersion2 <- out2$dispersion
## Store output
beta_out[i, 1:2] <- out1$coefficients[1, 1:2]
disp_out[i] <- out2$dispersion
}
mcmc_two_block <- coda::mcmc(cbind( beta1 = beta_out[, 1],beta2 = beta_out[, 2],
dispersion = disp_out ))
## Review output
cat("\nCODA summary (Two-block Gibbs):\n")
print(summary(mcmc_two_block))
cat("\nEffective sample size (dispersion):\n")
print(coda::effectiveSize(mcmc_two_block)["dispersion"])
## Same model using the lmb function
lmb.D9 <- lmb(n= 1000,weight ~ group,
pfamily = dIndependent_Normal_Gamma(ps$mu, ps$Sigma, shape = ps$shape_ING, rate = ps$rate))
## lmb summary
summary(lmb.D9)
## rlmb with dGamma prior (dispersion-only; coefficients fixed)
out_rlmb_dGamma <- rlmb(n = 100, y = y, x = x,
pfamily = dGamma(shape = shape, rate = rate_dg, beta = ps$coefficients),
weights = rep(1, length(y)))
summary(out_rlmb_dGamma)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.