Guidelines for comparing treatments when some patients receive different number of treatments I am working with a retrospective database where the researchers want to compare the effects between 2 treatments. Things get complicated with the fact that some of the patients receive treatments more than once and some even have treatment switching regimes at certain time points. What are some guidelines that people have for designing the set up of these models?  
 A: You should look into using the g-methods for sequential treatments. These are described in part 3 of the (currently free) book What If by Hernán and Robins. The most common of these methods is inverse probability weighting (IPW) for the estimation of marginal structural models (MSMs). This is an extension of IPW (i.e., propensity score weighting) for single time point observational studies. 
IPW is described in Robins, Hernán, and Brumback (2000), and there are other tutorials out there. Essentially, you imagine the analysis you would perform if you could randomly assign each participant to each treatment at each time point. This is likely a regression of the outcome on treatment at each time point and perhaps their interaction (if specific sequences of treatments are relevant beyond the effects of treatment at each time point). What distinguishes your study from this ideal study is that participants non-randomly enter treatments at each time point, so it's unclear if differences in outcomes are due to the treatment or to differences among the participants in each treatment. This is confounding. IPW is a method to adjust for confounding. It does so by creating a weighted sample in which confounding is eliminated. See the references I mentioned for instructions on how to perform IPW. In R, you can use the WeightIt package to estimate the weights.
