Bayesplot 包:可视化贝叶斯模型

简介: Bayesplot 包:可视化贝叶斯模型

简介

Bayesplot[1]包,提供了广泛的绘图函数库,用于在拟合贝叶斯模型后使用(通常使用 MCMC )。由 bayesplot 创建的图形是 ggplot 对象,这意味着在创建图形后,可以使用 ggplot2[2] 包中的各种函数进一步自定义。

目前,bayesplot 提供了各种后验抽样绘图、视觉 MCMC 诊断和图形后验(或先验)预测检查的绘图功能。未来的版本将添加其他功能(例如,用于预测/样本外预测和其他推断相关任务)。



Cheat sheet

bayesplot 的理念不仅是为用户提供方便的功能,还提供了一个共同的函数集,可以轻松地由开发人员在各种贝叶斯建模包上使用,特别是(但不一定是)由 RStan[3] 提供支持的包。



官网首页

关于贝叶斯分析的推文,小编还写了:贝叶斯 RStan 包入门教程资料分享|贝叶斯数据分析从贝叶斯滤波到粒子滤波

安装

从 CRAN 安装最新版本:

install.packages("bayesplot")

从 GitHub 安装最新的开发版本:

if (!require("devtools")) {
install.packages("devtools")
}
devtools::install_github("stan-dev/bayesplot")

例子1:回归模型

利用rstanarm[4]rstan[5]包获得 MCMC 样本,根据这个抽样样本,我们给出 bayesplot 包的入门示例。其他更加详细的教程可见:

  • Plotting MCMC draws using the bayesplot package[6]
  • Visual MCMC diagnostics using the bayesplot package[7]
  • Graphical posterior predictive checks using the bayesplot package[8]

我们以 mtcars 数据集作为例子,使用 stan_glm() 构建贝叶斯框架下的回归模型。根据后验样本,使用 bayesplot 包中的各种函数进行可视化。

library("bayesplot")
library("rstanarm")
library("ggplot2")
fit <- stan_glm(mpg ~ ., data = mtcars)
posterior <- as.matrix(fit)
  • 使用 mcmc_areas() 绘制后验分布的面积图
plot_title <- ggtitle("Posterior distributions",
                      "with medians and 80% intervals")
mcmc_areas(posterior,
           pars = c("cyl", "drat", "am", "wt"),
           prob = 0.8) + plot_title



使用 mcmc_areas() 绘制面积图

  • 使用 ppc_dens_overlay() 绘制预测分布和真实分布
color_scheme_set("red")
ppc_dens_overlay(y = fit$y,
                 yrep = posterior_predict(fit, draws = 50))


使用 ppc_dens_overlay() 绘制预测分布和真实分布

  • 使用 ppc_stat_grouped() 绘制预测分布柱状图
# also works nicely with piping
library("dplyr")
color_scheme_set("brightblue")
fit %>%
  posterior_predict(draws = 500) %>%
  ppc_stat_grouped(y = mtcars$mpg,
                   group = mtcars$carb,
                   stat = "median")



使用 ppc_stat_grouped() 绘制预测分布柱状图

例子2: rstan 中的八校数据

使用《贝叶斯数据分析》中最经典的八校数据进行分析。具体可见推文:贝叶斯 RStan 包入门教程

library("rstan")
fit2 <- stan_demo("eight_schools", warmup = 300, iter = 700)
posterior2 <- extract(fit2, inc_warmup = TRUE, permuted = FALSE)
  • 使用 mcmc_trace() 绘制各链轨迹图
color_scheme_set("mix-blue-pink")
p <- mcmc_trace(posterior2,  pars = c("mu", "tau"), n_warmup = 300,
                facet_args = list(nrow = 2, labeller = label_parsed))
p + facet_text(size = 15)



使用 mcmc_trace() 绘制各链轨迹图

  • 使用 mcmc_scatter() 绘制散点图
# scatter plot also showing divergences
color_scheme_set("darkgray")
mcmc_scatter(
  as.matrix(fit2),
  pars = c("tau", "theta[1]"), 
  np = nuts_params(fit2), 
  np_style = scatter_style_np(div_color = "green", div_alpha = 0.8)
)



