TTEPlan class for trial generation planning
TTEPlan class for trial generation planning
Details
Bundles the ETT grid, skeleton file paths, and design column names into a single object using a builder pattern. Create an empty plan with [TTEPlan$new()], then add ETTs one at a time with `$add_one_ett()`. Supports `plan[[i]]` to extract the i-th enrollment spec for interactive testing.
Design parameters (confounder_vars, person_id_var, exposure_var, etc.) are stored per-ETT in the `ett` data.table, allowing different ETTs to use different confounders or design columns. Within an enrollment_id (same follow_up + age_group), design params must match.
Computed properties
- max_follow_up
(read-only) The maximum `follow_up` across all ETTs. Used by `$enrollment_spec()` to set `design$follow_up_time` so that enrollment covers the longest follow-up per enrollment group. Returns `NA` when no ETTs have been added.
Methods
- `$add_one_ett(...)`
Add one ETT row to the plan. Returns `invisible(self)`.
- `$save(dir)`
Save the plan to disk as `.qs2`. Returns `invisible(path)`.
- `$enrollment_spec(i)`
Extract the i-th enrollment spec as a list with design, age_range, etc.
- `$s1_generate_enrollments_and_ipw(...)`
Run Loop 1: skeleton files to trial panels + IPW.
- `$s2_generate_analysis_files_and_ipcw_pp(...)`
Run Loop 2: per-ETT IPCW-PP + analysis file generation.
See also
[qs2_read()] to load from disk
Other tte_classes:
TTEDesign,
TTEEnrollment
Public fields
project_prefixCharacter, string used for file naming.
ettNULL or a data.table with per-ETT columns.
skeleton_filesCharacter vector of skeleton file paths.
global_max_isoyearweekAdmin censoring boundary.
specParsed study spec (from [tteplan_read_spec()]), or NULL.
expected_skeleton_file_countExpected number of skeleton files, or NULL.
code_registrydata.table from [RegistryStudy]`$summary_table()`, or NULL.
expected_n_idsTotal number of individuals across all batches, or NULL.
created_atPOSIXct. When this plan was created.
registry_study_created_atPOSIXct or NULL. When the source RegistryStudy was created.
skeleton_created_atPOSIXct or NULL. When skeleton files were created (from first file's attribute).
period_widthInteger, band width in weeks for enrollment (default: 4L).
enrollment_countsNamed list of per-enrollment TARGET Item 8 data. Each element is a list with:
- attrition
Long-format data.table (trial_id, criterion, n_persons, n_person_trials, n_exposed, n_unexposed) showing cumulative attrition at each eligibility step. Includes a
"before_exclusions"row with pre-filtering counts.- matching
data.table (trial_id, n_exposed_total, n_unexposed_total, n_exposed_enrolled, n_unexposed_enrolled).
Methods
Method new()
Create a new TTEPlan object.
Usage
TTEPlan$new(project_prefix, skeleton_files, global_max_isoyearweek, ett = NULL)Method check_version()
Check if this object's schema version matches the current class version. Warns if the object was saved with an older schema version.
Method print_spec_summary()
Print a target trial specification summary. Console-friendly summary derived from the study specification stored on this plan. When `$code_registry` is available, variable names are shown in red and matched code details in blue (ANSI colors).
Method print_target_checklist()
Print a TARGET-aligned reporting checklist.
Generates a self-contained document following the TARGET Statement (Cashin et al., JAMA 2025) 21-item checklist for transparent reporting of target trial emulations. Each item includes the full TARGET description, auto-filled content from the swereg spec where available, and `[FILL IN]` placeholders for PI completion.
Method add_one_ett()
Add one ETT to the plan.
An ETT (Emulated Target Trial) is one outcome x follow_up x age_group combination. ETTs sharing an enrollment_id use the same trial panels (same matching, same age group, same confounders). They differ only in outcome and/or follow-up duration. This avoids redundant re-enrollment for each outcome/follow-up combo.
Usage
TTEPlan$add_one_ett(
enrollment_id,
outcome_var,
outcome_name,
follow_up,
confounder_vars,
time_exposure_var,
eligible_var,
argset = list()
)Arguments
enrollment_idCharacter, enrollment group identifier (e.g., "01").
outcome_varCharacter, name of the outcome column.
outcome_nameCharacter, human-readable outcome name.
follow_upInteger, follow-up duration in weeks.
confounder_varsCharacter vector of confounder column names.
time_exposure_varCharacter or NULL, time-varying exposure column.
eligible_varCharacter or NULL, eligibility column.
argsetNamed list with age_group, age_min, age_max (and optional person_id_var).
Method save()
Save the plan to disk.
File is named {project_prefix}_plan.qs2 inside `dir`.
Saves the R6 object directly; reload with [qs2_read()].
Method enrollment_spec()
Extract enrollment spec for the i-th enrollment_id group.
Returns
A list with:
- design
A [TTEDesign] object with column mappings
- enrollment_id
Character, the enrollment group ID
- age_range
Numeric vector of length 2: c(min, max)
- n_threads
Integer, number of data.table threads to use
- exposure_impl
List with variable, exposed_value, comparator_value (present when plan was built from a spec)
- matching_ratio
Numeric, e.g. 2 for 1:2 matching (present when plan was built from a spec)
- seed
Integer for reproducible matching (present when plan was built from a spec)
Method s1_generate_enrollments_and_ipw()
Loop 1: Create trial panels from skeleton files and compute IPW.
Uses a two-pass pipeline to fix cross-batch matching ratio imbalance. Requires `self$spec` to be set (e.g., via [tteplan_from_spec_and_registrystudy()]).
**Pass 1a (scout)**: Lightweight parallel pass that reads each skeleton file, applies exclusions and exposure, and returns eligible `(person_id, trial_id, exposed)` tuples. No confounders or enrollment.
**Centralized matching**: Combines all tuples from all batches, then per `trial_id` keeps all exposed and samples `ratio * n_exposed` unexposed globally. Stores counts on `self$enrollment_counts` for TARGET Item 8 reporting.
**Pass 1b (full enrollment)**: Parallel pass that re-reads each skeleton file with full processing (exclusions + confounders + exposure), then enrolls using pre-matched IDs (skipping per-batch matching). Produces panel-expanded TTEEnrollment objects.
Method s2_generate_analysis_files_and_ipcw_pp()
Loop 2: Per-ETT IPCW-PP calculation and analysis file generation. For each ETT, loads the imputed enrollment file, calls `$s4_prepare_for_analysis()` (outcome + IPCW-PP + weight combination + truncation), and saves the analysis-ready file.
Usage
TTEPlan$s2_generate_analysis_files_and_ipcw_pp(
output_dir,
estimate_ipcw_pp_separately_by_exposure = TRUE,
estimate_ipcw_pp_with_gam = TRUE,
n_workers = 1L,
swereg_dev_path = NULL
)Arguments
output_dirDirectory containing imp files and where analysis files are saved.
estimate_ipcw_pp_separately_by_exposureLogical, estimate IPCW-PP separately by exposure group (default: TRUE).
estimate_ipcw_pp_with_gamLogical, use GAM for IPCW-PP estimation (default: TRUE).
n_workersInteger, concurrent subprocesses (default: 1L).
swereg_dev_pathPath to local swereg dev copy, or NULL.
Examples
if (FALSE) { # \dontrun{
plan <- TTEPlan$new(
project_prefix = "project002",
skeleton_files = skeleton_files,
global_max_isoyearweek = "2023-52"
)
plan$add_one_ett(
outcome_var = "death",
outcome_name = "Death",
follow_up = 52,
confounder_vars = c("age", "education"),
time_exposure_var = "rd_exposed",
eligible_var = "eligible",
argset = list(age_group = "50_60", age_min = 50, age_max = 60)
)
# Extract first enrollment spec for interactive testing
enrollment_spec <- plan[[1]]
enrollment_spec$design
enrollment_spec$age_range
} # }
