Skip to main content
Notice removed Authoritative reference needed by CommunityBot
Bounty Ended with EdM's answer chosen by CommunityBot
edited body
Source Link
geek45
  • 171
  • 7
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(1, 2, 3, 4, 5, 1, 2, 3, 4, 5),
  TIME_POINT = as.Date(c("2019-05-13", "2019-06-01", "2020-03-01", "2021-02-01", "2022-09-01", "2021-02-01", "2021-04-01", "2021-07-01", "2021-01-13", "2021-01-01")),
  EVENT_DATE = as.Date(c("2022-10-01", "2022-10-01", "2022-10-01", "2022-10-01", "2022-10-01", "2022-01-03", "2021-10-03", NA, NA, NA)),
  END_DATE = as.Date(c("2022-1210-15"01", "2022-1210-15"01", "2022-1210-15"01", "2022-1210-15"01", "2022-1210-15"01", "2022-01-03", "2021"2022-1210-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(1237, 1218, 944, 607, 30, 336, 185, 95, 233, 305),
  WEIGHT = c(0.834, 0.543, 0.234, 0.743, 0.323, 1, 1, 1, 1, 1)
)
ID TREAT GROUP TIME_POINT EVENT_DATE   END_DATE EVENT FOLLOW_UP_TIME WEIGHT

1     0     1 2019-05-13 2022-10-01 2022-1210-1501  1     1237            0.834
1     0     2 2019-06-01 2022-10-01 2022-1210-1501  1     1218            0.543
1     0     3 2020-03-01 2022-10-01 2022-1210-1501  1     944             0.234
1     0     4 2021-02-01 2022-10-01 2022-1210-1501  1     607             0.743
1     0     5 2022-09-01 2022-10-01 2022-1210-1501  1     30              0.323


2     1     1 2021-02-01 2022-01-03 2022-01-03  1     336                1
3     1     2 2021-04-01 2021-10-03 2021-1210-03  1     185                1
4     1     3 2021-07-01       <NA> 2021-10-04  0     95                 1
5     1     4 2021-01-13       <NA> 2021-09-03  0     233                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 2022-09-01 2022-10-01 2022-1210-1501  1     30              0.323
6     1     5 2021-01-01       <NA> 2021-11-02  0     305                1
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(1, 2, 3, 4, 5, 1, 2, 3, 4, 5),
  TIME_POINT = as.Date(c("2019-05-13", "2019-06-01", "2020-03-01", "2021-02-01", "2022-09-01", "2021-02-01", "2021-04-01", "2021-07-01", "2021-01-13", "2021-01-01")),
  EVENT_DATE = as.Date(c("2022-10-01", "2022-10-01", "2022-10-01", "2022-10-01", "2022-10-01", "2022-01-03", "2021-10-03", NA, NA, NA)),
  END_DATE = as.Date(c("2022-12-15", "2022-12-15", "2022-12-15", "2022-12-15", "2022-12-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(1237, 1218, 944, 607, 30, 336, 185, 95, 233, 305),
  WEIGHT = c(0.834, 0.543, 0.234, 0.743, 0.323, 1, 1, 1, 1, 1)
)
ID TREAT GROUP TIME_POINT EVENT_DATE   END_DATE EVENT FOLLOW_UP_TIME WEIGHT

1     0     1 2019-05-13 2022-10-01 2022-12-15  1     1237            0.834
1     0     2 2019-06-01 2022-10-01 2022-12-15  1     1218            0.543
1     0     3 2020-03-01 2022-10-01 2022-12-15  1     944             0.234
1     0     4 2021-02-01 2022-10-01 2022-12-15  1     607             0.743
1     0     5 2022-09-01 2022-10-01 2022-12-15  1     30              0.323


2     1     1 2021-02-01 2022-01-03 2022-01-03  1     336                1
3     1     2 2021-04-01 2021-10-03 2021-12-03  1     185                1
4     1     3 2021-07-01       <NA> 2021-10-04  0     95                 1
5     1     4 2021-01-13       <NA> 2021-09-03  0     233                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 2022-09-01 2022-10-01 2022-12-15  1     30              0.323
6     1     5 2021-01-01       <NA> 2021-11-02  0     305                1
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(1, 2, 3, 4, 5, 1, 2, 3, 4, 5),
  TIME_POINT = as.Date(c("2019-05-13", "2019-06-01", "2020-03-01", "2021-02-01", "2022-09-01", "2021-02-01", "2021-04-01", "2021-07-01", "2021-01-13", "2021-01-01")),
  EVENT_DATE = as.Date(c("2022-10-01", "2022-10-01", "2022-10-01", "2022-10-01", "2022-10-01", "2022-01-03", "2021-10-03", NA, NA, NA)),
  END_DATE = as.Date(c("2022-10-01", "2022-10-01", "2022-10-01", "2022-10-01", "2022-10-01", "2022-01-03", "2022-10-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(1237, 1218, 944, 607, 30, 336, 185, 95, 233, 305),
  WEIGHT = c(0.834, 0.543, 0.234, 0.743, 0.323, 1, 1, 1, 1, 1)
)
ID TREAT GROUP TIME_POINT EVENT_DATE   END_DATE EVENT FOLLOW_UP_TIME WEIGHT

1     0     1 2019-05-13 2022-10-01 2022-10-01  1     1237            0.834
1     0     2 2019-06-01 2022-10-01 2022-10-01  1     1218            0.543
1     0     3 2020-03-01 2022-10-01 2022-10-01  1     944             0.234
1     0     4 2021-02-01 2022-10-01 2022-10-01  1     607             0.743
1     0     5 2022-09-01 2022-10-01 2022-10-01  1     30              0.323


2     1     1 2021-02-01 2022-01-03 2022-01-03  1     336                1
3     1     2 2021-04-01 2021-10-03 2021-10-03  1     185                1
4     1     3 2021-07-01       <NA> 2021-10-04  0     95                 1
5     1     4 2021-01-13       <NA> 2021-09-03  0     233                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 2022-09-01 2022-10-01 2022-10-01  1     30              0.323
6     1     5 2021-01-01       <NA> 2021-11-02  0     305                1
added 87 characters in body
Source Link
geek45
  • 171
  • 7

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 4 initiated the therapy after 3 visits. A possible comparator is selected 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 TIME_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, the covariates are collected in a time window before the TIME_POINT on which an ID belongs to a GROUP.
In this minimalminimum example, assume covariates to be balanced using a weight specified in variable WEIGHT. In addition, the data outside the minimum example has several comparators per GROUP.

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 4 initiated the therapy after 3 visits. A possible comparator is selected 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 TIME_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, the covariates 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.

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 4 initiated the therapy after 3 visits. A possible comparator is selected 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 TIME_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, the covariates are collected in a time window before the TIME_POINT on which an ID belongs to a GROUP.
In this minimum example, assume covariates to be balanced using a weight specified in variable WEIGHT. In addition, the data outside the minimum example has several comparators per GROUP.

added 660 characters in body
Source Link
geek45
  • 171
  • 7

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 IDs 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 

I have data where one ID can serve as a comparator several times.
This is done taking into account different points in time (Variable GROUP classifies comparable observations based on the variable TIME_POINT).
ID 1 is a comparator as it is untreated (TREAT == 0).
ID 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 one EVENT in total.

Data: The covariates of the listed IDs were previously balanced and therefore no longer needed to be included in the cox model.
Therefore every ID has a weight applied.

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(5, 4, 3, 2, 1, 1, 2, 3, 4, 5),
  TIME_POINT = as.Date(c("2021-01-01", "2021-02-01", "2021-03-01", "2021-04-01", "2021-05-01", "2021-05-01", "2021-04-01", "2021-03-01", "2021-02-01", "2021-01-01")),
  EVENT_DATE = as.Date(c("2022-01-01", "2022-01-01", "2022-01-01", "2022-01-01", "2022-01-01", "2022-01-03", "2021-10-03", "0", "0", "0")),
  END_DATE = as.Date(c("2022-01-01", "2022-01-01", "2022-01-01", "2022-01-01", "2022-01-01", "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(365, 334, 306, 275, 245, 247, 246, 216, 214, 305),
  WEIGHT = c(0.323, 0.743, 0.234, 0.543, 0.834, 1, 1, 1, 1, 1)
)
ID TREAT GROUP TIME_POINT EVENT_DATE   END_DATE EVENT FOLLOW_UP_TIME WEIGHT
1     0     5 2021-01-01 2022-01-01 2022-01-01  1     365            0.323
1     0     4 2021-02-01 2022-01-01 2022-01-01  1     334            0.743
1     0     3 2021-03-01 2022-01-01 2022-01-01  1     306            0.234
1     0     2 2021-04-01 2022-01-01 2022-01-01  1     275            0.543
1     0     1 2021-05-01 2022-01-01 2022-01-01  1     245            0.834
2     1     1 2021-05-01 2022-01-03 2022-01-03  1     247                1
3     1     2 2021-04-01 2021-10-03 2021-12-03  1     246                1
4     1     3 2021-03-01       <NA> 2021-10-04  0     216                1
5     1     4 2021-02-01       <NA> 2021-09-03  0     214                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 2021-01-01  2022-01-01   2022-01-01  1      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)

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 0.2833    1.3275   1.1245    0.6617 0.428 0.669

Likelihood ratio test=0.06  on 1 df, p=0.7997
n= 10, number of events= 7 

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 4 initiated the therapy after 3 visits. A possible comparator is selected 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 TIME_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, the covariates 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 is a comparator as it is untreated (TREAT == 0) and has one event in total.
ID 2, 3, 4, 5 and 6 are different from each other and were treated (TREAT == 1).

ID 1 therefore contributes to different GROUPs as a comparator for ID 2, 3, 4, 5 and 6.

ID 1 has one event in total.
Accordingly, 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(1, 2, 3, 4, 5, 1, 2, 3, 4, 5),
  TIME_POINT = as.Date(c("2019-05-13", "2019-06-01", "2020-03-01", "2021-02-01", "2022-09-01", "2021-02-01", "2021-04-01", "2021-07-01", "2021-01-13", "2021-01-01")),
  EVENT_DATE = as.Date(c("2022-10-01", "2022-10-01", "2022-10-01", "2022-10-01", "2022-10-01", "2022-01-03", "2021-10-03", NA, NA, NA)),
  END_DATE = as.Date(c("2022-12-15", "2022-12-15", "2022-12-15", "2022-12-15", "2022-12-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(1237, 1218, 944, 607, 30, 336, 185, 95, 233, 305),
  WEIGHT = c(0.834, 0.543, 0.234, 0.743, 0.323, 1, 1, 1, 1, 1)
)
ID TREAT GROUP TIME_POINT EVENT_DATE   END_DATE EVENT FOLLOW_UP_TIME WEIGHT 

1     0     1 2019-05-13 2022-10-01 2022-12-15  1     1237            0.834
1     0     2 2019-06-01 2022-10-01 2022-12-15  1     1218            0.543
1     0     3 2020-03-01 2022-10-01 2022-12-15  1     944             0.234
1     0     4 2021-02-01 2022-10-01 2022-12-15  1     607             0.743
1     0     5 2022-09-01 2022-10-01 2022-12-15  1     30              0.323


2     1     1 2021-02-01 2022-01-03 2022-01-03  1     336                1
3     1     2 2021-04-01 2021-10-03 2021-12-03  1     185                1
4     1     3 2021-07-01       <NA> 2021-10-04  0     95                 1
5     1     4 2021-01-13       <NA> 2021-09-03  0     233                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 2022-09-01 2022-10-01 2022-12-15  1     30              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 2.053     7.792    1.950     1.396 1.47 0.141

Likelihood ratio test=1.58  on 1 df, p=0.2088
n= 10, number of events= 7 
added 18 characters in body
Source Link
geek45
  • 171
  • 7
Loading
added 44 characters in body
Source Link
geek45
  • 171
  • 7
Loading
added 44 characters in body
Source Link
geek45
  • 171
  • 7
Loading
Notice added Authoritative reference needed by geek45
Bounty Started worth 50 reputation by geek45
added 33 characters in body
Source Link
geek45
  • 171
  • 7
Loading
added 116 characters in body
Source Link
geek45
  • 171
  • 7
Loading
Source Link
geek45
  • 171
  • 7
Loading