Propensity scores in panel datasets I am working on longitudinal data and currently trying to test the potential preventive effect of a class of antihypertensive drugs on the evolution of cognitive performances over time. 
Patients were followed 7 times (each 6 months) and data regarding drug consumption, many covariates as well as cognition were collected at each time (visit). 
I initially thought to do a linear mixed model with the antihypertensive class at baseline and the cognition at each time as the outcome and adjust on different confounders. 
However, many patients changed of antihypertensive drugs over time. Thus, only considering the class at baseline is not a very good method. 


*

*Is considering the treatment as a time varying covariate a good idea? 

*Would you recommend other kind of models ? I heard about functional data analysis but I do not know at all how to implement it? 

*I would like to use propensity score matching but the fact that I am working on panel data strongly complicates the situation and I do not know how to use propensity scores in panel data? 


I am using STATA 15. 
Someome to help me a little bit to start with this problem? 
 A: The method you want to use is inverse probability weighting for marginal structural models. See Robins, Hernan, & Brumback (2000) or Thoemmes & Ong (2016) for an introduction to the method. 
Basically, at each time point, units can be assigned to any treatment. You want to mimic a sequentially randomized control trial (i.e., where each unit is randomly assigned to a treatment at each time point). With a sequential RCT, you could find the causal effect of any one pattern of drug assignments (e.g., drug at all time points vs. no drug at all time points, or drug at all time points. vs. drug at only the first few time points, etc.). You can also build a model that relates outcome to the number of time points where the drug was received, or the to the timing of drug receipt, etc.
In your situation, you might not have a sequential RCT; there may be confounding at each time point. What you need to do is generate weights that, when applied to your sample, mimic a sequential RCT, in that sense that at each time point, all covariates are balanced between the control and treated units. Then, a weighted analysis will give you an unbiased estimate of the causal effect of interest (assuming you have balanced all the relevant covariates at each time point).
The way you estimate the weights is the following: at each time point, model the probability of being treated based on all previously measured covariates (including previous treatments and intermediate outcomes). At time 1, your model will include baseline covariates. At time 2, your model will include baseline covariates, the time 1 treatment status, and any variables that may have changed values between time 1 and time 2 (e.g., a measure of disease progression after time 1 but before the next treatment was given). From each of these models, generate predicted probabilities (i.e., propensity scores), and the generate IPW weights by taking the inverse of the propensity score for treated units and the inverse of 1 minus the propensity score for control units. Then, for each unit, multiply all of their weights together. These are the weights that will balance your groups at each time point. Finally, do a weighted regression of your outcome on all the treatment variables using these weights. You can then interpret the parameters of the regression as causal (assuming assumptions are met).
