Description Usage Arguments Details Value Author(s) Examples
View source: R/functions_bayes.R
Draws a sample from the posterior distribution of parameters from a Bayesian Linear regression model.
1 2 3 4 5 6 7 8 9 10 11 | samplePosterior(
X,
y,
n,
a0 = 1,
b0 = 5e-05,
v0inv = 1/1000,
mu0 = 0,
returnParams = TRUE,
intercept = FALSE
)
|
X |
Design matrix of size |
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 |
mu0 |
Prior mean. Either a single value that will be repeated,
or a vector of length |
returnParams |
Logical indicating whether the parameters of the posterior distribution are returned. |
intercept |
Logical indicating whether an intercept is included in the model.
If |
This function draws a sample from the posterior distributions of the coefficient parameter (β) and error variance parameter (σ^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 (β) are multivariate normal.
A list containing the following elements:
sigma2 |
A vector containing the posterior sample of σ^2 values. |
beta |
Matrix containing the posterior sample of β values. |
postMu |
Vector containing the posterior mean (if |
postV |
Matrix giving the posterior mean (if |
an,bn |
Posterior hyperparameters for the inverse gamma
distribution of the error variance (if |
Joshua Keller
1 2 3 4 5 | 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)
|
$sigma2
[1] 2.868673 3.403235 4.607802 3.322915 4.273041 3.512462 4.388321 4.026195
[9] 3.447583 3.525198
$beta
[,1]
[1,] 0.6154488
[2,] 0.3792357
[3,] 0.5836652
[4,] 0.4707994
[5,] 0.5454538
[6,] 0.5812395
[7,] 0.5546316
[8,] 0.5718335
[9,] 0.3770112
[10,] 0.5025316
$postMu
[,1]
[1,] 0.5389217
$postV
[,1]
[1,] 0.00269026
$an
[1] 21
$bn
[1] 78.82741
$sigma2
[1] 1.3203508 1.6386825 1.6572594 1.2235741 1.3175189 1.3830814 1.8699524
[8] 1.1387318 0.9160876 2.0580829
$beta
x
[1,] -0.52632324 1.146118
[2,] -0.08519817 1.081203
[3,] -0.20564614 1.113614
[4,] -0.56673289 1.105723
[5,] -0.36094434 1.084923
[6,] -0.31615585 1.108784
[7,] -0.51685365 1.218552
[8,] -0.20448524 1.090676
[9,] -0.42557004 1.113534
[10,] 0.14789975 1.100594
$postMu
[,1]
-0.2082738
x 1.0759465
$postV
x
0.04990962 -0.011566814
x -0.01156681 0.005370929
$an
[1] 21
$bn
[1] 25.03791
$sigma2
NULL
$beta
NULL
$postMu
[,1]
-0.2081588
x 1.0759279
$postV
x
0.04990962 -0.011566814
x -0.01156681 0.005370929
$an
[1] 21
$bn
[1] 25.04561
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.