samplePosterior {eshrink}R Documentation

Posterior Sample for Bayesian Linear Regression

Description

Draws a sample from the posterior distribution of parameters from a Bayesian Linear regression model.

Usage

samplePosterior(
  X,
  y,
  n,
  a0 = 1,
  b0 = 5e-05,
  v0inv = 1/1000,
  mu0 = 0,
  returnParams = TRUE,
  intercept = FALSE
)

Arguments

X

Design matrix of size n by p.

y

Outcome variable

n

Size of posterior sample to be computed. A value of 0 is accepted.

a0, b0

Hyperparameters (shape, rate) for inverse gamma distribution of the error variance.

v0inv

Prior precision for the error term. Either a single value to be repeated in a diagonal precision matrix, or a p by p matrix.

mu0

Prior mean. Either a single value that will be repeated, or a vector of length p. Defaults to zero vector.

returnParams

Logical indicating whether the parameters of the posterior distribution are returned.

intercept

Logical indicating whether an intercept is included in the model. If FALSE, then y is centered.

Details

This function draws a sample from the posterior distributions of the coefficient parameter (\beta) and error variance parameter (\sigma^2) from a Bayesian linear regression model. The variance parameter is assumed to follow an inverse-gamma distribution. Conditional on the error variance parameter and a specified precision matrix, the coefficient parameters (\beta) are multivariate normal.

Value

A list containing the following elements:

sigma2

A vector containing the posterior sample of \sigma^2 values.

beta

Matrix containing the posterior sample of \beta values.

postMu

Vector containing the posterior mean (if returnParams =TRUE).

postV

Matrix giving the posterior mean (if returnParams =TRUE).

an, bn

Posterior hyperparameters for the inverse gamma distribution of the error variance (if returnParams =TRUE).

Author(s)

Joshua Keller

Examples

x <- rnorm(40, mean=2, sd=2)
y <- x + rnorm(40, sd=1)
samplePosterior(X=x, y=y, n=10)
samplePosterior(X=cbind(1, x), y=y, n=10, intercept=TRUE)
samplePosterior(X=cbind(1, x), y=y, n=0, mu=c(3, 3), intercept=TRUE)

[Package eshrink version 0.1.2 Index]