library(tidyverse)
library(survival)
library(survminer)
library(kableExtra)
d<-data.frame(
  time=c(2,3,2,9,16,18,7,16,5,5),
  event=c(1,0,1,1,1,0,1,1,1,0)
)
so<-Surv(d$time,d$event)
so |> t() |>  as.data.frame() |> kable() |> kable_classic()
V1 V2 V3 V4 V5 V6 V7 V8 V9 V10
time 2 3 2 9 16 18 7 16 5 5
status 1 0 1 1 1 0 1 1 1 0
fit<-survfit(so~1,data = d,type="kaplan-meier")

fit_frame<-data.frame(
  time=fit$time,
  risk=fit$n.risk,
  event=fit$n.event,
  survival=fit$surv,
  ci_lower=fit$lower,
  ci_uper=fit$upper
) |> filter(event!=0) |> kable() |> kable_classic()
fit_frame
time risk event survival ci_lower ci_uper
2 10 2 0.8000000 0.5868177 1.0000000
5 7 1 0.6857143 0.4447217 1.0000000
7 5 1 0.5485714 0.2962557 1.0000000
9 4 1 0.4114286 0.1782448 0.9496683
16 3 2 0.1371429 0.0225400 0.8344350
ggsurvplot(fit = fit,censor=T,data = d,conf.int = F,ggtheme = theme_bw())

d2<-data.frame(
  time=c(6,6,6,6, 7,9, 10,10, 11, 13,16,17, 19, 20, 22,23,25, 32, 32, 34, 35),
  event=c(1,1,1,0, 1,0, 1,0, 0, 1,1,0, 0, 0, 1,1,0, 0, 0, 0, 0)
)
so_d2<-Surv(time=d2$time,event = d2$event)
so_d2 |> t() |>  as.data.frame() |> kable() |> kable_classic()
V1 V2 V3 V4 V5 V6 V7 V8 V9 V10 V11 V12 V13 V14 V15 V16 V17 V18 V19 V20 V21
time 6 6 6 6 7 9 10 10 11 13 16 17 19 20 22 23 25 32 32 34 35
status 1 1 1 0 1 0 1 0 0 1 1 0 0 0 1 1 0 0 0 0 0
fit_d2<-survfit(so_d2~1,data = d2,type="kaplan-meier")
frame_2<-data.frame(
  time=fit_d2$time,
  risk=fit_d2$n.risk,
  event=fit_d2$n.event,
  survival=fit_d2$surv,
  ci_lower=fit_d2$lower,
  ci_uper=fit_d2$upper,
  nn=fit_d2$n.event/(fit_d2$n.event*(fit_d2$n.risk-fit_d2$n.event))
  
  
) |> mutate(
  csum=cumsum(nn),
  var=survival^2*csum
) |> filter(event!=0) |> kable(
  col.names=c(
    "Time",
    "Risk",
    "Event",
    "$S(t)$",
    "CI Lower",
    "CI Upper",
    "NN",
    "Csum",
    "var"
  )
) |> kable_classic()
frame_2
Time Risk Event \(S(t)\) CI Lower CI Upper NN Csum var
6 21 3 0.8571429 0.7198171 1.0000000 0.0555556 0.0555556 0.0408163
7 17 1 0.8067227 0.6531242 0.9964437 0.0625000 0.1180556 0.0768307
10 15 1 0.7529412 0.5859190 0.9675748 0.0714286 NaN NaN
13 12 1 0.6901961 0.5096131 0.9347692 0.0909091 NaN NaN
16 11 1 0.6274510 0.4393939 0.8959949 0.1000000 NaN NaN
22 7 1 0.5378151 0.3370366 0.8582008 0.1666667 NaN NaN
23 6 1 0.4481793 0.2487882 0.8073720 0.2000000 NaN NaN
ggsurvplot(fit_d2,data = d2,censor=T,conf.int = F,ggtheme = theme_bw())

