Longitudinal Data Analysis
Using R and LLMs
Stephen Vaisey, Ph.D.
Upcoming Seminar:
August 11-14, 2026, Livestream Seminar
*Please note, these materials are from a previous version of the course and don't include the LLM content.
Outline
1. Opportunities and challenges of panel data
2. Linear models
3. Logistic regression models
4. Count data models
5. Linear structural equation models
Section 1
OPPORTUNITIES AND CHALLENGES
2
Panel data
Data in which variables are measured at multiple points in time
for the same individuals.
Response variable yit with t = 1, 2,…, T
Vector of predictor variables xit .
Some of these may vary with time, others may not.
Assume, for now, that time points are the same for everyone in
the sample. (For some methods that assumption is not
essential).
5
Why are panel data desirable?
In Econometric Analysis of Panel Data (2008), Baltagi lists six potential
benefits of panel data:
1. Ability to control for individual heterogeneity
2. More informative data
3. Better ability to study the dynamics of adjustment
4. Ability to identify and measure effects not detectable in cross-sections
5. Ability to test more complicated behavioral models
6. Avoidance of aggregation bias
3
My list
1. Ability to control for unobservables
2. Ability to investigate causal ordering (sometimes!)
3. Ability to study the effect of a treatment on the trajectory of
an outcome
Problems with panel data
• Attrition and missing data
• Dependence among multiple observations from same
individual
– observations are correlated; individuals have tendencies
– conventional methods assume independent observations
– for this reason, estimated SEs tend to be too low
4
Estimating mixed models in R
• For mixed models that assume exchangeability, lme4::lmer
is probably best and easiest to use (and has nice plotting tools)
• For models that relax the exchangeability assumption,
nlme::lme is required
• Both can do random slopes/intercepts
• Both can do maximum likelihood
• Can get robust SEs much more easily after nlme::lme
49
49
Example lme4 syntax
specify the random intercept as
a term in the formula, grouped
this means use ML instead of “restricted by the cluster variable
maximum likelihood.” REML can be better
in small samples, but doesn’t allow model
comparisons based on different
specifications of the predictors
50
50
25
ICC = 1.2827/(.9929 + 1.2827) = .564
the same as the value of ρ in the
GLS-ML with exchangeable residuals
51
51
The two models are the same!
(other than a few tiny rounding
differences in the reported SEs)
52
52
26
Software note
If you are looking for a robust approach to estimating these models
using lme4, check out:
Koller, M. (2016). robustlmm: An R Package for Robust Estimation of Linear
Mixed-Effects Models. Journal of Statistical Software, 75(6).
[Link]
Wang, T., & Merkle, E. C. (2016). Derivative Computations and Robust
Standard Errors for Linear Mixed Effects Models in lme4. ArXiv:1612.04911
[Stat]. Retrieved from [Link]
You can use clubSandwich after nlme::lme.
53
53
Random Coefficients
We can allow for random coefficients for the time-varying
predictors by writing
yit t i xit zi i it
where the β coefficients now have a subscript i. In practice, we
might only do this for a subset (or just one of) the variables. We
assume that this β is a normally distributed random variable
(with a mean and a variance that we’ll estimate) that is
uncorrelated with everything else.
54
54
27
Recap: GEE vs. RE-ML for logistic models
Why prefer GEE?
• GEE is much faster
• GEE can allow for departures from exchangeability
Why prefer RE-ML?
• Subject-specific coefficients
• Weaker assumptions for missing data (MAR vs. MCAR)
• Random coefficients
• More than two levels
For both methods, robust SEs are preferable, but this is hard in lme4.
117
117
Solution 4: Fixed effects models
The fixed effects model has the same general form as the
random effects model
p
log it t xit zi i
1 pit
However, i is now regarded as a set of fixed constants rather
than as random variables. This allows for any correlation
between i, zi and xit.
118
118
59
For the linear FE model, we could handle i by including dummy
variables for each person. For logistic models, that produces
estimates biased away from 0, sometimes severely (unless T is large).
The preferred method is conditional likelihood, which conditions
on the number of 1’s and 0’s for each person. In effect, we are asking:
“Given that a girl is in poverty for two out of five years, why did
poverty occur in years 2 and 4, rather than in years 1, 3, or 5?”
Clearly, if a girl is in (or out of) poverty all five years, there is
nothing to explain. So girls with these response patterns are
effectively eliminated from the analysis.
119
119
Conditional logit with survival::clogit
I omitted time-constant variables before specifying the model
strata() is where you specify the index for individuals
120
120
60
121
121
No results are reported for AGE and BLACK.
These time-invariant predictors cannot explain
why poverty occurred in some years but not in
others.
Compared with RE estimates, the coefficients
for MOTHER, SPOUSE and HOURS are much
smaller in magnitude, with higher standard
errors. The INSCHOOL coefficient has actually
changed sign and is now statistically significant.
It’s common for FE results to be substantially
different from RE results, because FE controls
for all stable characteristics of the individuals.
122
122
61
Adding TV × TC interactions
The poverty risk of motherhood declines with age
123
123
Considerations for conditional logit
Like the RE model, the FE model assumes exchangeability:
correlations among the response variables at different times
don’t depend on how close or far apart in time they are. Because
that assumption is not realistic, it would be preferable to use
robust standard errors, which don’t assume exchangeability.
Unfortunately, survival::clogit doesn’t offer that option.
124
124
62