当我读到这篇文章时,我真的很感谢你的回答,威尔克。但我想知道如何调整 hdi 的可信质量。最后我找到了解决方案!当我弄清楚分位数参数的来源(我通过点 [[2]] 访问它)时,它单击了触发器。我编写了以下函数(因为将值传递给 HDInterval::hdi 不能开箱即用):
hdi_custWidth <- function(...) {
dots <- list(...)
quantiles <- dots[[2]]
hdi_width <- quantiles[[length(quantiles)]] # uses the last entry if its a vector which should be the biggest one; better pass a single double < 1.0
if (is.na(hdi_width)) hdi_width <- .89 # happens is quantiles = 1L
message(paste0('HDI credible interval width = ', hdi_width))
HDInterval::hdi(dots[[1]], credMass = hdi_width)
}
您可以使用它来更改上面帖子中的 repex:
library(tidyverse)
library(HDInterval)
library(ggridges)
#>
#> Attaching package: 'ggridges'
#> The following object is masked from 'package:ggplot2':
#>
#> scale_discrete_manual
## create data vector
set.seed(789)
dat <- rnorm(1000)
df <- tibble(dat)
## plot density curve with qplot and mark 95% hdi
ggplot(df, aes(x = dat, y = 0, fill = stat(quantile))) +
geom_density_ridges_gradient(quantile_lines = TRUE, quantile_fun = hdi_custWidth, quantiles = .90, vline_linetype = 2) +
scale_fill_manual(values = c("transparent", "lightblue", "transparent"), guide = "none")
#> Picking joint bandwidth of 0.227
当然,您可以在 quantiles 参数中选择 0 和 1 之间的任何值(不仅仅是 .90)并获得相应的 hdi 可信质量。