Leanpub Header

Skip to main content

Longitudinal Data Analysis using R

In this track, you will learn everything you need to run your own longitudinal analysis. It covers the core skills you need, from preparing longitudinal data to the most common research models: fixed effects, random effects, multilevel models for change, cross-lagged models, latent growth models, and survival analysis.

Minimum price

$499

$499

You pay

Author earns

$

Also available for 3 course credits with a Learner Membership

These courses have a total suggested price of $900. Get them now for only $499!
About

About

About the Track

Longitudinal data, or data collected multiple times from the same cases, let you study how people change over time, causal relationships, and events. Using this information requires two sets of skills. You have to prepare complex panel data and work with complex statistical models that fit your research question.

This track brings together four courses that cover both these skills. The track starts with Master Longitudinal Data Wrangling using R. In this course, you will learn how to select, recode, merge, and reshape panel data, so you can move from messy raw files to analysis-ready data with a clear and reproducible workflow.

Fixed, Random and Multilevel Models for Change then discusses how to use the two sources of variation in longitudinal data, between and within individuals, to estimate better causal relationships and look at individual-level change. In this course, you will estimate fixed effects, random effects, and hybrid models, and build multilevel models for change, including time-varying predictors and non-linear change.

Longitudinal Structural Equation Modelling will teach you how to use SEM to study stability, causal direction, and individual change. You will estimate cross-lagged, random-intercept cross-lagged, and latent growth models, and test your hypotheses by comparing models.

Finally, Survival Analysis looks at whether and when events happen. You will learn how to deal with censoring, build life tables and estimate discrete-time hazard models, Kaplan-Meier curves and Cox regression.

Across the four courses, learning is hands-on. Each course combines short video lessons, quizzes, and practical exercises with full solution code. Most of the practicals use a synthetic version of Understanding Society, one of the largest household panel studies in the world, so the skills you learn transfer directly to your own research.

By completing this track, you will be able to:

  • Prepare longitudinal data for analysis with a reproducible workflow in R
  • Separate within and between variation and choose between fixed effects, random effects and multilevel models
  • Model change in time with multilevel models for change and latent growth models
  • Study stability and causal direction with cross-lagged panel models
  • Analyse the occurrence and timing of events with discrete-time and continuous-time survival models
  • Test research hypotheses by comparing models

This track is designed for PhD students, researchers and data analysts in the social, behavioural and health sciences who work with panel, cohort or other repeated-measures data. You should already know basic R and linear regression. If you are new to R, consider taking Introduction to R for Social Researchers first.

 

Share this track

Certificate

Verified Certificate

When you successfully complete the Track, you will receive a verified Certificate of Completion from Leanpub, certifying that you have completed the entire Track.

Courses

Courses Included

Master Longitudinal Data Wrangling using R

Clean, Merge, and Reshape Panel Data Efficiently Using R

Longitudinal datasets are one of the most powerful sources of evidence in the social sciences. They allow researchers to study how individuals, organisations, and societies change over time. However, working with longitudinal data can quickly become overwhelming. Multiple waves, inconsistent variable names, missing data, and complex data structures make longitudinal data wrangling one of the biggest challenges researchers face.

This course will teach you how to efficiently clean, reshape, and prepare longitudinal datasets using R so you can move from messy raw data to analysis-ready datasets with confidence.

Instead of spending weeks struggling with merging waves, restructuring panel data, or fixing variable coding, you will learn a clear and reproducible workflow for longitudinal data wrangling in R. By the end of the course, you will know how to prepare complex panel datasets for statistical analysis while writing clean, reusable code.

The course focuses on practical data wrangling techniques used by researchers working with longitudinal and panel data, including selecting variables, recoding data, merging waves, reshaping datasets, handling missing values, and automating repetitive data preparation tasks.

All examples use realistic longitudinal survey data, so the skills you learn transfer directly to your own research projects.

What You Will Learn

Understanding longitudinal data structures

Before cleaning data, it is essential to understand how longitudinal datasets are organised.

In this section, you will learn the key concepts behind longitudinal and panel data, including:

  • The difference between longitudinal and cross-sectional datasets
  • Common longitudinal study designs
  • Wide vs long data structures
  • Time-varying and time-constant variables
  • The Age–Period–Cohort conundrum in longitudinal research

Understanding these concepts will help you design better workflows for longitudinal data preparation and analysis.

Core data wrangling tools in R

Cleaning longitudinal datasets often requires a combination of several data manipulation steps.

In this section, you will learn how to perform essential data wrangling tasks in R, including:

  • Selecting variables and cases from large datasets
  • Recoding variables and handling missing values
  • Working with categorical variables and factors
  • Creating new variables for analysis
  • Writing clear and reproducible data wrangling pipelines

These tools form the foundation for efficient data-cleaning workflows for panel and longitudinal data.

Merging and reshaping longitudinal datasets

One of the most common challenges in longitudinal research is combining data across waves and restructuring datasets into formats suitable for analysis.

In this section, you will learn how to:

  • Merge datasets across waves using unique identifiers
  • Identify and explore attrition and non-response patterns
  • Reshape data between wide and long formats
  • Prepare datasets for panel models and longitudinal analysis

These are core skills for anyone working with panel data, repeated measures datasets, or cohort studies.

Automating longitudinal data preparation

Longitudinal projects often involve repeating the same data preparation steps across many waves.

Instead of copying and modifying code manually, you will learn how to automate data wrangling tasks in R, including:

  • Iteration with functional programming
  • Writing reusable R functions
  • Processing multiple datasets efficiently
  • Building scalable workflows for large longitudinal studies

These techniques allow you to handle complex datasets while keeping your code clean, efficient, and reproducible.

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.

The practical exercises use a synthetic version of Understanding Society (UKHLS), one of the largest longitudinal household surveys in the world. You will prepare multiple waves of the dataset, clean variables, reshape the data, and explore patterns across time.

The practical includes:

  • Step-by-step exercises
  • Full solution code
  • Detailed walkthrough videos

By the end of the course, you will have built a complete workflow for preparing longitudinal 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 become confident working with longitudinal and panel data in R, including:

  • PhD students working with longitudinal datasets
  • Social scientists using panel studies or cohort data
  • Data analysts working with repeated-measures data
  • Researchers transitioning from cross-sectional to longitudinal analysis

Prerequisites

You should already be comfortable with:

  • Basic R syntax
  • Working with data frames
  • Basic data manipulation

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.

What you will be able to do after this course

After completing the course, you will be able to:

  • Prepare complex longitudinal datasets for analysis in R
  • Merge and reshape multi-wave panel data efficiently
  • Clean and recode variables across survey waves
  • Handle missing data and attrition patterns
  • Automate repetitive data preparation tasks

Most importantly, you will gain a clear workflow for longitudinal data wrangling, allowing you to focus on answering research questions rather than fighting with messy data.

 

Fixed, Random and Multilevel Models for Change

Separate Within and Between Variation and Model Change in Panel Data Using R

Longitudinal data, or data collected multiple times from the same cases, makes it possible to study how people change in time and why some people change more than others. This also brings a challenge. Each person is measured repeatedly, so the variation in the data has two sources: differences between people and changes within the same person.

This course will teach you the main regression-based models for this type of data and how to estimate them in R. You will start with fixed effects, random effects and hybrid models, which are useful when the main interest is in causal relationships. You will then learn the multilevel model for change. This model describes how people change in time, how much they differ in their starting point and rate of change, and what explains these differences.

The course focuses on the decisions you have to make when modelling change. These include which source of variation to model, whether to use fixed or random effects and how to code time. They also include how to add predictors that change in time and how to model change that is not linear.

The lectures and practicals use data from Understanding Society, a large UK household panel study, and all the models are estimated in R with the plm and lme4 packages. This way, the skills you learn transfer directly to your own research projects.

What You Will Learn

Within and between variation

Before modelling change, it helps to understand where the variation in longitudinal data comes from.

In this section, you will learn:

  • Why repeated measures are nested within people
  • The difference between within variation and between variation
  • The two ways of using panel data: as a quasi-experimental design to estimate causal relationships and as a way to explore change in time
  • Why time-constant variables can explain only the between variation, while time-varying variables have both types of variation

These concepts are the basis for every model in the course.

Fixed, random and hybrid models

When the main interest is in causal relationships and change in time is not the main focus, fixed and random effects models are a good place to start.

In this section, you will learn how to:

  • Estimate fixed effects (within) models, which control for all time-constant variables, measured or not
  • Estimate random effects models, which give both the within and the between variance and can include time-constant variables
  • Weigh the advantages and disadvantages of the two approaches, such as their assumptions and efficiency
  • Choose between them using the Hausman test
  • Estimate the hybrid (within-between) model, which gives the within and the between effect of the same predictor in one model
  • Run all of these models in R with the plm package

In the first practical, you will apply these models to explain log income.

The multilevel model for change

The multilevel model for change describes the average change in time and how much people differ from it.

In this section, you will learn how to:

  • Estimate the unconditional means model and split the variation into between and within parts
  • Calculate and interpret the intraclass correlation (ICC)
  • Estimate the unconditional growth model with random intercepts and random slopes using lme4
  • Interpret the correlation between the starting point and the rate of change
  • Explain differences in the starting point and in the rate of change with time-constant predictors
  • Compare models using likelihood ratio tests, AIC and BIC, and calculate the variance explained by a predictor
  • Read the results of a multilevel model for change in a published paper

These steps give you a sequence of models you can use to describe and explain change in your own data.

Extending the multilevel model for change

Many research questions involve predictors that change in time and trajectories that are not straight lines.

In this section, you will learn how to:

  • Include time-varying predictors and interact them with time
  • Separate a time-varying predictor into its between and within parts
  • Use lagged predictors and understand how they change the sample
  • Recode time so that the intercept refers to the start, the middle or the end of the study
  • Use the year of the interview, or age, as the time variable in place of the wave
  • Model non-linear change with polynomials or with time as a categorical variable, and decide between them
  • Build models in steps: empty models first, then unconditional change models, then predictors in blocks

With these extensions, you can adapt the multilevel model for change to the research questions and data in your own projects.

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.

The lectures help you gain hands on experince by applying what you learned to Understanding Society (UKHLS), one of the largest longitudinal household surveys in the world.

Each practical includes:

  • Step-by-step exercises
  • Full solution code, as a PDF and as an R script
  • Detailed walkthrough videos

By the end of the course, you will have run a full sequence of fixed effects, random effects and multilevel models for change in R. You can then adapt the same code to your own panel data.

Who this course is for

This course is designed for researchers who want to model change with longitudinal and panel data in R, including:

  • PhD students working with longitudinal datasets
  • Social scientists using panel studies or cohort data
  • Data analysts working with repeated-measures data
  • Researchers who know linear regression and want to move to models for change
  • Researchers who need to choose between fixed effects, random effects and multilevel models for their own data

Prerequisites

You should already be comfortable with:

  • Basic R syntax
  • Working with data frames
  • Basic data manipulation and plotting
  • Linear regression, including interactions

The course data is already cleaned and in long format. If you want to learn how to clean, merge and reshape panel data yourself, consider taking the course Master Longitudinal Data Wrangling using R.

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.

What you will be able to do after this course

After completing the course, you will be able to:

  • Decide whether fixed effects, random effects, a hybrid model or a multilevel model for change fits your research question
  • Estimate fixed and random effects models in R and choose between them
  • Separate the within and between effects of a time-varying predictor
  • Estimate and interpret multilevel models for change with random intercepts and slopes
  • Explain differences in starting points and rates of change with time-constant and time-varying predictors
  • Model non-linear change and choose a time variable that fits your question
  • Compare models and report the variance they explain

You will also have a clear order for building models of change, from the empty model to models with predictors, that you can follow with your own panel data.

 

Longitudinal Structural Equation Modelling

Model Stability, Causal Direction, and Individual Change in Panel Data Using R

Longitudinal data, or data collected multiple times from the same cases, let us see how people change over time and allow us to better understand causal relationships. Using this information requires flexible models that can account for its complexity. Structural equation modeling (SEM) is a framework that helps researchers understand change and multiple relationships at the same time. It estimates several regressions at once, can include latent variables and random effects, and makes it easy to test complex hypotheses.

This course will teach you how to use SEM to analyze longitudinal data in R using the lavaan package. We start with path analysis and simple models of stability. We then move on to cross-lagged panel models for questions about causal direction. Finally, we use latent growth models to estimate individual trajectories and explain how people change.

What You Will Learn

Path analysis, SEM and simple models of change

Structural equation models build on regression, so the course starts by estimating several regression equations at once.

In this section, you will learn:

  • How path analysis splits an effect into direct, indirect, and total effects
  • How SEM extends simple regression models through latent variables
  • How to compare competing models
  • How to model stability between two waves and interpret the stability coefficient

Cross-lagged panel models

Many research questions are about which variable comes first, for example, whether poor physical health lowers mental health or the other way around.

In this section, you will learn how to:

  • Specify a cross-lagged panel model with stability coefficients, cross-lagged effects and within-wave correlations
  • Test the causal direction by constraining the two cross-lagged effects
  • Test hypotheses about changes in stability, stationarity and equilibrium
  • Control for time-constant and time-varying confounders
  • Separate within-person and between-person variation with random intercepts (the random-intercept cross-lagged panel model)
  • Estimate longitudinal mediation models that control for earlier values of the mediator and the outcome

We also discuss the main limitations of the cross-lagged model, so you can interpret its results with care.

The latent growth model

Latent growth models describe how each individual changes in time and what explains differences in that change.

In this section, you will learn:

  • How the latent growth model relates to the multilevel model for change
  • How to estimate latent growth models in R with the growth() command
  • How to interpret the average trajectory, the differences between individuals, and the correlation between the intercept and the slope
  • How to explain the starting level and the rate of change with time-constant predictors
  • When to use the latent growth model and when to use the multilevel model for change

Flexible latent growth models

Change in time does not always follow a straight line, and many panel studies have missing or unequally spaced waves.

In this section, you will learn how to:

  • Include time-varying covariates in a latent growth model
  • Estimate parallel process models for two outcomes that change together
  • Deal with missing waves and unequal gaps between waves
  • Model non-linear change using quadratic growth and freely estimated loadings
  • Build a sequence of models, from a linear trajectory to other shapes of change and then predictors

Learn by doing with real longitudinal data

The course combines videos, written explanations, quizzes, and hands-on practical exercises so you learn by applying each model to data. Each lesson includes videos and accompanying slides. You will also gain access to a forum to ask questions related to the topics covered in the course.

There is a practical at the end of each module. The practicals use a synthetic version of Understanding Society (UKHLS), one of the largest longitudinal household surveys in the world. This provides an opportunity to implement what you have learned to help you transfer this to your own research.

Each practical includes:

  • Guided exercises
  • Full solution code and answers to all questions and tasks
  • Detailed walkthrough videos

You will finish the course with R code for each type of model that you can adapt to your own panel data.

Who this course is for

This course is designed for researchers who want to analyze longitudinal and panel data with structural equation models in R, including:

  • PhD students working with panel or cohort data
  • Social scientists who want to study causal direction and change with survey data
  • Health and behavioral researchers working with repeated measures
  • Researchers who know regression and want to move on to longitudinal SEM

Prerequisites

You should already be comfortable with:

  • Basic R syntax
  • Working with data frames and basic data manipulation
  • Linear regression

No previous knowledge of SEM or lavaan is needed, as the first module introduces both.

If you are new to R, consider taking the course Introduction to R for Social Researchers, which provides the foundations needed to follow the code taught here.

The practicals start from a cleaned dataset, so you do not need to prepare the data yourself. If you want to learn how to clean, merge, and reshape panel data before modeling it, the course Master Longitudinal Data Wrangling using R covers these steps.

For a longer introduction to path analysis and mediation with lavaan, see the course Mediation and Moderation Analysis using R. Taking it first is optional.

What you will be able to do after this course

After completing the course, you will be able to:

  • Specify and estimate path models, autoregressive models, and cross-lagged panel models in lavaan
  • Test hypotheses about stability and causal direction by comparing nested models
  • Use random intercepts to estimate within-person effects in a cross-lagged model
  • Estimate longitudinal mediation models
  • Fit and interpret latent growth models with time-constant and time-varying predictors
  • Model parallel processes, non-linear change, and missing waves

Most importantly, you will be able to choose the longitudinal SEM that fits your research question and explain what its coefficients say about stability and change.

 

 

Survival Analysis

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

Many questions we ask with longitudinal data concern 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.

Analyzing 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 before we can observe 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 analyze 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, life tables, and 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 model types. In the process, you will learn to use the survival package, glm(), and the tidyverse to estimate, visualize, and interpret these models.

What You Will Learn

Key concepts of event occurrence

Before modeling events, we need to define exactly what we are studying and how to handle 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

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 using logistic regressions
  • 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

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

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 modeling 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 with answers to all questions
  • Detailed walkthrough videos

By the end of the course, you will have a complete workflow for analyzing 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 in R
  • 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. 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

 

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