Confidence Intervals for Predictions for the Bayesian Linear Regression Model

I watched a great post on Bayesian linear regression (MHadaptive)

gives output for back confidence intervals

BCI(mcmc_r)
#              0.025       0.975
# slope       -5.3345970   6.841016
# intercept    0.4216079   1.690075
# epsilon      3.8863393   6.660037

What function am I using to build a model with confidence intervals from these parameters?

+3
source share
2 answers

Why not use distributions derived from the MCMC to predict distributions yfrom anywhere x? In the example you are using, here are the relevant sections where eggmass = y and length = x

##@  3.1  @##

## Function to compute predictions from the posterior
## distribution of the salmon regression model
predict_eggmass<-function(pars,length)
{
    a <- pars[, 1]      #intercept
    b <- pars[, 2]      #slope
    sigma <- pars[, 3]  #error    
    pred_mass <- a + b * length 
    pred_mass <- rnorm(length(a), pred_mass, sigma)
    return(pred_mass)
}

###  --  ###
##@  3.2  @##

## generate prediction
pred_length <- 80     # predict for an 80cm individual
pred <- predict_eggmass(mcmc_salmon$trace, length=pred_length)
## Plot prediction distribution
hist(pred, breaks=30, main='', probability=TRUE)

## What is the 95% BCI of the prediction?
pred_BCI <- quantile(pred, p=c(0.025, 0.975))
    2.5%    97.5% 
33.61029 43.16795

I think that the distribution you refer to in your comment is available here as pred, and the confidence interval pred_BCI.

+8

, density() , trace mcmc_r.

library(MHadaptive)
data(mcmc_r)

BCI(mcmc_r)
#              0.025    0.975   post_mean
# a       -6.6113522 7.038858 0.001852978
# b        0.2217377 1.543519 0.902057671
# epsilon  3.8094802 6.550360 4.957292114

head(mcmc_r$trace)
#           [,1]      [,2]     [,3]
# [1,] 3.1448136 0.7211228 5.449728
# [2,] 2.2287645 0.7155189 4.602004
# [3,] 2.0812509 0.8035820 4.224071
# [4,] 1.2444855 0.8448825 4.737466
# [5,] 3.2765630 0.5947548 4.740052
# [6,] 0.4271876 0.9014841 5.333821

plot(density(mcmc_r$trace[,3]), main=mcmc_r$par_names[3])

enter image description here

+1

All Articles