| lmb | R Documentation |
lmb is used to fit Bayesian linear models, specified by giving a symbolic descriptions of the linear
predictor and the prior distribution.
lmb(
formula,
pfamily,
n = 1000,
data,
subset,
weights,
na.action,
method = "qr",
model = TRUE,
x = TRUE,
y = TRUE,
qr = TRUE,
singular.ok = TRUE,
contrasts = NULL,
offset,
Gridtype = 2,
n_envopt = NULL,
use_parallel = TRUE,
use_opencl = FALSE,
verbose = FALSE,
...
)
## S3 method for class 'lmb'
print(x, digits = max(3, getOption("digits") - 3), ...)
formula |
an object of class |
pfamily |
a description of the prior distribution and associated constants to be used in the model.
For a single-response formula this should be a single |
n |
number of draws to generate. If |
data |
an optional data frame, list or environment (or object
coercible by |
subset |
an optional vector specifying a subset of observations to be used in the fitting process. |
weights |
an optional vector of weights to be used in the fitting
process. Should be |
na.action |
a function which indicates what should happen when the data contain |
method |
the method to be used in fitting the classical model during a call to |
model, x, y, qr |
logicals. If |
singular.ok |
logical. If |
contrasts |
an optional list. See the |
offset |
this can be used to specify an a priori known component to be
included in the linear predictor during fitting. This 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. |
... |
For |
digits |
the number of significant digits to use when printing. |
The function lmb is a Bayesian extension of the classical lm function.
It retains the familiar formula interface and model setup used in lm, while introducing
posterior simulation and prior specification via the pfamily argument. Internally,
lmb calls lm to obtain the classical least squares fit, then generates independent
draws from the posterior distribution using either multivariate normal simulation (for Gaussian priors)
or accept-reject sampling via likelihood-subgradient envelopes \insertCiteNygren2006glmbayes.
The symbolic formula interface follows \insertCiteWilkinsonRogers1973glmbayes, and the overall design of lm was inspired by the S system
\insertCiteChambers1992glmbayes. lmb comes with many of the same types of generic methods
that are available to lm and glm, including predict, residuals, extractAIC,
and summary. Many of these are inherited from glmb.
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. All models support the dNormal prior; the Gaussian family also supports
dNormalGamma and dIndependent_Normal_Gamma, which allow for more flexible prior structures
including independent priors on variance components.
Posterior draws are generated using the prior specification provided via pfamily. For Gaussian models
with conjugate priors, draws are obtained directly from the posterior distribution using standard simulation
procedures for multivariate normal densities \insertCiteRaiffa1961glmbayes. For non-conjugate setups, the
function uses envelope-based accept-reject sampling, where the Gridtype parameter controls the granularity
of the envelope construction. The number of candidates generated before acceptance is returned in the
iters component.
The output includes both the classical lm fit and Bayesian diagnostics such as the Deviance
Information Criterion (DIC), effective number of parameters (pD), and posterior summaries.
The DIC, introduced by \insertCiteSpiegelhalter2002glmbayes, provides a Bayesian analog to AIC
by balancing model fit and complexity using posterior expectations. This dual structure allows users
to compare classical and Bayesian fits side-by-side, and to leverage familiar modeling workflows
while gaining access to richer inferential tools.
The lmb function is a specialized version of glmb for Gaussian models,
and does not require a family argument. For conjugate models, it uses standard simulation methods for
posterior draws, avoiding the need for envelope construction or subgradient sampling.
Like glmb, it returns objects compatible with many standard methods from lm and glm,
including extractAIC, fitted.values, and residuals.
For more minimalistic workflows, rlmb and rglmb offer stripped-down interfaces
for posterior sampling without the overhead of full model objects. rlmb is called from within
lmb. The functions rlmb might be useful in Gibbs sampling or simulation-heavy contexts.
lmb returns an object of class "lmb". The function summary (i.e.,
summary.glmb) 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 lmb.
An object of class "lmb" is a list containing at least the following components:
lm |
an object of class |
coefficients |
a matrix of dimension |
coef.means |
a vector of |
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 list may vary based on the type of prior |
residuals |
a matrix of dimension |
fitted.values |
a matrix of dimension |
linear.predictors |
an |
deviance |
an |
pD |
An Estimate for the effective number of parameters |
Dbar |
Expected value for minus twice the log-likelihood function |
Dthetabar |
Value of minus twice the log-likelihood function evaluated at the mean value for the coefficients |
DIC |
Estimated Deviance Information criterion |
weights |
a vector of weights specified or implied by the model |
prior.weights |
a vector of weights specified or implied by the model |
y |
a vector of observations of length |
x |
a design matrix of dimension |
model |
if requested (the default),the model frame |
call |
the matched call |
formula |
the formula supplied |
terms |
the |
data |
the |
famfunc |
family functions used during estimation and post processing |
iters |
an |
contrasts |
(where relevant) the contrasts used. |
xlevels |
(where relevant) a record of the levels of the factors used in fitting |
pfamily |
the prior family specified |
digits |
the number of significant digits to use when printing. |
In addition, non-empty fits will have (yet to be implemented) components qr, R
and effects relating to the final weighted linear fit for the posterior mode.
Objects of class "lmb" are normally of class c("lmb","glmb","glm","lm"),
that is inherit from classes glmb. glm and lm and well-designed
methods from those classed will be applied when appropriate.
The R implementation of lmb has been written by Kjell Nygren and
was built to be a Bayesian version of the lm function and hence tries
to mirror the features of the lm function to the greatest extent possible while also taking advantage
of some of the method functions developed for the glmb function. For details
on the author(s) for the lm function see the documentation for lm.
The classical modeling functions lm and glm.
glmb, rglmb, rlmb
for related Bayesian GLM/linear interfaces;
EnvelopeBuild for envelope construction when accept–reject sampling is used.
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,glmbayesChapterA08glmbayes; independent Normal–Gamma sampler: \insertCiteglmbayesIndNormGammaVignetteglmbayes.
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(),
rglmb(),
rlmb()
## 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, gaussian())
# Prior_Setup supplies the prior mean and covariance components used below.
## Classical model
lm.D9 <- lm(weight ~ group, x = TRUE, y = TRUE)
summary(lm.D9)
vcov(lm.D9)
s <- summary(lm.D9)
disp_classical <- s$sigma^2
cat("Classical lm dispersion (sigma^2 = RSS/(n-p)):", disp_classical, "\n")
## Conjugate Normal Prior (fixed dispersion)
lmb.D9 <- lmb(
weight ~ group,
pfamily = dNormal(mu = ps$mu, ps$Sigma, dispersion = ps$dispersion)
)
summary(lmb.D9)
vcov(lmb.D9)
## Conjugate Normal_Gamma Prior (second argument is Sigma_0 from Prior_Setup)
lmb.D9_v2 <- lmb(
weight ~ group,
pfamily = dNormal_Gamma(
ps$mu,
Sigma_0 = ps$Sigma_0,
shape = ps$shape,
rate = ps$rate
)
)
summary(lmb.D9_v2)
vcov(lmb.D9_v2)
## Independent_Normal_Gamma_Prior (same mu, Sigma, rate as dNormal_Gamma; shape = ps$shape_ING)
lmb.D9_v3 <- lmb(
weight ~ group,
dIndependent_Normal_Gamma(
ps$mu,
ps$Sigma,
shape = ps$shape_ING,
rate = ps$rate
)
)
summary(lmb.D9_v3)
## anova
anova(lmb.D9)
## lmb with dGamma prior (dispersion-only; coefficients fixed)
rate_dg <- if (!is.null(ps$rate_gamma)) ps$rate_gamma else ps$rate
out_lmb_dGamma <- lmb(
weight ~ group,
pfamily = dGamma(shape = ps$shape, rate = rate_dg, beta = ps$coefficients))
summary(out_lmb_dGamma)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.