Leanpub Header

Skip to main content

Survival Analysis

Model the Occurrence and Timing of Events in Longitudinal Data Using R

Model whether and when events happen in longitudinal data, from finding a partner to breaking off a web survey, using R. In this course, you will learn how to handle censoring, build life tables and estimate discrete-time hazard models, Kaplan-Meier curves and Cox regression. By the end, you will be able to explain the occurrence and timing of events with time-constant and time-varying predictors and test the proportionality assumptions behind your models.

Interested in this course? Show your support by saying what you'd like to pay for it!

$
PDF
EPUB
WEB
About

About

About the Course

Many of the questions we ask with longitudinal data are about events. Do people experience an event, such as finding a partner, leaving a job or dropping out of a study? When does it happen? And what explains who experiences the event and when? Survival analysis, also known as event history analysis, is the set of methods developed to answer these questions.

Analysing events is difficult because of censoring. In most studies, some people have not experienced the event by the time the study ends, while others leave the study before we can see it. If we simply drop these cases, the results are biased towards the people who experience the event quickly.

This course will teach you how to analyse the occurrence and timing of events using R, from the life table to discrete-time hazard models and Cox regression.

The course starts with the main concepts: censoring, the life table, and the hazard and survival functions. It then moves to discrete-time hazard models, which treat time in waves, years or grades and can be estimated as logistic regressions on person-period data. Finally, it covers continuous-time methods, the Kaplan-Meier estimator and the Cox model, and how to test the proportionality assumptions behind both types of models. In the process, you will learn to use the survival package, glm() and the tidyverse to estimate, visualise and interpret these models.

The examples cover a wide range of events, from teachers leaving the profession and re-arrest after release from prison to finding a partner in a household panel study. The skills you learn will transfer directly to your own research projects.

What You Will Learn

Key concepts of event occurrence

Before modelling events, we need to define exactly what we are studying and how to deal with the people for whom we never see the event.

In this section, you will learn the main concepts behind survival analysis, including:

  • The three pieces of information every analysis needs: the target event, the beginning of time and the metric for clocking time
  • Right and left censoring, and why dropping censored cases biases the results
  • How to build and read a life table: the risk set, the events, the censored cases and the hazard
  • The hazard and survival functions, and how to read the median lifetime from the survival curve
  • How to compare hazard and survival curves between groups, with examples such as years in teaching, relapse to cocaine use and terms in office in the US Congress

These concepts form the basis for every model in the rest of the course.

Discrete-time hazard models

In panel studies, time is usually measured in waves, years or school grades, so we only know the period in which an event happened.

In this section, you will learn how to:

  • Move from hazards to odds and logit hazards, and why we model the hazard on the logit scale
  • Estimate discrete-time hazard models as logistic regressions with glm() and interpret the coefficients as odds ratios
  • Turn the estimated coefficients into fitted hazard and survival curves
  • Include time-constant and time-varying predictors
  • Test the proportionality assumption and relax it using interactions with time
  • Read a published application: a model of when respondents break off a web survey (Chen et al., 2022)

Because these models are logistic regressions on a different data structure, you can estimate them with tools you already know.

Continuous-time survival models

When time is measured precisely, for example in days, very few people share the same event time and the life table no longer works.

In this section, you will learn how to:

  • Explain why the hazard becomes a rate in continuous time
  • Estimate and plot Kaplan-Meier survival curves and compare them between groups
  • Prepare survival data with Surv() and fit Cox regression models with coxph()
  • Interpret hazard ratios
  • Test the proportional hazards assumption with cox.zph() and residual plots
  • Relax the assumption using stratification
  • Choose between discrete-time and continuous-time models for your own data

The examples use the lung cancer data that comes with the survival package, so you can run them on your own computer.

Learn by doing with real longitudinal data

The course combines videos, written explanations, quizzes, and hands-on practical exercises so you learn by applying the techniques directly. You will also gain access to a forum to ask questions related to the topics covered in the class.

In the first practical, you will use 15 waves of a synthetic extract of the British Household Panel Survey (BHPS) to study how long it takes people who are single to find a partner. You will create the event, time and censoring variables, build a life table and code the risk set. You will then compare discrete-time hazard models with different ways of modelling time and with predictors such as gender, age and income. In the second practical, you will use the re-arrest data from Singer and Willett’s Applied Longitudinal Data Analysis. You will estimate Kaplan-Meier curves and Cox models and test the proportional hazards assumption. A bonus section introduces recurrent events, using a clinical trial dataset that comes with the survival package.

The practicals include:

  • Exercises with activities and questions to answer
  • Full solution code
  • Detailed walkthrough videos

By the end of the course, you will have a complete workflow for analysing time-to-event data in R that you can adapt to your own datasets.

Who this course is for

This course is designed for researchers who want to study the occurrence and timing of events using R, including:

  • PhD students working with longitudinal or event history data
  • Social scientists studying life-course transitions, such as partnership, employment or leaving education, with panel or cohort data
  • Survey methodologists and data analysts interested in attrition, dropout or breakoff
  • Researchers who know logistic regression and want to model the time to an event

Prerequisites

You should already be comfortable with:

  • Basic R syntax
  • Working with data frames
  • Basic data manipulation with the tidyverse, such as mutate(), group_by() and lag()
  • Regression models, including logistic regression

If you are new to R, consider taking the course Introduction to R for Social Researchers, which provides the foundations needed to follow the workflows taught here. The first practical starts from data in long format and creates the event and time variables with dplyr. If you want to learn how to merge, reshape and clean panel data in more depth, take the course Master Longitudinal Data Wrangling using R. If you need a refresher on logistic regression, the chapter on regression models in the book Longitudinal Data Analysis Using R covers it. You do not need to know multilevel or structural equation models to follow this course.

What you will be able to do after this course

After completing the course, you will be able to:

  • Define the event, the beginning of time and the time metric for your own research question
  • Handle right censoring and build life tables in R
  • Estimate discrete-time hazard models with glm()
  • Turn the results of discrete-time models into fitted hazard and survival curves
  • Estimate Kaplan-Meier curves and Cox regression models with the survival package
  • Interpret odds ratios and hazard ratios
  • Test and relax the proportionality assumptions of discrete-time and Cox models

You will also know when to treat time as discrete and when as continuous, so you can choose the model that fits your own data.

Share this course

Instructor

About the Instructor

Alexandru Cernat

Alexandru Cernat is a professor in Methodology and Social Data Science at the University of Manchester.

He has a PhD in survey methodology from the University of Essex and was a post-doc at the National Centre for Research Methods and the Cathie Marsh Institute.

His research and teaching focus on: survey methodology, longitudinal data, measurement error, latent variable modelling, new forms of data and missing data.

He is also the founder of longitudinalanalysis.com, a platform that helps researchers and analysts learn to collect, clean, and analyse longitudinal data.

You can find out more about him and his research at: alexcernat.com.

The Leanpub Podcast

Episode 267

An Interview with Alexandru Cernat

The Leanpub 60 Day 100% Happiness Guarantee

Within 60 days of purchase you can get a 100% refund on any Leanpub purchase, in two clicks.

See full terms...

Earn $8 on a $10 Purchase, and $16 on a $20 Purchase

We pay 80% royalties on purchases of $7.99 or more, and 80% royalties minus a 50 cent flat fee on purchases between $0.99 and $7.98. You earn $8 on a $10 sale, and $16 on a $20 sale. So, if we sell 5000 non-refunded copies of your book for $20, you'll earn $80,000.

(Yes, some authors have already earned much more than that on Leanpub.)

Learn more about writing on Leanpub

Free Updates. DRM Free.

If you buy a Leanpub book, you get free updates for as long as the author updates the book! Many authors use Leanpub to publish their books in-progress, while they are writing them. All readers get free updates, regardless of when they bought the book or how much they paid (including free).

Most Leanpub books are available in PDF (for computers) and EPUB (for phones, tablets and Kindle). The formats that a book includes are shown at the top right corner of this page.

Finally, Leanpub books don't have any DRM copy-protection nonsense, so you can easily read them on any supported device.

Learn more about Leanpub's ebook formats and where to read them

Write and Publish on Leanpub

You can use Leanpub to easily write, publish and sell in-progress and completed ebooks and online courses!

Leanpub is a powerful platform for serious authors, combining a simple, elegant writing and publishing workflow with a store focused on selling in-progress ebooks.

Leanpub is a magical typewriter for authors: just write in plain text, and to publish your ebook, just click a button. (Or, if you are producing your ebook your own way, you can even upload your own PDF and/or EPUB files and then publish with one click!) It really is that easy.

Learn more about writing on Leanpub