简体   繁体   English

为每一行添加可信区间

[英]Add Credible Intervals to each line

I would appreciate your help with the following issue.感谢您对以下问题的帮助。

I am trying to add the 95% Credival Intervals (this is not the same as Confidence Intervals) to a density plot, which is separated by source.我正在尝试将 95% Credival Intervals(这与置信区间不同)添加到按源分隔的密度图中。

The best way to calculate the 95% CI is with the function hdi(x, ci = 0.95) ('HDInterval' package).计算 95% CI 的最佳方法是使用函数hdi(x, ci = 0.95) ('HDInterval' 包)。
I would like to make the plot in ggplot() , because it is easier to manipulate.我想在ggplot()中制作情节,因为它更容易操作。

Here are some simulated data:以下是一些模拟数据:

set.seed(14)
df <- data.frame(density = c(rgamma(400, 2, 10), rgamma(400, 2.25, 9),rgamma(400, 5, 7)),
                 source = rep(c("source_1", "source_2", "source_3"), 
                              each = 400))

For the ggplot对于 ggplot

ggplot(df, aes(x = density, color = source)) +
  geom_density(size=1.5, alpha = 0.4) +
  labs(y = "Density", x = "Source contribution")

And the plot where the filled areas represent the 95% CI.以及填充区域代表 95% CI 的图。

在此处输入图像描述

I also calculated the lower and upper 95% CI individually.我还分别计算了上下 95% CI。


S1 <- df %>% filter(df$source == "source_1")
S2 <- df %>% filter(df$source == "source_2")
S3 <- df %>% filter(df$source == "source_3")

data_lower <- tribble(~source, ~value,  ~hdi,
                      'S1' , 0.025, hdi(S1$density)["lower"],
                      'S2' , 0.025, hdi(S2$density)["lower"],
                      'S3' , 0.025, hdi(S3$density)["lower"])

data_upper <- tribble(~source, ~value, ~hdi,
                      's1', 0.975, hdi(S1$density)["upper"],
                      's2', 0.975, hdi(S2$density)["upper"],
                      's3', 0.975, hdi(S3$density)["upper"])

But they can also be calculated by source.但它们也可以按源计算。

hdi(S1$density, ci = 0.95)
hdi(S2$density, ci = 0.95)
hdi(S3$density, ci = 0.95)

I would appreciate your help.我会很感激你的帮助。 Thanks in advance.提前致谢。

Adapting this answer by @ClausWilke to your case you could achieve your result via ggridges:: geom_density_ridges_gradient .将@ClausWilke 的这个答案改编为您的案例,您可以通过ggridges:: geom_density_ridges_gradient实现您的结果。 Basically I use after_stat and an ifelse to fill only the upper and lower quantiles computed by hdi .基本上,我使用after_statifelse仅填充由hdi计算的上下分位数。

set.seed(14)

df <- data.frame(
  density = c(rgamma(400, 2, 10), rgamma(400, 2.25, 9), rgamma(400, 5, 7)),
  source = rep(c("source_1", "source_2", "source_3"),
    each = 400
  )
)

library(ggplot2)
library(HDInterval)
library(ggridges)

ggplot(df, aes(x = density, color = source, fill = after_stat(ifelse(quantile == 2, NA, color)))) +
  geom_density_ridges_gradient(aes(y = 0), quantile_lines = TRUE, quantile_fun = hdi, vline_linetype = 0) +
  labs(y = "Density", x = "Source contribution") +
  scale_fill_discrete(guide = "none", na.value = "transparent")
#> Picking joint bandwidth of 0.0546

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为 R 中的稳健相关提取可信区间 - Extract credible intervals for robust correlations in R 如何找到和plot不同的可信区间? - How to find and plot different credible intervals? 使用Rstanarm计算二项式Logit中的边际效应的可信区间 - Calculating credible intervals for marginal effects in binomial logit using rstanarm 从 glmmTMB output 中提取后验模式和可信区间 - Extracting posterior modes and credible intervals from glmmTMB output 为我的贝叶斯预测绘制 95% 的可信区间以及来自响应变量的实际观察值的点 - Drawing 95% credible intervals for my bayesian predictions along with the Points from the actual observed value of the response variable 为 R 中的 lme4 模型提取随机效应的后验估计和可信区间 - Extract posterior estimate and credible intervals for random effect for lme4 model in R 如何将阴影置信区间添加到具有指定值的线图 - How to add shaded confidence intervals to line plot with specified values 向网格中的每个构面添加一条线 - Add a line to each facet in a grid 在每个密度线上添加标签 - Add a label to each density line 两组之间的绝对差异及其在 R 中每行的 95% 置信区间,并将其添加到特定列中的相应行 - absolute differences between 2 groups and their 95% confidence intervals in R for each row and add that to corresponding row in a specific column
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM