使用贝叶斯图绘制来自多个模型的后验参数估计

gfg*_*fgm 6 r ggplot2 rstan rstanarm

我正在使用很棒的绘图库bayesplot来可视化我正在使用的模型的后验概率区间rstanarm。我想通过将系数的后验区间放到同一图上来以图形方式比较来自不同模型的绘制。

例如,想象一下,对于beta1, beta2, beta3两个不同模型的三个参数,我从后验中抽取了 1000 次:

# load the plotting library
library(bayesplot)
#> This is bayesplot version 1.6.0
#> - Online documentation and vignettes at mc-stan.org/bayesplot
#> - bayesplot theme set to bayesplot::theme_default()
#>    * Does _not_ affect other ggplot2 plots
#>    * See ?bayesplot_theme_set for details on theme setting
library(ggplot2)

# generate fake posterior draws from model1
fdata <- matrix(rnorm(1000 * 3), ncol = 3)
colnames(fdata) <- c('beta1', 'beta2', 'beta3')

# fake posterior draws from model 2
fdata2 <- matrix(rnorm(1000 * 3, 1, 2), ncol = 3)
colnames(fdata2) <- c('beta1', 'beta2', 'beta3')
Run Code Online (Sandbox Code Playgroud)

Bayesplot 为单个模型绘制制作了出色的可视化效果,它是 ggplot2 'under the hood',因此我可以根据需要进行自定义:

# a nice plot of 1
color_scheme_set("orange")
mcmc_intervals(fdata) + theme_minimal() + ggtitle("Model 1")
Run Code Online (Sandbox Code Playgroud)

# a nice plot of 2
color_scheme_set("blue")
mcmc_intervals(fdata2) + ggtitle("Model 2")
Run Code Online (Sandbox Code Playgroud)

但是我想要实现的是将这两个模型一起绘制在同一个图上,这样对于每个系数,我都有两个区间,并且可以通过将颜色映射到模型来区分哪个区间是哪个区间。但是我不知道如何做到这一点。一些不起作用的事情:

# doesnt work
mcmc_intervals(fdata) + mcmc_intervals(fdata2)
#> Error: Don't know how to add mcmc_intervals(fdata2) to a plot

# appears to pool
mcmc_intervals(list(fdata, fdata2))
Run Code Online (Sandbox Code Playgroud)

关于我如何做到这一点的任何想法?或者如何根据后绘制矩阵手动完成?

reprex 包(v0.2.1)于 2018 年 10 月 18 日创建

Dyl*_*mes 3

所以答案也发布在这里,我在@Manny T 的链接上扩展了代码(https://github.com/stan-dev/bayesplot/issues/232

# simulate having posteriors for two different models each with parameters beta[1],..., beta[4]
posterior_1 <- matrix(rnorm(4000), 1000, 4)
posterior_2 <- matrix(rnorm(4000), 1000, 4)
colnames(posterior_1) <- colnames(posterior_2) <- paste0("beta[", 1:4, "]")

# use bayesplot::mcmc_intervals_data() function to get intervals data in format easy to pass to ggplot
library(bayesplot)
combined <- rbind(mcmc_intervals_data(posterior_1), mcmc_intervals_data(posterior_2))
combined$model <- rep(c("Model 1", "Model 2"), each = ncol(posterior_1))

# make the plot using ggplot 
library(ggplot2)
theme_set(bayesplot::theme_default())
pos <- position_nudge(y = ifelse(combined$model == "Model 2", 0, 0.1))
ggplot(combined, aes(x = m, y = parameter, color = model)) + 
  geom_linerange(aes(xmin = l, xmax = h), position = pos, size=2)+
  geom_linerange(aes(xmin = ll, xmax = hh), position = pos)+
  geom_point(position = pos, color="black")
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

如果您像我一样,您会需要 80% 和 90% 的可信区间(而不是 50% 的内部区间),并且可能希望翻转坐标,让我们在 0 处添加一条虚线(模型估计没有变化)。你可以这样做:

# use bayesplot::mcmc_intervals_data() function to get intervals data in format easy to pass to ggplot
library(bayesplot)
combined <- rbind(mcmc_intervals_data(posterior_1,prob=0.8,prob_outer = 0.9), mcmc_intervals_data(posterior_2,prob=0.8,prob_outer = 0.9))
combined$model <- rep(c("Model 1", "Model 2"), each = ncol(posterior_1))

# make the plot using ggplot 
library(ggplot2)
theme_set(bayesplot::theme_default())
pos <- position_nudge(y = ifelse(combined$model == "Model 2", 0, 0.1))
ggplot(combined, aes(x = m, y = parameter, color = model)) + 
  geom_linerange(aes(xmin = l, xmax = h), position = pos, size=2)+
  geom_linerange(aes(xmin = ll, xmax = hh), position = pos)+
  geom_point(position = pos, color="black")+
  coord_flip()+
  geom_vline(xintercept=0,linetype="dashed")
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

关于最后一项,有几点需要注意。我添加了prob_outer = 0.9即使这是默认值,只是为了展示如何更改外部可信区间。虚线是用 和 在这里创建的,geom_vlinexintercept = 不是geom_hlineyintercept = 因为coord_flip(一切都是相反的)。因此,如果您不翻转轴,则需要执行相反的操作。