-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkaplan_meier_plot.qmd
More file actions
104 lines (80 loc) · 2.52 KB
/
kaplan_meier_plot.qmd
File metadata and controls
104 lines (80 loc) · 2.52 KB
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
---
title: "kaplan_meier_plot"
format: gfm
editor: visual
execute:
echo: true
warning: false
message: false
---
```{r setup, include=FALSE}
# Load required libraries-----
library(tidyverse)
library(survival)
library(survminer)
library(dplyr)
```
```{r}
#Load Data-----
surv_data <- read_tsv("survival_data.tsv") #There are 60 TARGET samples with survival data"
clusters <- read_tsv("cluster_assignments_targetsamples.tsv")
```
```{r}
#Data clean up-----
#There are 75 TARGET samples with cluster assignements
clusters <- clusters %>% rename(cluster_id = Cluster)
# Filter cluster data to match samples present in survival data
common_samples <- intersect(surv_data$sample, clusters$sample)
surv_data_filtered <- surv_data %>% filter(sample %in% common_samples)
clusters_filtered <- clusters %>% filter(sample %in% common_samples)
# Merge the data by sample ID
merged_data <- inner_join(surv_data_filtered, clusters_filtered, by = "sample")
```
```{r}
# Data Check-----
# Check if you have survival status data (e.g., OS.event).)
if (!"OS.event" %in% colnames(merged_data)) {
merged_data$OS.event <- 1 # Assume all events occurred (placeholder)
}
```
```{r}
#Data Preparation for plotting-----
# Convert cluster_id to factor for plotting
merged_data$cluster_id <- as.factor(merged_data$cluster_id)
table(merged_data$OS.event, useNA = "ifany")
surv_object <- Surv(time = merged_data$OS.time, event = merged_data$OS.event)
```
```{r}
#Designing Fit Survival Curves
fit <- survfit(surv_object ~ cluster_id, data = merged_data)
summary(merged_data$OS.time)
summary(merged_data$OS.event)
summary(merged_data$cluster_id)
```
```{r}
# Plot Kaplan-Meier curves-----
ggsurvplot(fit, data = merged_data,
pval = TRUE,
risk.table = TRUE,
surv.median.line = "hv",
ggtheme = theme_minimal(),
palette = "Dark2")
```
```{r}
# Save the KM plot and Export Image-----
km_plot <- ggsurvplot(fit, data = merged_data,
pval = TRUE,
risk.table = TRUE,
surv.median.line = "hv",
ggtheme = theme_minimal(),
palette = "Dark2")
# Export the plot (you can set file type by extension)
ggsave("kaplan_meier_plot_target_assignments.png", plot = km_plot$plot, width = 8, height = 6, dpi = 300)
```
```{r}
#Log-Test-----
# Run the log-rank test to compare survival curves across clusters
log_rank_test <- survdiff(surv_object ~ cluster_id, data = merged_data)
# Display the results of the log-rank test
log_rank_test
```