Skip to content

Commit

Permalink
Doc and vignette uodates to support new WeightIt version
Browse files Browse the repository at this point in the history
  • Loading branch information
ngreifer committed Mar 25, 2024
1 parent dd0a26b commit 5b3f7fe
Show file tree
Hide file tree
Showing 13 changed files with 154 additions and 99 deletions.
6 changes: 3 additions & 3 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
Package: cobalt
Title: Covariate Balance Tables and Plots
Version: 4.5.4.9001
Version: 4.5.4.9002
Authors@R: c(
person("Noah", "Greifer", role=c("aut", "cre"),
email = "noah.greifer@gmail.com",
comment=c(ORCID="0000-0003-3067-7154"))
)
Description: Generate balance tables and plots for covariates of groups preprocessed through
matching, weighting or subclassification, for example, using propensity scores. Includes
integration with 'MatchIt', 'twang', 'Matching', 'optmatch', 'CBPS', 'ebal', 'WeightIt',
integration with 'MatchIt', 'WeightIt', 'MatchThem', 'twang', 'Matching', 'optmatch', 'CBPS', 'ebal',
'cem', 'sbw', and 'designmatch' for assessing balance on the output of their preprocessing
functions. Users can also specify data for balance assessment not generated through
the above packages. Also included are methods for assessing balance in clustered or
Expand All @@ -26,7 +26,7 @@ Imports:
backports (>= 1.1.9)
Suggests:
MatchIt (>= 4.0.0),
WeightIt (>= 0.14.0),
WeightIt (>= 1.0.0),
twang (>= 1.6),
twangContinuous,
Matching,
Expand Down
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

* Improved processing when no covariates are specified.

* Documentation updates

# cobalt 4.5.4

