Creating a Kaplan-Meier Plot in Shiny for Survival Analysis and Data Visualization

Making a Kaplan-Meier Plot in Shiny

=====================================================

In this article, we will explore how to create a Kaplan-Meier plot using the Shiny framework. The Kaplan-Meier plot is a survival analysis tool that provides a graphical representation of the survival distribution over time for a group of individuals with different levels of exposure to an unknown risk factor.

Introduction


Survival analysis is a branch of statistics that deals with the study of the time until a specific event occurs. In this article, we will focus on creating a Kaplan-Meier plot in Shiny, which allows users to select the variable used to stratify the data by exposure level.

The Kaplan-Meier plot is a powerful tool for visualizing survival distributions and can be used to compare the survival patterns of different groups over time. In this article, we will explore how to create a Shiny app that generates a Kaplan-Meier plot using user-selectable variables.

Background


To understand how to create a Kaplan-Meier plot in Shiny, it is essential to have a basic understanding of survival analysis and the Shiny framework. The following concepts are assumed to be familiar:

  • Survival analysis: Understanding of the concept of survival time, censoring, and Kaplan-Meier estimation.
  • Shiny app development: Familiarity with the basics of Shiny app development, including UI components, server-side logic, and reactive expressions.

Kaplan-Meier Estimation


The Kaplan-Meier plot is based on the Kaplan-Meier estimator, which is a non-parametric estimate of the survival distribution. The Kaplan-Meier estimator is defined as:

  • For censored data: $\hat S(t) = \frac{\prod_{i=1}^n (1 - \frac{d_i}{r_i})}{\prod_{i=1}^k (1 - \frac{n_i}{N_i})}$, where $d_i$ is the number of failures at time $t_i$, $r_i$ is the number of individuals at risk at time $t_i$, and $n_i$ is the number of events observed at time $t_i$.
  • For uncensored data: $\hat S(t) = \prod_{i=1}^n (1 - \frac{d_i}{r_i})$

where $S(t)$ is the survival function, and $N$ is the total number of individuals.

Creating the Shiny App


To create a Shiny app that generates a Kaplan-Meier plot, we will use the following steps:

1. Define the UI Components

The first step in creating a Shiny app is to define the user interface components. In this case, we will use a fluidPage component with a titlePanel, sidebarLayout, and mainPanel.

ui <- fluidPage(
  titlePanel("Kaplan-Meier Plot"),
  sidebarLayout(
    sidebarPanel(
      selectInput("variable", "Variable:", 
                  c("Distance_iTLS_intra" = "Distance_iTLS_intra",
                    "Distance_iTLS_peri" = "Distance_iTLS_peri",
                    "Distance_iTLS" = "Distance_iTLS"))
    ),
    mainPanel(
      plotOutput("output$KM")
    )
  )
)

2. Define the Server-Side Logic

The second step is to define the server-side logic that generates the Kaplan-Meier plot.

server <- function(input, output) {
  access_sam2rx <- reactive({
    access_sam2 %>% 
      mutate(factorHL = ifelse(access_sam2[[input$variable]] > (median(access_sam2[[input$variable]], na.rm=TRUE)), "high", "low"))
  })
  
  surv_fit <- reactive({
    survfit(Surv(time = access_sam2rx$Progression_Free_Time) ~ access_sam2rx$factorHL, data = access_sam2rx)
  })
  
  output$KM <- renderPlot({
    ggsurvplot(surv_fit(), risk.table = TRUE, data = access_sam2rx())
  })
}

3. Create the Shiny App

The final step is to create the Shiny app by combining the UI and server-side logic components.

shinyApp(ui, server)

Example Use Cases


Here are a few example use cases for the Kaplan-Meier plot in Shiny:

  • Comparing Survival Patterns: By selecting different variables, users can compare the survival patterns of different groups over time.
  • Identifying High-Risk Groups: The Kaplan-Meier plot can be used to identify high-risk groups by plotting the survival function for each group.
  • Visualizing Hazard Rates: The Kaplan-Meier plot can also be used to visualize hazard rates, which represent the probability of failure at a given time.

Conclusion


In this article, we explored how to create a Kaplan-Meier plot in Shiny. We defined the necessary UI and server-side logic components, and provided example use cases for the plot. By following these steps, you can create your own Shiny app that generates a Kaplan-Meier plot for visualizing survival distributions.

Additional Resources


For further reading on survival analysis and Shiny app development, we recommend the following resources:

  • Survival Analysis: “Survival Analysis: A Comprehensive Introduction” by Robert E. K single
  • Shiny App Development: “R Shiny: Visualizing Data with Interactive Web Applications” by Hadley Wickham

Last modified on 2024-01-18