簡體   English   中英

R繪制生存曲線並計算特定時間的P值

[英]R draw survival curve and calculate P-value at specific times

我想弄清楚如何生成生存曲線並計算特定時間點的 P 值,而不是整個生存曲線的 P 值。

我用的是survsurvfit從包中的方法survminersurvival創造了生存對象和ggsurvplot畫出曲線和它的p值。

df_surv <- Surv(time = df$diff_in_days, event = df$survivalstat)
df_survfit <- survfit(dat_surv ~ Schedule, data = df)

ggsurvplot(
  df_survfit , 
  data = df,
  pval = TRUE
)

現在它計算整個 2500 多天曲線的 p 值。 我還想以精確的間隔計算 P 值。 假設我想知道 / 最多 365 天的生存概率。

我不能簡單地切斷所有存活時間超過 x(例如 365)天的記錄,如下所示。 然后所有生存概率都下降到 0%,因為沒有考慮事件發生時間晚於 365 的受試者。

事件沒有發生,x 天后也沒有人活着。

df <- df[df$diff_in_days <= 365, ]

如何從整體曲線計算特定時間的 P 值?

我的數據幀的dput(head(df)可重現示例。

structure(list(diff_in_days = structure(c(2160, 84, 273, 1245, 
2175, 114), class = "difftime", units = "days"), Schedule = c(1, 
1, 1, 2, 2, 2), survivalstat = c(0, 1, 1, 0, 1, 1)), row.names = c(12L, 
28L, 33L, 38L, 58L, 62L), class = "data.frame")

我的數據框

  • UID(每一行都是一個新條目)
  • 事件發生否/是 (0,1)
  • 事件發生前的整數天數(如果尚未發生,則計算從監視開始到當前的天數(右刪失))

編輯:

使用以下代碼在 365 天后將每個人的事件發生率設置為 0。

dat$survivalstat <- ifelse(dat$diff_in_days > 365, 0, dat$survivalstat)

它確實計算了 p 值,但仍在整個曲線上。 365 天后它保持水平直到 2500 多天結束(因為沒有發生任何事件)並且 365 天后的那些事件仍然被考慮在內,因為它們仍然在曲線中。 (我假設即使 365 之后的所有數據點都相同,它們仍然會影響 P 值?)

如果您想要特定時間點的 p 值,您可以在特定時間點進行 z 檢驗。 在下面的示例中,我使用了來自生存包的肺數據集。 為了更好地幫助了解這種方法是否合適,我會在交叉驗證上發布這個問題。

library(survival)
library(dplyr)
library(broom)
library(ggplot2)
fit1 <- survfit(Surv(time,status)~sex,data = lung)
          #turn into df
df <- broom::tidy(fit1) 

fit_df <- df  %>% 
          #group by strata
          group_by(strata) %>% 
          #get day  of interest or day before it
          filter(time <= 365) %>% 
          arrange(time) %>% 
          # pulls last date
          do(tail(.,1))

#calculate z score based on 2 sample test at that time point
z <- (fit_df$estimate[1]-fit_df$estimate[2]) /
      (sqrt( fit_df$std.error[1]^2+ fit_df$std.error[2]^2))
#get probability of z score
pz <- pnorm(abs(z))
#get p value
pvalue <- round(2 * (1-pz),2)



ggplot(data = df,  aes(x=time, y=estimate, group=strata, color= strata)) +
  geom_line(size = 1.5)+
  geom_ribbon(aes(ymin = conf.low, ymax = conf.high), alpha = 0.2)+
  geom_vline(aes(xintercept=365))+
  geom_text(aes(x = 500,y=.8,label = paste0("p = " ,pvalue) ))+
  scale_y_continuous("Survival",
                     limits = c(0,1))+
  scale_x_continuous("Time")+
  scale_color_manual(" ", values = c("grey", "blue"))+
  scale_fill_discrete(guide = FALSE)+
  theme(axis.text.x = element_text(angle = 45, hjust = 1, size=14),
        axis.title.x = element_text(size =14),
        axis.text.y = element_text(size = 14),
        strip.text.x = element_text(size=14),
        axis.title.y = element_blank())+
  theme_bw()

在此處輸入圖片說明

更新 - 使用對數排名獲得特定時間點的 p 值

#First censor and make follow time to the time point of interest 
lung2 <- lung %>% 
          mutate(time2 = ifelse(time >= 365, 365, time),
                 status2 = ifelse(time >= 365, 1,status))
#Compute log rank test using survdiff
sdf <- survdiff(Surv(time2,status2)~sex,data = lung2)
#extract p-value
p.val <- round(1 - pchisq(sdf$chisq, length(sdf$n) - 1),3)

ggplot上面的代碼可以取代pvaluep.val所以它顯示日志等級分數。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM