| rglmb | R Documentation |
rglmb is used to generate iid samples for Bayesian Generalized Linear Models.
The model is specified by providing a data vector, a design matrix,
the family (determining the likelihood function) and the pfamily (determining the
prior distribution).
rglmb(
n = 1,
y,
x,
family = gaussian(),
pfamily,
offset = NULL,
weights = 1,
Gridtype = 2,
n_envopt = NULL,
use_parallel = TRUE,
use_opencl = FALSE,
verbose = FALSE
)
## S3 method for class 'rglmb'
print(x, digits = max(3, getOption("digits") - 3), ...)
n |
number of draws to generate. If |
y |
a vector of observations of length |
x |
for |
family |
a description of the error distribution and link
function to be used in the model. 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 ‘prior 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. |
digits |
the number of significant digits to use when printing. |
... |
For For |
The function rglmb is a minimalistic engine for Bayesian generalized linear model simulation.
It is designed to generate independent draws from the posterior distribution of a GLM, given a design matrix,
response vector, likelihood family, and prior specification. Unlike glmb, which wraps formula parsing,
model setup, and method dispatch, rglmb operates directly on numeric inputs and is optimized for speed,
transparency, and integration into simulation workflows.
The original R implementation of glm was written by Simon Davies (under Ross Ihaka at the University of Auckland)
and has since been extensively rewritten by members of the R Core Team; its design was inspired by the S function
described in \insertCiteHastie1992glmbayes, which in turn relies on the formula framework described in
\insertCiteWilkinsonRogers1973glmbayes.
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. For any implemented combination of family, link, and
pfamily, rglmb generates independent draws from the posterior density-no MCMC chains are required.
A helper, Prior_Setup, assists users in choosing prior parameters. It ships with sensible defaults
but also allows full customization. In particular, the default for dNormal is a reparameterization of
Zellner's g-prior \insertCitezellner1986gpriorglmbayes.
Currently supported response families are gaussian (identity link), poisson and quasipoisson
(log link), gamma (log link), and binomial and quasibinomial (logit, probit, cloglog).
All families support a dNormal prior; the Gaussian family also offers dNormalGamma and
dIndependent_Normal_Gamma.
For the Gaussian family, draws under dNormal and dNormalGamma come from posterior distributions
resulting from conjugate prior distributions \insertCiteRaiffa1961glmbayes. For all other priors or response families,
we use an accept-reject sampler built on the likelihood-subgradient envelope method
\insertCiteNygren2006glmbayes. The Gridtype argument controls how many tangent points are used
in the envelope-trading off envelope tightness against construction cost-and iters reports candidate
counts before acceptance.
By default, rglmb draws n = 1 sample, uses parallel CPU simulation, and-if use_opencl = TRUE-
GPU-accelerated envelope building. The function returns a list containing posterior samples, prior specifications,
dispersion estimates, and the envelope used during sampling. It does not return a full model object, and does not
support formula-based modeling or method dispatch. Instead, it is called internally by glmb and
and may be useful for Gibbs sampling implementations or other workflows where full model
reconstruction is unnecessary.
rglmb returns a object of class "rglmb". 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 rglmb.
An object of class "rglmb" 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 "rglmb" are normally of class c("rglmb","glmb","glm","lm"),
meaning they inherit from glmb, glm, and lm. This allows methods defined
for these upstream classes to be applied to "rglmb" objects when appropriate, while
supporting extensions for regularized Bayesian GLMs with structured priors.
The R implementation of rglmb has been written by Kjell Nygren and
was built to be a Bayesian version of the glm function but with a more minimalistic interface
than the glmb function. It also borrows some of its structure from other random generating function
like rnorm and hence the r prefix.
glmb for the formula interface; lm and
glm for classical modeling functions.
EnvelopeBuild, EnvelopeSize, EnvelopeEval
for envelope construction and grid evaluation used in non-conjugate sampling.
family for documentation of family functions used to specify priors.
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; OpenCL/GPU: \insertCiteglmbayesChapter12,glmbayesChapterA10glmbayes.
summary.glmb, predict.glmb, residuals.glmb, simulate.glmb,
extractAIC.glmb, dummy.coef.glmb and methods(class="glmb") for glmb
and the methods and generic functions for classes glm and lm from which class glmb inherits.
glmbayes Modeling Functions
glmb(),
lmb(),
rlmb()
set.seed(333)
## Dobson (1990) Page 93: Randomized Controlled Trial :
counts <- c(18, 17, 15, 20, 10, 20, 25, 13, 12)
outcome <- gl(3, 1, 9)
treatment <- gl(3, 3)
print(d.AD <- data.frame(treatment, outcome, counts))
## Classical Model
glm.D93 <- glm(counts ~ outcome + treatment, family = poisson(link = log))
summary(glm.D93)
## Poisson prior and rglmb (same prior as \code{\link{glmb}} with \code{\link{Prior_Setup}})
ps <- Prior_Setup(counts ~ outcome + treatment, family = poisson(), data = d.AD)
rglmb.D93 <- rglmb(
n = 1000,
y = ps$y,
x = as.matrix(ps$x),
pfamily = dNormal(mu = ps$mu, Sigma = ps$Sigma),
family = poisson(),
weights = rep(1, nrow(ps$x))
)
summary(rglmb.D93)
## Menarche model with informative prior. See \code{\link{glmb}} and \code{\link{Prior_Setup}}
## for default g-priors; for data and fitted curves using \code{predict.glmb}, see the
## menarche block in \code{\link{glmb}};
## see \code{vignette("Chapter-05", package = "glmbayes")}
## and \code{vignette("Chapter-06", package = "glmbayes")}.
data(menarche, package = "MASS")
summary(menarche)
design_df <- data.frame(
Age = menarche$Age,
Age2 = menarche$Age - 13,
Proportion = menarche$Menarche / menarche$Total,
Total = menarche$Total
)
x <- model.matrix(~ Age2, data = design_df)
y <- design_df$Proportion
wt <- design_df$Total
# Extract coefficient names from design matrix
coef_names <- colnames(x)
# Set up prior mean with names
mu <- matrix(0, nrow = length(coef_names), ncol = 1)
mu[2, 1] <- (log(0.9 / 0.1) - log(0.5 / 0.5)) / 3
rownames(mu) <- coef_names
# Set up prior covariance matrix with named rows and columns
V1 <- 1 * diag(as.numeric(2.0))
# 2 standard deviations for prior estimate at age 13 between 0.1 and 0.9
## Specifies uncertainty around the point estimates
V1[1, 1] <- ((log(0.9 / 0.1) - log(0.5 / 0.5)) / 2)^2
V1[2, 2] <- (3 * mu[2, 1] / 2)^2 # Allows slope to be up to 3 times as large as point estimate
rownames(V1) <- coef_names
colnames(V1) <- coef_names
out <- rglmb(
n = 1000, y = y, x = x, pfamily = dNormal(mu = mu, Sigma = V1), weights = wt,
family = binomial(logit)
)
summary(out)
## rglmb with dGamma prior (dispersion-only; coefficients fixed)
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_dg <- Prior_Setup(weight ~ group, family = gaussian())
rate_dg <- if (!is.null(ps_dg$rate_gamma)) ps_dg$rate_gamma else ps_dg$rate
out_dGamma <- rglmb(
n = 100, y = ps_dg$y, x = as.matrix(ps_dg$x),
pfamily = dGamma(shape = ps_dg$shape, rate = rate_dg, beta = ps_dg$coefficients),
weights = rep(1, length(ps_dg$y)), family = gaussian()
)
summary(out_dGamma)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.