melil
melil

Reputation: 81

How do I run a Kaplan-Meier (KM) analysis to determine any difference in survival between two groups?

I want to create a data matrix by combining the group variable and the ann file. Then, I want to run a Kaplan-Meier (KM) analysis to determine if a difference in survival exists between the two groups (i.e., 1 vs 0) against the status (living or dead). I want to extract the p-value from the chi squared test output.

Eg:

library(survival)
library(tidyverse)
library(survminer)

df <- x %>%  
  rename(status=vital_status,
         time=months_to_event) %>% 
  mutate(status=case_when(status=="LIVING" ~ 0,
                            status=="DECEASED" ~ 1))

f <- survdiff(Surv(time,status)~group, data=df)

plot(f,lwd=2,xlab='Weeks',ylab='S_hat(t)',main='KM Plots for Survival data')

Error in xy.coords(x, y, xlabel, ylabel, log) : 'x' is a list, but does not have components 'x' and 'y'

> dput(f)
structure(list(n = structure(c(`group=0` = 86L, `group=1` = 30L
), .Dim = 2L, .Dimnames = list(groups = c("group=0", "group=1"
)), class = "table"), obs = c(27, 5), exp = c(22.3862602609156, 
9.61373973908443), var = structure(c(6.62091719145294, -6.62091719145294, 
-6.62091719145294, 6.62091719145294), .Dim = c(2L, 2L)), chisq = 3.21505219963876, 
    na.action = structure(c(`89` = 76L, `124` = 105L, `129` = 110L
    ), class = "omit"), call = survdiff(formula = Surv(time, 
        status) ~ group, data = df)), class = "survdiff")

Upvotes: 2

Views: 386

Answers (1)

TarJae
TarJae

Reputation: 78917

library(tidyverse)
library(survival)
library(survminer)

df1 <- df %>%  
  rename(status = vital_status,
         time = months_to_event) %>% 
  mutate(status = case_when(status=="LIVING" ~ 0,
                            status=="DECEASED" ~ 1))


f1 <- survfit(Surv(time, status) ~ group, data = df1)
ggsurvplot(f1,
           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"))

enter image description here

Upvotes: 4

Related Questions