I was looking at an excellent post on Bayesian Linear Regression (MHadaptive)
giving an output for posterior Credible 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 do I now use to construct a model with confidence intervals from these parameters?
Why not use the distributions you obtained from the MCMC to predict a distribution of y
from any point x
? In the example you're 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 the distribution you refer to in your comment is available here as pred
and the confidence interval is pred_BCI
.
If you want to take a look in the posterior marginal density for each parameter, you can use density()
with the samples that are stored in the trace
component of the mcmc_r
object.
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])
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With