我正在做一個決策分析,我試圖用 R 來說明假陽性(假通過)與假陰性(假不通過)之間的權衡。我創建了一個帶有零假設和備擇假設的密度圖曲線,但想通過這些示例圖進一步說明關系。非常感謝您在創建示例 1 和 2(尤其是示例 1)時提供的幫助。謝謝!
密度圖

示例 1

示例 2

uj5u.com熱心網友回復:
我使用了兩個偏移正態分布,前提是你能想象到的任何曲線。
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")))
情節 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")
)

情節 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()
)

uj5u.com熱心網友回復:
看起來您的癥結在于弄清楚如何在給定的 x 或 y 值下訪問密度曲線的值。
您可以使用來訪問由函式構造ggplot_build()的底層。這里有一些進一步的討論。data.framegeom_density
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/420804.html
標籤:
