Fit IPW weights and expand a cohort data.frame in one call, in memory (wrapper)
Source:R/tters-package.R
expand_trial_weighted_fitted_df.RdFrame-in / frame-out analogue of expand_trial_weighted_fitted(): takes a raw
cohort data.frame straight to a weighted, expanded data.frame in one call —
fitting the switching and/or IPCW models in Rust (no pre-computed factor table),
expanding under estimand, and accumulating the fitted factor into the
cumulative weight. The six structural columns are bit-exact; weight matches
the Oracle within the staged ~1e-6 tolerance. Wraps the extendr-generated
expand_weighted_fitted_df(). A bit64::integer64 id round-trips exactly.
Usage
expand_trial_weighted_fitted_df(
cohort,
id_col = "id",
period_col = "period",
treatment_col = "treatment",
eligible_col = "eligible",
outcome_col = "outcome",
first_period = 0L,
last_period = .Machine$integer.max,
estimand = "PP",
switch_numerator = NULL,
switch_denominator = NULL,
censor_col = NULL,
censor_numerator = NULL,
censor_denominator = NULL,
pool_censor = "none"
)Arguments
- cohort
A
data.frame(or tibble /data.table/ ArrowTable) of long person-time rows. Coerced withas.data.frame().- id_col, period_col, treatment_col, eligible_col, outcome_col
Column names. Defaults match the TrialEmulation conventions.
- first_period, last_period
Inclusive integer period bounds.
- estimand
"ITT"or"PP".- switch_numerator, switch_denominator
Character vectors of covariate column names for the switching numerator / denominator models, or
NULLto omit switching weights.- censor_col
Name of the
{0,1}censoring-indicator column; the modelled response is1 - censor_col.NULLomits IPCW weights.- censor_numerator, censor_denominator
Character vectors of covariate column names for the IPCW numerator / denominator models.
- pool_censor
How the IPCW models are pooled across the previous-treatment strata:
"none","numerator", or"both".
Details
Model presence follows the same rule as fit_trial_weights_df().
See also
expand_trial_weighted_fitted() for the Parquet-path equivalent;
fit_trial_weights_df() to return only the factor table.
Examples
if (requireNamespace("arrow", quietly = TRUE)) {
cohort <- as.data.frame(arrow::read_parquet(system.file(
"extdata", "weights", "input_data_censored.parquet", package = "tters")))
weighted <- expand_trial_weighted_fitted_df(cohort, estimand = "PP",
switch_numerator = "x2", switch_denominator = c("x2", "x1"))
}