Skip to contents

Fits the seven models the workshop compares and returns them in a named list. The sequence walks from the most naive analysis to the marginal structural model, so a learner can see where each one lands relative to the truth:

Usage

fit_all_models(data)

Arguments

data

A long cohort from simulate_hr_agent().

Value

A named list of coxph fits m1–m7, plus ps_fit (the propensity model) and data_w (the cohort with ps and weight columns).

Details

m1

Crude Cox on the baseline dose only (~ drug1).

m2

Crude Cox on the follow-up dose only (~ drug2).

m3

Baseline dose adjusted for baseline heart rate (~ drug1 + baseline_hr).

m4

Follow-up dose adjusted for baseline heart rate and the prior dose (~ drug2 + drug1 + baseline_hr) – conditioning on the mediator.

m5

Follow-up dose adjusted for the propensity score (~ drug2 + ps).

m6

Marginal model of both doses, unweighted (~ drug1 + drug2, cluster = id) – the MSM's structural model without the weights, so still confounded.

m7

The MSM: the same model fitted with IPT weights (weights = weight, cluster = id).

m6 and m7 differ only by the weights, which isolates what the weighting buys you.

See also

build_forest() to turn these into a plottable table.

Examples

dat <- simulate_hr_agent(n = 2000)
fits <- fit_all_models(dat)
summary(fits$m7)
#> Call:
#> survival::coxph(formula = surv("drug1 + drug2"), data = data_w, 
#>     weights = weight, cluster = id)
#> 
#>   n= 3936, number of events= 114 
#> 
#>          coef exp(coef) se(coef) robust se      z Pr(>|z|)    
#> drug1 -0.9266    0.3959   0.1451    0.2279 -4.065  4.8e-05 ***
#> drug2 -0.7941    0.4520   0.2208    0.3046 -2.607  0.00912 ** 
#> ---
#> Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
#> 
#>       exp(coef) exp(-coef) lower .95 upper .95
#> drug1    0.3959      2.526    0.2533    0.6189
#> drug2    0.4520      2.213    0.2488    0.8210
#> 
#> Concordance= 0.629  (se = 0.024 )
#> Likelihood ratio test= 59.74  on 2 df,   p=1e-13
#> Wald test            = 23.4  on 2 df,   p=8e-06
#> Score (logrank) test = 57.71  on 2 df,   p=3e-13,   Robust = 19.54  p=6e-05
#> 
#>   (Note: the likelihood ratio and score tests assume independence of
#>      observations within a cluster, the Wald and robust score tests do not).