使用 mcmc_scatter() 绘制散点图

  • 使用 mcmc_nuts_energy() 进行 NUTS 能量诊断
color_scheme_set("red")
np <- nuts_params(fit2)
mcmc_nuts_energy(np) + ggtitle("NUTS Energy Diagnostic")



使用 mcmc_nuts_energy() 进行 NUTS 能量诊断

小编有话说

  • 读者可以使用自己数据集在贝叶斯框架下进行分析,之后使用本文的 R 包进行结果可视化。

参考资料

[1]

Bayesplot: https://mc-stan.org/bayesplot/index.html

[2]

ggplot2: https://ggplot2.tidyverse.org/

[3]

RStan: https://mc-stan.org/rstan/

[4]

rstanarm: https://mc-stan.org/rstanarm/

[5]

rstan: https://mc-stan.org/rstan/

[6]

Plotting MCMC draws using the bayesplot package: https://mc-stan.org/bayesplot/articles/plotting-mcmc-draws.html

[7]

Visual MCMC diagnostics using the bayesplot package: https://mc-stan.org/bayesplot/articles/visual-mcmc-diagnostics.html

[8]

Graphical posterior predictive checks using the bayesplot package: https://mc-stan.org/bayesplot/articles/graphical-ppcs.html

目录
打赏
0
0
0
0
13
分享
相关文章
LightGBM高级教程:深度集成与迁移学习
LightGBM高级教程:深度集成与迁移学习【2月更文挑战第6天】
450 4
基于Python Django的房价数据分析平台,包括大屏和后台数据管理,有线性、向量机、梯度提升树、bp神经网络等模型
本文介绍了一个基于Python Django框架开发的房价数据分析平台,该平台集成了多种机器学习模型,包括线性回归、SVM、GBDT和BP神经网络,用于房价预测和市场分析,同时提供了前端大屏展示和后台数据管理功能。
177 9
【超详细】MMLab分类任务mmclassification:环境配置说明、训练、预测及模型结果可视化展示(2)
【超详细】MMLab分类任务mmclassification:环境配置说明、训练、预测及模型结果可视化展示
PYTHON集成机器学习:用ADABOOST、决策树、逻辑回归集成模型分类和回归和网格搜索超参数优化
PYTHON集成机器学习:用ADABOOST、决策树、逻辑回归集成模型分类和回归和网格搜索超参数优化
构建自定义机器学习模型:Scikit-learn的高级应用
【4月更文挑战第17天】本文探讨了如何利用Scikit-learn构建自定义机器学习模型,包括创建自定义估计器、使用管道集成数据处理和模型、深化特征工程以及调优与评估模型。通过继承`BaseEstimator`和相关Mixin类,用户可实现自定义算法。管道允许串联多个步骤,而特征工程涉及多项式特征和自定义变换。模型调优可借助交叉验证和参数搜索工具。掌握这些高级技巧能提升机器学习项目的效果和效率。
bayesplot|分享一个可视化贝叶斯模型的R包
`bayesplot` 是一个用于贝叶斯模型可视化的R包,专注于MCMC样本的分析。它提供了后验分布图(如密度图、直方图和区间图)、MCMC诊断图(如追踪图和自相关图)、后验预测检查以及模型比较和评估工具。DEMO展示了如何使用`bayesplot`绘制后验分布、后验预测检查图以及MCMC链的轨迹图和能量诊断图。这些图帮助评估模型的收敛性、预测能力及不确定性。
133 0
【Python 机器学习专栏】模型选择中的交叉验证与网格搜索
【4月更文挑战第30天】交叉验证和网格搜索是机器学习中优化模型的关键技术。交叉验证通过划分数据集进行多次评估,如K折和留一法,确保模型性能的稳定性。网格搜索遍历预定义参数组合,寻找最佳参数设置。两者结合能全面评估模型并避免过拟合。Python中可使用`sklearn`库实现这一过程,但需注意计算成本、过拟合风险及数据适应性。理解并熟练应用这些方法能提升模型性能和泛化能力。
315 0
神经网络可视化新工具:TorchExplorer
TorchExplorer是一个交互式探索神经网络的可视化工具,他的主要功能如下:
205 1
AI助理

你好,我是AI助理

可以解答问题、推荐解决方案等