Longitudinal patient stratification of electronic health records with flexible adjustment for clinical outcomes