Conditional Average Treatment Effects Suppose I am interested in understanding the effectiveness of two type of medicine, A and B.
In the usual setting, each patient is considered an experiment unit, and is assigned one of the medicine. Through analyzing condition treatment effect, we can answer the question of the type "what kind of patients are most benefited from medicine A?" I believe this is the framework of potential outcome.
Now, consider the situation where a patient need to take the medicine every 2 hours. Each time a medicine is needed, we randomly select between A and B. After say 1 hour, we can measure it's effectiveness (e.g. decrease in blood pressure for hypertension), and the effect will be gone by the end of the 2hour period (that's why we need med every 2 hours!). By the end the experiment, we can calculate the avg/total effectiveness for both A and B.
In this case, we are able to observe the outcomes from both medicines (control and treatment). My question is, can we still apply the same "potential outcome" framework, expressing each data point as (Y, W, X), where Y is the realized outcome, W is indicator on control vs treatment, and X is the feature vector. Again we are trying to answer the question of the form "what kind of patients are most benefited from medicine A?". What is a suitable framework for such scenario?
Thank you!

Firstly, we can express this via potential outcomes. Building off the provided notation, we can index the variables by time. So $X_0$ and $W_0$ would be treatment and the covariates measured as baseline, respectively. Then the potential outcomes would be defined something like: $Y_t(\textbf{x})$, where $\textbf{x}=(x_0, x_1, ..., x_{t-1})$. As can be seen from this form of the potential outcomes, you can think about your data as longitudinal.
Now that we have the potential outcomes written, we can talk about 'effectiveness'. Effectiveness can be defined in a number of ways, as suggested by the potential outcomes. To start, we need to be very specific about what $\textbf{x}$ is. For example, the always-treated would be: $\textbf{x}=(x_0=1, x_1=1, ..., x_{t-1}=1)$. Next, we can consider an estimand like the conditional average treatment effect (CATE) instead of the average treatment effect. However, we can also consider something that builds on top of the CATE: the optimal decision rule. The optimal decision rule takes the CATE and then further determines the 'optimal' decision rule of when to give A or B, where optimal is defined as maximizing (or minimizing) the mean of the event in the population.
The question: "what kind of patients are most benefited from medicine A vs medicine B?" I would interpret as most closely align with the CATE. But it is important to distinguish between the options above.
CATE
For estimation of CATE, there are a few different options. This paper provides a good overview of some of the methods you could consider in applying. Depending on what $W$ is, you could simply stratify by $W$ and apply estimators to each strata. If $W$ is high-dimensional, some model will become necessary to estimate the CATE. This paper details a general approach extending AIPW / TMLE to CATE. While the focus is on estimating CATE with machine learning, the same process can also be done with parametric models. However, the time-varying component makes the CATE at little bit more complicated. Namely, there could be different CATE for each time point.
Another option is G-Estimation, which was developed for effect measure modification in longitudinal data. A good discussion and application on g-estimation can be found in this paper, but it does use survival analysis instead.
Optimal Decisions
Since CATE can vary over time, some researchers have focused on the optimal rules instead. This avoids having to summarize all the time-varying CATEs by instead developing the optimal decision rules. This paper provides motivation for the problem and discusses how to apply it in longitudinal data. G-Estimation can also be applied for optimal decisions, but the linked paper is a beast. There are a lot of different approaches in this area. Mine have been from a biostatistics / epidemiology background, but there is also work on this problem in computer science.
