| glmb | R Documentation |
glmb is used to fit Bayesian generalized linear models, specified by giving a symbolic descriptions of
the linear predictor, the error distribution, and the prior distribution.
glmb(
formula,
family = binomial,
pfamily = dNormal(mu, Sigma, dispersion = 1),
n = 1000,
data,
weights,
use_parallel = TRUE,
use_opencl = FALSE,
verbose = FALSE,
subset,
offset,
na.action,
Gridtype = 2,
n_envopt = NULL,
start = NULL,
etastart,
mustart,
control = list(...),
model = TRUE,
method = "glm.fit",
x = FALSE,
y = TRUE,
contrasts = NULL,
...
)
## S3 method for class 'glmb'
print(x, digits = max(3, getOption("digits") - 3), ...)
formula |
an object of class |
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 |
n |
number of draws to generate. If |
data |
an optional data frame, list or environment (or object
coercible by |
weights |
an optional vector of ‘prior weights’ to be used
in the fitting process. Should be |
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. |
subset |
an optional vector specifying a subset of observations to be used in the fitting process. |
offset |
this can be used to specify an a priori known component to be included in the linear
predictor during fitting. This should be |
na.action |
a function which indicates what should happen when the data contain |
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 |
start |
starting values for the parameters in the linear predictor. |
etastart |
starting values for the linear predictor. |
mustart |
starting values for the vector of means. |
control |
a list of parameters for controlling the fitting
process. For |
model |
a logical value indicating whether model frame should be included as a component of the returned value. |
method |
the method to be used in fitting the model. The default
method User-supplied fitting functions can be supplied either as a function
or a character string naming a function, with a function which takes
the same arguments as |
x, y |
For For |
contrasts |
an optional list. See the |
... |
For For |
digits |
the number of significant digits to use when printing. |
The function glmb is a Bayesian version of the classical
glm function. 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.
Setup (including the use of formulas and families) mirrors that of glm but adds a
required pfamily argument to specify the prior distribution. 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,
glmb generates independent draws from the posterior density-
no MCMC chains are required. Results can be printed or summarized
with methods that mirror those for glm (e.g.\ print.glmb,
summary.glmb), as well as all the usual glm/lm
generics (predict, residuals, etc.).
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.
Two conjugate pfamilies add closed-form IID sampling for intercept-only
models with an identity link: dBeta for
binomial(link = "identity") (Beta–Binomial conjugacy) and
dGamma(Inv_Dispersion = FALSE) for
poisson(link = "identity") and Gamma(link = "identity")
(Gamma–Poisson and Gamma–Gamma rate conjugacy).
For dispersion estimation with fixed coefficients, dGamma
(default Inv_Dispersion = TRUE) places a Gamma prior on the
inverse dispersion for Gaussian and Gamma(log) models.
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, glmb draws n = 1000 samples, uses parallel
CPU simulation, and-if use_opencl = TRUE-GPU-accelerated
envelope building. "glmb" comes with many of the same kinds of method functions
that come with "glm" and "lm", so you can still call extractAIC,
fitted.values, or any other standard method.
The lmb function is a Bayesian version of the lm function that can
be used to estimate models from the Gaussian family without the need for a family argument.
rglmb and rlmb are functions with more minimalistic interfaces for estimating the same
models without most of the internal overhead (these functions are called internally by glmb and lmb).
The reduced overhead may be beneficial for Gibbs sampling implementations.
glmb returns an object of class "glmb". 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 glmb.
An object of class "glmb" is a list containing at least the following components:
glm |
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 |
fitted.values |
a matrix of dimension |
family |
the |
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 |
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 |
model |
if requested (the default),the model frame |
call |
the matched call |
formula |
the formula supplie |
terms |
the |
data |
the |
famfunc |
Family functions used during estimation process |
iters |
an |
contrasts |
(where relevant) the contrasts used. |
xlevels |
(where relevant) a record of the levels of the factors used in fitting |
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 "glmb" are normall of class c("glmb","glm","lm"),
that is inherit from classes glm and lm and well-designed
methods from those classed will be applied when appropriate.
If a binomial glmb model was specified by giving a two-column
response, the weights returned by prior.weights are the total number of
cases (factored by the supplied case weights) and the component of y
of the result is the proportion of successes.
The R implementation of glmb has been written by Kjell Nygren and
was built to be a Bayesian version of the glm function and hence tries
to mirror the features of the glm function to the greatest extent possible. For details
on the author(s) for the glm function see the documentation for glm.
Dobson1990glmbayes
lm, glm, family, formula
for classical modeling functions, family objects, and formula syntax
pfamily for documentation of pfamily functions used to specify priors.
Prior_Setup, Prior_Check for functions used to initialize and to check priors,
EnvelopeBuild for envelope construction methods.
Further reading: \insertCiteNygren2006glmbayes; \insertCiteglmbayesChapter00,glmbayesChapterA02,glmbayesSimmethods,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
lmb(),
rglmb(),
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 Model
ps=Prior_Setup(counts ~ outcome + treatment,family = poisson())
mu=ps$mu
V=ps$Sigma
# Step 2: Call the glmb function
glmb.D93<-glmb(counts ~ outcome + treatment, family=poisson(),
pfamily=dNormal(mu=mu,Sigma=V))
summary(glmb.D93)
# Menarche Binomial Data Example
data(menarche,package="MASS")
Age2=menarche$Age-13
## Classical Model
glm.out<-glm(cbind(Menarche, Total-Menarche) ~ Age2, family=binomial(logit), data=menarche)
summary(glm.out)
## Logit Prior and Model
ps1=Prior_Setup(cbind(Menarche, Total-Menarche) ~ Age2,family=binomial(logit), data=menarche)
glmb.out1<-glmb(cbind(Menarche, Total-Menarche) ~ Age2,
family=binomial(logit),pfamily=dNormal(mu=ps1$mu,Sigma=ps1$Sigma), data=menarche)
summary(glmb.out1)
## Posterior mean fitted probabilities on the response scale (see also
## \code{vignette("Chapter-05", package = "glmbayes")})
require(graphics)
pred1 <- predict(glmb.out1, type = "response")
pred1_m <- colMeans(pred1)
plot(
Menarche / Total ~ Age,
data = menarche,
main = "Proportion with menarche (data and posterior mean fit)"
)
lines(menarche$Age, pred1_m, col = "blue", lwd = 2)
## Probit Prior and Model
ps2=Prior_Setup(cbind(Menarche, Total-Menarche) ~ Age2,family=binomial(probit), data=menarche)
glmb.out2<-glmb(cbind(Menarche, Total-Menarche) ~ Age2,
family=binomial(probit),pfamily=dNormal(mu=ps2$mu,Sigma=ps2$Sigma), data=menarche)
summary(glmb.out2)
## clog-log Prior and Model
ps3=Prior_Setup(cbind(Menarche, Total-Menarche) ~ Age2,family=binomial(cloglog), data=menarche)
glmb.out3<-glmb(cbind(Menarche, Total-Menarche) ~ Age2,
family=binomial(cloglog),pfamily=dNormal(mu=ps3$mu,Sigma=ps3$Sigma), data=menarche)
summary(glmb.out3)
## Comparison of DIC Statistics
DIC_Out=rbind(extractAIC(glmb.out1),extractAIC(glmb.out2),extractAIC(glmb.out3))
rownames(DIC_Out)=c("logit","probit","clog-log")
colnames(DIC_Out)=c("pD","DIC")
DIC_Out
### Gamma regression
data(carinsca)
carinsca$Merit <- ordered(carinsca$Merit)
carinsca$Class <- factor(carinsca$Class)
oldopt <- options(contrasts = c("contr.treatment", "contr.treatment"))
Claims=carinsca$Claims
Insured=carinsca$Insured
Merit=carinsca$Merit
Class=carinsca$Class
Cost=carinsca$Cost
out <- glm(Cost/Claims~Merit+Class,family=Gamma(link="log"),weights=Claims,x=TRUE)
summary(out)
disp=gamma.dispersion(out)
ps=Prior_Setup(Cost/Claims~Merit+Class,family=Gamma(link="log"),weights=Claims)
mu=ps$mu
V=ps$Sigma
out3 <- glmb(Cost/Claims~Merit+Class,family=Gamma(link="log"),
pfamily=dNormal(mu=mu,Sigma=V,dispersion=disp),weights=Claims)
summary(out)
summary(out3)
options(oldopt)
## glmb 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_glmb_dGamma <- glmb(weight ~ group, family = gaussian(),
pfamily = dGamma(shape = ps_dg$shape, rate = rate_dg, beta = ps_dg$coefficients))
summary(out_glmb_dGamma)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.