1

我想知道是否可以将颜色直接应用于带有bayesplot的rstanarm模型的参数

例如来自https://mc-stan.org/bayesplot/articles/plotting-mcmc-draws.html

library("bayesplot")
library("ggplot2")
library("rstanarm")

fit <- stan_glm(mpg ~ ., data = mtcars, seed = 1111)
posterior <- as.array(fit)

color_scheme_set("red")
mcmc_intervals(posterior, pars = c("cyl", "drat", "am", "sigma"))

返回:

在此处输入图像描述

该文档描述了自定义方案或混合,但它们仍然适用于图中的每个参数:

color_scheme_set("mix-blue-red")

plot(fit, pars = c("cyl", "drat", "am", "sigma"))

在此处输入图像描述

plot 函数(带有 rstanarm 模型)不再接受col能够指定每个点的参数。无论如何为图中的每个参数指定一串颜色(或方案)?

编辑:根据@Limey 的评论,您可以将绘图修改为 ggobjects,但我没有看到一种直接访问原始绘图美学的方法。

例如,scale_colour_manual(或任何其他scale_colour_...)不会改变绘制的值:

plot(fit, pars = c("cyl", "drat", "am", "sigma"))+
  scale_colour_manual(values=c("blue", "purple", "orange","red"))

(见上图,看起来一样)。另一个例子,我可以在原图之上绘制点:

plot(fit, pars = c(names(mtcars)[-1]))+
  geom_point(aes(x=fit$coefficients[-1],y=names(fit$coefficients)[-1]),color="black")

在此处输入图像描述

并使它们更大以匹配(注意我在这里也将它们设为粉红色):

plot(fit, pars = c(names(mtcars)[-1]))+
  geom_point(aes(x=fit$coefficients[-1],y=names(fit$coefficients)[-1]),color="pink",size=3)

在此处输入图像描述

我也可以对每个误差条对象执行此操作,但此时我并没有真正修改原始图,而是在它的顶部绘制。在这种情况下,将 rstanarm 绘图函数全部丢弃并简单地手动执行,或者创建我自己的函数是有意义的,这很好。但是,如果有人知道如何plot(rstanarm_object)直接修改颜色,我仍然有兴趣听听。

4

1 回答 1

1

这并不能直接解决问题,但如果没有其他解决方案出现,它可能仍然是一种可以改进的解决方法。

通过@Limey 的评论源于对 OP 的编辑:

您可以自己从模型对象中绘制所有内容,该对象接近绘制的值。我意识到这并不完美,也许有人对这些值如何完美重叠提出了建议。rstanarm绘图默认为 50% 和 90%的可信区间。我在这里使用了 t 表和 50% 和 90%的置信区间,它们并不相同,但结果很接近。

color_scheme_set("gray")

vars<-c(2,5,9,11) # select values from fit$coefficients 
                  # (this is assumed to match with names(mtcars))
                  # Note I have switched `sigma` to `carb` for ease of model retrieval

plot(fit, pars = c(names(mtcars)[vars]))+ # the original plot, now in grey

# the first error bar. With a t table, 1.645 is a 90% confidence interval,
# which isn't the same here as the 90% credible interval from rstanarm, but it 
# gets us close
  geom_errorbarh(aes(xmin=(fit$coefficients[vars]-fit$ses[vars]*1.645),
                     xmax=(fit$coefficients[vars]+fit$ses[vars]*1.645),
                     y=names(fit$coefficients)[vars]),
                 height=0,alpha=0.5,color="yellow" # transparency is so you can see grey through the yellow.. I know this isn't pretty
                     )+

  geom_errorbarh(aes(xmin=(fit$coefficients[vars]-fit$ses[vars]*.675), # this is .675, which corresponds to a 50% confidence interval (again not the same as a credible interval, but it is getting us close
                     xmax=(fit$coefficients[vars]+fit$ses[vars]*.675),
                     y=names(fit$coefficients)[vars]),
                 height=0,lwd=2,alpha=0.5,color="yellow"
                     )+
# now plot the points over the lines:
  geom_point(aes(x=fit$coefficients[vars],
y=names(fit$coefficients)[vars]),
color="yellow",size=3,alpha=0.5)

在此处输入图像描述

这不漂亮,也不准确(你可以看到黄色边缘有点灰色),但它很接近。如果您可以修改每个值集的颜色,那么您可以交替使用颜色:

colors=c("red","blue","red","blue") # selecting colors one by one
colors=rep(c("red","blue"),2) # or just have alternating colors repeat (2 times here)

ggplot()+ # note that I have removed `rstanarm` object, which is no longer necessary
  geom_errorbarh(aes(xmin=(fit$coefficients[vars]-fit$ses[vars]*1.645),
                     xmax=(fit$coefficients[vars]+fit$ses[vars]*1.645),
                     y=names(fit$coefficients)[vars]),
                 height=0,alpha=0.5,color=colors
                     )+
  geom_errorbarh(aes(xmin=(fit$coefficients[vars]-fit$ses[vars]*.675),
                     xmax=(fit$coefficients[vars]+fit$ses[vars]*.675),
                     y=names(fit$coefficients)[vars]),
                 height=0,lwd=2,alpha=0.5,color=colors
                     )+
  geom_point(aes(x=fit$coefficients[vars],
y=names(fit$coefficients)[vars]),
color=colors,size=3,alpha=0.5)

在此处输入图像描述

或完全古怪的东西:

colors=c("red","blue","green","yellow")
colors2=rev(colors)

ggplot()+
  geom_errorbarh(aes(xmin=(fit$coefficients[vars]-fit$ses[vars]*1.645),
                     xmax=(fit$coefficients[vars]+fit$ses[vars]*1.645),
                     y=names(fit$coefficients)[vars]),
                 height=0,alpha=0.5,color=colors
                     )+
  geom_errorbarh(aes(xmin=(fit$coefficients[vars]-fit$ses[vars]*.675),
                     xmax=(fit$coefficients[vars]+fit$ses[vars]*.675),
                     y=names(fit$coefficients)[vars]),
                 height=0,lwd=2,alpha=0.5,color=colors
                     )+
  geom_point(aes(x=fit$coefficients[vars],
                 y=names(fit$coefficients)[vars]),
             color=colors2,size=3,alpha=0.5)

在此处输入图像描述

于 2020-06-16T17:01:46.797 回答