Bayesian inference for simple linear regression
Description
This function is used to find the posterior distribution of the simple linear regression slope variable beta when we have a random sample of ordered pairs (x_{i}, y_{i}) from the simple linear regression model:
y_i = alpha_xbar + beta*x_i+epsilon_i
y_i = alpha_xbar + beta*x_i+epsilon_i
y_i = alpha_xbar + beta*x_i+epsilon_i
where the observation errors are, epsilon_i, independent normal(0,sigma^2) with known variance.
Usage
1 2 3  bayes.lin.reg(y, x, slope.prior = "flat", intcpt.prior = "flat", mb0 = 0,
sb0 = 0, ma0 = 0, sa0 = 0, sigma = NULL, alpha = 0.05,
plot.data = FALSE, pred.x = NULL)

Arguments
y 
the vector of responses. 
x 
the value of the explantory variable associated with each response. 
slope.prior 
use a “flat” prior or a “normal” prior. for beta 
intcpt.prior 
use a “flat” prior or a “normal” prior. for alpha_xbar 
mb0 
the prior mean of the simple linear regression slope variable beta. This argument is ignored for a flat prior. 
sb0 
the prior std. deviation of the simple linear regression slope variable beta  must be greater than zero. This argument is ignored for a flat prior. 
ma0 
the prior mean of the simple linear regression intercept variable alpha_xbar. This argument is ignored for a flat prior. 
sa0 
the prior std. deviation of the simple linear regression variable alpha_xbar  must be greater than zero. This argument is ignored for a flat prior. 
sigma 
the value of the std. deviation of the residuals. By default, this is assumed to be unknown and the sample value is used instead. This affects the prediction intervals. 
alpha 
controls the width of the credible interval. 
plot.data 
if true the data are plotted, and the posterior regression line superimposed on the data. 
pred.x 
a vector of x values for which the predicted y values are obtained and the std. errors of prediction 
Value
A list will be returned with the following components:
post.coef 
the posterior mean of the intecept and the slope 
post.coef 
the posterior standard deviation of the intercept the slope 
pred.x 
the vector of values for which predictions have been requested. If pred.x is NULL then this is not returned 
pred.y 
the vector predicted values corresponding to pred.x. If pred.x is NULL then this is not returned 
pred.se 
The standard errors of the predicted values in pred.y. If pred.x is NULL then this is not returned 
Examples
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30  ## generate some data from a known model, where the true value of the
## intercept alpha is 2, the true value of the slope beta is 3, and the
## errors come from a normal(0,1) distribution
x = rnorm(50)
y = 22+3*x+rnorm(50)
## use the function with a flat prior for the slope beta and a
## flat prior for the intercept, alpha_xbar.
bayes.lin.reg(y,x)
## use the function with a normal(0,3) prior for the slope beta and a
## normal(30,10) prior for the intercept, alpha_xbar.
bayes.lin.reg(y,x,"n","n",0,3,30,10)
## use the same data but plot it and the credible interval
bayes.lin.reg(y,x,"n","n",0,3,30,10, plot.data = TRUE)
## The heart rate vs. O2 uptake example 14.1
O2 = c(0.47,0.75,0.83,0.98,1.18,1.29,1.40,1.60,1.75,1.90,2.23)
HR = c(94,96,94,95,104,106,108,113,115,121,131)
plot(HR,O2,xlab="Heart Rate",ylab="Oxygen uptake (Percent)")
bayes.lin.reg(O2,HR,"n","f",0,1,sigma=0.13)
## Repeat the example but obtain predictions for HR = 100 and 110
bayes.lin.reg(O2,HR,"n","f",0,1,sigma=0.13,pred.x=c(100,110))
