UPDATE-----------------
I have data where one ID can serve as a comparator several times.
This is done by creating a variable to group the treated in variable GROUP
. Assume variable GROUP
, as an example, containing the number of visits to a physician until a therapy is initiated. Thus, ID
can serve as a comparator several times4 initiated the therapy after 3 visits.
This A possible comparator is done taking into accountselected if it also has 3 physician visits but does not initiate the therapy. Therefore, both belong to GROUP == 3
. Note that this could happen on different points in time specified in variable (VariableTIME_POINT
. As ID
1 never starts therapy it contributes also a possible comparator observation for someone who initiated therapy after 5 physician visits in GROUP == 5
.
Next, within every value of variable GROUP
classifies comparable observations based on, the variablecovariates are collected in a time window before the TIME_POINT
) on which an ID
belongs to a GROUP
.
In this minimal example, assume covariates to be balanced using a weight specified in variable WEIGHT
.
Kept short:
ID 1
1 is a comparator as it is untreated (TREAT == 0
) and has one event in total.
ID 2, 3, 4, 5 and 6
2, 3, 4, 5 and 6 are different from each other and were treated (TREAT == 1
).
ID 1 therefore has different points in time to serve as a comparator for ID 2, 3, 4, 5 and 6
.
ID 1 has an event. Accordingly, the duration of the follow-up time (Variable FOLLOW_UP_TIME
) naturally shortens as time progresses, but of course this ID
only experiences one1 therefore contributes to different EVENTGROUP
in totals as a comparator for ID
2, 3, 4, 5 and 6.
Data:
The covariates of the listed ID
s were previously balanced and therefore no longer needed to be included 1 has one event in the cox modeltotal.
Therefore every ID has a weight appliedAccordingly, the duration of the follow-up time (Variable FOLLOW_UP_TIME
) naturally shortens as value of GROUP
and the associated variable TIME_POINT
increases.
Data:
data <- data.frame(
ID = c(1, 1, 1, 1, 1, 2, 3, 4, 5, 6),
TREAT = c(0, 0, 0, 0, 0, 1, 1, 1, 1, 1),
GROUP = c(51, 42, 3, 24, 15, 1, 2, 3, 4, 5),
TIME_POINT = as.Date(c("2021"2019-0105-01"13", "2021"2019-0206-01", "2021"2020-03-01", "2021-0402-01", "2021"2022-0509-01", "2021-0502-01", "2021-04-01", "2021-0307-01", "2021-0201-01"13", "2021-01-01")),
EVENT_DATE = as.Date(c("2022-0110-01", "2022-0110-01", "2022-0110-01", "2022-0110-01", "2022-0110-01", "2022-01-03", "2021-10-03", "0"NA, "0"NA, "0"NA)),
END_DATE = as.Date(c("2022-0112-01"15", "2022-0112-01"15", "2022-0112-01"15", "2022-0112-01"15", "2022-0112-01"15", "2022-01-03", "2021-12-03", "2021-10-04", "2021-09-03", "2021-11-02")),
EVENT = c(1, 1, 1, 1, 1, 1, 1, 0, 0, 0),
FOLLOW_UP_TIME = c(3651237, 3341218, 306944, 275607, 24530, 247336, 246185, 21695, 214233, 305),
WEIGHT = c(0.323834, 0.743543, 0.234, 0.543743, 0.834323, 1, 1, 1, 1, 1)
)
ID TREAT GROUP TIME_POINT EVENT_DATE END_DATE EVENT FOLLOW_UP_TIME WEIGHT
1 0 51 20212019-0105-0113 2022-0110-01 2022-0112-0115 1 3651237 0.323834
1 0 42 20212019-0206-01 2022-0110-01 2022-0112-0115 1 3341218 0.743543
1 0 3 20212020-03-01 2022-0110-01 2022-0112-0115 1 306944 0.234
1 0 24 2021-0402-01 2022-0110-01 2022-0112-0115 1 275607 0.543743
1 0 15 20212022-0509-01 2022-0110-01 2022-0112-0115 1 24530 0.834323
2 1 1 2021-0502-01 2022-01-03 2022-01-03 1 247336 1
3 1 2 2021-04-01 2021-10-03 2021-12-03 1 246185 1
4 1 3 2021-0307-01 <NA> 2021-10-04 0 21695 1
5 1 4 2021-02-01-13 <NA> 2021-09-03 0 214233 1
6 1 5 2021-01-01 <NA> 2021-11-02 0 305 1
ID TREAT GROUP TIME_POINT EVENT_DATE END_DATE EVENT FOLLOW_UP_TIME WEIGHT
1 0 5 20212022-0109-01 2022-0110-01 2022-0112-0115 1 30 365 0.323
6 1 5 2021-01-01 <NA> 2021-11-02 0 305 1
ph <- coxph(Surv(FOLLOW_UP_TIME, EVENT) ~ TREAT, data = data, robust = TRUE, weights = WEIGHT, cluster = GROUP)
> ph
Call:
coxph(formula = Surv(FOLLOW_UP_TIME, EVENT) ~ TREAT, data = data,
weights = WEIGHT, robust = TRUE, cluster = GROUP)
coef exp(coef) se(coef) robust se z p
TREAT 02.2833053 1 7.3275792 1.1245950 0 1.6617396 01.42847 0.669141
Likelihood ratio test=0test=1.0658 on 1 df, p=0.79972088
n= 10, number of events= 7