Skip to content

Commit b1c1620

Browse files
committed
calc_bal_stats change
omitting weighting on calc_bal_stats histories for which exposure is all 0s (in which case col_w_smd() cannot calculate bal stats)
1 parent d139330 commit b1c1620

5 files changed

Lines changed: 1412 additions & 8 deletions

File tree

R/assessBalance.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -460,7 +460,7 @@ summary.devMSM_bal_stats <- function(object, i = NA, save.out = FALSE, ...) {
460460
if (!is.na(i)) {
461461
sprintf("Imbalanced Covariates for imputation %s using `%s`", i, weight_method)
462462
} else {
463-
sprintf("Imbalanced Covariates Averaging Across Imputed Datasetrs using `%s`", weight_method)
463+
sprintf("Imbalanced Covariates Averaging Across Imputed Datasets using `%s`", weight_method)
464464
}
465465
} else {
466466
if (!is.na(i)) {

R/calc_bal_stats.R

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,16 @@ calc_bal_stats <- function(data, obj, weights = NULL, balance_thresh = NULL, imp
122122
prop_sum$history[prop_sum$Freq == 1 | prop_sum$Freq == 0]
123123
)
124124
} # ends hist exc
125-
125+
126+
# added by IS 4/8/26 to skip histories for which exposures are all 0s (cannot calc bal stats)
127+
omitted_histories <- Filter(
128+
function(h) {
129+
which_idx <- (history == h)
130+
sum(data[[exposure_name]][which_idx]) == 0
131+
},
132+
setdiff(prop_sum$history, omitted_histories)
133+
)
134+
126135
# finding balance by history
127136
# if weighted, use IPTW weights from weightitmsm and weight by history
128137
history_bal_stats <- sapply(
@@ -137,10 +146,15 @@ calc_bal_stats <- function(data, obj, weights = NULL, balance_thresh = NULL, imp
137146
)
138147
}
139148
)
140-
149+
150+
# IS amended to exclude ommitted histories
141151
# getting weighted mean across histories (cols), weighting by proportion of those w/ that same history
142-
weighted_bal_stats <- history_bal_stats %*% prop_sum$prop
143-
152+
# weighted_bal_stats <- history_bal_stats %*% prop_sum$prop
153+
keep <- !(prop_sum$history %in% omitted_histories)
154+
props <- as.numeric(prop_sum$prop[keep])
155+
weighted_bal_stats <- history_bal_stats %*% props
156+
157+
144158
if (exposure_type == "continuous") {
145159
# unweighted covar sd *
146160
s1 <- cobalt::col_w_sd(bal_vars)

vignettes/Data_Requirements.qmd

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -424,7 +424,7 @@ The user can specify an imputation method compatible with `mice()` (see above).
424424

425425
The user may also specify any additional inputs accepted by `mice::mice()` and we advise consulting the <a href:="https://www.rdocumentation.org/packages/mice/versions/3.16.0/topics/mice">\[*mice* documentation\]</a> for more information.
426426

427-
The user can also indicate if they have already created imputed datasets from this function and wish to read them in (`read_imps_from_file = TRUE` rather than recreate them (default).
427+
The user can also indicate if they have already created imputed datasets from this function and wish to read them in (`read_imps_from_file = TRUE`) rather than recreate them (default).
428428

429429
For this example, we create 2 imputed datasets using the default random forest method and 0 iterations (just for illustrative purposes), set a seed for reproducibility, and assign the output to `data` for use with *devMSMs*. This code takes some time to run. (Note: given the challenges of imputing data from .rda files, we have set `m` = 2 and `maxit` = 0 here just for illustrative purposes. We recommend setting both `m` = 5 and `maxit` = 5 (*mice* default) when running data.)
430430

0 commit comments

Comments
 (0)