I asked a colleague this question and this is her answer...
This isn't really a competing hazards problem in the way it's currently framed. A competing hazards approach is more useful when there are two co-varying processes (such as conditions X and Y), but once either one of them happens, you stop observation and call it a day. Here, you don't want to stop when Condition Y happens, only when X happens.
While you do have some complexities to your data (Y can happen multiple times, etc), I think a simple solution would be to do a standard survival analysis regression model using time to X as your outcome (not sure what your approach generally is - Cox PH? Something parametric?) but include the number of times the individual had condition Y as another covariate (along with age, gender, etc). I wouldn't start the counter on "time to X" until the last instance of condition Y has been resolved. So, for example, if you had these people in your data set:
- Y never happens, X happens at t = 5.
- Y resolved at t = 1, 3, 6, X happens at t = 8.
- Y resolved at t = 2, X happens at t = 9.
- Y never happens, X never happens, and you stop observing at t = 10.
Then, your covariates/response times would be:
- numTimesY = 0, timeToX = 5
- numTimesY = 3, timeToX = 2
- numTimesY = 1, timeToX = 7
- numTimesY = 0, time to X = 10,
Another similar option would be to include the length of time the person spent in condition Y as a predictor, perhaps interacted with the number of times Y happened, or the average time the person spent in condition Y. If you have this information, then you could include time to X from the first observation point and not from when Y last happened.
This solution isn't perfect, because if you never see X (i.e., the person is right-censored), then technically you're not sure if Y could have happened before X. If you want to fully address that detail you would need to get into some kind of missing data scheme (EM algorithm, imputation, etc). I'm not sure if you want to go down that route or not, but it's an option if you want to be thorough about how previous instances of Y are included in the model.
However you frame the covariates/time to X, you should exclude anyone who has condition Y the entire time you're observing them. Those people are not at risk (yet) so can't be included in the analysis.
Another thing to think about is whether you have left-censoring on your data. How important is it to you that Y may have happened before you started observing the person? This may also depend on how much of an impact Y has on the time to X.