r - 假阳性与假阴性权衡图

我正在进行决策分析,我试图使用 R 来说明假阳性(假通过)与假阴性(假不通过)之间的权衡。我创建了一个密度图,其中包含 null和替代假设曲线,但想通过这些示例图进一步说明这种关系。感谢您帮助创建示例 1 和 2 等图,尤其是示例 1。谢谢!

密度图

示例 1

示例 2

最佳答案

我使用了两个偏移正态分布,前提是你能想象到的任何曲线。

library(ggplot2)

xs <- seq(-2, 4, length.out = 201)
dat <- do.call(rbind,
  list(data.frame(x=xs, y=dnorm(xs), id="1"),
       data.frame(x=xs, y=dnorm(xs, 2), id="2")))

地 block 1

vline <- 1
eps <- 1e-3

ggplot(dat, aes(x, y, group = id, color = id)) +
  geom_line() +
  geom_area(aes(fill = id),
            data = ~ subset(., (id == "1" & x > (vline+eps)) | (id == "2" & x < (vline-eps)))) +
  geom_vline(xintercept = vline, linetype = "dashed") +
  labs(x = "Hazard Ratio", y = NULL) +
  guides(color = "none", fill = "none") +
  theme_classic() +
  theme(
    axis.line.y = element_blank(),
    axis.text.y = element_blank(),
    axis.ticks.y = element_blank()
  )

vline 是这里的区分线,如果它不在交叉点,那么它仍然有用。例如,

vline <- 1.2

图2

rng <- c(0.75, 0.85)
rngdat <- do.call(rbind,
  by(dat, dat$id, function(z) with(z, data.frame(approx(x, y, xout = rng), id = id[1]))))
rngdat$otherx <- fifelse(rngdat$id == "1", Inf, -Inf)

ggplot(dat, aes(x, y, group = id, color = id)) +
  geom_line(na.rm = TRUE) +
  geom_segment(aes(xend = x, yend = 0),
               data = subset(rngdat, id == 1),
               color = "black", linetype = "dashed") +
  geom_segment(aes(xend = otherx, yend = y),
               data = rngdat, linetype = "dashed") +
  coord_cartesian(xlim = c(0, 2)) +
  scale_x_continuous(name = "HR gate") +
  scale_y_continuous(
    name = "False Go Probability",
    sec.axis = sec_axis(~ ., name = "False No-Go Probability")) +
  scale_color_manual(values = c("1" = "blue", "2" = "red")) +
  guides(color = "none") +
  theme_classic() +
  theme(
    axis.line.y.left = element_line(color = "red"),
    axis.line.y.right = element_line(color = "blue")
  )

地 block 3

offset <- max(rngdat$y[rngdat$id == "1"]) + 0.1
cutoff <- 0
dat <- transform(
  dat,
  yoff = ifelse(id == "1", 0.05 + offset, 0),
  cat = ifelse(id == "1",
               ifelse(x < cutoff, "True Positive", "False Negative"),
               ifelse(x < cutoff, "False Positive", "True Negative")))

ggplot(dat, aes(x, y = y + yoff)) +
  geom_ribbon(aes(ymin = yoff, ymax = y + yoff,
                  group = cat, fill = cat, alpha = cat),
              na.rm = TRUE) +
  geom_vline(xintercept = cutoff) +
  scale_fill_manual(
    name = NULL,
    values = c("True Positive" = "red", "False Negative" = "red",
               "False Positive" = "blue", "True Negative" = "blue")) +
  scale_alpha_manual(
    name = NULL,
    values = c("True Positive" = 1, "False Negative" = 0.2,
               "False Positive" = 0.2, "True Negative" = 1)) +
  labs(x = NULL, y = NULL) +
  theme(
    legend.position = "bottom",
    axis.text.y = element_blank(),
    axis.ticks.y = element_blank()
  )

https://stackoverflow.com/questions/70841834/

相关文章:

npm - sh : hardhat: command not found when install

typescript - 如何在 Typescript 中使用 Chai expect() 验证特定

python - 将列名传递给 Pandas read_csv() 函数

java - 如何对 Map> 进行排序?

postgresql - GROUP BY 时前两个值的差异

python - 有没有办法用mypy覆盖python中继承的类属性类型?

flutter - 如何修复此语法错误?不可为 null 的实例字段 'students' 必须初始

julia - JuMP 非线性优化约束被打破到 O(1e-9)

pine-script - 如何在版本 5 中使用多个指标 ()

python - 如何使用 pybind11 从 C++ 调用 python 函数?