Title: | Bayesian Constrained Generalised Linear Models |
---|---|
Description: | Fits generalised partial linear regression models using a Bayesian approach, where shape and smoothness constraints are imposed on nonparametrically modelled predictors through shape-restricted splines, and no constraints are imposed on optional parametrically modelled covariates. See Meyer et al. (2011) <doi/10.1080/10485252.2011.597852> for more details. IMPORTANT: before installing 'bcgam', you need to install 'Rtools' (Windows) or 'Xcode' (Mac OS X). These are required for the correct installation of 'nimble' (<https://r-nimble.org/download>). |
Authors: | Cristian Oliva-Aviles [aut, cre], Mary C. Meyer [aut] |
Maintainer: | Cristian Oliva-Aviles <[email protected]> |
License: | GPL (>= 2) |
Version: | 1.0 |
Built: | 2024-12-04 07:13:13 UTC |
Source: | CRAN |
bcgam
is used to fit generalised partial linear regression models using a Bayesian
approach, where shape and smoothness constraints are imposed on nonparametrically modelled predictors
through shape-restricted splines, and no constraints are imposed on optional parametrically modelled covariates.
bcgam(formula, family = gaussian(), data = NULL, nloop = 10000, burnin = trunc(nloop/10))
bcgam(formula, family = gaussian(), data = NULL, nloop = 10000, burnin = trunc(nloop/10))
formula |
an object of class
|
family |
a description of the error distribution and link function to be used in the model. This accepts
only the following families: |
data |
an optional data frame, list or environment containing the variables in the model. The default is |
nloop |
length of the MCMC. The default is |
burnin |
a positive value, smaller than |
We assume the additive model for each systematic component element given by
where is a vector
of variables to be modelled parametrically and
is a parameter vector. The functions
of the continuous predictors
are assumed to be smooth, and shape restrictions
such as monotonicity and/or convexity might be assumed. Generally, the vector
is approximated by
where for all
. The
's represent
the basis vectors used to approximate the
functions. The
consists of the
one vector and the vectors of the observed values of covariates to be modelled parametrically. In addition,
when
is assumed to be convex, the
vector is included as one of the
.
A Bayesian approach is considered for estimation and inference of the model above. As the
coefficients are constrained to be non-negative, then a gamma prior with hyperparameters
(shape)
and
(scale) is assumed for each
. The values
and
are
chosen in a way that a large variance can be combined with a small mean, so that it is close to a
non-informative gamma prior. Further, a normal prior distribution with mean zero and large variance
is considered for the
coefficients.
bcgam
makes use of the system "nimble" to set the Bayesian (hierarchical) model and compute the MCMC. Hence,
"nimble" has to be loaded in R
to be able to use bcgam
. Information about how to download
and install "nimble" can be found at https://r-nimble.org.
bcgam
returns an object of class "bcgam".
The generic routines summary
and print
are used to obtain and print a summary
of the results. Further, 2D and 3D plots can be created using plot
and persp
, respectively.
An object of class "bcgam" is a list containing at least the following components:
coefs |
a vector of posterior means of the |
sd.coefs |
a vector of posterior standard errors of the |
etahat |
a vector of posterior means of the systematic component |
muhat |
a vector of posterior means of |
alpha.sims |
a matrix of posterior samples (after burn-in) of the |
beta.sims |
a matrix of posterior samples (after burn-in) of the |
sigma.sims |
a matrix of posterior samples (after burn-in) of |
eta.sims |
a matrix of posterior samples (after burn-in) of the systematic component |
mu.sims |
a matrix of posterior samples (after burn-in) of |
delta |
a matrix that contains the basis functions |
zmat |
a matrix that contains the vectors |
knots |
a list of the knots. |
shapes |
a list of numbers that indicate the shape categories. |
sps |
a character vector of the space parameter used to create the knots. |
nloop |
the length of the MCMC. |
burnin |
the burn-in value. |
family |
the family parameter. |
y |
the response variable. |
Cristian Oliva-Aviles and Mary C. Meyer
Meyer, M. C. (2008) Inference using shape-restricted regression splines. Annals of Applied Statistics 2(3), 1013-1033.
Meyer, M. C., Hackstadt, A. J., and Hoeting J. A. (2011) Bayesian estimation and inference for generalised partial linear models using shape-restricted splines. Journal of Nonparametric Statistics 23(4), 867-884.
## Not run: ## Example 1 (gaussian) data(duncan) bcgam.fit <- bcgam(income~sm.incr(prestige, space="E")+sm.conv(education)+type, data=duncan) print(bcgam.fit) summary(bcgam.fit) plot(bcgam.fit, prestige, col=4) persp(bcgam.fit, prestige, education, level=0.90) ## Example 2 (poisson) set.seed(2018) n<-50 x1<-sqrt(1:n) z<-as.factor(rbinom(n, 1, 0.5)) log.eta<-x1/7+0.2*as.numeric(z)+rnorm(50, sd=0.6) eta<-exp(log.eta) y<-rpois(n,eta) bcgam.fit <- bcgam(y~sm.conv(x1)+z, family="poisson") summary(bcgam.fit) predict(bcgam.fit, newdata=data.frame(x1=0.2, z="0"), interval="credible") plot(bcgam.fit, x1, col=3, col.inter=4) ## End(Not run)
## Not run: ## Example 1 (gaussian) data(duncan) bcgam.fit <- bcgam(income~sm.incr(prestige, space="E")+sm.conv(education)+type, data=duncan) print(bcgam.fit) summary(bcgam.fit) plot(bcgam.fit, prestige, col=4) persp(bcgam.fit, prestige, education, level=0.90) ## Example 2 (poisson) set.seed(2018) n<-50 x1<-sqrt(1:n) z<-as.factor(rbinom(n, 1, 0.5)) log.eta<-x1/7+0.2*as.numeric(z)+rnorm(50, sd=0.6) eta<-exp(log.eta) y<-rpois(n,eta) bcgam.fit <- bcgam(y~sm.conv(x1)+z, family="poisson") summary(bcgam.fit) predict(bcgam.fit, newdata=data.frame(x1=0.2, z="0"), interval="credible") plot(bcgam.fit, x1, col=3, col.inter=4) ## End(Not run)
The duncan
data frame has 45 rows and columns. Data on the
prestige and other charachteristics of 45 U. S. occupations in 1950.
duncan
duncan
This data frame contains the following columns:
type. Type of occupation. A factor with the following levels:
prof
, professional and managerial; wc
, white-collar;
bc
, blue-collar.
income. Percent of males in occupation earning $3500 or more in 1950.
education. Percent of males in occupation in 1950 who where high-school graduates.
prestige. Percent of raters in NORC study rating occupation as excellent or good in prestige.
Duncan, O. D. (1961) A socioeconomic index for all occupations. In Reiss, A. J. Jr. (Ed.) Occupations and Social Status. Free Press [Table VI-1].
A symbolic routine to define that the systematic component is
smooth and concave with respect to a predictor in a bcgam formula.
sm.conc(x, numknots = 0, knots = 0, space = "Q")
sm.conc(x, numknots = 0, knots = 0, space = "Q")
x |
a numeric predictor of length |
numknots |
number of knots used to constrain |
knots |
knots used to constrain |
space |
a character specifying the method to create knots. It is ignored when the |
sm.conc
returns the vector x
and assigns five attributes to it: name, shape (4 for
"smooth and concave"), numknots, knots and space.
This routine does not create the splines basis vectors by itself.
x |
The numeric predictor |
Cristian Oliva-Aviles and Mary C. Meyer
Meyer, M. C. (2008) Inference using shape-restricted regression splines. Annals of Applied Statistics 2(3), 1013-1033.
data(duncan) prestige <- duncan$prestige # specify knots sm.conc(prestige, knots=c(3,9,30,57,86,97)) # specify number of knots prestige.smconc <- sm.conc(prestige, numknots=7) # check attributes attributes(prestige.smconc)
data(duncan) prestige <- duncan$prestige # specify knots sm.conc(prestige, knots=c(3,9,30,57,86,97)) # specify number of knots prestige.smconc <- sm.conc(prestige, numknots=7) # check attributes attributes(prestige.smconc)
A symbolic routine to define that the systematic component is
smooth and convex with respect to a predictor in a bcgam formula.
sm.conv(x, numknots = 0, knots = 0, space = "Q")
sm.conv(x, numknots = 0, knots = 0, space = "Q")
x |
a numeric predictor of length |
numknots |
number of knots used to constrain |
knots |
knots used to constrain |
space |
a character specifying the method to create knots. It is ignored when the |
sm.conv
returns the vector x
and assigns five attributes to it: name, shape (3 for
"smooth and convex"), numknots, knots and space.
This routine does not create the splines basis vectors by itself.
x |
the numeric predictor |
Cristian Oliva-Aviles and Mary C. Meyer
Meyer, M. C. (2008) Inference using shape-restricted regression splines. Annals of Applied Statistics 2(3), 1013-1033.
data(duncan) prestige <- duncan$prestige # specify knots sm.conv(prestige, knots=c(3,9,30,57,86,97)) # specify number of knots prestige.smconv <- sm.conv(prestige, numknots=7) # check attributes attributes(prestige.smconv)
data(duncan) prestige <- duncan$prestige # specify knots sm.conv(prestige, knots=c(3,9,30,57,86,97)) # specify number of knots prestige.smconv <- sm.conv(prestige, numknots=7) # check attributes attributes(prestige.smconv)
A symbolic routine to define that the systematic component is
smooth and decreasing with respect to a predictor in a bcgam formula.
sm.decr(x, numknots = 0, knots = 0, space = "Q")
sm.decr(x, numknots = 0, knots = 0, space = "Q")
x |
a numeric predictor of length |
numknots |
number of knots used to constrain |
knots |
knots used to constrain |
space |
a character specifying the method to create knots. It is ignored when the |
sm.decr
returns the vector x
and assigns five attributes to it: name, shape (2 for
"smooth and decreasing"), numknots, knots and space.
This routine does not create the splines basis vectors by itself.
x |
The numeric predictor |
Cristian Oliva-Aviles and Mary C. Meyer
Meyer, M. C. (2008) Inference using shape-restricted regression splines. Annals of Applied Statistics 2(3), 1013-1033.
data(duncan) prestige <- duncan$prestige # specify knots sm.decr(prestige, knots=c(3,9,30,57,86,97)) # specify number of knots prestige.smdecr <- sm.decr(prestige, numknots=7) # check attributes attributes(prestige.smdecr)
data(duncan) prestige <- duncan$prestige # specify knots sm.decr(prestige, knots=c(3,9,30,57,86,97)) # specify number of knots prestige.smdecr <- sm.decr(prestige, numknots=7) # check attributes attributes(prestige.smdecr)
A symbolic routine to define that the systematic component is
smooth, decreasing and concave with respect to a predictor in a bcgam formula.
sm.decr.conc(x, numknots = 0, knots = 0, space = "Q")
sm.decr.conc(x, numknots = 0, knots = 0, space = "Q")
x |
a numeric predictor of length |
numknots |
number of knots used to constrain |
knots |
knots used to constrain |
space |
a character specifying the method to create knots. It is ignored when the |
sm.decr.conc
returns the vector x
and assigns five attributes to it: name, shape (8 for
"smooth, decreasing and concave"), numknots, knots and space.
This routine does not create the splines basis vectors by itself.
x |
The numeric predictor |
Cristian Oliva-Aviles and Mary C. Meyer
Meyer, M. C. (2008) Inference using shape-restricted regression splines. Annals of Applied Statistics 2(3), 1013-1033.
data(duncan) prestige <- duncan$prestige # specify knots sm.decr.conc(prestige, knots=c(3,9,30,57,86,97)) # specify number of knots prestige.smdecrconc <- sm.decr.conc(prestige, numknots=7) # check attributes attributes(prestige.smdecrconc)
data(duncan) prestige <- duncan$prestige # specify knots sm.decr.conc(prestige, knots=c(3,9,30,57,86,97)) # specify number of knots prestige.smdecrconc <- sm.decr.conc(prestige, numknots=7) # check attributes attributes(prestige.smdecrconc)
A symbolic routine to define that the systematic component is
smooth, decreasing and convex with respect to a predictor in a bcgam formula.
sm.decr.conv(x, numknots = 0, knots = 0, space = "Q")
sm.decr.conv(x, numknots = 0, knots = 0, space = "Q")
x |
a numeric predictor of length |
numknots |
number of knots used to constrain |
knots |
knots used to constrain |
space |
a character specifying the method to create knots. It is ignored when the |
sm.decr.conv
returns the vector x
and assigns five attributes to it: name, shape (7 for
"smooth, decreasing and convex"), numknots, knots and space.
This routine does not create the splines basis vectors by itself.
x |
The numeric predictor |
Cristian Oliva-Aviles and Mary C. Meyer
Meyer, M. C. (2008) Inference using shape-restricted regression splines. Annals of Applied Statistics 2(3), 1013-1033.
data(duncan) prestige <- duncan$prestige # specify knots sm.decr.conv(prestige, knots=c(3,9,30,57,86,97)) # specify number of knots prestige.smdecrconv <- sm.decr.conv(prestige, numknots=7) # check attributes attributes(prestige.smdecrconv)
data(duncan) prestige <- duncan$prestige # specify knots sm.decr.conv(prestige, knots=c(3,9,30,57,86,97)) # specify number of knots prestige.smdecrconv <- sm.decr.conv(prestige, numknots=7) # check attributes attributes(prestige.smdecrconv)
A symbolic routine to define that the systematic component is
smooth and increasing with respect to a predictor in a bcgam formula.
sm.incr(x, numknots = 0, knots = 0, space = "Q")
sm.incr(x, numknots = 0, knots = 0, space = "Q")
x |
a numeric predictor of length |
numknots |
number of knots used to constrain |
knots |
knots used to constrain |
space |
a character specifying the method to create knots. It is ignored when the |
sm.incr
returns the vector x
and assigns five attributes to it: name, shape (1 for
"smooth and increasing"), numknots, knots and space.
This routine does not create the splines basis vectors by itself.
x |
The numeric predictor |
Cristian Oliva-Aviles and Mary C. Meyer
Meyer, M. C. (2008) Inference using shape-restricted regression splines. Annals of Applied Statistics 2(3), 1013-1033.
data(duncan) prestige <- duncan$prestige # specify knots sm.incr(prestige, knots=c(3,9,30,57,86,97)) # specify number of knots prestige.smincr <- sm.incr(prestige, numknots=7) # check attributes attributes(prestige.smincr)
data(duncan) prestige <- duncan$prestige # specify knots sm.incr(prestige, knots=c(3,9,30,57,86,97)) # specify number of knots prestige.smincr <- sm.incr(prestige, numknots=7) # check attributes attributes(prestige.smincr)
A symbolic routine to define that the systematic component is
smooth, increasing and concave with respect to a predictor in a bcgam formula.
sm.incr.conc(x, numknots = 0, knots = 0, space = "Q")
sm.incr.conc(x, numknots = 0, knots = 0, space = "Q")
x |
a numeric predictor of length |
numknots |
number of knots used to constrain |
knots |
knots used to constrain |
space |
a character specifying the method to create knots. It is ignored when the |
sm.incr.conc
returns the vector x
and assigns five attributes to it: name, shape (1 for
"smooth, increasing and concave"), numknots, knots and space.
This routine does not create the splines basis vectors by itself.
x |
The numeric predictor |
Cristian Oliva-Aviles and Mary C. Meyer
Meyer, M. C. (2008) Inference using shape-restricted regression splines. Annals of Applied Statistics 2(3), 1013-1033.
data(duncan) prestige <- duncan$prestige # specify knots sm.incr.conc(prestige, knots=c(3,9,30,57,86,97)) # specify number of knots prestige.smincrconc <- sm.incr.conc(prestige, numknots=7) # check attributes attributes(prestige.smincrconc)
data(duncan) prestige <- duncan$prestige # specify knots sm.incr.conc(prestige, knots=c(3,9,30,57,86,97)) # specify number of knots prestige.smincrconc <- sm.incr.conc(prestige, numknots=7) # check attributes attributes(prestige.smincrconc)
A symbolic routine to define that the systematic component is
smooth, increasing and convex with respect to a predictor in a bcgam formula.
sm.incr.conv(x, numknots = 0, knots = 0, space = "Q")
sm.incr.conv(x, numknots = 0, knots = 0, space = "Q")
x |
a numeric predictor of length |
numknots |
number of knots used to constrain |
knots |
knots used to constrain |
space |
a character specifying the method to create knots. It is ignored when the |
sm.incr.conv
returns the vector x
and assigns five attributes to it: name, shape (5 for
"smooth, increasing and convex"), numknots, knots and space.
This routine does not create the splines basis vectors by itself.
x |
The numeric predictor |
Cristian Oliva-Aviles and Mary C. Meyer
Meyer, M. C. (2008) Inference using shape-restricted regression splines. Annals of Applied Statistics 2(3), 1013-1033.
data(duncan) prestige <- duncan$prestige # specify knots sm.incr.conv(prestige, knots=c(3,9,30,57,86,97)) # specify number of knots prestige.smincrconv <- sm.incr.conv(prestige, numknots=7) # check attributes attributes(prestige.smincrconv)
data(duncan) prestige <- duncan$prestige # specify knots sm.incr.conv(prestige, knots=c(3,9,30,57,86,97)) # specify number of knots prestige.smincrconv <- sm.incr.conv(prestige, numknots=7) # check attributes attributes(prestige.smincrconv)
summary
method for class "bcgam"
.
## S3 method for class 'bcgam' summary(object, ...)
## S3 method for class 'bcgam' summary(object, ...)
object |
an object of class |
... |
further arguments passed to or from other methods. |
All summary statistics are based on the posterior distribution in the bcgam
object.
The function summary.bcgam
computes and return a list of summary
statistics (estimated mean, standard error, 95% bounds, estimated median) of the fitted bcgam
given in object
.
Cristian Oliva-Aviles and Mary C. Meyer
## Not run: n<-50 x<-(1:n)^{1/3} z<-as.factor(rbinom(n, 1, 0.6)) y<-x+7*as.numeric(z)+rnorm(n,sd=2) bcgam.fit <- bcgam(y~sm.incr(x)+z, nloop=100) summary(bcgam.fit) ## End(Not run)
## Not run: n<-50 x<-(1:n)^{1/3} z<-as.factor(rbinom(n, 1, 0.6)) y<-x+7*as.numeric(z)+rnorm(n,sd=2) bcgam.fit <- bcgam(y~sm.incr(x)+z, nloop=100) summary(bcgam.fit) ## End(Not run)