简体   繁体   English

为 R 中的风险比绘制正态分布密度 plot

[英]Plotting normal distribution density plot for hazard ratio in R

I'm trying to plot 2 normal distribution density plots for null and alternative hazard ratios of 1 and 0.65, respectively, to replicate an example (plot attached).我正在尝试 plot 2 正态分布密度图 null 和替代风险比分别为 1 和 0.65,以复制示例(附图)。 Here's my code so far but it doesn't makes sense to me to have negative values for hazard ratios, but when I don't have negative values, the distributions are cut off.到目前为止,这是我的代码,但对我来说,风险比为负值是没有意义的,但是当我没有负值时,分布就会被切断。 So I know I'm doing something wrong here.所以我知道我在这里做错了什么。 Thanks!谢谢!

x <- seq(-2, 2, length.out = 100000)
df <- do.call(rbind,
  list(data.frame(x=x, y=dnorm(x, mean = log(1), sd = sqrt(1/60 + 1/60)), id="H0, HR = 1"),
       data.frame(x=x, y=dnorm(x, mean = log(0.65), sd = sqrt(1/60 + 1/60)), id="H1, HR = 0.65")))

vline <- 0.65
p1 <- ggplot(df, aes(x, y, group = id, color = id)) +
  geom_line() +
  geom_area(aes(fill = id),
            data = ~ subset(., (id == "H1, HR = 0.65" & x > (vline)) | (id == "H0, HR = 1" & x < (vline))),
            alpha = 0.3) +
  geom_vline(xintercept = vline, linetype = "dashed") +
  labs(x = "log(Hazard Ratio)", y = 'Density') + xlim(-2, 2) +
  guides(fill = "none", color = guide_legend(override.aes = list(fill = "white"))) +
  theme_classic() + 
  theme(legend.title=element_text(size=10), legend.position = c(0.8, 0.4),
        legend.text = element_text(size = 10), 
    axis.line.y = element_blank(),
    axis.text.y = element_blank(),
    axis.ticks.y = element_blank()
  ) + 
  scale_color_manual(name = '', values = c('red', 'blue')) +
  scale_fill_manual(values = c('red', 'blue'))

The plot I'm trying to replicate我正在尝试复制的 plot

我试图复制的情节

This gets reasonably close to the image that you have posted.这与您发布的图像相当接近。

You should not use the log() of the means, but rather the mean s as is.您不应该使用log()的手段,而应该使用mean的原样。 Moreover if you use the normal distribution, you assume that parameters can take any value between -Inf and Inf , albeit with very small densities far from the mean.此外,如果您使用正态分布,您假设参数可以取-InfInf之间的任何值,尽管密度非常小,远离平均值。 Therefore, you cannot expect all values to be positive.因此,您不能期望所有值都是正数。 If you would like your values to be bounded by 0, then you should use a gamma distribution instead.如果您希望您的值以 0 为界,那么您应该改用伽玛分布

x <- seq(-2, 2, length.out = 1000)
df <- do.call(rbind,
              list(data.frame(x=x, y=dnorm(x, mean = 1, sd = sqrt(1/50)), id="H0, HR = 1"),
                   data.frame(x=x, y=dnorm(x, mean = 0.65, sd = sqrt1/50)), id="H1, HR = 0.65")))

vline <- 0.65

ggplot(df, aes(x, y, group = id, color = id)) +
  geom_line() +
  geom_area(aes(fill = id),
            data = ~ subset(., (id == "H1, HR = 0.65" & x > (vline)) | (id == "H0, HR = 1" & x < (vline))),
            alpha = 0.3) +
  geom_vline(xintercept = vline, linetype = "dashed") +
  labs(x = "log(Hazard Ratio)", y = 'Density') + xlim(-2, 2) +
  guides(fill = "none", color = guide_legend(override.aes = list(fill = "white"))) +
  theme_classic() + 
  theme(legend.title=element_text(size=10), legend.position = c(0.8, 0.4),
        legend.text = element_text(size = 10), 
        axis.line.y = element_blank(),
        axis.text.y = element_blank(),
        axis.ticks.y = element_blank()
  ) + 
  scale_color_manual(name = '', values = c('red', 'blue')) +
  scale_fill_manual(values = c('red', 'blue')) +
  scale_x_continuous(breaks = seq(-0.3, 2.1, 0.3),
                     limits = c(-0.3, 2.1))

在此处输入图像描述

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM