Causal inference for longitudinal data