Bayesian analysis
Start with the expanded example for each command. Open the variants for options and alternative R approaches. Read the qualifications before adapting the code.
bayesmh
Bayesian Gaussian regression with normal and inverse-gamma priors
Stata
bayesmh y x, likelihood(normal({var})) prior({y:}, normal(0,100)) prior({var}, igamma(1,1)) rseed(135)R Package:MCMCpack
MCMCpack::MCMCregress(y ~ x, data = data, burnin = 100, mcmc = 1000, b0 = 0, B0 = .01, c0 = 2, d0 = 2, seed = 135, verbose = 0)Differs from Stata Fixed workflow; adapt the named data columns explicitly. Simple normal-regression model only; samplers and initialization differ. Short chains demonstrate syntax, not convergence.
bayespredict
Posterior mean Gaussian regression response
Stata
bayespredict pmean, meanR Package:MCMCpack
drop(cbind(1, data$x) %*% colMeans(as.matrix(draws)[, c("(Intercept)", "x")]))Differs from Stata Fixed workflow; adapt the named data columns explicitly. Conditional mean prediction; observation noise is integrated to zero.
bayesreps
Replicated outcomes from a Gaussian posterior predictive distribution
Stata
bayesreps yrep*, nreps(10)R Package:MCMCpack
{ set.seed(136); keep <- seq(1, nrow(draws), length.out = 10); B <- as.matrix(draws)[keep, ]; means <- cbind(1, data$x) %*% t(B[, c("(Intercept)", "x")]); means + matrix(rnorm(length(means), sd = rep(sqrt(B[, "sigma2"]), each = nrow(data))), nrow(data)) }Differs from Stata Fixed workflow; adapt the named data columns explicitly. Includes posterior parameter uncertainty and observation noise; no prediction files are written.
bayesstats
Effective MCMC sample size
Stata
bayesstats essR Package:coda
coda::effectiveSize(draws)Differs from Stata Fixed workflow; adapt the named data columns explicitly. Spectral-density ESS estimator differs from some Stata autocorrelation/batch calculations.
Posterior means, standard deviations and quantiles
Stata
bayesstats summaryR Package:coda
summary(draws)Differs from Stata Fixed workflow; adapt the named data columns explicitly. Summarizes supplied posterior draws; Monte Carlo errors differ by estimator.
Posterior summary of a transformed coefficient
Stata
bayesstats summary (effect:exp({y:x}))R Package:coda
summary(exp(draws[, "x", drop = FALSE]))Differs from Stata Fixed workflow; adapt the named data columns explicitly. Transform draws before summarizing; exp(E[b]) differs from E[exp(b)].
Ninety-percent highest-posterior-density interval
Stata
bayesstats summary {y:x}, hpd clevel(90)R Package:coda
coda::HPDinterval(draws[, "x", drop = FALSE], prob = .9)Differs from Stata Fixed workflow; adapt the named data columns explicitly. HPD algorithms and multimodality handling can differ.
bayestest
Posterior probability of a positive slope
Stata
bayestest interval {y:x}, lower(0)R Package:base
mean(draws[, "x"] > 0)Differs from Stata Fixed workflow; adapt the named data columns explicitly. Monte Carlo probability of an interval hypothesis; not a frequentist p-value.
Posterior probability within a substantive effect interval
Stata
bayestest interval {y:x}, lower(1.8) upper(2.2)R Package:base
mean(draws[, "x"] > 1.8 & draws[, "x"] < 2.2)Differs from Stata Fixed workflow; adapt the named data columns explicitly. Choose the substantive interval before inspecting posterior results.