我想知道是否可以使用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的评论,您可以将绘图修改为ggobject,但我看不到直接访问原始绘图美学的方法。
例如,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)

我也可以对每个errorbar对象执行此操作,但在这一点上,我实际上并没有修改原始的绘图,而是在它的顶部进行绘图。在这种情况下,将rstanarm plot函数一起丢弃并简单地手动执行,或者创建我自己的函数是有意义的,这很好。但是,如果有人知道如何直接修改plot(rstanarm_object)的颜色,我还是会很感兴趣的。
发布于 2020-06-17 01:01:47
这不能直接解决问题,但如果没有其他解决方案出现,它可能仍然是一个可以改进的变通方法。
来自@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)

https://stackoverflow.com/questions/62400386
复制相似问题