d3<-data.frame(
  time=c(2,3,2,9,18,7,16,5,5,16),
  event=c(1,0,1,1,0,1,1,1,0,1)
)
so_d3<-Surv(d3$time,d3$event)
so_d3 |> t() |> kable() |> kable_classic()
time 2 3 2 9 18 7 16 5 5 16
status 1 0 1 1 0 1 1 1 0 1
fit_d3<-survfit(so_d3~1,data=d3,type="kaplan-meier")
summary(fit_d3)
## Call: survfit(formula = so_d3 ~ 1, data = d3, type = "kaplan-meier")
## 
##  time n.risk n.event survival std.err lower 95% CI upper 95% CI
##     2     10       2    0.800   0.126       0.5868        1.000
##     5      7       1    0.686   0.151       0.4447        1.000
##     7      5       1    0.549   0.172       0.2963        1.000
##     9      4       1    0.411   0.176       0.1782        0.950
##    16      3       2    0.137   0.126       0.0225        0.834
fit_frame3<-data.frame(
  time=fit_d3$time,
  risk=fit_d3$n.risk,
  event=fit_d3$n.event,
  survival=fit_d3$surv,
  ci_lower=fit_d3$lower,
  ci_uper=fit_d3$upper
) 

fit_frame3 |> filter(event!=0) |> kable() |> kable_classic()
time risk event survival ci_lower ci_uper
2 10 2 0.8000000 0.5868177 1.0000000
5 7 1 0.6857143 0.4447217 1.0000000
7 5 1 0.5485714 0.2962557 1.0000000
9 4 1 0.4114286 0.1782448 0.9496683
16 3 2 0.1371429 0.0225400 0.8344350
ggsurvplot(fit_d3,data=d3,conf.int = F,censor=T,ggtheme = theme_bw(),surv.median.line = "hv")

d4<-data.frame(
  time=c(2,3,2,9,18,7,16,5,5,16),
  event=c(1,0,1,1,0,1,1,1,0,1)
)
so_d4<-Surv(d4$time,d4$event)
so_d4 |> t() |> kable() |> kable_classic()
time 2 3 2 9 18 7 16 5 5 16
status 1 0 1 1 0 1 1 1 0 1
fit_d4<-survfit(so_d4~1,data=so_d4,type="kaplan-meier")

fit_frame_4<-data.frame(
  time=fit_d4$time,
  risk=fit_d4$n.risk,
  event=fit_d4$n.event,
  survival=fit_d4$surv,
  ci_lower=fit_d4$lower,
  ci_uper=fit_d4$upper
) 
fit_frame_4 |> filter(event!=0) |> kable() |> kable_classic()
time risk event survival ci_lower ci_uper
2 10 2 0.8000000 0.5868177 1.0000000
5 7 1 0.6857143 0.4447217 1.0000000
7 5 1 0.5485714 0.2962557 1.0000000
9 4 1 0.4114286 0.1782448 0.9496683
16 3 2 0.1371429 0.0225400 0.8344350
ggsurvplot(fit_d4,data=d4,conf.int = F,censor=T,ggtheme = theme_bw(),surv.median.line = "hv")

d5<-data.frame(
  time=c(5,11,14,21,25,32,48,2,12,23,35),
  event=c(1,1,1,1,1,1,1,0,0,0,0)
)
d5 |> t() |>  kable() |> kable_classic()
time 5 11 14 21 25 32 48 2 12 23 35
event 1 1 1 1 1 1 1 0 0 0 0
so_d5<-Surv(d5$time,d5$event)
so_d5 |>as.data.frame() |>   kable() |> kable_classic()
x
5
11
14
21
25
32
48
2+
12+
23+
35+
fit_d5<-survfit(so_d5~1,data=d5,type="kaplan-meier")

