Computes IPTW weights from a pre-computed propensity score column and
appends them to the dataset. estimand picks the target: the average
treatment effect in the whole sample (ATE, the default), in the treated
(ATT) or in the controls (ATC). For the matched population (ATM), use the
mt_wt column from ps_logistic() or ps_forest() instead.
Returns a ps_weight object whose $tables slot contains SMD tables
(unweighted and weighted) and effective-N diagnostics. Its $data feeds
hvtiPlotR::hv_mirror_hist() in weighted mode; hvtiPlotR::hv_balance()
needs the SMD tables stacked into long form (see the vignette).
Usage
ps_weight(
data,
treatment_col = "tavr",
score_col = "prob_t",
estimand = c("ATE", "ATT", "ATC"),
stabilise = TRUE,
trim = NULL,
covariates = NULL,
weight_col = "iptw",
treated_level = NULL,
id_col = "id"
)Arguments
- data
A data frame. Must contain
treatment_colandscore_col.- treatment_col
Name of the binary treatment column (0/1 or logical). Default
"tavr".- score_col
Name of the numeric propensity score column (values in [0, 1]). Default
"prob_t".- estimand
Causal estimand. One of:
"ATE"(default): average treatment effect. Treated weights =1/ps; control weights =1/(1-ps)."ATT": average treatment effect on the treated. Treated weights =1; control weights =ps/(1-ps)."ATC": average treatment effect on the controls. Treated weights =(1-ps)/ps; control weights =1.
- stabilise
Logical. If
TRUE(default) andestimand = "ATE", each weight is multiplied by the marginal probability of the patient's own arm (P(treated)orP(control)). Stabilisation is defined for the ATE only: for"ATT"and"ATC"it has no effect, so the reference arm keeps weights of exactly 1.$meta$stabilisedrecords whether it was applied.- trim
Optional numeric in (0, 0.5). If supplied, weights outside the (
trim,1 - trim) quantile range are winsorised. DefaultNULL(no trimming).- covariates
Character vector of covariate columns for SMD diagnostics, used as given. If
NULL(default), every numeric column that varies is used, excepttreatment_col,score_col,weight_col,id_coland the fixed columns this package appends (prob_t,logit_t,mt_wt,iptw,match,pair_id,quintile,decile,bs,cluster).- weight_col
Name of the output weight column appended to
$data. Default"iptw". If the column already exists it is overwritten.- treated_level
The value of
treatment_colthat is treated.NULL(default) requires a 0/1 or logical column and takes 1 orTRUE. Give a value, for example"transcatheter", to use a column holding any two values.- id_col
The patient identifier column, left out of the default covariates. Default
"id"; it need not exist indata.
Value
An object of class c("ps_weight", "ps_data") with:
$dataThe input data frame with
weight_colappended.$metaNamed list:
treatment_col,treated_level(the value oftreatment_coltaken as treated),score_col,weight_col,estimand,stabilised,trim,method,n_total,n_excluded(rows with a missing treatment or score).$tablesNamed list:
smd_unweighted,smd_weighted(variable,smd),group_counts(group,treated,n) andeffective_n(group,treated,n_effective, the Kish effective sample size). Group tables name each group by its value intreatment_col(0/1,FALSE/TRUE, or the two values whentreated_levelis given) and carry a logicaltreatedcolumn.
Details
A patient with a missing treatment or a missing score cannot be weighted.
Those rows stay in $data with a missing weight_col, are left out of the
stabilising treatment proportion, the SMD tables, the group counts and the
effective sample sizes, and one warning states how many there are.
$meta$n_excluded keeps the count.
Examples
dta <- sample_ps_data(n = 200, seed = 42)
# 1. ATE weights (default)
obj <- ps_weight(dta)
print(obj)
#> <ps_weight>
#> N total : 400
#> Treatment : tavr
#> PS column : prob_t
#> Method : IPTW-ATE
#> Tables : smd_unweighted, smd_weighted, group_counts, effective_n
summary(obj)
#> Summary of <ps_weight>
#>
#> Smd unweighted:
#> variable smd
#> age age -0.7994
#> female female 0.3989
#> ef ef 0.6139
#> diabetes diabetes -0.0554
#> hypertension hypertension -0.0310
#>
#> Smd weighted:
#> variable smd
#> age age 0.1143
#> female female -0.0953
#> ef ef -0.0298
#> diabetes diabetes -0.0281
#> hypertension hypertension -0.0985
#>
#> Group counts:
#> group treated n
#> 1 0 FALSE 200
#> 2 1 TRUE 200
#>
#> Effective n:
#> group treated n_effective
#> 1 0 FALSE 127.9
#> 2 1 TRUE 107.6
#>
# 2. ATT weights: treated patients keep a weight of 1
obj_att <- ps_weight(dta, estimand = "ATT")
summary(obj_att)
#> Summary of <ps_weight>
#>
#> Smd unweighted:
#> variable smd
#> age age -0.7994
#> female female 0.3989
#> ef ef 0.6139
#> diabetes diabetes -0.0554
#> hypertension hypertension -0.0310
#>
#> Smd weighted:
#> variable smd
#> age age 0.0734
#> female female -0.0984
#> ef ef 0.0305
#> diabetes diabetes -0.1243
#> hypertension hypertension -0.2256
#>
#> Group counts:
#> group treated n
#> 1 0 FALSE 200
#> 2 1 TRUE 200
#>
#> Effective n:
#> group treated n_effective
#> 1 0 FALSE 62.5
#> 2 1 TRUE 200.0
#>
# 3. Extract data with weights appended
head(obj$data[, c("id", "tavr", "prob_t", "iptw")])
#> id tavr prob_t iptw
#> 1 1 0 0.1112484 0.5625869
#> 2 2 0 0.5270896 1.0572826
#> 3 3 0 0.3951929 0.8267098
#> 4 4 0 0.2836606 0.6979932
#> 5 5 0 0.3751620 0.8002074
#> 6 6 0 0.3148051 0.7297193