如果您打算进行许多贝叶斯统计,您会发现学习BUGS / JAGS语言很有帮助,可以通过R2OpenBUGS或R2WinBUGS软件包在R中对其进行访问。
但是,为了得到一个不需要了解BUGS语法的快速示例,您可以使用“ bayesm”软件包,该软件包具有runiregGibbs函数,可以从后验分布中进行采样。这是一个示例,其数据类似于您描述的数据.....
library(bayesm)
podwt <- structure(list(wt = c(1.76, 1.45, 1.03, 1.53, 2.34, 1.96, 1.79, 1.21, 0.49, 0.85, 1, 1.54, 1.01, 0.75, 2.11, 0.92), treat = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("I", "U"), class = "factor"), mus = c(4.15, 2.76, 1.77, 3.11, 4.65, 3.46, 3.75, 2.04, 1.25, 2.39, 2.54, 3.41, 1.27, 1.26, 3.87, 1.01)), .Names = c("wt", "treat", "mus"), row.names = c(NA, -16L), class = "data.frame")
# response
y1 <- podwt$wt
# First run a one-way anova
# Create the design matrix - need to insert a column of 1s
x1 <- cbind(matrix(1,nrow(podwt),1),podwt$treat)
# data for the Bayesian analysis
dt1 <- list(y=y1,X=x1)
# runiregGibbs uses a normal prior for the regression coefficients and
# an inverse chi-squared prior for va
# mean of the normal prior. We have 2 estimates - 1 intercept
# and 1 regression coefficient
betabar1 <- c(0,0)
# Pecision matrix for the normal prior. Again we have 2
A1 <- 0.01 * diag(2)
# note this is a very diffuse prior
# degrees of freedom for the inverse chi-square prior
n1 <- 3
# scale parameter for the inverse chi-square prior
ssq1 <- var(y1)
Prior1 <- list(betabar=betabar1, A=A1, nu=n1, ssq=ssq1)
# number of iterations of the Gibbs sampler
iter <- 10000
# thinning/slicing parameter. 1 means we keep all all values
slice <- 1
MCMC <- list(R=iter, keep=slice)
sim1 <- runiregGibbs(dt1, Prior1, MCMC)
plot(sim1$betadraw)
plot(sim1$sigmasqdraw)
summary(sim1$betadraw)
summary(sim1$sigmasqdraw)
# compare with maximum likelihood estimates:
fitpodwt <- lm(wt~treat, data=podwt)
summary(fitpodwt)
anova(fitpodwt)
# now for ordinary linear regression
x2 <- cbind(matrix(1,nrow(podwt),1),podwt$mus)
dt2 <- list(y=y1,X=x2)
sim2 <- runiregGibbs(dt1, Prior1, MCMC)
summary(sim1$betadraw)
summary(sim1$sigmasqdraw)
plot(sim$betadraw)
plot(sim$sigmasqdraw)
# compare with maximum likelihood estimates:
summary(lm(podwt$wt~mus,data=podwt))
# now with both variables
x3 <- cbind(matrix(1,nrow(podwt),1),podwt$treat,podwt$mus)
dt3 <- list(y=y1,X=x3)
# now we have an additional estimate so modify the prior accordingly
betabar1 <- c(0,0,0)
A1 <- 0.01 * diag(3)
Prior1 <- list(betabar=betabar1, A=A1, nu=n1, ssq=ssq1)
sim3 <- runiregGibbs(dt3, Prior1, MCMC)
plot(sim3$betadraw)
plot(sim3$sigmasqdraw)
summary(sim3$betadraw)
summary(sim3$sigmasqdraw)
# compare with maximum likelihood estimates:
summary(lm(podwt$wt~treat+mus,data=podwt))
从输出中提取的是:
方差分析:
贝叶斯分析:
Summary of Posterior Marginal Distributions
Moments
mean std dev num se rel eff sam size
1 2.18 0.40 0.0042 0.99 9000
2 -0.55 0.25 0.0025 0.87 9000
Quantiles
2.5% 5% 50% 95% 97.5%
1 1.4 1.51 2.18 2.83 2.976
2 -1.1 -0.97 -0.55 -0.13 -0.041
lm():
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 1.6338 0.1651 9.895 1.06e-07 ***
treatU -0.5500 0.2335 -2.355 0.0336 *
简单线性回归:
贝叶斯:
Summary of Posterior Marginal Distributions
Moments
mean std dev num se rel eff sam size
1 0.23 0.208 0.00222 1.0 4500
2 0.42 0.072 0.00082 1.2 4500
Quantiles
2.5% 5% 50% 95% 97.5%
1 -0.18 -0.10 0.23 0.56 0.63
2 0.28 0.31 0.42 0.54 0.56
lm():
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 0.23330 0.14272 1.635 0.124
mus 0.42181 0.04931 8.554 6.23e-07 ***
2个协变量模型:
贝叶斯:
Summary of Posterior Marginal Distributions
Moments
mean std dev num se rel eff sam size
1 0.48 0.437 0.00520 1.3 4500
2 -0.12 0.184 0.00221 1.3 4500
3 0.40 0.083 0.00094 1.2 4500
Quantiles
2.5% 5% 50% 95% 97.5%
1 -0.41 -0.24 0.48 1.18 1.35
2 -0.48 -0.42 -0.12 0.18 0.25
3 0.23 0.26 0.40 0.53 0.56
lm():
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 0.36242 0.19794 1.831 0.0901 .
treatU -0.11995 0.12688 -0.945 0.3617
mus 0.39590 0.05658 6.997 9.39e-06 ***
从中我们可以看到,结果与这些简单模型和先验先验所期望的大致可比。当然,还值得检查MCMC诊断图-后密度,迹线图,自动相关-我还给出了上面的代码(图中未显示)。