-
Notifications
You must be signed in to change notification settings - Fork 0
/
analysis.R
56 lines (41 loc) · 1.47 KB
/
analysis.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# installing the R package for Survival analysis
install.packages("survival")
install.packages("survminer")
# loading the libraries
library("survival")
library("survminer")
# loading the dataset
data(diabetic, package='survival')
# displaying the top few rows of the diabetic dataset
head(diabetic)
# columns in the diabetic dataframe
names(diabetic)
# only those rows where patient was diagonised
diabetic2 <- subset(diabetic, trt==1)
# fitting the survival curve
fit<-survfit(Surv(time, status) ~ laser, data=diabetic2)
# Summary of survival curves
summary(fit)
# Access to the sort summary table
summary(fit)$table
# dataframe to create the Kaplan-Meire Curve
d <- data.frame(time = fit$time,
n.risk = fit$n.risk,
n.event = fit$n.event,
n.censor = fit$n.censor,
surv = fit$surv,
upper = fit$upper,
lower = fit$lower
)
# Visualizing the Kaplan-Meire Curve
ggsurvplot(fit,
pval = TRUE, conf.int = TRUE,
risk.table = TRUE, # Add risk table
risk.table.col = "strata", # Change risk table color by groups
linetype = "strata", # Change line type by groups
surv.median.line = "hv", # Specify median survival
ggtheme = theme_bw(), # Change ggplot2 theme
palette = c("#E7B800", "#2E9FDF")
)
# Log-Rank test comparing survival curves
surv_diff <- survdiff(Surv(time, status) ~ laser, data = diabetic2)