* Minor update to accommodate `ggplot2` 3.5.0. Thanks to @teunbrand. (#80)
Expand Down
52 changes: 30 additions & 22 deletions R/bal.tab.time.list.R
Original file line number Diff line number Diff line change
Expand Up @@ -25,41 +25,49 @@
#' * [`class-bal.tab.imp`] for more information on multiply imputed data.
#' * [`bal.tab.multi()`][class-bal.tab.multi] for more information on multi-category treatments.
#'
#' @examplesIf requireNamespace("twang", quietly = TRUE)
#' data("iptwExWide", package = "twang")
#' library("cobalt")
#' @examplesIf requireNamespace("WeightIt", quietly = TRUE)
#' data("msmdata", package = "WeightIt")
#'
#' ## Estimating longitudinal propensity scores and weights
#' ps1 <- glm(tx1 ~ age + gender + use0,
#' data = iptwExWide,
#' ps1 <- glm(A_1 ~ X1_0 + X2_0,
#' data = msmdata,
#' family = "binomial")$fitted.values
#' w1 <- ifelse(iptwExWide$tx1 == 1, 1/ps1, 1/(1-ps1))
#' ps2 <- glm(tx2 ~ age + gender + use0 + tx1 + use1,
#' data = iptwExWide,
#' w1 <- ifelse(msmdata$A_1 == 1, 1 / ps1, 1 / (1 - ps1))
#'
#' ps2 <- glm(A_2 ~ X1_1 + X2_1 +
#' A_1 + X1_0 + X2_0,
#' data = msmdata,
#' family = "binomial")$fitted.values
#' w2 <- ifelse(iptwExWide$tx2 == 1, 1/ps2, 1/(1-ps2))
#' ps3 <- glm(tx3 ~ age + gender + use0 + tx1 + use1 + tx2 + use2,
#' data = iptwExWide,
#' w2 <- ifelse(msmdata$A_2 == 1, 1 / ps2, 1 / (1 - ps2))
#'
#' ps3 <- glm(A_3 ~ X1_2 + X2_2 +
#' A_2 + X1_1 + X2_1 +
#' A_1 + X1_0 + X2_0,
#' data = msmdata,
#' family = "binomial")$fitted.values
#' w3 <- ifelse(iptwExWide$tx3 == 1, 1/ps3, 1/(1-ps3))
#' w3 <- ifelse(msmdata$A_3 == 1, 1 / ps3, 1 / (1 - ps3))
#'
#' w <- w1*w2*w3
#' w <- w1 * w2 * w3
#'
#' # Formula interface plus addl:
#' bal.tab(list(tx1 ~ use0 + gender,
#' tx2 ~ use0 + gender + use1 + tx1,
#' tx3 ~ use0 + gender + use1 + tx1 + use2 + tx2),
#' data = iptwExWide,
#' bal.tab(list(A_1 ~ X1_0 + X2_0,
#' A_2 ~ X1_1 + X2_1 +
#' A_1 + X1_0 + X2_0,
#' A_3 ~ X1_2 + X2_2 +
#' A_2 + X1_1 + X2_1 +
#' A_1 + X1_0 + X2_0),
#' data = msmdata,
#' weights = w,
#' distance = list(~ps1, ~ps2, ~ps3),
#' addl = ~age*gender,
#' addl = ~X1_0 * X2_0,
#' un = TRUE)
#'
#' # data frame interface:
#' bal.tab(list(iptwExWide[c("use0", "gender")],
#' iptwExWide[c("use0", "gender", "use1", "tx1")],
#' iptwExWide[c("use0", "gender", "use1", "tx1", "use2", "tx2")]),
#' treat.list = iptwExWide[c("tx1", "tx2", "tx3")],
#' bal.tab(list(msmdata[c("X1_0", "X2_0")],
#' msmdata[c("X1_1", "X2_1", "A_1", "X1_0", "X2_0")],
#' msmdata[c("X1_2", "X2_2", "A_2", "X1_1", "X2_1",
#' "A_1", "X1_0", "X2_0")]),
#' treat.list = msmdata[c("A_1", "A_2", "A_3")],
#' weights = w,
#' distance = list(~ps1, ~ps2, ~ps3),
#' un = TRUE)
Expand Down
24 changes: 14 additions & 10 deletions R/bal.tab.weightit.R
Original file line number Diff line number Diff line change
Expand Up @@ -26,32 +26,36 @@
#' @seealso
#' * [bal.tab()] for details of calculations.
#'
#' @examplesIf all(sapply(c("WeightIt", "twang"), requireNamespace, quietly = TRUE))
#' @examplesIf all(sapply(c("WeightIt"), requireNamespace, quietly = TRUE))
#' library(WeightIt)
#' data("lalonde", package = "cobalt")
#'
#' ## Basic propensity score weighting
#' w.out1 <- weightit(treat ~ age + educ + race +
#' married + nodegree + re74 + re75,
#' data = lalonde, method = "ps")
#' data = lalonde, method = "glm")
#' bal.tab(w.out1, un = TRUE, m.threshold = .1,
#' v.threshold = 2)
#'
#' ## Weighting with a multi-category treatment
#' w.out2 <- weightit(race ~ age + educ + married +
#' nodegree + re74 + re75,
#' data = lalonde, method = "ps",
#' estimand = "ATE", use.mlogit = FALSE)
#' data = lalonde, method = "glm",
#' estimand = "ATE")
#' bal.tab(w.out2, un = TRUE)
#' bal.tab(w.out2, un = TRUE, pairwise = FALSE)
#'
#' ## IPW for longitudinal treatments
#' data("iptwExWide", package = "twang")
#' wmsm.out <- weightitMSM(list(tx1 ~ use0 + gender,
#' tx2 ~ use0 + gender + use1 + tx1,
#' tx3 ~ use0 + gender + use1 + tx1 + use2 + tx2),
#' data = iptwExWide,
#' stabilize = TRUE)
#' data("msmdata", package = "WeightIt")
#'
#' wmsm.out <- weightitMSM(list(A_1 ~ X1_0 + X2_0,
#' A_2 ~ X1_1 + X2_1 +
#' A_1 + X1_0 + X2_0,
#' A_3 ~ X1_2 + X2_2 +
#' A_2 + X1_1 + X2_1 +
#' A_1 + X1_0 + X2_0),
#' data = msmdata,
#' method = "glm")
#' bal.tab(wmsm.out)

#' @exportS3Method bal.tab weightit
Expand Down
2 changes: 1 addition & 1 deletion R/balance-summary.R
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@
#' covs0 <- splitfactor(covs, drop.first = "if2")
#' bin.vars <- c(FALSE, FALSE, TRUE, TRUE, TRUE,
#' TRUE, TRUE, FALSE, FALSE)
#' W <- WeightIt::weightit(treat ~ covs, method = "ps",
#' W <- WeightIt::weightit(treat ~ covs, method = "glm",
#' estimand = "ATE")
#' weights <- W$weights
#'
Expand Down
52 changes: 30 additions & 22 deletions man/bal.tab.time.list.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 14 additions & 10 deletions man/bal.tab.weightit.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion man/balance-summary.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 2 additions & 3 deletions vignettes/cobalt.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -432,7 +432,7 @@ In `cobalt`, users can assess and present balance for multi-category treatments

`love.plot()` allows for the display of each pairwise treatment or the range of balance across treatment pairs for each covariate. `bal.plot()` displays distributional balance for the requested covariate across all treatment groups.

Below is an example of using `cobalt` with multi-category treatments. For this example, `race` will be the "treatment"; this type of analysis is not meant to be causal, but rather represents a method to examine disparities among groups accounting for covariates that might otherwise explain differences among groups. We will use `WeightIt` to generate balanced groups by estimating energy balancing weights.
Below is an example of using `cobalt` with multi-category treatments. For this example, `race` will be the "treatment"; this type of analysis is not meant to be causal, but rather represents a method to examine disparities among groups accounting for covariates that might otherwise explain differences among groups. We will use `WeightIt` to generate balanced groups by estimating generalized propensity score weights using multinomial logistic regression.

```{r}
data("lalonde", package = "cobalt")
Expand All @@ -442,8 +442,7 @@ data("lalonde", package = "cobalt")
W.out.mn <- WeightIt::weightit(race ~ age + educ + married +
nodegree + re74 + re75,
data = lalonde,
method = "energy",
use.mlogit = FALSE)
method = "glm")
```

First, we can examine balance numerically using `bal.tab()`. There are three possible pairwise comparisons, all of which can be requested with `which.treat = .all`. See `?bal.tab.multi` for more details.
Expand Down
Loading

0 comments on commit 5b3f7fe

Please sign in to comment.