Package 'bcgam'

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

Help Index


Fitting Bayesian Constrained Generalised Additive Models

Description

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.

Usage

bcgam(formula, family = gaussian(), data = NULL, nloop = 10000,
  burnin = trunc(nloop/10))

Arguments

formula

an object of class formula that contains a symbolic description of the model to be fitted. It has the form "response~nonparam+param", where "nonparam" are the predictors to be modelled nonparametrically and "param" are the optional predictors to be modelled parametrically. The user has to specify the relationship between the systematic component η\eta and any nonparametrically modelled predictor xx. The options are:

  • sm.incr(x): xx is smooth and increasing in η\eta. See sm.incr for more details.

  • sm.decr(x): xx is smooth and decreasing in η\eta. See sm.decr for more details.

  • sm.conv(x): xx is smooth and convex in η\eta. See sm.conv for more details.

  • sm.conc(x): xx is smooth and concave in η\eta. See sm.conc for more details.

  • sm.incr.conv(x): xx is smooth, increasing and convex in η\eta. See sm.incr.conv for more details.

  • sm.decr.conv(x): xx is smooth, decreasing and convex in η\eta. See sm.decr.conv for more details.

  • sm.incr.conc(x): xx is smooth, increasing and concave in η\eta. See sm.incr.conc for more details.

  • sm.decr.conc(x): xx is smooth, decreasing and concave in η\eta. See sm.decr.conc for more details.

family

a description of the error distribution and link function to be used in the model. This accepts only the following families: "gaussian" (normal errors model), "binomial" (logistic model), and "poisson" (Poisson model). See family for details of family functions.

data

an optional data frame, list or environment containing the variables in the model. The default is "NULL".

nloop

length of the MCMC. The default is 10000.

burnin

a positive value, smaller than nloop, that indicates the amount of initial MCMC values to be discarded. By default, it burns-in the first 10% chain values.

Details

We assume the additive model for each systematic component element ηi\eta_i given by

ηi=f1(x1i)+...+fL(xLi)+ziγ,\eta_i = f_1(x_{1i}) + ... + f_L(x_{Li}) + z_i'\gamma,

where ziz_i is a vector of variables to be modelled parametrically and γ\gamma is a parameter vector. The functions flf_l of the continuous predictors xlx_l are assumed to be smooth, and shape restrictions such as monotonicity and/or convexity might be assumed. Generally, the vector η=(η1,...,ηn)\eta=(\eta_1, ..., \eta_n)' is approximated by

j=1m1β1jδ1j+...+j=1mLβLjδLj+j=1pαjνj,\sum_{j=1}^{m_1}\beta_{1j}\delta_{1j} + ... + \sum_{j=1}^{m_L}\beta_{Lj}\delta_{Lj} + \sum_{j=1}^{p}\alpha_j\nu_j,

where βlj0\beta_{lj} \ge 0 for all l,jl,j. The δ\delta's represent the basis vectors used to approximate the ff functions. The νj\nu_j consists of the one vector and the vectors of the observed values of covariates to be modelled parametrically. In addition, when flf_l is assumed to be convex, the xlx_l vector is included as one of the νj\nu_j.

A Bayesian approach is considered for estimation and inference of the model above. As the β\beta coefficients are constrained to be non-negative, then a gamma prior with hyperparameters cl1c_{l1} (shape) and cl2c_{l2} (scale) is assumed for each βlj\beta_{lj}. The values cl1c_{l1} and cl2c_{l2} 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 MM is considered for the α\alpha 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.

Value

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 α\alpha and β\beta coefficients.

sd.coefs

a vector of posterior standard errors of the α\alpha and β\beta coefficients.

etahat

a vector of posterior means of the systematic component η\eta.

muhat

a vector of posterior means of μ\mu. μ\mu is obtained by transforming η\eta using the inverse of the link function.

alpha.sims

a matrix of posterior samples (after burn-in) of the α\alpha coefficients.

beta.sims

a matrix of posterior samples (after burn-in) of the β\beta coefficients.

sigma.sims

a matrix of posterior samples (after burn-in) of σ\sigma. This is only shown when family="gaussian".

eta.sims

a matrix of posterior samples (after burn-in) of the systematic component η\eta.

mu.sims

a matrix of posterior samples (after burn-in) of μ\mu. μ\mu is obtained by transforming η\eta using the inverse of the link function.

delta

a matrix that contains the basis functions δ\delta in its columns.

zmat

a matrix that contains the vectors ν\nu in its columns.

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.

Author(s)

Cristian Oliva-Aviles and Mary C. Meyer

References

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.

See Also

predict.bcgam

Examples

## 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)

Duncan's occupational prestige data

Description

The duncan data frame has 45 rows and columns. Data on the prestige and other charachteristics of 45 U. S. occupations in 1950.

Usage

duncan

Format

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.

Source

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].


