Skip to contents

Generates a two-interval counting-process data set for a hypothetical trial of a heart-rate (HR)-lowering agent started after severe myocardial infarction.

Usage

simulate_hr_agent(
  n = 9000,
  h0 = 0.035,
  b_drug = log(0.62),
  b_hr = 0.6,
  b_hr0 = 0.4,
  g0 = -0.2,
  g_hr = 0.9,
  g_drug1 = 0.6,
  g_hr0 = 0.4,
  rho_hr = 0.5,
  delta_drug1 = 1.2,
  hr_mean = 80,
  hr_sd = 12,
  rand = FALSE,
  seed = 1
)

Arguments

n

Number of patients.

h0

Baseline per-interval hazard.

b_drug

Log hazard ratio for being on the drug – the causal effect (log(0.62) by default, a genuinely protective drug).

b_hr, b_hr0

Log-hazard effects of the follow-up and baseline heart rate.

g0, g_hr, g_drug1, g_hr0

Treatment-assignment (confounding) coefficients on the logit scale.

rho_hr

Carry-over of baseline heart rate into follow-up heart rate.

delta_drug1

Amount by which the baseline dose lowers follow-up heart rate (the mediator path drug1 -> HR1).

hr_mean, hr_sd

Mean and standard deviation of the observed heart rate (bpm) scale.

rand

If TRUE, randomise treatment (no confounding). Used for the oracle.

seed

Random seed, for reproducibility.

Value

A long counting-process tibble – one row per patient per interval at risk – with columns:

id

patient identifier

t_start, t_stop

the (t_start, t_stop] interval

on_drug

on the drug during this interval (the exposure the weight model predicts)

drug1, drug2

the baseline and follow-up doses (drug2 = 0 until the follow-up visit); used by the treatment-history models

prev_drug

the dose in the previous interval; a predictor in the weight model

heart_rate

heart rate during this interval (baseline in interval 1, HR1 in interval 2)

baseline_hr, HR1

baseline and follow-up heart rate (bpm)

death

died during this interval

Details

The data-generating process is deliberately a time-varying-confounding trap. Resting heart rate is both

  • a confounder – a faster heart rate makes a patient more likely to be treated and more likely to die, and

  • a mediator – the drug lowers the heart rate at follow-up (drug1 -> HR1), and a lower heart rate means fewer deaths.

So conditioning on follow-up heart rate blocks part of the drug's benefit (biasing the baseline dose), while not conditioning leaves the follow-up dose confounded. Only reweighting (IPTW), not conditioning, escapes the trap.

Confounding is generated forward: treatment is made more likely for higher-heart-rate (frailer) patients through plogis(g0 + g_hr * z). Setting rand = TRUE randomises treatment instead (a coin flip), which removes confounding and gives the randomised "oracle" used by oracle_hr().

Internally the risk score z is standardised (mean 0, sd 1); the observed heart rate is a linear rescaling hr_mean + hr_sd * z, so the bpm scale changes readability only, never any result.

Examples

dat <- simulate_hr_agent(n = 2000)
head(dat)
#> # A tibble: 6 × 11
#>      id t_start t_stop on_drug drug1 drug2 prev_drug heart_rate baseline_hr
#>   <int>   <dbl>  <dbl>   <int> <int> <int>     <int>      <dbl>       <dbl>
#> 1     1       0      1       0     0     0         0       72.5        72.5
#> 2     1       1      2       0     0     0         0       62.6        72.5
#> 3     2       0      1       0     0     0         0       82.2        82.2
#> 4     2       1      2       1     0     1         0       90.3        82.2
#> 5     3       0      1       0     0     0         0       70.0        70.0
#> 6     3       1      2       0     0     0         0       81.8        70.0
#> # ℹ 2 more variables: HR1 <dbl>, death <int>