fit_frame_5<-data.frame(
  time=fit_d5$time,
  risk=fit_d5$n.risk,
  event=fit_d5$n.event,
  survival=fit_d5$surv,
  ci_lower=fit_d5$lower,
  ci_uper=fit_d5$upper
) 
fit_frame_5 |> filter(event!=0) |> kable() |> kable_classic()
time risk event survival ci_lower ci_uper
5 10 1 0.9000000 0.7320116 1.0000000
11 9 1 0.8000000 0.5868177 1.0000000
14 7 1 0.6857143 0.4447217 1.0000000
21 6 1 0.5714286 0.3258387 1.0000000
25 4 1 0.4285714 0.1930923 0.9512211
32 3 1 0.2857143 0.0923355 0.8840871
48 1 1 0.0000000 NA NA
ggsurvplot(fit_d5,data=d5,conf.int = F,censor=T,ggtheme = theme_bw(),surv.median.line = "hv")

d6<-data.frame(
  time=c(10,13,18,19,23,30,36,38,54,56,59,75,73,97,104,107,107,107),
  event=c(1,0,0,1,0,1,1,0,0,0,1,1,1,1,0,1,0,0)
)

so_d6<-Surv(d6$time,d6$event)

so_d6 |>as.data.frame() |>   kable() |> kable_classic()
x
10
13+
18+
19
23+
30
36
38+
54+
56+
59
75
73
97
104+
107
107+
107+
fit_d6<-survfit(so_d6~1,data=d6,type="kaplan-meier")

fit_frame_6<-data.frame(
  time=fit_d6$time,
  risk=fit_d6$n.risk,
  event=fit_d6$n.event,
  survival=fit_d6$surv,
  ci_lower=fit_d6$lower,
  ci_uper=fit_d6$upper
) 
fit_frame_6 |> filter(event!=0) |> kable() |> kable_classic()
time risk event survival ci_lower ci_uper
10 18 1 0.9444444 0.8443382 1.0000000
19 15 1 0.8814815 0.7394986 1.0000000
30 13 1 0.8136752 0.6429314 1.0000000
36 12 1 0.7458689 0.5576521 0.9976121
59 8 1 0.6526353 0.4412662 0.9652515
73 7 1 0.5594017 0.3411298 0.9173349
75 6 1 0.4661681 0.2531720 0.8583600
97 5 1 0.3729345 0.1758973 0.7906891
107 3 1 0.2486230 0.0829480 0.7452069
ggsurvplot(fit_d6,data=d6,conf.int = F,censor=T,ggtheme = theme_bw(),surv.median.line = "hv")

ggsurvplot(fit_d6,data=d6,conf.int = F,censor=F,ggtheme = theme_bw(),fun = "cumhaz")

d7<-data.frame(
  time=c(23,47,69,70,100,148,190,208,212,214),
  event=c(1,1,1,0,0,1,0,0,0,0)
)

so_d7<-Surv(d7$time,d7$event)

so_d7 |>as.data.frame() |>   kable() |> kable_classic()
x
23
47
69
70+
100+
148
190+
208+
212+
214+
fit_d7<-survfit(so_d7~1,data=d7,type="kaplan-meier")

fit_frame_7<-data.frame(
  time=fit_d7$time,
  risk=fit_d7$n.risk,
  event=fit_d7$n.event,
  survival=fit_d7$surv,
  ci_lower=fit_d7$lower,
  ci_uper=fit_d7$upper
) 
fit_frame_7 |> filter(event!=0) |> kable() |> kable_classic()
time risk event survival ci_lower ci_uper
23 10 1 0.90 0.7320116 1
47 9 1 0.80 0.5868177 1
69 8 1 0.70 0.4665332 1
148 5 1 0.56 0.3081810 1
ggsurvplot(fit_d7,data=d7,conf.int = F,censor=T,ggtheme = theme_bw(),surv.median.line = "hv")
## Warning in .add_surv_median(p, fit, type = surv.median.line, fun = fun, :
## Median survival not reached.

ggsurvplot(fit_d7,data=d7,conf.int = F,censor=F,ggtheme = theme_bw(),fun = "cumhaz")