Specify a smooth and concave shape-restriction in a bcgam formula

Description

A symbolic routine to define that the systematic component η\eta is smooth and concave with respect to a predictor in a bcgam formula.

Usage

sm.conc(x, numknots = 0, knots = 0, space = "Q")

Arguments

x

a numeric predictor of length nn.

numknots

number of knots used to constrain x. It is ignored when the knots argument is specified by the user. If neither numknots nor knots are specified by the user, then numknots is floor4+n(1/7)floor{4+n^(1/7)}. The default is 0.

knots

knots used to constrain x. If they are not specified by the user, then they will be automatically created based on numknots and space. The default is 0.

space

a character specifying the method to create knots. It is ignored when the knots argument is specified by the user. If space="E", then equally spaced knots will be created; if space="Q", then a vector of equal quantiles will be created based on x with duplicate elements removed. The default is "Q".

Details

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.

Value

x

The numeric predictor x.

Author(s)

Cristian Oliva-Aviles and Mary C. Meyer

References

Meyer, M. C. (2008) Inference using shape-restricted regression splines. Annals of Applied Statistics 2(3), 1013-1033.

Examples

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)

Specify a smooth and convex shape-restriction in a bcgam formula

Description

A symbolic routine to define that the systematic component η\eta is smooth and convex with respect to a predictor in a bcgam formula.

Usage

sm.conv(x, numknots = 0, knots = 0, space = "Q")

Arguments

x

a numeric predictor of length nn.

numknots

number of knots used to constrain x. It is ignored when the knots argument is specified by the user. If neither numknots nor knots are specified by the user, then numknots is floor4+n(1/7)floor{4+n^(1/7)}. The default is 0.

knots

knots used to constrain x. If they are not specified by the user, then they will be automatically created based on numknots and space. The default is 0.

space

a character specifying the method to create knots. It is ignored when the knots argument is specified by the user. If space="E", then equally spaced knots will be created; if space="Q", then a vector of equal quantiles will be created based on x with duplicate elements removed. The default is "Q".

Details

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.

Value

x

the numeric predictor x.

Author(s)

Cristian Oliva-Aviles and Mary C. Meyer

References

Meyer, M. C. (2008) Inference using shape-restricted regression splines. Annals of Applied Statistics 2(3), 1013-1033.

Examples

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)

Specify a smooth and decreasing shape-restriction in a bcgam formula

Description

A symbolic routine to define that the systematic component η\eta is smooth and decreasing with respect to a predictor in a bcgam formula.

Usage

sm.decr(x, numknots = 0, knots = 0, space = "Q")

Arguments

x

a numeric predictor of length nn.

numknots

number of knots used to constrain x. It is ignored when the knots argument is specified by the user. If neither numknots nor knots are specified by the user, then numknots is floor4+n(1/7)floor{4+n^(1/7)}. The default is 0.

knots

knots used to constrain x. If they are not specified by the user, then they will be automatically created based on numknots and space. The default is 0.

space

a character specifying the method to create knots. It is ignored when the knots argument is specified by the user. If space="E", then equally spaced knots will be created; if space="Q", then a vector of equal quantiles will be created based on x with duplicate elements removed. The default is "Q".

Details

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.

Value

x

The numeric predictor x.

Author(s)

Cristian Oliva-Aviles and Mary C. Meyer

References

Meyer, M. C. (2008) Inference using shape-restricted regression splines. Annals of Applied Statistics 2(3), 1013-1033.

Examples

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)

Specify a smooth, decreasing and concave shape-restriction in a bcgam formula

Description

A symbolic routine to define that the systematic component η\eta is smooth, decreasing and concave with respect to a predictor in a bcgam formula.

Usage

sm.decr.conc(x, numknots = 0, knots = 0, space = "Q")

Arguments

x

a numeric predictor of length nn.

numknots

number of knots used to constrain x. It is ignored when the knots argument is specified by the user. If neither numknots nor knots are specified by the user, then numknots is floor4+n(1/7)floor{4+n^(1/7)}. The default is 0.

knots

knots used to constrain x. If they are not specified by the user, then they will be automatically created based on numknots and space. The default is 0.

space

a character specifying the method to create knots. It is ignored when the knots argument is specified by the user. If space="E", then equally spaced knots will be created; if space="Q", then a vector of equal quantiles will be created based on x with duplicate elements removed. The default is "Q".

Details

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.

Value

x

The numeric predictor x.

Author(s)

Cristian Oliva-Aviles and Mary C. Meyer

References

Meyer, M. C. (2008) Inference using shape-restricted regression splines. Annals of Applied Statistics 2(3), 1013-1033.

Examples

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)

Specify a smooth, decreasing and convex shape-restriction in a bcgam formula

Description

A symbolic routine to define that the systematic component η\eta is smooth, decreasing and convex with respect to a predictor in a bcgam formula.

Usage

