Survival Analysis? It's Not That Hard! Understanding Kaplan-Meier Models in R: A Quick Guide
Have you ever heard of survival analysis? It may seem intimidating when you first hear it, but it's actually not that complicated. Today, we're going to show you how to do it with the Kaplan-Meier survival analysis modelAn Easy Introduction to RI'm going to try it.
While survival analysis is often used in life science or medical research, Customer churn analysisor Product lifetimeis also a powerful tool that is often used to measure outcomes. Let's take the Kaplan-Meier model, the classic model for survival analysis, as an example.
What is Kaplan-Meier survival analysis?
Survival analysis compares the effect of Events (e.g., going out of business, churn, death, etc.)This model allows you to analyze the time until a specific event occurs. For example, The probability of a customer churning your serviceor How long until the product failsto measure the
The Kaplan-Meier model creates a survival curve and uses the The likelihood of an event occurring over timeas a visual representation. Every time this curve falls, it means an event has occurred.
Learn the Kaplan-Meier model with an easy example
Now, let's look at an easy example that you can apply in real life. Customer churn analysisFor example, we can assume that we have 10 customers using our service. How many of them will continue to use the service and how many will churn? We can use the Kaplan-Meier model to analyze the probability of churn.
We'll use R code to visualize this process.
# Load the required library
library(survival)
library(survminer)
# Create data for 10 customers
# Record the years customers used your service and the years they churned
data <- data.frame(
customer = 1:10,
start_year = rep(2020, 10), # All customers started using the service in 2020
leave_year = c(2021, NA, 2022, NA, 2023, 2024, NA, NA, 2023, 2024) # If they didn't leave, NA
)
# Calculate how long the customer has been using the service
data$time_in_service <- ifelse(is.na(data$leave_year), 2024 - data$start_year, data$leave_year - data$start_year)
# Show whether a customer has churned or not as a 1 or 0 (1: churned, 0: still using)
data$status <- ifelse(is.na(data$leave_year), 0, 1)Code description:
- Generate data: Create data for 10 customers who started using your service in 2020. Some customers start churning in 2021, while others are still using the service.
- Calculate churn periodsCalculate how many years each customer has used the service. Customers who have not churned are considered to have used the service until 2024.
- Bounce or not: Displays whether the customer has churned, as 1 (churned) and 0 (still using).
Visualize Kaplan-Meier survival curves
Now let's plot a Kaplan-Meier survival curve based on our data. We'll create a curve that visually represents how long customers have been using our service.
Apply a # Kaplan-Meier survival model
fit <- survfit(Surv(time_in_service, status) ~ 1, data = data)
Visualize the # survival curve (passing the data explicitly)
ggsurvplot(fit,
data = data, # Pass data to ggsurvplot
conf.int = TRUE, # Show confidence intervals
xlab = "Length of time using the service (years)", # label x-axis
ylab = "Percentage of customers using the service", # y-axis label
title = "Survival curve of customers using the service", # graph title
palette = "blue", # Graph color
risk.table = TRUE, # Add risk table for each year
ggtheme = theme_minimal()) # Apply minimal themeCode description:
- Apply the Kaplan-Meier modelCalculate the probability of churn by applying a Kaplan-Meier survival model to customer data.
- Visualize survival curves:
ggsurvplotUse a function to draw a survival curve for a customer based on how long they've been using your service.conf.int = TRUEindicates a confidence interval to increase the confidence in the result. - Add a risk table: Add a table below the graph that shows the number of customers using the service in each year.
Interpreting visualization results

1. Survival curve overview
- This graph shows the survival curve of customers using your service.
- x-Axisindicates how long you've been using the service (in years), y-Axisrepresents the percentage of customers still using the service.
- Initially, all customers (100%) are using the service, and over time, the survival curve steps down as customers churn.
2. key points on the curve
- The graph is a 0 年and initially shows that all customers (10) are using the service.
- 1 yearand the first churn occurs, at which point the survival curve shifts from 100% to about 90%in the list.
- 2 yearsthen churn will occur again, causing the survival curve to drop to About 80%in the list.
- 3 years Since then, the survival curve has dropped again, 60%of customers remain and continue to use the service.
- 4 years At that point, the survival curve drops further, finally reaching around 50%is the only customer left.
3. confidence intervals (shades of gray)
- Around the blue survival curve Shades of grayis Confidence Intervalto indicate the
- This shows the confidence in the survival probability at each point, with wider shading indicating greater uncertainty in the prediction.
- The confidence interval widens with each downward slope of the survival curve, increasing the uncertainty of the prediction over time.
4. Number at Risk table
- At the bottom of the graph is a table called "Number at Risk".
- This table shows the number of customers still using the service for each year.
- 0 年 At this point, all 10 customers are using the service.
- 1 year Afterward, there are 9 customers left, 2 years Later, 8 people, 3 years Later, 6 people are still using the service.
5. Interpret Curves
- Falling survival curveThe downward sloping part of the curve indicates that customers have abandoned the service. The longer a service is used, the more customers churn, and the survival curve gradually declines.
- Last survival rate: After four years, half (50%) of the customers are still using the service, meaning the other 50% have churned.
This graph visually shows how long customers stay with your service and how they tend to churn using Kaplan-Meier survival analysis. You can get a clear picture of the percentage of customers who churn over time.
Recap: Kaplan-Meier Model R Analysis, No Longer Difficult!
Today we demystified the Kaplan-Meier survival analysis model and even visualized it ourselves using R. This model may seem intimidating at first, but it becomes much easier to understand when applied to a real-world example like customer churn analysis. We hope you'll use this model for a variety of data analysis in the future!
Copy the entire code below at once and add it to the RStudioIf you're curious to see a real-world application of this Kaplan-Meier model, you can find it in the The Michelin curse that even black-and-white chefs can't escape: the story analyzed in R We hope you'll review the post.
#Full R code
# Load the required library
library(survival)
library(survminer)
# Create data for 10 customers
# Record the years customers used your service and the years they churned
data <- data.frame(
customer = 1:10,
start_year = rep(2020, 10), # All customers started using the service in 2020
leave_year = c(2021, NA, 2022, NA, 2023, 2024, NA, NA, 2023, 2024) # If they didn't leave, NA
)
# Calculate how long the customer has been using the service
data$time_in_service <- ifelse(is.na(data$leave_year), 2024 - data$start_year, data$leave_year - data$start_year)
# Show whether a customer has churned or not as a 1 or 0 (1: churned, 0: still using)
data$status <- ifelse(is.na(data$leave_year), 0, 1)
Apply a # Kaplan-Meier survival model
fit <- survfit(Surv(time_in_service, status) ~ 1, data = data)
Visualize the # survival curve (passing the data explicitly)
ggsurvplot(fit,
data = data, # Pass data to ggsurvplot
conf.int = TRUE, # Show confidence intervals
xlab = "Length of time using the service (years)", # label x-axis
ylab = "Percentage of customers using the service", # y-axis label
title = "Survival curve of customers using the service", # graph title
palette = "blue", # Graph color
risk.table = TRUE, # Add risk table for each year
ggtheme = theme_minimal()) # Apply minimal theme



