简体   繁体   English

(R) 为绘图添加置信区间

[英](R) Adding Confidence Intervals To Plots

I am using R.我正在使用 R。 I am following this tutorial over here (https://rviews.rstudio.com/2017/09/25/survival-analysis-with-r/ ) and I am trying to adapt the code for a similar problem.我在这里关注本教程(https://rviews.rstudio.com/2017/09/25/survival-analysis-with-r/ )并且我正在尝试针对类似问题调整代码。

In this tutorial, a statistical model is developed on a dataset and then this statistical model is used to predict 3 news observations.在本教程中,在数据集上开发了统计 model,然后使用此统计 model 预测 3 个新闻观察。 We then plot the results for these 3 observations:然后我们 plot 这三个观察的结果:

#load libraries
library(survival)

library(dplyr)

library(ranger)

library(data.table)

library(ggplot2)

#use the built in "lung" data set
#remove missing values (dataset is called "a")

a = na.omit(lung)

#create id variable

a$ID <- seq_along(a[,1])

#create test set with only the first 3 rows

new = a[1:3,]

#create a training set by removing first three rows

a = a[-c(1:3),]



#fit survival model (random survival forest)

r_fit <- ranger(Surv(time,status) ~ age + sex + ph.ecog + ph.karno + pat.karno + meal.cal + wt.loss, data = a, mtry = 4, importance = "permutation", splitrule = "extratrees", verbose = TRUE)

#create new intermediate variables required for the survival curves

death_times <- r_fit$unique.death.times

surv_prob <-data.frame(r_fit$survival)

avg_prob <- sapply(surv_prob, mean)

#use survival model to produce estimated survival curves for the first three observations

pred <- predict(r_fit, new, type = 'response')$survival

pred <- data.table(pred)

colnames(pred) <- as.character(r_fit$unique.death.times)

#plot the results for these 3 patients

plot(r_fit$unique.death.times, pred[1,], type = "l", col = "red")

lines(r_fit$unique.death.times, r_fit$survival[2,], type = "l", col = "green")

lines(r_fit$unique.death.times, r_fit$survival[3,], type = "l", col = "blue")

在此处输入图像描述

From here, I would like to try an add confidence interval (confidence regions) to each of these 3 curves, so that they look something like this:从这里开始,我想尝试为这 3 条曲线中的每条曲线添加置信区间(置信区域),使它们看起来像这样:

在此处输入图像描述

I found a previous stackoverflow post ( survfit() Shade 95% confidence interval survival plot ) that shows how to do something similar, but I am not sure how to extend the results from this post to each individual observation.我发现了以前的 stackoverflow 帖子( survfit() 阴影 95% 置信区间生存 plot )显示了如何做类似的事情,但我不确定如何将这篇文章的结果扩展到每个单独的观察。

Does anyone know if there is a direct way to add these confidence intervals?有谁知道是否有直接的方法来添加这些置信区间?

Thanks谢谢

If you create your plot using ggplot , you can use the geom_ribbon function to draw confidence intervals as follows:如果您使用 ggplot 创建plot ,则可以使用geom_ribbon function 绘制置信区间,如下所示:

    ggplot(data=...)+
    geom_line(aes(x=..., y=...),color=...)+
    geom_ribbon(aes(x=.. ,ymin =.., ymax =..), fill=.. , alpha =.. )+
    geom_line(aes(x=..., y=...),color=...)+
    geom_ribbon(aes(x=.. ,ymin =.., ymax =..), fill=.. , alpha =.. )
    

You can put + after geom_line and repeat the same steps for each observation.您可以将 + 放在geom_line之后,并对每次观察重复相同的步骤。 You can also check: Having trouble plotting multiple data sets and their confidence intervals on the same GGplot.您还可以检查: 在同一个 GGplot 上绘制多个数据集及其置信区间时遇到问题。 Data Frame included and https://bookdown.org/ripberjt/labbook/appendix-guide-to-data-visualization.html 包括数据框https://bookdown.org/ripberjt/labbook/appendix-guide-to-data-visualization.html

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

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