clonecensorweighting is an R package for building reproducible
clone-censor-weighting workflows for target trial emulation.
This repository is meant to be easy to use from a fresh GitHub clone and easy to maintain as a shared collaboration project. The recommended setup uses:
rigto install and switch to the project R versionrenvto restore the same package environment for every collaborator- GitHub Actions to check reproducibility and package health
git clone https://github.com/CausalInferenceLab/clonecensorweighting.git
cd clonecensorweightingThis project is pinned to R 4.4.2 for reproducibility.
rig add 4.4.2
rig default 4.4.2If you already have R 4.4.2, you can skip rig add 4.4.2.
Open R in the project directory and run:
install.packages("renv")
renv::restore()renv::restore() installs the package versions recorded in renv.lock, so
everyone works with the same dependency set.
Note: this repository includes a .Rprofile file that activates renv
automatically when you open the project in R.
From the project root:
R CMD INSTALL .Then in R:
library(clonecensorweighting)library(clonecensorweighting)
data(lungcancer)
arms <- c("Control", "Surgery")
clones <- clone_arms(lungcancer, arms)
policies <- create_policy_A(
arms,
treatment = "surgery",
time_to_treatment = "timetosurgery",
grace_period = 182.62,
outcome = "death",
followup = "fup_obs",
clone_outcome = "outcome",
clone_followup = "fup"
)
clones_policy <- apply_logics(clones, policies)
censoring_logics <- create_censoring_logics_A(
arms,
treatment = "surgery",
time_to_treatment = "timetosurgery",
grace_period = 182.62,
followup = "fup_obs",
clone_censoring = "censoring",
clone_uncensored_followup = "fup_uncensored"
)
clones_censored <- apply_logics(clones_policy, censoring_logics)
clones_final <- create_final_data(
clones_censored,
clone_followup = "fup",
clone_outcome = "outcome",
clone_censoring = "censoring",
col_ids = "id"
)
clones_estimated <- estimate_censoring(
clones_final,
predictors = c("age", "sex"),
method = "pooled_logit"
)
clones_weighted <- weight_cases(clones_estimated)
fit <- emul_estimate(
clones_weighted,
method = "Cox",
weights = "weight_Cox",
predictors = c("age", "sex")
)
exp(stats::coef(fit))The full process is:
- Clone each patient into the target trial arms with
clone_arms(). - Define and apply treatment-policy logic with
create_policy_A()andapply_logics(). - Define and apply artificial censoring logic with
create_censoring_logics_A()andapply_logics(). - Expand cloned observations into long-form interval data with
create_final_data(). - Estimate censoring probabilities with
estimate_censoring(). - Add inverse probability of censoring weights with
weight_cases(). - Estimate the emulated trial effect with
emul_estimate(). - Use
emul_estimate_bootstrap()when bootstrap confidence intervals are needed.
The package is still an early, lightweight foundation for future work. Right now it includes:
read_trial_data()to read trial-style CSV data into a tibbleclone_censor_weighting()to create a starter cloned dataset across regimesmake_surv_response()to build asurvival::Surv()response objectclone_arms()to duplicate observations across treatment strategiescreate_policy_A()andcreate_censoring_logics_A()to generate example treatment-policy and artificial-censoring logic for the lung cancer scenarioapply_logics()to apply policy or censoring logic to cloned datacreate_final_data()to create long-form interval data for censoring modelsestimate_censoring()andweight_cases()to estimate censoring probabilities and add IPC weightsemul_estimate()andemul_estimate_bootstrap()to estimate treatment effects and bootstrap confidence intervals
For collaborative work, the safest pattern is:
- Clone the repository.
- Switch to R
4.4.2withrig. - Run
renv::restore(). - Make your changes in a branch.
- Run checks before opening a pull request.
Useful commands:
R CMD INSTALL .
R CMD check --no-manual .In R:
testthat::test_local()If you add, remove, or upgrade dependencies, update the lockfile from R:
renv::settings$snapshot.type("explicit")
renv::status(dev = TRUE)
renv::snapshot(dev = TRUE)Please commit both code changes and the updated renv.lock when dependency
changes are intentional.
This repository includes two GitHub Actions workflows:
check-reproducible.yamlrunsR CMD checkwith pinned R4.4.2andrenvcheck-latest.yamlruns broader checks across operating systems and R versions
Together, these workflows help keep the project reproducible for collaborators and stable for future users.