| pfamily | R Documentation |
Prior family objects provide a convenient way to specify the details of the priors
used by functions such as glmb. See the documentations for lmb,
glmb, glmb, and rglmb for the details of how such model fitting
takes place.
Under a Beta(shape1, shape2) prior on the binomial probability \theta
and a Binomial(n_i, \theta) likelihood with identity link (\theta = \beta
directly), the posterior is:
\theta \mid y \sim \mathrm{Beta}(\texttt{shape1} + \sum n_i y_i,\;
\texttt{shape2} + \sum n_i (1 - y_i)).
pfamily(object, ...)
dNormal(mu, Sigma, dispersion = NULL)
dGamma(
shape,
rate,
beta,
Inv_Dispersion = TRUE,
lik_shape = 1,
max_disp_perc = 0.99,
disp_lower = NULL,
disp_upper = NULL
)
dBeta(shape1, shape2, beta)
## S3 method for class 'pfamily'
print(x, ...)
dNormal_Gamma(mu, Sigma_0, shape, rate)
dIndependent_Normal_Gamma(
mu,
Sigma,
shape,
rate,
max_disp_perc = 0.99,
disp_lower = NULL,
disp_upper = NULL
)
object |
the function |
mu |
a prior mean vector for the the modeling coefficients used in several pfamilies |
Sigma |
a prior variance-covariance matrix for |
dispersion |
the dispersion to be assumed when it is not given a prior. Should be provided
when the Normal prior is for the |
shape |
The prior shape parameter for the gamma piece (inverse dispersion / precision).
When taking defaults from |
rate |
The prior rate parameter paired with |
beta |
Initial coefficient matrix (1 |
Inv_Dispersion |
Logical (default
|
lik_shape |
Known shape parameter |
max_disp_perc |
Specifies the percentile used to truncate the posterior dispersion
distribution when constructing the envelope for accept-reject sampling. This determines
the lower and upper bounds for the dispersion ( |
disp_lower |
lower bound truncation for dispersion |
disp_upper |
upper bound truncation for dispersion |
shape1 |
First shape parameter |
shape2 |
Second shape parameter |
x |
an object, a pfamily function that is to be printed |
Sigma_0 |
prior variance-covariance on the precision-weighted coefficient scale for
|
... |
additional argument(s) for methods. |
pfamily is a generic with methods for fitted objects such as glmb and
lmb. The dNormal() prior is supported for all response families.
The gaussian() family additionally supports dNormal_Gamma(),
dIndependent_Normal_Gamma(), and dGamma() (precision prior).
Intercept-only models with an identity link support two closed-form conjugate priors:
dBeta() for binomial(link = "identity") and
dGamma(Inv_Dispersion = FALSE) for poisson(link = "identity") and
Gamma(link = "identity").
A pfamily object represents a structured prior specification for use in Bayesian generalized linear modeling.
Each constructor function (e.g., dNormal(), dGamma(), dNormal_Gamma(), dBeta()) returns an object of
class "pfamily" containing the prior parameters, supported likelihood families, compatible link functions,
and a simulation function for posterior sampling.
These priors are designed to integrate seamlessly with modeling functions such as glmb() and rlmb() in the
glmbayes package, which consume the pfamily object to define the prior distribution over model parameters.
The pfamily() generic retrieves the embedded prior from a fitted model object, while print.pfamily() displays its structure.
prior_list and simfun. The named list prior_list holds the hyperparameters for the chosen
prior family. When a model function draws from the posterior, it passes prior_list into the element
simfun (e.g., rNormal_reg, rGamma_reg) so the low-level sampler receives
one consistent list structure regardless of which constructor built the pfamily.
Prior_Setup and default hyperparameters. Prior_Setup() fits an auxiliary GLM and returns
default mu, Sigma / Sigma_0, dispersion, Gamma shape and rate, and
related fields aligned with the data and prior-weight (pwt) choices. Those values can be supplied as
arguments to the pfamily constructors when you want package-default priors on the same scale as the model
matrix. Recommended use of shape and rate is not identical across constructors: for
dIndependent_Normal_Gamma(), pass shape = ps$shape_ING from Prior_Setup (not the
scalar ps$shape used by dNormal_Gamma()). For dGamma() with fixed coefficients
(beta), pass rate = ps$rate_gamma when that field is present (otherwise ps$rate); see
Prior_Setup and compute_gaussian_prior.
dNormal(): Specifies a multivariate normal prior over regression coefficients. It is conjugate for
Gaussian likelihoods with an identity link function, and serves as the primary implemented prior for all
other supported likelihood families in the current framework. This structure facilitates efficient posterior
sampling and analytical tractability. The returned prior_list includes ddef: TRUE when
dispersion was omitted or NULL (so the default 1 was used), FALSE when
dispersion was supplied explicitly (including 1).
For models with log-concave likelihood functions-such as Poisson, Binomial, and Gamma families-
posterior sampling under a dNormal prior is performed using a \insertCiteNygren2006glmbayes
likelihood subgradient approach. This method constructs tight enveloping functions around the posterior
using subgradients of the log-likelihood, enabling efficient accept-reject sampling even in high dimensions.
When the posterior distribution is approximately normal (typically the case for large sample sizes), the
area under the enveloping function is bounded above by a constant factor-approximately 2 / \sqrt{\pi} \approx 1.128
in the univariate case, and (2 / \sqrt{\pi})^k in k-dimensional models. These bounds ensure that
the rejection rate remains manageable and that the sampler remains computationally efficient.
The concept of conjugate priors was first formalized by \insertCiteRaiffa1961glmbayes, and further developed for regression models using g-prior structures by \insertCitezellner1986gpriorglmbayes.
dGamma(): A Gamma prior with two distinct roles controlled by Inv_Dispersion:
Inv_Dispersion = TRUE (default): prior on the inverse dispersion (precision
1/\phi or shape k). Used for dispersion estimation in Gaussian and
Gamma(log) models, typically in a Gibbs step with beta held fixed
\insertCiteGelman2013,Dobson1990,McCullagh1989glmbayes.
With Gaussian Prior_Setup output, prefer rate_gamma for rate
(see Details above).
Inv_Dispersion = FALSE: conjugate Gamma prior on the rate parameter
\beta directly. Supports intercept-only models with an identity link:
Poisson (Gamma–Poisson conjugacy) and Gamma (Gamma–Gamma conjugacy).
Posterior draws are closed-form IID samples via rGamma_Conjugate_reg.
The lik_shape argument specifies the known Gamma likelihood shape (default 1,
i.e.\ exponential). Prior_Setup returns calibrated conj_poisson
hyperparameters for this path.
dBeta(): A Beta prior on the binomial probability \theta for intercept-only
binomial(link = "identity") models. The posterior is a closed-form Beta draw
(Beta–Binomial conjugacy) produced by rBeta_reg. Arguments shape1
and shape2 are the prior pseudo-success and pseudo-failure counts.
Prior_Setup returns calibrated conj_beta hyperparameters for this path.
dNormal_Gamma(): Combines a multivariate normal prior on coefficients with a gamma prior on precision,
forming a conjugate structure for Gaussian models with unknown variance. The second argument is Sigma_0
(precision-weighted scale); it is aliased internally to Sigma in prior_list.
This formulation parallels classical Normal-Gamma models and is compatible with hierarchical extensions
\insertCiteGelman2013,Raiffa1961glmbayes.
dIndependent_Normal_Gamma(): Similar to dNormal_Gamma(), but assumes independence between the
coefficient and precision priors. This structure is useful for models where prior independence is desired
or analytically convenient. With Prior_Setup on a Gaussian model, pass shape_ING as
the shape argument (see Details above).
Each pfamily object includes:
pfamily, prior_list, okfamilies, plinks, simfun, and pfun (see Value).
mu / Sigma: the surrogate Normal mean is shape1/(shape1+shape2) and
the surrogate variance is the Beta variance
shape1*shape2/((shape1+shape2)^2*(shape1+shape2+1)).
An object of class "pfamily" (with a concise print method). A list with elements:
pfamily |
Character string: the constructor name ( |
prior_list |
Named list of prior hyperparameters. It is passed into
|
okfamilies |
Character vector of implemented |
plinks |
Function of one |
simfun |
Function used to generate posterior draws (e.g., |
pfun |
Prior-simulation function paired with this constructor (e.g.,
|
The design of the pfamily set of functions was developed by Kjell Nygren and was
inspired by the family used by the glmb function to specify the likelihood
function. That design in turn was inspired by S functions of the same names from
the statistical modeling literature.
glmb, rlmb, lmb, rglmb for modeling functions that consume pfamily objects.
rNormal_reg, rNormalGamma_reg, rGamma_reg, rGamma_Conjugate_reg, rindepNormalGamma_reg for lower-level sampling functions used by pfamily constructors.
Prior_Setup, Prior_Check for initializing and validating prior specifications.
EnvelopeBuild for envelope construction methods used in likelihood subgradient sampling \insertCiteNygren2006glmbayes.
See also \insertCiteHastie1992glmbayes for the original S modeling framework that inspired the design of pfamily.
## During CRAN checks, run examples sequentially.
use_parallel <- identical(Sys.getenv("NOT_CRAN"), "true")
## 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))
## Set up Prior for Poisson Model
ps <- Prior_Setup(counts ~ outcome + treatment, family = poisson())
ps
## Normal prior for glmb
glmb.D93 <- glmb(
counts ~ outcome + treatment,
family = poisson(),
pfamily = dNormal(mu = ps$mu, Sigma = ps$Sigma),
use_parallel = use_parallel
)
## Extract pfamily and pfamily settings for call to glmb
pfamily(glmb.D93)
## 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)
## Set up prior for gaussian model
ps2 <- Prior_Setup(weight ~ group, family = gaussian())
ps2
## Conjugate Normal Prior (fixed dispersion)
lmb.D9 <- lmb(
weight ~ group,
pfamily = dNormal(mu = ps2$mu, ps2$Sigma, dispersion = ps2$dispersion),
use_parallel = use_parallel
)
pfamily(lmb.D9)
## Conjugate Normal_Gamma Prior
lmb.D9_v2 <- lmb(
weight ~ group,
pfamily = dNormal_Gamma(
ps2$mu,
Sigma_0 = ps2$Sigma_0,
shape = ps2$shape,
rate = ps2$rate
),
use_parallel = use_parallel
)
pfamily(lmb.D9_v2)
## Independent_Normal_Gamma_Prior
lmb.D9_v3 <- lmb(
weight ~ group,
dIndependent_Normal_Gamma(
ps2$mu,
ps2$Sigma,
shape = ps2$shape_ING,
rate = ps2$rate
),
use_parallel = use_parallel
)
pfamily(lmb.D9_v3)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.