Two popular questions arising from longitudinal electronic health records (EHR) data are on understanding the dynamic relationships between variables measured longitudinally and characterizing the population trajectory. The dynamic relationship between clinical variables in EHR studies the patterns and associations over repeated measurements to capture the evolving nature of health conditions. One popular method of investigation is the state space model (SSM), which integrates both observed data and unobserved latent variables to estimate and predict the hidden states. By doing so, it enables the examination of how these unobservable factors influence the observed longitudinal data. We propose to select the variables in the transition matrix by using the L1 penalty to induce sparsity. We design an EM-based algorithm and apply it to a large dialysis patient EHR dataset to reveal dynamic associations between key factors for anemia. The second project explores the modeling of the population trajectory when only aggregate-level data are available, a common challenge in practice when accessing EHR data. We compare the performance of the estimate using aggregated data to those based on nonparametric mixed-effects models using individual-level data. Through comprehensive simulations, we find that simple nonparametric regression on aggregated data performs equally well in capturing population trajectories compared to individual-level data modeled with mixed-effect nonparametric regression. The use of aggregated data leads to a significant reduction in computational cost and protects patient data privacy. Analysis of real-world data from COVID-19 detection in dialysis patients examines the performance of the two options and reports similar effectiveness in modeling the trajectory before and after COVID-19 confirmation.