sm.decr.conv(x, numknots = 0, knots = 0, space = "Q")

Arguments

x

a numeric predictor of length nn.

numknots

number of knots used to constrain x. It is ignored when the knots argument is specified by the user. If neither numknots nor knots are specified by the user, then numknots is floor4+n(1/7)floor{4+n^(1/7)}. The default is 0.

knots

knots used to constrain x. If they are not specified by the user, then they will be automatically created based on numknots and space. The default is 0.

space

a character specifying the method to create knots. It is ignored when the knots argument is specified by the user. If space="E", then equally spaced knots will be created; if space="Q", then a vector of equal quantiles will be created based on x with duplicate elements removed. The default is "Q".

Details

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.

Value

x

The numeric predictor x.

Author(s)

Cristian Oliva-Aviles and Mary C. Meyer

References

Meyer, M. C. (2008) Inference using shape-restricted regression splines. Annals of Applied Statistics 2(3), 1013-1033.

Examples

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)

Specify a smooth and increasing shape-restriction in a bcgam formula

Description

A symbolic routine to define that the systematic component η\eta is smooth and increasing with respect to a predictor in a bcgam formula.

Usage

sm.incr(x, numknots = 0, knots = 0, space = "Q")

Arguments

x

a numeric predictor of length nn.

numknots

number of knots used to constrain x. It is ignored when the knots argument is specified by the user. If neither numknots nor knots are specified by the user, then numknots is floor4+n(1/7)floor{4+n^(1/7)}. The default is 0.

knots

knots used to constrain x. If they are not specified by the user, then they will be automatically created based on numknots and space. The default is 0.

space

a character specifying the method to create knots. It is ignored when the knots argument is specified by the user. If space="E", then equally spaced knots will be created; if space="Q", then a vector of equal quantiles will be created based on x with duplicate elements removed. The default is "Q".

Details

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.

Value

x

The numeric predictor x.

Author(s)

Cristian Oliva-Aviles and Mary C. Meyer

References

Meyer, M. C. (2008) Inference using shape-restricted regression splines. Annals of Applied Statistics 2(3), 1013-1033.

Examples

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)

Specify a smooth, increasing and concave shape-restriction in a bcgam formula

Description

A symbolic routine to define that the systematic component η\eta is smooth, increasing and concave with respect to a predictor in a bcgam formula.

Usage

sm.incr.conc(x, numknots = 0, knots = 0, space = "Q")

Arguments

x

a numeric predictor of length nn.

numknots

number of knots used to constrain x. It is ignored when the knots argument is specified by the user. If neither numknots nor knots are specified by the user, then numknots is floor4+n(1/7)floor{4+n^(1/7)}. The default is 0.

knots

knots used to constrain x. If they are not specified by the user, then they will be automatically created based on numknots and space. The default is 0.

space

a character specifying the method to create knots. It is ignored when the knots argument is specified by the user. If space="E", then equally spaced knots will be created; if space="Q", then a vector of equal quantiles will be created based on x with duplicate elements removed. The default is "Q".

Details

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.

Value

x

The numeric predictor x.

Author(s)

Cristian Oliva-Aviles and Mary C. Meyer

References

Meyer, M. C. (2008) Inference using shape-restricted regression splines. Annals of Applied Statistics 2(3), 1013-1033.

Examples

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)

Specify a smooth, increasing and convex shape-restriction in a bcgam formula

Description

A symbolic routine to define that the systematic component η\eta is smooth, increasing and convex with respect to a predictor in a bcgam formula.

Usage

sm.incr.conv(x, numknots = 0, knots = 0, space = "Q")

Arguments

x

a numeric predictor of length nn.

numknots

number of knots used to constrain x. It is ignored when the knots argument is specified by the user. If neither numknots nor knots are specified by the user, then numknots is floor4+n(1/7)floor{4+n^(1/7)}. The default is 0.

knots

knots used to constrain x. If they are not specified by the user, then they will be automatically created based on numknots and space. The default is 0.

space

a character specifying the method to create knots. It is ignored when the knots argument is specified by the user. If space="E", then equally spaced knots will be created; if space="Q", then a vector of equal quantiles will be created based on x with duplicate elements removed. The default is "Q".

Details

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.

Value

x

The numeric predictor x.

Author(s)

Cristian Oliva-Aviles and Mary C. Meyer

References

Meyer, M. C. (2008) Inference using shape-restricted regression splines. Annals of Applied Statistics 2(3), 1013-1033.

Examples

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)

Summarizing bcgam fits

Description

summary method for class "bcgam".

Usage

## S3 method for class 'bcgam'
summary(object, ...)

Arguments

object

an object of class "bcgam".

...

further arguments passed to or from other methods.

Details

All summary statistics are based on the posterior distribution in the bcgam object.

Value

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.

Author(s)

Cristian Oliva-Aviles and Mary C. Meyer

Examples

## 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)